implement some responses that finish a request
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 23 May 2008 12:21:00 +0000 (12:21 +0000)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 23 May 2008 12:21:00 +0000 (12:21 +0000)
git-svn-id: http://trooper/svn/projects/jFCPlib/branch/high-level-client@932 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/fcp/highlevel/HighLevelCallback.java
src/net/pterodactylus/fcp/highlevel/HighLevelClient.java
src/net/pterodactylus/fcp/highlevel/HighLevelProgress.java

index 541e502..101e16d 100644 (file)
@@ -135,7 +135,7 @@ public class HighLevelCallback<R extends HighLevelResult> {
         */
        public R getResult(long waitTime) throws InterruptedException {
                synchronized (syncObject) {
-                       if (!resultComplete) {
+                       while (!resultComplete) {
                                syncObject.wait(waitTime);
                        }
                        return result;
@@ -163,7 +163,7 @@ public class HighLevelCallback<R extends HighLevelResult> {
                                return;
                        }
                        resultComplete = true;
-                       syncObject.notify();
+                       syncObject.notifyAll();
                }
                fireGotResult();
        }
index 14aa5f3..b7064d0 100644 (file)
@@ -792,7 +792,20 @@ public class HighLevelClient {
                 *      net.pterodactylus.fcp.DataFound)
                 */
                public void receivedDataFound(FcpConnection fcpConnection, DataFound dataFound) {
-                       /* TODO */
+                       if (fcpConnection != HighLevelClient.this.fcpConnection) {
+                               return;
+                       }
+                       String identifier = dataFound.getIdentifier();
+                       HighLevelProgressCallback<DownloadResult> downloadCallback = downloadCallbacks.get(identifier);
+                       if (downloadCallback != null) {
+                               DownloadResult downloadResult = downloadCallback.getIntermediaryResult();
+                               downloadResult.setFinished(true);
+                               downloadResult.setFailed(false);
+                               downloadCallback.progressUpdated();
+                               downloadCallback.setDone();
+                       }
+                       HighLevelProgress highLevelProgress = new HighLevelProgress(identifier, true);
+                       fireProgressReceived(identifier , highLevelProgress);
                }
 
                /**
@@ -859,7 +872,9 @@ public class HighLevelClient {
                        String identifier = getFailed.getIdentifier();
                        HighLevelProgressCallback<DownloadResult> downloadCallback = downloadCallbacks.remove(identifier);
                        if (downloadCallback != null) {
-                               downloadCallback.getIntermediaryResult().setFailed(true);
+                               DownloadResult downloadResult = downloadCallback.getIntermediaryResult();
+                               downloadResult.setFailed(true);
+                               downloadResult.setFinished(true);
                                downloadCallback.setDone();
                                return;
                        }
@@ -1055,7 +1070,21 @@ public class HighLevelClient {
                 *      net.pterodactylus.fcp.PutFailed)
                 */
                public void receivedPutFailed(FcpConnection fcpConnection, PutFailed putFailed) {
-                       /* TODO */
+                       if (fcpConnection != HighLevelClient.this.fcpConnection) {
+                               return;
+                       }
+                       String identifier = putFailed.getIdentifier();
+                       HighLevelProgressCallback<DownloadResult> downloadCallback = downloadCallbacks.get(identifier);
+                       if (downloadCallback != null) {
+                               DownloadResult downloadResult = downloadCallback.getIntermediaryResult();
+                               downloadResult.setFailed(true);
+                               downloadResult.setFinished(true);
+                               downloadCallback.progressUpdated();
+                               downloadCallback.setDone();
+                       }
+                       /* TODO - check inserts */
+                       HighLevelProgress highLevelProgress = new HighLevelProgress(identifier, true);
+                       fireProgressReceived(identifier, highLevelProgress);
                }
 
                /**
@@ -1063,7 +1092,14 @@ public class HighLevelClient {
                 *      net.pterodactylus.fcp.PutFetchable)
                 */
                public void receivedPutFetchable(FcpConnection fcpConnection, PutFetchable putFetchable) {
-                       /* TODO */
+                       if (fcpConnection != HighLevelClient.this.fcpConnection) {
+                               return;
+                       }
+                       String identifier = putFetchable.getIdentifier();
+                       /* TODO - check inserts */
+                       HighLevelProgress highLevelProgress = new HighLevelProgress(identifier);
+                       highLevelProgress.setFetchable(true);
+                       fireProgressReceived(identifier, highLevelProgress);
                }
 
                /**
@@ -1071,7 +1107,20 @@ public class HighLevelClient {
                 *      net.pterodactylus.fcp.PutSuccessful)
                 */
                public void receivedPutSuccessful(FcpConnection fcpConnection, PutSuccessful putSuccessful) {
-                       /* TODO */
+                       if (fcpConnection != HighLevelClient.this.fcpConnection) {
+                               return;
+                       }
+                       String identifier = putSuccessful.getIdentifier();
+                       HighLevelProgressCallback<DownloadResult> downloadCallback = downloadCallbacks.get(identifier);
+                       if (downloadCallback != null) {
+                               DownloadResult downloadResult = downloadCallback.getIntermediaryResult();
+                               downloadResult.setFinished(true);
+                               downloadResult.setFailed(false);
+                               downloadCallback.progressUpdated();
+                       }
+                       /* TODO - check inserts */
+                       HighLevelProgress highLevelProgress = new HighLevelProgress(identifier, true);
+                       fireProgressReceived(identifier, highLevelProgress);
                }
 
                /**
@@ -1114,6 +1163,7 @@ public class HighLevelClient {
                                downloadResult.setTotalFinalized(simpleProgress.isFinalizedTotal());
                                downloadCallback.progressUpdated();
                        }
+                       /* TODO - check inserts */
                        HighLevelProgress highLevelProgress = new HighLevelProgress(identifier, simpleProgress.getTotal(), simpleProgress.getRequired(), simpleProgress.getSucceeded(), simpleProgress.getFailed(), simpleProgress.getFatallyFailed(), simpleProgress.isFinalizedTotal());
                        fireProgressReceived(identifier, highLevelProgress);
                }
index 675b950..403ac30 100644 (file)
@@ -21,12 +21,31 @@ package net.pterodactylus.fcp.highlevel;
 
 /**
  * Result for operations that send progress messages until they have completed.
+ * The fields of the progress message has to be checked in given order because
+ * if you receive this progress asynchronously via a
+ * {@link HighLevelProgressListener} the progress will not have any state, you
+ * will simply get the latest results, with other fields unset. First you should
+ * check whether {@link #isFinished()} returns <code>true</code>. If it does,
+ * the request is finished and {@link #isFailed()} will tell you whether the
+ * request has failed or succeeded. Other fields are not set. If the request is
+ * not yet finished, {@link #isFetchable()} will tell you whether the request
+ * has progressed to a state that allows other clients to fetch the inserted
+ * data. This is of course only valid for Put and PutDir requests. If none of
+ * those methods return <code>true</code>, you can use the block count
+ * methods to get detailed progress statistics. When progress you received is a
+ * {@link DownloadResult} you do not need to check
  * 
  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
  * @version $Id$
  */
 public class HighLevelProgress extends HighLevelResult {
 
+       /** Whether the request is finished. */
+       private boolean finished;
+
+       /** Whether a Put request should be fetchable now. */
+       private boolean fetchable;
+
        /** The number of total blocks. */
        private int totalBlocks;
 
@@ -56,6 +75,21 @@ public class HighLevelProgress extends HighLevelResult {
        }
 
        /**
+        * Creates a new high-level progress for a request that is finished.
+        * 
+        * @param identifier
+        *            The identifier of the request
+        * @param successful
+        *            <code>true</code> if the request finished successfully,
+        *            <code>false</code> otherwise
+        */
+       public HighLevelProgress(String identifier, boolean successful) {
+               this(identifier);
+               finished = true;
+               setFailed(!successful);
+       }
+
+       /**
         * Creates a new high-level progress with the given values.
         * 
         * @param identifier
@@ -85,6 +119,51 @@ public class HighLevelProgress extends HighLevelResult {
        }
 
        /**
+        * Returns whether this progress means that a request has finished. Use
+        * {@link #isFailed()} to check if the request failed.
+        * 
+        * @see #isFailed()
+        * @return <code>true</code> if the request has finished
+        */
+       public boolean isFinished() {
+               return finished;
+       }
+
+       /**
+        * Sets whether the request described by this progress has finished.
+        * 
+        * @param finished
+        *            <code>true</code> if the request has finished,
+        *            <code>false</code> otherwise
+        */
+       void setFinished(boolean finished) {
+               this.finished = finished;
+       }
+
+       /**
+        * Returns whether the request should be fetchable now, in case it was a Put
+        * request.
+        * 
+        * @return <code>true</code> if the request should be fetchable now,
+        *         <code>false</code> otherwise
+        */
+       public boolean isFetchable() {
+               return fetchable;
+       }
+
+       /**
+        * Sets whether the request should be fetchable now, in case it was a Put
+        * request.
+        * 
+        * @param fetchable
+        *            <code>true</code> if the request should be fetchable now,
+        *            <code>false</code> otherwise
+        */
+       void setFetchable(boolean fetchable) {
+               this.fetchable = fetchable;
+       }
+
+       /**
         * Returns the number of total blocks.
         * 
         * @return The number of total blocks