Author: toad
Date: 2007-03-24 18:26:08 +0000 (Sat, 24 Mar 2007)
New Revision: 12321

Added:
   trunk/freenet/src/freenet/client/FECJob.java
Modified:
   trunk/freenet/src/freenet/client/FECCodec.java
   trunk/freenet/src/freenet/client/StandardOnionFECCodec.java
   trunk/freenet/src/freenet/client/async/SplitFileFetcherSegment.java
   trunk/freenet/src/freenet/client/async/SplitFileInserterSegment.java
Log:
Refactor nextgens' changes to make them less hackish

Modified: trunk/freenet/src/freenet/client/FECCodec.java
===================================================================
--- trunk/freenet/src/freenet/client/FECCodec.java      2007-03-24 18:20:45 UTC 
(rev 12320)
+++ trunk/freenet/src/freenet/client/FECCodec.java      2007-03-24 18:26:08 UTC 
(rev 12321)
@@ -45,44 +45,10 @@
        }

        /**
-        * Decode all missing *data* blocks.
-        * Requires that the total number of available blocks is equal to or 
greater than the length of
-        * the data blocks array. (i.e. it is > k).
-        * Note that the last data bucket may be returned padded.
-        * This is one reason why it is important to set the data length,
-        * and truncate to it, when using FEC codes.
-        * @param dataBlockStatus The data blocks.
-        * @param checkBlockStatus The check blocks.
-        * @param blockLength The block length in bytes.
-        * @param bf The BucketFactory to use to generate buckets.
-        * @throws IOException If there is an error in decoding caused by an 
I/O error (usually involving buckets).
-        */
-       public abstract void decode(SplitfileBlock[] dataBlockStatus, 
SplitfileBlock[] checkBlockStatus, int blockLength, BucketFactory bf) throws 
IOException;
-
-       /**
-        * Encode all missing *check* blocks.
-        * Requires that all the data blocks be present.
-        * @param dataBlocks The data blocks.
-        * @param checkBlocks The check blocks.
-        * @param blockLength The block length in bytes.
-        * @param bf The BucketFactory to use to generate buckets.
-        * @throws IOException If there is an error in decoding caused by an 
I/O error (usually involving buckets).
-        */
-       public abstract void encode(SplitfileBlock[] dataBlocks, 
SplitfileBlock[] checkBlocks, int blockLength, BucketFactory bucketFactory) 
throws IOException;
-
-       /**
-        * Encode all missing *check* blocks.
-        * Requires that all the data blocks be present.
-        * @param dataBlocks The data blocks.
-        * @param checkBlocks The check blocks.
-        * @param blockLength The block length in bytes.
-        * @param bf The BucketFactory to use to generate buckets.
-        * @throws IOException If there is an error in decoding caused by an 
I/O error (usually involving buckets).
-        */
-       public abstract void encode(Bucket[] dataBlocks, Bucket[] checkBlocks, 
int blockLength, BucketFactory bucketFactory) throws IOException;
-
-       /**
         * How many check blocks?
         */
        public abstract int countCheckBlocks();
+       
+       /** Queue an asynchronous encode or decode job */
+       public abstract void addToQueue(FECJob job);
 }

Added: trunk/freenet/src/freenet/client/FECJob.java
===================================================================
--- trunk/freenet/src/freenet/client/FECJob.java                                
(rev 0)
+++ trunk/freenet/src/freenet/client/FECJob.java        2007-03-24 18:26:08 UTC 
(rev 12321)
@@ -0,0 +1,51 @@
+/**
+ * 
+ */
+package freenet.client;
+
+import 
freenet.client.StandardOnionFECCodec.StandardOnionFECCodecEncoderCallback;
+import freenet.support.api.Bucket;
+import freenet.support.api.BucketFactory;
+
+/**
+ * A class bundling the data meant to be FEC processed
+ * 
+ * @author Florent Daignière <nextgens at freenetproject.org>
+ */
+public class FECJob {
+       
+       final Bucket[] dataBlocks, checkBlocks;
+       final SplitfileBlock[] dataBlockStatus, checkBlockStatus;
+       final BucketFactory bucketFactory;
+       final int blockLength;
+       final StandardOnionFECCodecEncoderCallback callback;
+       final boolean isADecodingJob;
+       
+       public FECJob(SplitfileBlock[] dataBlockStatus, SplitfileBlock[] 
checkBlockStatus,  int blockLength, BucketFactory bucketFactory, 
StandardOnionFECCodecEncoderCallback callback, boolean isADecodingJob) {
+               this.dataBlockStatus = dataBlockStatus;
+               this.checkBlockStatus = checkBlockStatus;
+               
+               this.dataBlocks = new Bucket[dataBlockStatus.length];
+               this.checkBlocks = new Bucket[checkBlockStatus.length];
+               for(int i=0;i<dataBlocks.length;i++)
+                       this.dataBlocks[i] = dataBlockStatus[i].getData();
+               for(int i=0;i<checkBlocks.length;i++)
+                       this.checkBlocks[i] = checkBlockStatus[i].getData();
+               
+               this.blockLength = blockLength;
+               this.bucketFactory = bucketFactory;
+               this.callback = callback;
+               this.isADecodingJob = isADecodingJob;                   
+       }
+       
+       public FECJob(Bucket[] dataBlocks, Bucket[] checkBlocks, int 
blockLength, BucketFactory bucketFactory, StandardOnionFECCodecEncoderCallback 
callback, boolean isADecodingJob) {
+               this.dataBlocks = dataBlocks;
+               this.checkBlocks = checkBlocks;
+               this.dataBlockStatus = null;
+               this.checkBlockStatus = null;
+               this.blockLength = blockLength;
+               this.bucketFactory = bucketFactory;
+               this.callback = callback;
+               this.isADecodingJob = isADecodingJob;
+       }
+}
\ No newline at end of file

Modified: trunk/freenet/src/freenet/client/StandardOnionFECCodec.java
===================================================================
--- trunk/freenet/src/freenet/client/StandardOnionFECCodec.java 2007-03-24 
18:20:45 UTC (rev 12320)
+++ trunk/freenet/src/freenet/client/StandardOnionFECCodec.java 2007-03-24 
18:26:08 UTC (rev 12321)
@@ -112,20 +112,6 @@
                logMINOR = Logger.shouldLog(Logger.MINOR, this);
        }

-       /**
-        * @deprecated
-        */
-       public void decode(SplitfileBlock[] dataBlockStatus, SplitfileBlock[] 
checkBlockStatus, int blockLength, BucketFactory bf) throws IOException {
-               logMINOR = Logger.shouldLog(Logger.MINOR, getClass());
-               if(logMINOR) 
-                       Logger.minor(this, "Queueing decode: " + 
dataBlockStatus.length
-                               + " data blocks, " + checkBlockStatus.length
-                               + " check blocks, block length " + blockLength 
+ " with "
-                               + this, new Exception("debug"));
-               
-               realDecode(dataBlockStatus, checkBlockStatus, blockLength, bf);
-       }
-       
        private void realDecode(SplitfileBlock[] dataBlockStatus, 
SplitfileBlock[] checkBlockStatus, int blockLength, BucketFactory bf) throws 
IOException {
                if(logMINOR)
                        Logger.minor(this, "Doing decode: " + 
dataBlockStatus.length
@@ -255,51 +241,6 @@
        }

        /**
-        * @deprecated
-        */
-       public void encode(Bucket[] dataBlockStatus, Bucket[] checkBlockStatus, 
int blockLength, BucketFactory bf) throws IOException {
-               logMINOR = Logger.shouldLog(Logger.MINOR, getClass());
-               if(logMINOR)
-                       Logger.minor(this, "Queueing encode: " + 
dataBlockStatus.length
-                                       + " data blocks, " + 
checkBlockStatus.length
-                                       + " check blocks, block length " + 
blockLength + " with "
-                                       + this, new Exception("debug"));
-
-               Thread currentThread = Thread.currentThread();
-               final int currentThreadPriority = currentThread.getPriority();
-               final String oldThreadName = currentThread.getName();
-
-               currentThread.setName("Encoder thread for k="+k+", n="+n+" 
enabled at "+System.currentTimeMillis());
-               currentThread.setPriority(Thread.MIN_PRIORITY);
-
-               long startTime = System.currentTimeMillis();
-               realEncode(dataBlockStatus, checkBlockStatus, blockLength, bf);
-               long endTime = System.currentTimeMillis();
-               if(logMINOR)
-                       Logger.minor(this, "Splitfile encode: k="+k+", n="+n+" 
encode took "+(endTime-startTime)+"ms");
-
-               currentThread.setName(oldThreadName);
-               currentThread.setPriority(currentThreadPriority);
-       }
-       
-       /**
-        * @deprecated
-        */
-       public void encode(SplitfileBlock[] dataBlockStatus, SplitfileBlock[] 
checkBlockStatus, int blockLength, BucketFactory bf) throws IOException {
-               Bucket[] dataBlocks = new Bucket[dataBlockStatus.length];
-               Bucket[] checkBlocks = new Bucket[checkBlockStatus.length];
-               for(int i=0;i<dataBlocks.length;i++)
-                       dataBlocks[i] = dataBlockStatus[i].getData();
-               for(int i=0;i<checkBlocks.length;i++)
-                       checkBlocks[i] = checkBlockStatus[i].getData();
-               encode(dataBlocks, checkBlocks, blockLength, bf);
-               for(int i=0;i<dataBlocks.length;i++)
-                       dataBlockStatus[i].setData(dataBlocks[i]);
-               for(int i=0;i<checkBlocks.length;i++)
-                       checkBlockStatus[i].setData(checkBlocks[i]);
-       }
-
-       /**
         * Do the actual encode.
         */
        private void realEncode(Bucket[] dataBlockStatus,
@@ -482,48 +423,6 @@
        }

        /**
-        * A class bundleing the data meant to be FEC processed
-        * 
-        * @author Florent Daigni&egrave;re &lt;nextgens at 
freenetproject.org&gt;
-        */
-       public class FECJob {
-               final Bucket[] dataBlocks, checkBlocks;
-               final SplitfileBlock[] dataBlockStatus, checkBlockStatus;
-               final BucketFactory bucketFactory;
-               final int blockLength;
-               final StandardOnionFECCodecEncoderCallback callback;
-               final boolean isADecodingJob;
-               
-               public FECJob(SplitfileBlock[] dataBlockStatus, 
SplitfileBlock[] checkBlockStatus,  int blockLength, BucketFactory 
bucketFactory, StandardOnionFECCodecEncoderCallback callback, boolean 
isADecodingJob) {
-                       this.dataBlockStatus = dataBlockStatus;
-                       this.checkBlockStatus = checkBlockStatus;
-                       
-                       this.dataBlocks = new Bucket[dataBlockStatus.length];
-                       this.checkBlocks = new Bucket[checkBlockStatus.length];
-                       for(int i=0;i<dataBlocks.length;i++)
-                               this.dataBlocks[i] = 
dataBlockStatus[i].getData();
-                       for(int i=0;i<checkBlocks.length;i++)
-                               this.checkBlocks[i] = 
checkBlockStatus[i].getData();
-                       
-                       this.blockLength = blockLength;
-                       this.bucketFactory = bucketFactory;
-                       this.callback = callback;
-                       this.isADecodingJob = isADecodingJob;                   
-               }
-               
-               public FECJob(Bucket[] dataBlocks, Bucket[] checkBlocks, int 
blockLength, BucketFactory bucketFactory, StandardOnionFECCodecEncoderCallback 
callback, boolean isADecodingJob) {
-                       this.dataBlocks = dataBlocks;
-                       this.checkBlocks = checkBlocks;
-                       this.dataBlockStatus = null;
-                       this.checkBlockStatus = null;
-                       this.blockLength = blockLength;
-                       this.bucketFactory = bucketFactory;
-                       this.callback = callback;
-                       this.isADecodingJob = isADecodingJob;
-               }
-       }
-       
-       /**
         * A private Thread started by {@link StandardOnionFECCodec}...
         * 
         * @author Florent Daigni&egrave;re &lt;nextgens at 
freenetproject.org&gt;

Modified: trunk/freenet/src/freenet/client/async/SplitFileFetcherSegment.java
===================================================================
--- trunk/freenet/src/freenet/client/async/SplitFileFetcherSegment.java 
2007-03-24 18:20:45 UTC (rev 12320)
+++ trunk/freenet/src/freenet/client/async/SplitFileFetcherSegment.java 
2007-03-24 18:26:08 UTC (rev 12321)
@@ -9,6 +9,7 @@

 import freenet.client.ArchiveContext;
 import freenet.client.FECCodec;
+import freenet.client.FECJob;
 import freenet.client.FailureCodeTracker;
 import freenet.client.FetchContext;
 import freenet.client.FetchException;
@@ -194,8 +195,7 @@
                codec = FECCodec.getCodec(splitfileType, dataKeys.length, 
checkKeys.length);

                if(splitfileType != Metadata.SPLITFILE_NONREDUNDANT) {
-                       StandardOnionFECCodec fec = 
(StandardOnionFECCodec)codec;
-                       fec.addToQueue(fec.new FECJob(dataBuckets, 
checkBuckets, CHKBlock.DATA_LENGTH, fetchContext.bucketFactory, this, true));
+                       codec.addToQueue(new FECJob(dataBuckets, checkBuckets, 
CHKBlock.DATA_LENGTH, fetchContext.bucketFactory, this, true));
                        // Now have all the data blocks (not necessarily all 
the check blocks)
                }
        }
@@ -233,8 +233,7 @@

                // Encode any check blocks we don't have
                if(codec != null) {
-                       StandardOnionFECCodec fec = (StandardOnionFECCodec) 
codec;
-                       fec.addToQueue(fec.new FECJob(dataBuckets, 
checkBuckets, 32768, fetchContext.bucketFactory, this, false));
+                       codec.addToQueue(new FECJob(dataBuckets, checkBuckets, 
32768, fetchContext.bucketFactory, this, false));
                }
        }


Modified: trunk/freenet/src/freenet/client/async/SplitFileInserterSegment.java
===================================================================
--- trunk/freenet/src/freenet/client/async/SplitFileInserterSegment.java        
2007-03-24 18:20:45 UTC (rev 12320)
+++ trunk/freenet/src/freenet/client/async/SplitFileInserterSegment.java        
2007-03-24 18:26:08 UTC (rev 12321)
@@ -3,6 +3,7 @@
 import java.net.MalformedURLException;

 import freenet.client.FECCodec;
+import freenet.client.FECJob;
 import freenet.client.FailureCodeTracker;
 import freenet.client.InserterContext;
 import freenet.client.InserterException;
@@ -423,8 +424,7 @@
                                // Encode blocks
                                synchronized(this) {
                                        if(!encoded){
-                                               StandardOnionFECCodec fec = 
(StandardOnionFECCodec) splitfileAlgo; 
-                                               fec.addToQueue(fec.new 
FECJob(dataBlocks, checkBlocks, CHKBlock.DATA_LENGTH, 
blockInsertContext.persistentBucketFactory, this, false));
+                                               splitfileAlgo.addToQueue(new 
FECJob(dataBlocks, checkBlocks, CHKBlock.DATA_LENGTH, 
blockInsertContext.persistentBucketFactory, this, false));
                                        }
                                }                               
                                fin = false;


Reply via email to