Author: toad
Date: 2006-01-23 21:30:18 +0000 (Mon, 23 Jan 2006)
New Revision: 7903

Modified:
   branches/async-client/src/freenet/client/FailureCodeTracker.java
   branches/async-client/src/freenet/client/FetchException.java
   branches/async-client/src/freenet/client/InserterException.java
   branches/async-client/src/freenet/client/async/Client.java
   branches/async-client/src/freenet/client/async/ClientGet.java
   branches/async-client/src/freenet/client/async/ClientGetState.java
   branches/async-client/src/freenet/client/async/ClientPut.java
   branches/async-client/src/freenet/client/async/ClientPutState.java
   branches/async-client/src/freenet/client/async/ClientRequest.java
   
branches/async-client/src/freenet/client/async/MultiPutCompletionCallback.java
   branches/async-client/src/freenet/client/async/PutCompletionCallback.java
   branches/async-client/src/freenet/client/async/SingleBlockInserter.java
   branches/async-client/src/freenet/client/async/SingleFileInserter.java
   branches/async-client/src/freenet/client/async/SplitFileFetcher.java
   branches/async-client/src/freenet/client/async/SplitFileFetcherSegment.java
   branches/async-client/src/freenet/client/async/SplitFileInserter.java
   branches/async-client/src/freenet/client/async/SplitFileInserterSegment.java
Log:
client.async compiles; both requests and inserts implemented.
but still no scheduler, and not wired in, so won't actually do anything yet.

Modified: branches/async-client/src/freenet/client/FailureCodeTracker.java
===================================================================
--- branches/async-client/src/freenet/client/FailureCodeTracker.java    
2006-01-23 20:10:20 UTC (rev 7902)
+++ branches/async-client/src/freenet/client/FailureCodeTracker.java    
2006-01-23 21:30:18 UTC (rev 7903)
@@ -106,5 +106,30 @@
        public synchronized int getFirstCode() {
                return ((Integer) map.keySet().toArray()[0]).intValue();
        }
+
+       public synchronized boolean isFatal(boolean insert) {
+               Iterator i = map.keySet().iterator();
+               while(i.hasNext()) {
+                       Integer code = (Integer) i.next();
+                       if(((Item)map.get(code)).x == 0) continue;
+                       if(insert) {
+                               if(InserterException.isFatal(code.intValue())) 
return true;
+                       } else {
+                               if(FetchException.isFatal(code.intValue())) 
return true;
+                       }
+               }
+               return false;
+       }
+
+       public void merge(InserterException e) {
+               if(!insert) throw new IllegalArgumentException("This is not an 
insert yet merge("+e+") called!");
+               if(e.errorCodes != null)
+                       merge(e.errorCodes);
+               inc(e.getMode());
+       }
+
+       public boolean isEmpty() {
+               return map.isEmpty();
+       }

 }

Modified: branches/async-client/src/freenet/client/FetchException.java
===================================================================
--- branches/async-client/src/freenet/client/FetchException.java        
2006-01-23 20:10:20 UTC (rev 7902)
+++ branches/async-client/src/freenet/client/FetchException.java        
2006-01-23 21:30:18 UTC (rev 7903)
@@ -198,6 +198,10 @@

        /** Is an error fatal i.e. is there no point retrying? */
        public boolean isFatal() {
+               return isFatal(mode);
+       }
+
+       public static boolean isFatal(int mode) {
                switch(mode) {
                // Problems with the data as inserted. No point retrying.
                case FetchException.ARCHIVE_FAILURE:
@@ -238,7 +242,7 @@
                        return true;

                default:
-                       Logger.error(this, "Do not know if error code is fatal: 
"+getMessage(mode));
+                       Logger.error(FetchException.class, "Do not know if 
error code is fatal: "+getMessage(mode));
                        return false; // assume it isn't
                }
        }

Modified: branches/async-client/src/freenet/client/InserterException.java
===================================================================
--- branches/async-client/src/freenet/client/InserterException.java     
2006-01-23 20:10:20 UTC (rev 7902)
+++ branches/async-client/src/freenet/client/InserterException.java     
2006-01-23 21:30:18 UTC (rev 7903)
@@ -111,4 +111,45 @@
                        return "Unknown error "+mode;
                }
        }
+
+       /** Is this error fatal? Non-fatal errors are errors which are likely 
to go away with
+        * more retries, or at least for which there is some point retrying.
+        */
+       public boolean isFatal() {
+               return isFatal(mode);
+       }
+       
+       public static boolean isFatal(int mode) {
+               switch(mode) {
+               case INVALID_URI:
+               case FATAL_ERRORS_IN_BLOCKS:
+               case COLLISION:
+               case CANCELLED:
+                       return true;
+               case BUCKET_ERROR: // maybe
+               case INTERNAL_ERROR: // maybe
+               case REJECTED_OVERLOAD:
+               case TOO_MANY_RETRIES_IN_BLOCKS:
+               case ROUTE_NOT_FOUND:
+               case ROUTE_REALLY_NOT_FOUND:
+                       return false;
+               default:
+                       Logger.error(InserterException.class, "Error unknown to 
isFatal(): "+getMessage(mode));
+                       return false;
+               }
+       }
+
+       public static InserterException construct(FailureCodeTracker errors) {
+               if(errors == null) return null;
+               if(errors.isEmpty()) return null;
+               if(errors.isOneCodeOnly()) {
+                       return new InserterException(errors.getFirstCode());
+               }
+               int mode;
+               if(errors.isFatal(true))
+                       mode = FATAL_ERRORS_IN_BLOCKS;
+               else
+                       mode = TOO_MANY_RETRIES_IN_BLOCKS;
+               return new InserterException(mode, errors, null);
+       }
 }

Modified: branches/async-client/src/freenet/client/async/Client.java
===================================================================
--- branches/async-client/src/freenet/client/async/Client.java  2006-01-23 
20:10:20 UTC (rev 7902)
+++ branches/async-client/src/freenet/client/async/Client.java  2006-01-23 
21:30:18 UTC (rev 7903)
@@ -3,6 +3,7 @@
 import freenet.client.FetchException;
 import freenet.client.FetchResult;
 import freenet.client.InserterException;
+import freenet.keys.FreenetURI;

 /**
  * A client process. Something that initiates requests, and can cancel
@@ -19,4 +20,6 @@

        public void onFailure(InserterException e, ClientPut state);

+       public void onGeneratedURI(FreenetURI uri, ClientPut state);
+       
 }

Modified: branches/async-client/src/freenet/client/async/ClientGet.java
===================================================================
--- branches/async-client/src/freenet/client/async/ClientGet.java       
2006-01-23 20:10:20 UTC (rev 7902)
+++ branches/async-client/src/freenet/client/async/ClientGet.java       
2006-01-23 21:30:18 UTC (rev 7903)
@@ -66,4 +66,12 @@
                client.onFailure(e, this);
        }

+       public void cancel() {
+               synchronized(this) {
+                       super.cancel();
+                       if(currentState != null)
+                               currentState.cancel();
+               }
+       }
+       
 }

Modified: branches/async-client/src/freenet/client/async/ClientGetState.java
===================================================================
--- branches/async-client/src/freenet/client/async/ClientGetState.java  
2006-01-23 20:10:20 UTC (rev 7902)
+++ branches/async-client/src/freenet/client/async/ClientGetState.java  
2006-01-23 21:30:18 UTC (rev 7903)
@@ -10,4 +10,6 @@

        public abstract void schedule();

+       public abstract void cancel();
+
 }

Modified: branches/async-client/src/freenet/client/async/ClientPut.java
===================================================================
--- branches/async-client/src/freenet/client/async/ClientPut.java       
2006-01-23 20:10:20 UTC (rev 7902)
+++ branches/async-client/src/freenet/client/async/ClientPut.java       
2006-01-23 21:30:18 UTC (rev 7903)
@@ -4,6 +4,7 @@
 import freenet.client.InsertBlock;
 import freenet.client.InserterContext;
 import freenet.client.InserterException;
+import freenet.keys.ClientKey;
 import freenet.keys.FreenetURI;
 import freenet.support.Bucket;

@@ -17,12 +18,13 @@
        final ClientRequestScheduler scheduler;
        private ClientPutState currentState;
        private boolean finished;
-       private boolean cancelled;
+       private final boolean getCHKOnly;

        public ClientPut(Client client, Bucket data, FreenetURI targetURI, 
ClientMetadata cm, InserterContext ctx,
-                       ClientRequestScheduler scheduler, short priorityClass) {
+                       ClientRequestScheduler scheduler, short priorityClass, 
boolean getCHKOnly) {
                super(priorityClass);
                this.cm = cm;
+               this.getCHKOnly = getCHKOnly;
                this.client = client;
                this.data = data;
                this.targetURI = targetURI;
@@ -39,7 +41,7 @@

        private void start() throws InserterException {
                currentState =
-                       new SingleFileInserter(this, this, new 
InsertBlock(data, cm, targetURI), false, ctx, false, false);
+                       new SingleFileInserter(this, this, new 
InsertBlock(data, cm, targetURI), false, ctx, false, false, getCHKOnly);
        }

        void setCurrentState(ClientPutState s) {
@@ -57,4 +59,17 @@
                currentState = null;
                client.onFailure(e, this);
        }
+
+       public void onEncode(ClientKey key, ClientPutState state) {
+               client.onGeneratedURI(key.getURI(), this);
+       }
+       
+       public void cancel() {
+               synchronized(this) {
+                       super.cancel();
+                       if(currentState != null)
+                               currentState.cancel();
+               }
+       }
+       
 }

Modified: branches/async-client/src/freenet/client/async/ClientPutState.java
===================================================================
--- branches/async-client/src/freenet/client/async/ClientPutState.java  
2006-01-23 20:10:20 UTC (rev 7902)
+++ branches/async-client/src/freenet/client/async/ClientPutState.java  
2006-01-23 21:30:18 UTC (rev 7903)
@@ -9,4 +9,6 @@

        public abstract ClientPut getParent();

+       public abstract void cancel();
+
 }

Modified: branches/async-client/src/freenet/client/async/ClientRequest.java
===================================================================
--- branches/async-client/src/freenet/client/async/ClientRequest.java   
2006-01-23 20:10:20 UTC (rev 7902)
+++ branches/async-client/src/freenet/client/async/ClientRequest.java   
2006-01-23 21:30:18 UTC (rev 7903)
@@ -9,7 +9,7 @@

        // FIXME move the priority classes from RequestStarter here
        private short priorityClass;
-       private boolean cancelled;
+       protected boolean cancelled;

        public short getPriorityClass() {
                return priorityClass;

Modified: 
branches/async-client/src/freenet/client/async/MultiPutCompletionCallback.java
===================================================================
--- 
branches/async-client/src/freenet/client/async/MultiPutCompletionCallback.java  
    2006-01-23 20:10:20 UTC (rev 7902)
+++ 
branches/async-client/src/freenet/client/async/MultiPutCompletionCallback.java  
    2006-01-23 21:30:18 UTC (rev 7903)
@@ -3,11 +3,13 @@
 import java.util.LinkedList;

 import freenet.client.InserterException;
+import freenet.keys.ClientKey;

 public class MultiPutCompletionCallback implements PutCompletionCallback, 
ClientPutState {

        private final LinkedList waitingFor;
        private final PutCompletionCallback cb;
+       private ClientPutState generator;
        private final ClientPut parent;
        private boolean finished;
        private boolean started;
@@ -45,6 +47,11 @@
                        cb.onSuccess(this);
        }

+       public synchronized void addURIGenerator(ClientPutState ps) {
+               add(ps);
+               generator = ps;
+       }
+       
        public synchronized void add(ClientPutState ps) {
                if(finished) return;
                waitingFor.add(ps);
@@ -58,4 +65,20 @@
                return parent;
        }

+       public void onEncode(ClientKey key, ClientPutState state) {
+               synchronized(this) {
+                       if(state != generator) return;
+               }
+               cb.onEncode(key, this);
+       }
+
+       public void cancel() {
+               ClientPutState[] states = new ClientPutState[waitingFor.size()];
+               synchronized(this) {
+                       states = (ClientPutState[]) waitingFor.toArray(states);
+               }
+               for(int i=0;i<states.length;i++)
+                       states[i].cancel();
+       }
+
 }

Modified: 
branches/async-client/src/freenet/client/async/PutCompletionCallback.java
===================================================================
--- branches/async-client/src/freenet/client/async/PutCompletionCallback.java   
2006-01-23 20:10:20 UTC (rev 7902)
+++ branches/async-client/src/freenet/client/async/PutCompletionCallback.java   
2006-01-23 21:30:18 UTC (rev 7903)
@@ -12,6 +12,6 @@

        public void onFailure(InserterException e, ClientPutState state);

-       public void onEncode(ClientKey key);
+       public void onEncode(ClientKey key, ClientPutState state);

 }

Modified: 
branches/async-client/src/freenet/client/async/SingleBlockInserter.java
===================================================================
--- branches/async-client/src/freenet/client/async/SingleBlockInserter.java     
2006-01-23 20:10:20 UTC (rev 7902)
+++ branches/async-client/src/freenet/client/async/SingleBlockInserter.java     
2006-01-23 21:30:18 UTC (rev 7903)
@@ -148,6 +148,7 @@

        public ClientKeyBlock getBlock() {
                try {
+                       if(finished) return null;
                        return encode();
                } catch (InserterException e) {
                        cb.onFailure(e, this);
@@ -183,4 +184,12 @@
                return parent;
        }

+       public void cancel() {
+               synchronized(this) {
+                       if(finished) return;
+                       finished = true;
+               }
+               cb.onFailure(new 
InserterException(InserterException.CANCELLED), this);
+       }
+
 }

Modified: branches/async-client/src/freenet/client/async/SingleFileInserter.java
===================================================================
--- branches/async-client/src/freenet/client/async/SingleFileInserter.java      
2006-01-23 20:10:20 UTC (rev 7902)
+++ branches/async-client/src/freenet/client/async/SingleFileInserter.java      
2006-01-23 21:30:18 UTC (rev 7903)
@@ -37,8 +37,11 @@
        /** If true, we are not the top level request, and should not
         * update our parent to point to us as current put-stage. */
        final boolean dontTellParent;
+       private boolean cancelled = false;

-       SingleFileInserter(ClientPut parent, PutCompletionCallback cb, 
InsertBlock block, boolean metadata, InserterContext ctx, boolean dontCompress, 
boolean dontTellParent, boolean getCHKOnly) throws InserterException {
+       SingleFileInserter(ClientPut parent, PutCompletionCallback cb, 
InsertBlock block, 
+                       boolean metadata, InserterContext ctx, boolean 
dontCompress, 
+                       boolean dontTellParent, boolean getCHKOnly) throws 
InserterException {
                this.parent = parent;
                this.block = block;
                this.ctx = ctx;
@@ -245,13 +248,25 @@
                        return parent;
                }

-               public void onEncode(ClientKey key) {
+               public void onEncode(ClientKey key, ClientPutState state) {
                        // Ignore
+                       
                }
+
+               public void cancel() {
+                       if(sfi != null)
+                               sfi.cancel();
+                       if(metadataPutter != null)
+                               metadataPutter.cancel();
+               }

        }

        public ClientPut getParent() {
                return parent;
        }
+
+       public void cancel() {
+               cancelled = true;
+       }
 }

Modified: branches/async-client/src/freenet/client/async/SplitFileFetcher.java
===================================================================
--- branches/async-client/src/freenet/client/async/SplitFileFetcher.java        
2006-01-23 20:10:20 UTC (rev 7902)
+++ branches/async-client/src/freenet/client/async/SplitFileFetcher.java        
2006-01-23 21:30:18 UTC (rev 7903)
@@ -241,4 +241,9 @@
                }
        }

+       public void cancel() {
+               for(int i=0;i<segments.length;i++)
+                       segments[i].cancel();
+       }
+
 }

Modified: 
branches/async-client/src/freenet/client/async/SplitFileFetcherSegment.java
===================================================================
--- branches/async-client/src/freenet/client/async/SplitFileFetcherSegment.java 
2006-01-23 20:10:20 UTC (rev 7902)
+++ branches/async-client/src/freenet/client/async/SplitFileFetcherSegment.java 
2006-01-23 21:30:18 UTC (rev 7903)
@@ -321,4 +321,8 @@
                }
        }

+       public void cancel() {
+               fail(new FetchException(FetchException.CANCELLED));
+       }
+
 }

Modified: branches/async-client/src/freenet/client/async/SplitFileInserter.java
===================================================================
--- branches/async-client/src/freenet/client/async/SplitFileInserter.java       
2006-01-23 20:10:20 UTC (rev 7902)
+++ branches/async-client/src/freenet/client/async/SplitFileInserter.java       
2006-01-23 21:30:18 UTC (rev 7903)
@@ -5,6 +5,7 @@

 import freenet.client.ClientMetadata;
 import freenet.client.FECCodec;
+import freenet.client.FailureCodeTracker;
 import freenet.client.InserterContext;
 import freenet.client.InserterException;
 import freenet.client.Metadata;
@@ -105,7 +106,7 @@
                return (SplitFileInserterSegment[]) segs.toArray(new 
SplitFileInserterSegment[segs.size()]);
        }

-       public void start() {
+       public void start() throws InserterException {
                for(int i=0;i<segments.length;i++)
                        segments[i].start();
        }
@@ -142,7 +143,7 @@

                                if(!missingURIs) {
                                        // Create Metadata
-                                       Metadata metadata = new 
Metadata(splitfileAlgorithm, dataURIs, checkURIs, segmentSize, 
checkSegmentSize, cm, dataLength, compressionCodec, isMetadata);
+                                       m = new Metadata(splitfileAlgorithm, 
dataURIs, checkURIs, segmentSize, checkSegmentSize, cm, dataLength, 
compressionCodec, isMetadata);
                                }
                                haveSentMetadata = true;
                        }
@@ -211,4 +212,55 @@
                return parent;
        }

+       public void segmentFinished(SplitFileInserterSegment segment) {
+               Logger.minor(this, "Segment finished: "+segment);
+               boolean allGone = true;
+               synchronized(this) {
+                       if(finished) return;
+                       for(int i=0;i<segments.length;i++)
+                               if(!segments[i].isFinished()) allGone = false;
+                       if(segment.getException().isFatal()) {
+                               cancel();
+                       } else {
+                               if(!allGone) return;
+                       }
+                       finished = true;
+               }
+               try {
+               // Finished !!
+               FailureCodeTracker tracker = new FailureCodeTracker(true);
+               boolean allSucceeded = true;
+               for(int i=0;i<segments.length;i++) {
+                       InserterException e = segments[i].getException();
+                       if(e == null) continue;
+                       allSucceeded = false;
+                       if(e.errorCodes != null)
+                               tracker.merge(e.errorCodes);
+                       tracker.inc(e.getMode());
+               }
+               if(allSucceeded)
+                       cb.onSuccess(this);
+               else {
+                       InserterException e;
+                       if(tracker.isFatal(true))
+                               cb.onFailure(new 
InserterException(InserterException.FATAL_ERRORS_IN_BLOCKS, tracker, null), 
this);
+                       else
+                               cb.onFailure(new 
InserterException(InserterException.TOO_MANY_RETRIES_IN_BLOCKS, tracker, null), 
this);
+               }
+               } catch (Throwable t) {
+                       // We MUST tell the parent *something*!
+                       Logger.error(this, "Caught "+t, t);
+                       cb.onFailure(new 
InserterException(InserterException.INTERNAL_ERROR), this);
+               }
+       }
+
+       public void cancel() {
+               synchronized(this) {
+                       if(finished) return;
+                       finished = true;
+               }
+               for(int i=0;i<segments.length;i++)
+                       segments[i].cancel();
+       }
+
 }

Modified: 
branches/async-client/src/freenet/client/async/SplitFileInserterSegment.java
===================================================================
--- 
branches/async-client/src/freenet/client/async/SplitFileInserterSegment.java    
    2006-01-23 20:10:20 UTC (rev 7902)
+++ 
branches/async-client/src/freenet/client/async/SplitFileInserterSegment.java    
    2006-01-23 21:30:18 UTC (rev 7903)
@@ -3,12 +3,14 @@
 import java.io.IOException;

 import freenet.client.FECCodec;
+import freenet.client.FailureCodeTracker;
 import freenet.client.InserterContext;
 import freenet.client.InserterException;
 import freenet.keys.ClientCHKBlock;
 import freenet.keys.ClientKey;
 import freenet.keys.FreenetURI;
 import freenet.support.Bucket;
+import freenet.support.Logger;

 public class SplitFileInserterSegment implements PutCompletionCallback {

@@ -25,9 +27,13 @@
        private boolean encoded;
        private boolean finished;
        private InserterException toThrow;
+       private final FailureCodeTracker errors;
+       private int blocksGotURI;
+       private int blocksCompleted;

        public SplitFileInserterSegment(SplitFileInserter parent, FECCodec 
splitfileAlgo, Bucket[] origDataBlocks, InserterContext blockInsertContext, 
boolean getCHKOnly, int segNo) {
                this.parent = parent;
+               this.errors = new FailureCodeTracker(true);
                this.blockInsertContext = blockInsertContext;
                this.splitfileAlgo = splitfileAlgo;
                this.dataBlocks = origDataBlocks;
@@ -40,7 +46,10 @@
                this.segNo = segNo;
        }

-       public void start() {
+       public void start() throws InserterException {
+               for(int i=0;i<dataBlockInserters.length;i++)
+                       dataBlockInserters[i] = 
+                               new SingleBlockInserter(parent.parent, 
dataBlocks[i], (short)-1, FreenetURI.EMPTY_CHK_URI, blockInsertContext, this, 
false, ClientCHKBlock.DATA_LENGTH, i);
                if(splitfileAlgo == null) {
                        // Don't need to encode blocks
                } else {
@@ -65,9 +74,6 @@
                        encoded = true;
                        parent.encodedSegment(this);
                        // Start the inserts
-                       for(int i=0;i<dataBlockInserters.length;i++)
-                               dataBlockInserters[i] = 
-                                       new SingleBlockInserter(parent.parent, 
dataBlocks[i], (short)-1, FreenetURI.EMPTY_CHK_URI, blockInsertContext, this, 
false, ClientCHKBlock.DATA_LENGTH, i);
                        for(int i=0;i<checkBlockInserters.length;i++)
                                checkBlockInserters[i] = 
                                        new SingleBlockInserter(parent.parent, 
checkBlocks[i], (short)-1, FreenetURI.EMPTY_CHK_URI, blockInsertContext, this, 
false, ClientCHKBlock.DATA_LENGTH, i + dataBlocks.length);
@@ -91,16 +97,88 @@
                parent.segmentFinished(this);
        }

+       private void finish() {
+               synchronized(this) {
+                       if(finished) return;
+                       finished = true;
+               }
+               toThrow = InserterException.construct(errors);
+               parent.segmentFinished(this);
+       }
+       
+       public void onEncode(ClientKey key, ClientPutState state) {
+               SingleBlockInserter sbi = (SingleBlockInserter)state;
+               int x = sbi.token;
+               synchronized(this) {
+                       if(x > dataBlocks.length) {
+                               if(checkURIs[x-dataBlocks.length] != null) {
+                                       Logger.normal(this, "Got uri twice for 
check block "+x+" on "+this);
+                                       return;
+                               }
+                               checkURIs[x-dataBlocks.length] = key.getURI();
+                       } else {
+                               if(dataURIs[x] != null) {
+                                       Logger.normal(this, "Got uri twice for 
data block "+x+" on "+this);
+                                       return;
+                               }
+                               dataURIs[x] = key.getURI();
+                       }
+                       blocksGotURI++;
+                       if(blocksGotURI != dataBlocks.length + 
checkBlocks.length) return;
+               }
+               // Double check
+               for(int i=0;i<checkURIs.length;i++)
+                       if(checkURIs[i] == null) {
+                               Logger.error(this, "Check URI "+i+" is null");
+                               return;
+                       }
+               for(int i=0;i<dataURIs.length;i++) {
+                       if(dataURIs[i] == null) {
+                               Logger.error(this, "Data URI "+i+" is null");
+                               return;
+                       }
+               }
+               parent.segmentHasURIs(this);
+       }
+
        public void onSuccess(ClientPutState state) {
-               // TODO Auto-generated method stub
-               
+               SingleBlockInserter sbi = (SingleBlockInserter)state;
+               int x = sbi.token;
+               if(completed(x)) return;
+               finish();
        }

        public void onFailure(InserterException e, ClientPutState state) {
-               // TODO Auto-generated method stub
-               
+               SingleBlockInserter sbi = (SingleBlockInserter)state;
+               int x = sbi.token;
+               errors.merge(e);
+               if(completed(x)) return;
+               finish();
        }

+       private boolean completed(int x) {
+               synchronized(this) {
+                       if(x > dataBlocks.length) {
+                               if(checkBlockInserters[x-dataBlocks.length] == 
null) {
+                                       Logger.error(this, "Completed twice: 
check block "+x+" on "+this);
+                                       return true;
+                               }
+                               checkBlockInserters[x-dataBlocks.length] = null;
+                       } else {
+                               if(dataBlockInserters[x] == null) {
+                                       Logger.error(this, "Completed twice: 
data block "+x+" on "+this);
+                                       return true;
+                               }
+                               dataBlockInserters[x] = null;
+                       }
+                       blocksCompleted++;
+                       if(blocksCompleted != dataBlockInserters.length + 
checkBlockInserters.length) return true;
+                       if(finished) return true;
+                       finished = true;
+                       return false;
+               }
+       }
+
        public boolean isFinished() {
                return finished;
        }
@@ -118,13 +196,30 @@
        }

        public FreenetURI[] getDataURIs() {
-               // TODO Auto-generated method stub
-               return null;
+               return dataURIs;
        }

-       public void onEncode(ClientKey key) {
-               // TODO Auto-generated method stub
-               
+       InserterException getException() {
+               return toThrow;
        }

+       public void cancel() {
+               synchronized(this) {
+                       if(finished) return;
+                       finished = true;
+               }
+               if(toThrow != null)
+                       toThrow = new 
InserterException(InserterException.CANCELLED);
+               for(int i=0;i<dataBlockInserters.length;i++) {
+                       SingleBlockInserter sbi = dataBlockInserters[i];
+                       if(sbi != null)
+                               sbi.cancel();
+               }
+               for(int i=0;i<checkBlockInserters.length;i++) {
+                       SingleBlockInserter sbi = checkBlockInserters[i];
+                       if(sbi != null)
+                               sbi.cancel();
+               }
+               parent.segmentFinished(this);
+       }
 }


Reply via email to