Author: toad
Date: 2005-10-26 19:45:40 +0000 (Wed, 26 Oct 2005)
New Revision: 7455

Added:
   trunk/freenet/src/freenet/client/ArchiveContext.java
   trunk/freenet/src/freenet/client/ArchiveElement.java
Log:
Hmmm. Oops.

Added: trunk/freenet/src/freenet/client/ArchiveContext.java
===================================================================
--- trunk/freenet/src/freenet/client/ArchiveContext.java        2005-10-26 
19:38:23 UTC (rev 7454)
+++ trunk/freenet/src/freenet/client/ArchiveContext.java        2005-10-26 
19:45:40 UTC (rev 7455)
@@ -0,0 +1,25 @@
+package freenet.client;
+
+import java.util.HashSet;
+
+import freenet.keys.ClientKey;
+import freenet.keys.FreenetURI;
+
+/**
+ * Object passed down a full fetch, including all the recursion.
+ * Used, at present, for detecting archive fetch loops, hence the
+ * name.
+ */
+public class ArchiveContext {
+
+       HashSet soFar = new HashSet();
+       int maxArchiveLevels;
+       
+       public synchronized void doLoopDetection(ClientKey key) throws 
ArchiveFailureException {
+               if(!soFar.add(key))
+                       throw new ArchiveFailureException("Archive loop 
detected");
+               if(soFar.size() > maxArchiveLevels)
+                       throw new 
ArchiveFailureException(ArchiveFailureException.TOO_MANY_LEVELS);
+       }
+
+}

Added: trunk/freenet/src/freenet/client/ArchiveElement.java
===================================================================
--- trunk/freenet/src/freenet/client/ArchiveElement.java        2005-10-26 
19:38:23 UTC (rev 7454)
+++ trunk/freenet/src/freenet/client/ArchiveElement.java        2005-10-26 
19:45:40 UTC (rev 7455)
@@ -0,0 +1,50 @@
+package freenet.client;
+
+import freenet.keys.ClientKey;
+import freenet.keys.FreenetURI;
+import freenet.support.Bucket;
+
+/**
+ * An element in an archive. Does synchronization (on fetches, to avoid
+ * having to do them twice), checks cache, does fetch, adds to cache.
+ *
+ * DO LOOP DETECTION!
+ */
+public class ArchiveElement {
+
+       ArchiveElement(ArchiveManager manager, FreenetURI uri, String filename, 
short archiveType) {
+               this.manager = manager;
+               this.key = uri;
+               this.filename = filename;
+               this.archiveType = archiveType;
+       }
+       
+       final ArchiveManager manager;
+       final FreenetURI key;
+       final String filename;
+       final short archiveType;
+       
+       /**
+        * Fetch the element.
+        * If fetchContext is null, return null unless the data is cached.
+        * @throws ArchiveFailureException If there was a fatal error in the 
archive extraction. 
+        * @throws ArchiveRestartException If the archive changed, and 
therefore we need to
+        * restart the request.
+        * @throws FetchException If we could not fetch the key.
+        * @throws MetadataParseException If the key's metadata was invalid.
+        */
+       public Bucket get(FetcherContext fetchContext, ClientMetadata dm, int 
recursionLevel, ArchiveContext archiveContext) 
+       throws ArchiveFailureException, MetadataParseException, FetchException, 
ArchiveRestartException {
+               
+               synchronized(this) {
+                       // Synchronized during I/O to avoid doing it twice
+                       Bucket cached = manager.getCached(key, filename);
+                       if(cached != null) return cached;
+                       if(fetchContext == null) return null;
+                       Fetcher fetcher = new Fetcher(key, fetchContext, 
archiveContext);
+                       FetchResult result = fetcher.realRun(dm, 
recursionLevel, key);
+                       manager.extractToCache(key, archiveType, result.data, 
archiveContext);
+                       return manager.getCached(key, filename);
+               }
+       }
+}

_______________________________________________
cvs mailing list
[email protected]
http://emu.freenetproject.org/cgi-bin/mailman/listinfo/cvs

Reply via email to