add listener for progress events
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 12 May 2008 22:10:31 +0000 (22:10 +0000)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 12 May 2008 22:10:31 +0000 (22:10 +0000)
make high-level progress non-abstract

git-svn-id: http://trooper/svn/projects/jFCPlib/branch/high-level-client@857 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/fcp/highlevel/HighLevelClient.java
src/net/pterodactylus/fcp/highlevel/HighLevelProgress.java
src/net/pterodactylus/fcp/highlevel/HighLevelProgressListener.java [new file with mode: 0644]

index 1521703..a09b289 100644 (file)
@@ -120,6 +120,9 @@ public class HighLevelClient {
        /** The listener for the connection. */
        private HighLevelClientFcpListener highLevelClientFcpListener = new HighLevelClientFcpListener();
 
+       /** The listeners for progress events. */
+       private List<HighLevelProgressListener> highLevelProgressListeners = Collections.synchronizedList(new ArrayList<HighLevelProgressListener>());
+
        /** The callback for {@link #connect()}. */
        private HighLevelCallback<ConnectResult> connectCallback;
 
@@ -249,6 +252,41 @@ public class HighLevelClient {
                }
        }
 
+       /**
+        * Adds a high-level progress listener.
+        * 
+        * @param highLevelProgressListener
+        *            The high-level progress listener to add
+        */
+       public void addHighLevelProgressListener(HighLevelProgressListener highLevelProgressListener) {
+               highLevelProgressListeners.add(highLevelProgressListener);
+       }
+
+       /**
+        * Removes a high-level progress listener.
+        * 
+        * @param highLevelProgressListener
+        *            The high-level progress listener to remove
+        */
+       public void removeHighLevelProgressListener(HighLevelProgressListener highLevelProgressListener) {
+               highLevelProgressListeners.remove(highLevelProgressListener);
+       }
+
+       /**
+        * Notifies all listeners that the request with the given identifier made
+        * some progress.
+        * 
+        * @param identifier
+        *            The identifier of the request
+        * @param highLevelProgress
+        *            The progress of the request
+        */
+       private void fireProgressReceived(String identifier, HighLevelProgress highLevelProgress) {
+               for (HighLevelProgressListener highLevelProgressListener: highLevelProgressListeners) {
+                       highLevelProgressListener.progressReceived(identifier, highLevelProgress);
+               }
+       }
+
        //
        // ACCESSORS
        //
@@ -1035,8 +1073,8 @@ public class HighLevelClient {
                                downloadCallback.progressUpdated();
                                return;
                        }
-                       /* unknown identifier? */
-                       logger.warning("unknown identifier for SimpleProgress: " + identifier);
+                       HighLevelProgress highLevelProgress = new HighLevelProgress(identifier, simpleProgress.getTotal(), simpleProgress.getRequired(), simpleProgress.getSucceeded(), simpleProgress.getFailed(), simpleProgress.getFatallyFailed(), simpleProgress.isFinalizedTotal());
+                       fireProgressReceived(identifier, highLevelProgress);
                }
 
                /**
index ec648f8..675b950 100644 (file)
@@ -25,7 +25,7 @@ package net.pterodactylus.fcp.highlevel;
  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
  * @version $Id$
  */
-public abstract class HighLevelProgress extends HighLevelResult {
+public class HighLevelProgress extends HighLevelResult {
 
        /** The number of total blocks. */
        private int totalBlocks;
@@ -56,6 +56,35 @@ public abstract class HighLevelProgress extends HighLevelResult {
        }
 
        /**
+        * Creates a new high-level progress with the given values.
+        * 
+        * @param identifier
+        *            The identifier of the request
+        * @param totalBlocks
+        *            The total number of blocks
+        * @param requiredBlocks
+        *            The number of required blocks
+        * @param successfulBlocks
+        *            The number of successful blocks
+        * @param failedBlocks
+        *            The number of failed blocks
+        * @param fatallyFailedBlocks
+        *            The number of fatally failed blocks
+        * @param totalFinalized
+        *            <code>true</code> if the total number of blocks is
+        *            finalized, <code>false</code> otherwise
+        */
+       public HighLevelProgress(String identifier, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean totalFinalized) {
+               this(identifier);
+               this.totalBlocks = totalBlocks;
+               this.requiredBlocks = requiredBlocks;
+               this.successfulBlocks = successfulBlocks;
+               this.failedBlocks = failedBlocks;
+               this.fatallyFailedBlocks = fatallyFailedBlocks;
+               this.totalFinalized = totalFinalized;
+       }
+
+       /**
         * Returns the number of total blocks.
         * 
         * @return The number of total blocks
diff --git a/src/net/pterodactylus/fcp/highlevel/HighLevelProgressListener.java b/src/net/pterodactylus/fcp/highlevel/HighLevelProgressListener.java
new file mode 100644 (file)
index 0000000..926ea32
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * jFCPlib-high-level-client - HighLevelProgressListener.java -
+ * Copyright © 2008 David Roden
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+package net.pterodactylus.fcp.highlevel;
+
+import java.util.EventListener;
+
+/**
+ * Interface for objects that want to observe the progression of requests.
+ * 
+ * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
+ * @version $Id$
+ */
+public interface HighLevelProgressListener extends EventListener {
+
+       /**
+        * Notifies a listener that the request with the given identifier has made
+        * some progress.
+        * 
+        * @param identifier
+        *            The identifier of the request
+        * @param highLevelProgress
+        *            The progress of the request
+        */
+       public void progressReceived(String identifier, HighLevelProgress highLevelProgress);
+
+}