Author: toad
Date: 2005-11-09 20:14:20 +0000 (Wed, 09 Nov 2005)
New Revision: 7504

Added:
   trunk/freenet/src/freenet/client/StdSplitfileBlock.java
Modified:
   trunk/freenet/src/freenet/client/BlockFetcher.java
   trunk/freenet/src/freenet/client/BlockInserter.java
   trunk/freenet/src/freenet/client/InsertSegment.java
   trunk/freenet/src/freenet/client/SplitInserter.java
   trunk/freenet/src/freenet/node/Version.java
Log:
More splitfile stuff. Still doesn't work.

Modified: trunk/freenet/src/freenet/client/BlockFetcher.java
===================================================================
--- trunk/freenet/src/freenet/client/BlockFetcher.java  2005-11-09 18:55:33 UTC 
(rev 7503)
+++ trunk/freenet/src/freenet/client/BlockFetcher.java  2005-11-09 20:14:20 UTC 
(rev 7504)
@@ -7,109 +7,87 @@
 import freenet.support.Bucket;
 import freenet.support.Logger;

-public class BlockFetcher extends SplitfileBlock implements Runnable {
+public class BlockFetcher extends StdSplitfileBlock implements Runnable {

        private final Segment segment;
-       private final RetryTracker tracker;
-       /** Splitfile index - [0,k[ is the data blocks, [k,n[ is the check 
blocks */
-       final int index;
        final FreenetURI uri;
        final boolean dontEnterImplicitArchives;
        int completedTries;
-       Bucket fetchedData;
        boolean actuallyFetched;

        public BlockFetcher(Segment segment, RetryTracker tracker, FreenetURI 
freenetURI, int index, boolean dontEnterImplicitArchives) {
+               super(tracker, index);
                this.segment = segment;
-               this.tracker = tracker;
                uri = freenetURI;
                completedTries = 0;
                fetchedData = null;
-               this.index = index;
                actuallyFetched = false;
                this.dontEnterImplicitArchives = dontEnterImplicitArchives;
        }

-       public void start() {
-               if(fetchedData != null) {
-                       throw new IllegalStateException("Already have data");
-               }
+       public void run() {
+               // Already added to runningFetches.
+               // But need to make sure we are removed when we exit.
                try {
-                       Thread t = new Thread(this);
-                       t.setDaemon(true);
-                       t.start();
-               } catch (Throwable error) {
-                       tracker.fatalError(this, 
InserterException.INTERNAL_ERROR);
-                       Logger.error(this, "Caught "+error+" creating thread 
for "+this);
+                       realRun();
+               } catch (Throwable t) {
+                       fatalError(t, FetchException.INTERNAL_ERROR);
                }
        }

-       public void run() {
-               // Already added to runningFetches.
-               // But need to make sure we are removed when we exit.
+       private void realRun() {
+               // Do the fetch
+               Fetcher f = new Fetcher(uri, this.segment.blockFetchContext);
                try {
-                       // Do the fetch
-                       Fetcher f = new Fetcher(uri, 
this.segment.blockFetchContext);
-                       try {
-                               FetchResult fr = f.realRun(new 
ClientMetadata(), this.segment.recursionLevel, uri, 
-                                               
(!this.segment.nonFullBlocksAllowed) || dontEnterImplicitArchives);
-                               actuallyFetched = true;
-                               fetchedData = fr.data;
-                       } catch (MetadataParseException e) {
-                               fatalError(e, FetchException.INVALID_METADATA);
-                       } catch (FetchException e) {
-                               int code = e.getMode();
-                               switch(code) {
-                               case FetchException.ARCHIVE_FAILURE:
-                               case FetchException.BLOCK_DECODE_ERROR:
-                               case FetchException.HAS_MORE_METASTRINGS:
-                               case FetchException.INVALID_METADATA:
-                               case FetchException.NOT_IN_ARCHIVE:
-                               case FetchException.TOO_DEEP_ARCHIVE_RECURSION:
-                               case FetchException.TOO_MANY_ARCHIVE_RESTARTS:
-                               case FetchException.TOO_MANY_METADATA_LEVELS:
-                               case FetchException.TOO_MANY_REDIRECTS:
-                               case FetchException.TOO_MUCH_RECURSION:
-                               case FetchException.UNKNOWN_METADATA:
-                               case FetchException.UNKNOWN_SPLITFILE_METADATA:
-                                       // Fatal, probably an error on insert
-                                       fatalError(e, code);
-                                       return;
+                       FetchResult fr = f.realRun(new ClientMetadata(), 
this.segment.recursionLevel, uri, 
+                                       (!this.segment.nonFullBlocksAllowed) || 
dontEnterImplicitArchives);
+                       actuallyFetched = true;
+                       fetchedData = fr.data;
+                       tracker.success(this);
+               } catch (MetadataParseException e) {
+                       fatalError(e, FetchException.INVALID_METADATA);
+               } catch (FetchException e) {
+                       int code = e.getMode();
+                       switch(code) {
+                       case FetchException.ARCHIVE_FAILURE:
+                       case FetchException.BLOCK_DECODE_ERROR:
+                       case FetchException.HAS_MORE_METASTRINGS:
+                       case FetchException.INVALID_METADATA:
+                       case FetchException.NOT_IN_ARCHIVE:
+                       case FetchException.TOO_DEEP_ARCHIVE_RECURSION:
+                       case FetchException.TOO_MANY_ARCHIVE_RESTARTS:
+                       case FetchException.TOO_MANY_METADATA_LEVELS:
+                       case FetchException.TOO_MANY_REDIRECTS:
+                       case FetchException.TOO_MUCH_RECURSION:
+                       case FetchException.UNKNOWN_METADATA:
+                       case FetchException.UNKNOWN_SPLITFILE_METADATA:
+                               // Fatal, probably an error on insert
+                               fatalError(e, code);
+                               return;
+                       
+                       case FetchException.DATA_NOT_FOUND:
+                       case FetchException.ROUTE_NOT_FOUND:
+                       case FetchException.REJECTED_OVERLOAD:
+                       case FetchException.TRANSFER_FAILED:
+                               // Non-fatal
+                               nonfatalError(e, code);
+                               return;

-                               case FetchException.DATA_NOT_FOUND:
-                               case FetchException.ROUTE_NOT_FOUND:
-                               case FetchException.REJECTED_OVERLOAD:
-                               case FetchException.TRANSFER_FAILED:
-                                       // Non-fatal
-                                       nonfatalError(e, code);
-                                       
-                               case FetchException.BUCKET_ERROR:
-                               case FetchException.INTERNAL_ERROR:
-                                       // Maybe fatal
-                                       nonfatalError(e, code);
-                               }
-                       } catch (ArchiveFailureException e) {
-                               fatalError(e, FetchException.ARCHIVE_FAILURE);
-                       } catch (ArchiveRestartException e) {
-                               Logger.error(this, "Got an 
ArchiveRestartException in a splitfile - WTF?");
-                               fatalError(e, FetchException.ARCHIVE_FAILURE);
+                       case FetchException.BUCKET_ERROR:
+                       case FetchException.INTERNAL_ERROR:
+                               // Maybe fatal
+                               nonfatalError(e, code);
+                               return;
                        }
-               } finally {
-                       completedTries++;
-                       // Add before removing from runningFetches, to avoid 
race
-                       synchronized(this.segment.recentlyCompletedFetches) {
-                               this.segment.recentlyCompletedFetches.add(this);
-                       }
-                       synchronized(this.segment.runningFetches) {
-                               this.segment.runningFetches.remove(this);
-                       }
-                       synchronized(this.segment) {
-                               this.segment.notify();
-                       }
+               } catch (ArchiveFailureException e) {
+                       fatalError(e, FetchException.ARCHIVE_FAILURE);
+               } catch (ArchiveRestartException e) {
+                       Logger.error(this, "Got an ArchiveRestartException in a 
splitfile - WTF?");
+                       fatalError(e, FetchException.ARCHIVE_FAILURE);
                }
        }

-       private void fatalError(Exception e, int code) {
+       private void fatalError(Throwable e, int code) {
                Logger.normal(this, "Giving up on block: "+this+": "+e);
                completedTries = -1;
                tracker.fatalError(this, code);
@@ -134,29 +112,22 @@

        }

-       public int getNumber() {
-               return index;
+       public void kill() {
+               // Do nothing, for now
        }

-       public boolean hasData() {
-               return fetchedData != null;
+       public FreenetURI getURI() {
+               return uri;
        }
-
-       public Bucket getData() {
-               return fetchedData;
-       }
-
+       
        public void setData(Bucket data) {
                actuallyFetched = false;
-               fetchedData = data;
+               super.setData(data);
        }

-       public void kill() {
-               // Do nothing, for now
+       protected void checkStartable() {
+               if(fetchedData != null) {
+                       throw new IllegalStateException("Already have data");
+               }
        }
-
-       public FreenetURI getURI() {
-               // TODO Auto-generated method stub
-               return null;
-       }
 }
\ No newline at end of file

Modified: trunk/freenet/src/freenet/client/BlockInserter.java
===================================================================
--- trunk/freenet/src/freenet/client/BlockInserter.java 2005-11-09 18:55:33 UTC 
(rev 7503)
+++ trunk/freenet/src/freenet/client/BlockInserter.java 2005-11-09 20:14:20 UTC 
(rev 7504)
@@ -2,55 +2,104 @@

 import freenet.keys.FreenetURI;
 import freenet.support.Bucket;
+import freenet.support.Logger;

 /**
  * Inserts a single splitfile block.
  */
-public class BlockInserter extends SplitfileBlock {
+public class BlockInserter extends StdSplitfileBlock implements Runnable {

-       private Bucket data;
-       private final int num;
+       private boolean succeeded;
+       private int completedTries;
+       private final InserterContext ctx;
+       private final InsertBlock block;
+       private FreenetURI uri;

        /**
         * Create a BlockInserter.
         * @param bucket The data to insert, or null if it will be filled in 
later.
         * @param num The block number in the splitfile.
         */
-       public BlockInserter(Bucket bucket, int num) {
+       public BlockInserter(Bucket bucket, int num, RetryTracker tracker, 
InserterContext ctx) {
+               super(tracker, num);
                this.data = bucket;
                if(bucket == null) throw new NullPointerException();
-               this.num = num;
+               succeeded = false;
+               this.ctx = ctx;
+               block = new InsertBlock(bucket, null, FreenetURI.EMPTY_CHK_URI);
        }

-       int getNumber() {
-               return num;
+       public synchronized void setData(Bucket data) {
+               if(this.data != null) throw new 
IllegalArgumentException("Cannot set data when already have data");
+               this.data = data;
        }

-       boolean hasData() {
-               return true;
+       public void kill() {
+               // Do nothing, for now.
        }

-       Bucket getData() {
-               return data;
+       public FreenetURI getURI() {
+               return uri;
        }

-       synchronized void setData(Bucket data) {
-               if(this.data != null) throw new 
IllegalArgumentException("Cannot set data when already have data");
-               this.data = data;
+       public void run() {
+               try {
+                       realRun();
+               } catch (Throwable t) {
+                       fatalError(t, InserterException.INTERNAL_ERROR);
+               } finally {
+                       completedTries++;
+               }
        }
-
-       void start() {
-               // TODO Auto-generated method stub
-
+       
+       private void realRun() {
+               FileInserter inserter = new FileInserter(ctx);
+               try {
+                       uri = inserter.run(block, false);
+                       succeeded = true;
+                       tracker.success(this);
+               } catch (InserterException e) {
+                       switch(e.mode) {
+                       case InserterException.REJECTED_OVERLOAD:
+                       case InserterException.ROUTE_NOT_FOUND:
+                               nonfatalError(e, e.mode);
+                       case InserterException.INTERNAL_ERROR:
+                       case InserterException.BUCKET_ERROR:
+                               fatalError(e, e.mode);
+                               return;
+                       case InserterException.FATAL_ERRORS_IN_BLOCKS:
+                       case InserterException.TOO_MANY_RETRIES_IN_BLOCKS:
+                               // Huh?
+                               Logger.error(this, "Got error inserting blocks 
("+e.mode+") while inserting a block - WTF?");
+                               fatalError(e, InserterException.INTERNAL_ERROR);
+                               return;
+                       case InserterException.INVALID_URI:
+                               Logger.error(this, "Got invalid URI error but 
URI was CHK@ in block insert");
+                               fatalError(e, InserterException.INTERNAL_ERROR);
+                               return;
+                       default:
+                               Logger.error(this, "Unknown insert error 
"+e.mode+" while inserting a block");
+                               fatalError(e, InserterException.INTERNAL_ERROR);
+                               return;
+                       }
+                       // FIXME add more cases as we create them
+               }
+               
        }

-       public void kill() {
-               // TODO Auto-generated method stub
-
+       private void fatalError(Throwable e, int code) {
+               Logger.normal(this, "Giving up on block: "+this+": "+e);
+               completedTries = -1;
+               tracker.fatalError(this, code);
        }

-       public FreenetURI getURI() {
-               // TODO Auto-generated method stub
-               return null;
+       private void nonfatalError(Exception e, int code) {
+               Logger.minor(this, "Non-fatal error on "+this+": "+e);
+               tracker.nonfatalError(this, code);
        }
+       
+       protected void checkStartable() {
+               if(succeeded)
+                       throw new IllegalStateException("Already inserted 
block");
+       }
 }

Modified: trunk/freenet/src/freenet/client/InsertSegment.java
===================================================================
--- trunk/freenet/src/freenet/client/InsertSegment.java 2005-11-09 18:55:33 UTC 
(rev 7503)
+++ trunk/freenet/src/freenet/client/InsertSegment.java 2005-11-09 20:14:20 UTC 
(rev 7504)
@@ -38,10 +38,10 @@
         * Encode the data blocks into check blocks.
         * @return The number of check blocks generated.
         */
-       public int encode(int offset) {
+       public int encode(int offset, RetryTracker tracker, InserterContext 
ctx) {
                if(codec == null) return 0; // no FEC
                for(int i=0;i<checkBlocks.length;i++)
-                       checkBlocks[i] = new BlockInserter(null, offset + i);
+                       checkBlocks[i] = new BlockInserter(null, offset + i, 
tracker, ctx);
                codec.encode(origDataBlocks, checkBlocks, blockLength, bf);
                return checkBlocks.length;
        }

Modified: trunk/freenet/src/freenet/client/SplitInserter.java
===================================================================
--- trunk/freenet/src/freenet/client/SplitInserter.java 2005-11-09 18:55:33 UTC 
(rev 7503)
+++ trunk/freenet/src/freenet/client/SplitInserter.java 2005-11-09 20:14:20 UTC 
(rev 7504)
@@ -138,7 +138,7 @@

        private int encodeSegment(int i, int offset) {
                encodingSegment = segments[i];
-               return encodingSegment.encode(offset);
+               return encodingSegment.encode(offset, tracker, ctx);
        }

        /**
@@ -157,7 +157,7 @@
                Bucket[] dataBuckets = BucketTools.split(origData, 
NodeCHK.BLOCK_SIZE, ctx.bf);
                origDataBlocks = new SplitfileBlock[dataBuckets.length];
                for(int i=0;i<origDataBlocks.length;i++) {
-                       origDataBlocks[i] = new BlockInserter(dataBuckets[i], 
i);
+                       origDataBlocks[i] = new BlockInserter(dataBuckets[i], 
i, tracker, ctx);
                }
        }


Added: trunk/freenet/src/freenet/client/StdSplitfileBlock.java
===================================================================
--- trunk/freenet/src/freenet/client/StdSplitfileBlock.java     2005-11-09 
18:55:33 UTC (rev 7503)
+++ trunk/freenet/src/freenet/client/StdSplitfileBlock.java     2005-11-09 
20:14:20 UTC (rev 7504)
@@ -0,0 +1,48 @@
+package freenet.client;
+
+import freenet.support.Bucket;
+import freenet.support.Logger;
+
+public abstract class StdSplitfileBlock extends SplitfileBlock implements 
Runnable {
+
+       Bucket fetchedData;
+       protected final RetryTracker tracker;
+       /** Splitfile index - [0,k[ is the data blocks, [k,n[ is the check 
blocks */
+       protected final int index;
+       protected Bucket data;
+
+       public StdSplitfileBlock(RetryTracker tracker2, int index2) {
+               this.tracker = tracker2;
+               this.index = index2;
+       }
+
+       public int getNumber() {
+               return index;
+       }
+
+       public boolean hasData() {
+               return fetchedData != null;
+       }
+
+       public Bucket getData() {
+               return fetchedData;
+       }
+
+       public void setData(Bucket data) {
+               fetchedData = data;
+       }
+
+       public void start() {
+               checkStartable();
+               try {
+                       Thread t = new Thread(this);
+                       t.setDaemon(true);
+                       t.start();
+               } catch (Throwable error) {
+                       tracker.fatalError(this, 
InserterException.INTERNAL_ERROR);
+                       Logger.error(this, "Caught "+error+" creating thread 
for "+this);
+               }
+       }
+
+       protected abstract void checkStartable();
+}

Modified: trunk/freenet/src/freenet/node/Version.java
===================================================================
--- trunk/freenet/src/freenet/node/Version.java 2005-11-09 18:55:33 UTC (rev 
7503)
+++ trunk/freenet/src/freenet/node/Version.java 2005-11-09 20:14:20 UTC (rev 
7504)
@@ -20,7 +20,7 @@
        public static final String protocolVersion = "1.0";

        /** The build number of the current revision */
-       public static final int buildNumber = 145;
+       public static final int buildNumber = 146;

        /** Oldest build of Fred we will talk to */
        public static final int lastGoodBuild = 144;


Reply via email to