WIP
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 13 May 2008 16:45:55 +0000 (16:45 +0000)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 13 May 2008 16:45:55 +0000 (16:45 +0000)
git-svn-id: http://trooper/svn/projects/jSite/trunk@863 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/jsite/core/Core.java
src/net/pterodactylus/jsite/core/CoreImpl.java
src/net/pterodactylus/jsite/core/NodeManager.java
src/net/pterodactylus/jsite/core/RequestListener.java
src/net/pterodactylus/jsite/core/RequestManager.java
src/net/pterodactylus/jsite/gui/SwingInterface.java

index 0bf2cca..f04bb40 100644 (file)
@@ -19,6 +19,7 @@
 
 package net.pterodactylus.jsite.core;
 
+import java.net.UnknownHostException;
 import java.util.List;
 
 /**
@@ -50,8 +51,10 @@ public interface Core {
         * 
         * @param node
         *            The node to add
+        * @throws UnknownHostException
+        *             if the hostname of the node can not be resolved
         */
-       public void addNode(Node node);
+       public void addNode(Node node) throws UnknownHostException;
 
        /**
         * Removes the given node from the core.
@@ -104,4 +107,4 @@ public interface Core {
         */
        public void disconnectFromNode(Node node);
 
-}
\ No newline at end of file
+}
index f3ac5c0..682199e 100644 (file)
@@ -20,6 +20,7 @@
 package net.pterodactylus.jsite.core;
 
 import java.io.IOException;
+import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -264,6 +265,33 @@ public class CoreImpl implements Core, NodeListener, RequestListener {
                }
        }
 
+       /**
+        * Notifies all listeners that a request progressed.
+        * 
+        * @param node
+        *            The node that runs the request
+        * @param request
+        *            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 finalizedTotal
+        *            <code>true</code> if the total number of blocks in final,
+        *            <code>false</code> otherwise
+        */
+       private void fireRequestProgressed(Node node, Request request, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) {
+               for (CoreListener coreListener: coreListeners) {
+                       coreListener.requestProgressed(request, totalBlocks, requiredBlocks, successfulBlocks, failedBlocks, fatallyFailedBlocks, finalizedTotal);
+               }
+       }
+
        //
        // ACCESSORS
        //
@@ -375,7 +403,7 @@ public class CoreImpl implements Core, NodeListener, RequestListener {
        /**
         * {@inheritDoc}
         */
-       public void addNode(Node node) {
+       public void addNode(Node node) throws UnknownHostException {
                nodeManager.addNode(node);
                fireNodeAdded(node);
        }
@@ -393,7 +421,6 @@ public class CoreImpl implements Core, NodeListener, RequestListener {
         */
        public void connectToNode(Node node) {
                fireNodeConnecting(node);
-               nodeManager.addNode(node);
                nodeManager.connect(node);
        }
 
@@ -467,4 +494,12 @@ public class CoreImpl implements Core, NodeListener, RequestListener {
                fireRequestAdded(node, request);
        }
 
+       /**
+        * @see net.pterodactylus.jsite.core.RequestListener#requestProgressed(net.pterodactylus.jsite.core.Node,
+        *      net.pterodactylus.jsite.core.Request, int, int, int, int, int,
+        *      boolean)
+        */
+       public void requestProgressed(Node node, Request request, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) {
+       fireRequestProgressed(node, request, totalBlocks, requiredBlocks, successfulBlocks, failedBlocks, fatallyFailedBlocks, finalizedTotal);
+    }
 }
index b919cfe..971d5c5 100644 (file)
@@ -25,6 +25,7 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -257,10 +258,9 @@ public class NodeManager implements Iterable<Node>, HighLevelClientListener {
                logger.fine("loaded " + loadedNodes.size() + " nodes from config");
                synchronized (syncObject) {
                        nodes.clear();
-                       nodes.addAll(loadedNodes);
-               }
-               for (Node node: nodes) {
-                       fireNodeAdded(node);
+                       for (Node node: nodes) {
+                               addNode(node);
+                       }
                }
        }
 
@@ -301,11 +301,17 @@ public class NodeManager implements Iterable<Node>, HighLevelClientListener {
         * @see #connect(Node)
         * @param node
         *            The node to connect to
+        * @throws UnknownHostException
+        *             if the hostname of the node can not be resolved
         */
-       public void addNode(Node node) {
+       public void addNode(Node node) throws UnknownHostException {
                synchronized (syncObject) {
                        if (!nodes.contains(node)) {
+                               HighLevelClient highLevelClient= new HighLevelClient(clientName, node.getHostname(), node.getPort());
                                nodes.add(node);
+                               clientNodes.put(highLevelClient, node);
+                               nodeClients.put(node, highLevelClient);
+                               highLevelClient.addHighLevelClientListener(this);
                                fireNodeAdded(node);
                        }
                }
@@ -337,13 +343,11 @@ public class NodeManager implements Iterable<Node>, HighLevelClientListener {
         *            The node to connect to
         */
        public void connect(Node node) {
+               HighLevelClient highLevelClient;
+               synchronized (syncObject) {
+                       highLevelClient = nodeClients.get(node);
+               }
                try {
-                       HighLevelClient highLevelClient = new HighLevelClient(clientName, node.getHostname(), node.getPort());
-                       synchronized (syncObject) {
-                               clientNodes.put(highLevelClient, node);
-                               nodeClients.put(node, highLevelClient);
-                       }
-                       highLevelClient.addHighLevelClientListener(this);
                        highLevelClient.connect();
                } catch (IOException ioe1) {
                        fireNodeDisconnected(node, ioe1);
index eb0433a..32c55a8 100644 (file)
@@ -39,4 +39,27 @@ public interface RequestListener extends EventListener {
         */
        public void requestAdded(Node node, Request request);
 
+       /**
+        * Notifies a listener that a request made progress.
+        * 
+        * @param node
+        *            The node that runs the request
+        * @param request
+        *            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 finalizedTotal
+        *            <code>true</code> if the total number of blocks in final,
+        *            <code>false</code> otherwise
+        */
+       public void requestProgressed(Node node, Request request, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal);
+
 }
index 925573d..b312d43 100644 (file)
@@ -32,6 +32,8 @@ import java.util.logging.Logger;
 import net.pterodactylus.fcp.highlevel.HighLevelCallback;
 import net.pterodactylus.fcp.highlevel.HighLevelCallbackListener;
 import net.pterodactylus.fcp.highlevel.HighLevelClient;
+import net.pterodactylus.fcp.highlevel.HighLevelProgress;
+import net.pterodactylus.fcp.highlevel.HighLevelProgressListener;
 import net.pterodactylus.fcp.highlevel.RequestListResult;
 import net.pterodactylus.fcp.highlevel.RequestResult;
 import net.pterodactylus.util.logging.Logging;
@@ -45,7 +47,7 @@ import net.pterodactylus.util.logging.Logging;
  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
  * @version $Id$
  */
-public class RequestManager implements NodeListener {
+public class RequestManager implements NodeListener, HighLevelProgressListener {
 
        /** Logger. */
        private static final Logger logger = Logging.getLogger(RequestManager.class.getName());
@@ -98,6 +100,33 @@ public class RequestManager implements NodeListener {
                }
        }
 
+       /**
+        * Notifies all listeners that a request progressed.
+        * 
+        * @param node
+        *            The node that runs the request
+        * @param request
+        *            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 finalizedTotal
+        *            <code>true</code> if the total number of blocks in final,
+        *            <code>false</code> otherwise
+        */
+       private void fireRequestProgressed(Node node, Request request, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) {
+               for (RequestListener requestListener: requestListeners) {
+                       requestListener.requestProgressed(node, request, totalBlocks, requiredBlocks, successfulBlocks, failedBlocks, fatallyFailedBlocks, finalizedTotal);
+               }
+       }
+
        //
        // ACCESSORS
        //
@@ -168,7 +197,15 @@ public class RequestManager implements NodeListener {
         * {@inheritDoc}
         */
        public void nodeAdded(Node node) {
-               /* ignore. */
+               HighLevelClient highLevelClient = nodeManager.borrowHighLevelClient(node);
+               if (highLevelClient == null) {
+                       return;
+               }
+               try {
+                       highLevelClient.addHighLevelProgressListener(this);
+               } finally {
+                       nodeManager.returnHighLevelClient(highLevelClient);
+               }
        }
 
        /**
@@ -208,4 +245,16 @@ public class RequestManager implements NodeListener {
                /* TODO - remove all requests. */
        }
 
+       //
+       // INTERFACE HighLevelProgressListener
+       //
+
+       /**
+        * @see net.pterodactylus.fcp.highlevel.HighLevelProgressListener#progressReceived(java.lang.String,
+        *      net.pterodactylus.fcp.highlevel.HighLevelProgress)
+        */
+       public void progressReceived(String identifier, HighLevelProgress highLevelProgress) {
+               fireRequestProgressed(null, new Request(identifier), highLevelProgress.getTotalBlocks(), highLevelProgress.getRequiredBlocks(), highLevelProgress.getSuccessfulBlocks(), highLevelProgress.getFailedBlocks(), highLevelProgress.getFatallyFailedBlocks(), highLevelProgress.isTotalFinalized());
+       }
+
 }
index 53cd87e..bb0bb9e 100644 (file)
@@ -964,6 +964,7 @@ public class SwingInterface implements CoreListener, LoggingListener {
         */
        public void requestProgressed(Request request, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) {
                /* TODO - update table model */
+               mainWindow.setStatusBarText(request.getIdentifier() + " @ " + ((10000 * successfulBlocks / requiredBlocks) / 100.0));
        }
 
        //