Author: toad
Date: 2006-01-28 00:31:38 +0000 (Sat, 28 Jan 2006)
New Revision: 7961

Modified:
   trunk/freenet/src/freenet/client/async/ClientPutter.java
   trunk/freenet/src/freenet/client/async/GetCompletionCallback.java
   trunk/freenet/src/freenet/client/async/MultiPutCompletionCallback.java
   trunk/freenet/src/freenet/client/async/PutCompletionCallback.java
   trunk/freenet/src/freenet/client/async/SimpleManifestPutter.java
   trunk/freenet/src/freenet/client/async/SingleFileInserter.java
   trunk/freenet/src/freenet/client/async/SplitFileInserter.java
   trunk/freenet/src/freenet/client/async/SplitFileInserterSegment.java
   trunk/freenet/src/freenet/node/Version.java
Log:
412:
FinalizedTotal works on inserts too, but not very useful because we can't set 
it until we have all the blocks encoded, which doesn't happen until we have 
nearly finished (except on really small inserts).
Advice to clients is different for requests and inserts:
- Requests:
Client authors are recommended not to show a progress bar until they have the 
FinalizedTotal. It varies
wildly before that, and will often be 100% or nearly when still very close to 
the beginning of the request.
- Inserts:
Client authors are recommended to show it immediately, because the total will 
not increase by very much.

Modified: trunk/freenet/src/freenet/client/async/ClientPutter.java
===================================================================
--- trunk/freenet/src/freenet/client/async/ClientPutter.java    2006-01-27 
23:42:36 UTC (rev 7960)
+++ trunk/freenet/src/freenet/client/async/ClientPutter.java    2006-01-28 
00:31:38 UTC (rev 7961)
@@ -97,4 +97,9 @@
                ctx.eventProducer.produceEvent(new 
SplitfileProgressEvent(this.totalBlocks, this.successfulBlocks, 
this.failedBlocks, this.fatallyFailedBlocks, this.minSuccessBlocks, 
this.blockSetFinalized));
        }

+       public void onBlockSetFinished(ClientPutState state) {
+               Logger.minor(this, "Set finished", new Exception("debug"));
+               blockSetFinalized();
+       }
+       
 }

Modified: trunk/freenet/src/freenet/client/async/GetCompletionCallback.java
===================================================================
--- trunk/freenet/src/freenet/client/async/GetCompletionCallback.java   
2006-01-27 23:42:36 UTC (rev 7960)
+++ trunk/freenet/src/freenet/client/async/GetCompletionCallback.java   
2006-01-28 00:31:38 UTC (rev 7961)
@@ -17,4 +17,5 @@
         * all the blocks it will need to fetch.
         */
        public void onBlockSetFinished(ClientGetState state);
+
 }

Modified: trunk/freenet/src/freenet/client/async/MultiPutCompletionCallback.java
===================================================================
--- trunk/freenet/src/freenet/client/async/MultiPutCompletionCallback.java      
2006-01-27 23:42:36 UTC (rev 7960)
+++ trunk/freenet/src/freenet/client/async/MultiPutCompletionCallback.java      
2006-01-28 00:31:38 UTC (rev 7961)
@@ -11,7 +11,11 @@

 public class MultiPutCompletionCallback implements PutCompletionCallback, 
ClientPutState {

+       // LinkedList's rather than HashSet's for memory reasons.
+       // This class will not be used with large sets, so O(n) is cheaper than 
O(1) -
+       // at least it is on memory!
        private final LinkedList waitingFor;
+       private final LinkedList waitingForBlockSet;
        private final PutCompletionCallback cb;
        private ClientPutState generator;
        private final BaseClientPutter parent;
@@ -21,6 +25,7 @@
        public MultiPutCompletionCallback(PutCompletionCallback cb, 
BaseClientPutter parent) {
                this.cb = cb;
                this.waitingFor = new LinkedList();
+               this.waitingForBlockSet = new LinkedList();
                this.parent = parent;
                finished = false;
        }
@@ -103,4 +108,12 @@
                }
        }

+       public void onBlockSetFinished(ClientPutState state) {
+               synchronized(this) {
+                       this.waitingForBlockSet.remove(state);
+                       if(!started) return;
+               }
+               cb.onBlockSetFinished(this);
+       }
+
 }

Modified: trunk/freenet/src/freenet/client/async/PutCompletionCallback.java
===================================================================
--- trunk/freenet/src/freenet/client/async/PutCompletionCallback.java   
2006-01-27 23:42:36 UTC (rev 7960)
+++ trunk/freenet/src/freenet/client/async/PutCompletionCallback.java   
2006-01-28 00:31:38 UTC (rev 7961)
@@ -23,4 +23,9 @@
         */
        public void onMetadata(Metadata m, ClientPutState state);

+       /** Called when the ClientPutState knows that it knows about
+        * all the blocks it will need to put.
+        */
+       public void onBlockSetFinished(ClientPutState state);
+
 }

Modified: trunk/freenet/src/freenet/client/async/SimpleManifestPutter.java
===================================================================
--- trunk/freenet/src/freenet/client/async/SimpleManifestPutter.java    
2006-01-27 23:42:36 UTC (rev 7960)
+++ trunk/freenet/src/freenet/client/async/SimpleManifestPutter.java    
2006-01-28 00:31:38 UTC (rev 7961)
@@ -130,11 +130,20 @@
                public void notifyClients() {
                        // FIXME generate per-filename events???
                }
+
+               public void onBlockSetFinished(ClientPutState state) {
+                       synchronized(SimpleManifestPutter.this) {
+                               waitingForBlockSets.remove(this);
+                               if(!waitingForBlockSets.isEmpty()) return;
+                       }
+                       SimpleManifestPutter.this.blockSetFinalized();
+               }
        }

        private final HashMap putHandlersByName;
        private final HashSet runningPutHandlers;
        private final HashSet putHandlersWaitingForMetadata;
+       private final HashSet waitingForBlockSets;
        private FreenetURI finalURI;
        private FreenetURI targetURI;
        private boolean finished;
@@ -160,6 +169,7 @@
                putHandlersByName = new HashMap();
                runningPutHandlers = new HashSet();
                putHandlersWaitingForMetadata = new HashSet();
+               waitingForBlockSets = new HashSet();
                Iterator it = bucketsByName.keySet().iterator();
                while(it.hasNext()) {
                        String name = (String) it.next();
@@ -321,4 +331,8 @@
                ctx.eventProducer.produceEvent(new 
SplitfileProgressEvent(this.totalBlocks, this.successfulBlocks, 
this.failedBlocks, this.fatallyFailedBlocks, this.minSuccessBlocks, 
this.blockSetFinalized));
        }

+       public void onBlockSetFinished(ClientPutState state) {
+               this.blockSetFinalized();
+       }
+
 }

Modified: trunk/freenet/src/freenet/client/async/SingleFileInserter.java
===================================================================
--- trunk/freenet/src/freenet/client/async/SingleFileInserter.java      
2006-01-27 23:42:36 UTC (rev 7960)
+++ trunk/freenet/src/freenet/client/async/SingleFileInserter.java      
2006-01-28 00:31:38 UTC (rev 7961)
@@ -158,6 +158,7 @@
                                // Just insert it
                                SingleBlockInserter bi = new 
SingleBlockInserter(parent, data, codecNumber, block.desiredURI, ctx, cb, 
metadata, (int)block.getData().size(), -1, getCHKOnly, true);
                                bi.schedule();
+                               cb.onBlockSetFinished(this);
                                cb.onTransition(this, bi);
                                return;
                        }
@@ -170,6 +171,7 @@
                                cb.onMetadata(meta, this);
                                cb.onTransition(this, dataPutter);
                                dataPutter.schedule();
+                               cb.onBlockSetFinished(this);
                        } else {
                                MultiPutCompletionCallback mcb = 
                                        new MultiPutCompletionCallback(cb, 
parent);
@@ -188,6 +190,7 @@
                                mcb.arm();
                                dataPutter.schedule();
                                metaPutter.schedule();
+                               cb.onBlockSetFinished(this);
                        }
                        return;
                }
@@ -222,6 +225,8 @@
                boolean finished = false;
                boolean splitInsertSuccess = false;
                boolean metaInsertSuccess = false;
+               boolean splitInsertSetBlocks = false;
+               boolean metaInsertSetBlocks = false;

                public synchronized void onTransition(ClientPutState oldState, 
ClientPutState newState) {
                        if(oldState == sfi)
@@ -300,8 +305,10 @@
                private synchronized void fail(InserterException e) {
                        Logger.minor(this, "Failing: "+e, e);
                        if(finished) return;
-                       sfi.cancel();
-                       metadataPutter.cancel();
+                       if(sfi != null)
+                               sfi.cancel();
+                       if(metadataPutter != null)
+                               metadataPutter.cancel();
                        finished = true;
                        cb.onFailure(e, this);
                }
@@ -321,6 +328,18 @@
                        if(metadataPutter != null)
                                metadataPutter.cancel();
                }
+
+               public void onBlockSetFinished(ClientPutState state) {
+                       synchronized(this) {
+                               if(state == sfi)
+                                       splitInsertSetBlocks = true;
+                               else if (state == metadataPutter)
+                                       metaInsertSetBlocks = true;
+                               if(!(splitInsertSetBlocks && 
metaInsertSetBlocks)) 
+                                       return;
+                       }
+                       cb.onBlockSetFinished(this);
+               }

        }


Modified: trunk/freenet/src/freenet/client/async/SplitFileInserter.java
===================================================================
--- trunk/freenet/src/freenet/client/async/SplitFileInserter.java       
2006-01-27 23:42:36 UTC (rev 7960)
+++ trunk/freenet/src/freenet/client/async/SplitFileInserter.java       
2006-01-28 00:31:38 UTC (rev 7961)
@@ -112,6 +112,13 @@

        public void encodedSegment(SplitFileInserterSegment segment) {
                Logger.minor(this, "Encoded segment "+segment.segNo+" of 
"+this);
+               synchronized(this) {
+                       for(int i=0;i<segments.length;i++) {
+                               if(segments[i] == null || 
!segments[i].isEncoded())
+                                       return;
+                       }
+               }
+               cb.onBlockSetFinished(this);
        }

        public void segmentHasURIs(SplitFileInserterSegment segment) {
@@ -120,42 +127,38 @@
                        return;
                }

-               boolean allHaveURIs = true;
                synchronized(this) {
                        for(int i=0;i<segments.length;i++) {
                                if(!segments[i].isEncoded())
-                                       allHaveURIs = false;
+                                       return;
                        }
                }

-               if(allHaveURIs) {
-                       Logger.minor(this, "Have URIs from all segments");
-                       boolean missingURIs;
-                       Metadata m = null;
-                       synchronized(this) {
-                               // Create metadata
-                               FreenetURI[] dataURIs = getDataURIs();
-                               FreenetURI[] checkURIs = getCheckURIs();
-                               
-                               Logger.minor(this, "Data URIs: 
"+dataURIs.length+", check URIs: "+checkURIs.length);
-                               
-                               missingURIs = anyNulls(dataURIs) || 
anyNulls(checkURIs);
-                               
-                               if(!missingURIs) {
-                                       // Create Metadata
-                                       m = new Metadata(splitfileAlgorithm, 
dataURIs, checkURIs, segmentSize, checkSegmentSize, cm, dataLength, 
compressionCodec, isMetadata);
-                               }
-                               haveSentMetadata = true;
+               Logger.minor(this, "Have URIs from all segments");
+               boolean missingURIs;
+               Metadata m = null;
+               synchronized(this) {
+                       // Create metadata
+                       FreenetURI[] dataURIs = getDataURIs();
+                       FreenetURI[] checkURIs = getCheckURIs();
+                       
+                       Logger.minor(this, "Data URIs: "+dataURIs.length+", 
check URIs: "+checkURIs.length);
+                       
+                       missingURIs = anyNulls(dataURIs) || anyNulls(checkURIs);
+                       
+                       if(!missingURIs) {
+                               // Create Metadata
+                               m = new Metadata(splitfileAlgorithm, dataURIs, 
checkURIs, segmentSize, checkSegmentSize, cm, dataLength, compressionCodec, 
isMetadata);
                        }
-                       if(missingURIs) {
-                               Logger.minor(this, "Missing URIs");
-                               // Error
-                               fail(new 
InserterException(InserterException.INTERNAL_ERROR, "Missing URIs after 
encoding", null));
-                               return;
-                       } else
-                               cb.onMetadata(m, this);
+                       haveSentMetadata = true;
                }
-
+               if(missingURIs) {
+                       Logger.minor(this, "Missing URIs");
+                       // Error
+                       fail(new 
InserterException(InserterException.INTERNAL_ERROR, "Missing URIs after 
encoding", null));
+                       return;
+               } else
+                       cb.onMetadata(m, this);
        }

        private void fail(InserterException e) {

Modified: trunk/freenet/src/freenet/client/async/SplitFileInserterSegment.java
===================================================================
--- trunk/freenet/src/freenet/client/async/SplitFileInserterSegment.java        
2006-01-27 23:42:36 UTC (rev 7960)
+++ trunk/freenet/src/freenet/client/async/SplitFileInserterSegment.java        
2006-01-28 00:31:38 UTC (rev 7961)
@@ -76,15 +76,16 @@
        void encode() {
                try {
                        splitfileAlgo.encode(dataBlocks, checkBlocks, 
ClientCHKBlock.DATA_LENGTH, blockInsertContext.bf);
-                       // Success! Start the fetches.
-                       encoded = true;
-                       parent.encodedSegment(this);
                        // Start the inserts
                        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, getCHKOnly, false);
                                checkBlockInserters[i].schedule();
                        }
+                       // Tell parent only after have started the inserts.
+                       // Because of the counting.
+                       encoded = true;
+                       parent.encodedSegment(this);
                } catch (IOException e) {
                        InserterException ex = 
                                new 
InserterException(InserterException.BUCKET_ERROR, e, null);
@@ -238,4 +239,9 @@
        public void onMetadata(Metadata m, ClientPutState state) {
                Logger.error(this, "Got onMetadata from "+state);
        }
+
+       public void onBlockSetFinished(ClientPutState state) {
+               // Ignore
+               Logger.error(this, "Should not happen: 
onBlockSetFinished("+state+") on "+this);
+       }
 }

Modified: trunk/freenet/src/freenet/node/Version.java
===================================================================
--- trunk/freenet/src/freenet/node/Version.java 2006-01-27 23:42:36 UTC (rev 
7960)
+++ trunk/freenet/src/freenet/node/Version.java 2006-01-28 00:31:38 UTC (rev 
7961)
@@ -20,7 +20,7 @@
        public static final String protocolVersion = "1.0";

        /** The build number of the current revision */
-       private static final int buildNumber = 411;
+       private static final int buildNumber = 412;

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


Reply via email to