fix warnings
[jSite2.git] / src / net / pterodactylus / jsite / core / RequestManager.java
index f2aa23f..b312d43 100644 (file)
@@ -22,13 +22,18 @@ package net.pterodactylus.jsite.core;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
 import java.util.logging.Level;
 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;
@@ -42,7 +47,7 @@ import net.pterodactylus.util.logging.Logging;
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
  * @version $Id$
  */
-public class RequestManager implements NodeListener {
+public class RequestManager implements NodeListener, HighLevelProgressListener {
 
        /** Logger. */
        private static final Logger logger = Logging.getLogger(RequestManager.class.getName());
@@ -53,6 +58,10 @@ public class RequestManager implements NodeListener {
        /** The node manager. */
        private NodeManager nodeManager;
 
+       /** Request lists for all nodes. */
+       @SuppressWarnings("unused")
+       private Map<Node, Set<Request>> nodeRequests = Collections.synchronizedMap(new HashMap<Node, Set<Request>>());
+
        //
        // EVENT MANAGEMENT
        //
@@ -91,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
        //
@@ -109,6 +145,10 @@ public class RequestManager implements NodeListener {
        // ACTIONS
        //
 
+       //
+       // PRIVATE ACTIONS
+       //
+
        /**
         * Requests a list of all running requests from a node. This method will
         * block until the request has been sent!
@@ -118,7 +158,7 @@ public class RequestManager implements NodeListener {
         * @throws IOException
         *             if an I/O error occurs while communicating with the node
         */
-       public void getRequests(final Node node) throws IOException {
+       private void getRequests(final Node node) throws IOException {
                HighLevelClient highLevelClient = nodeManager.borrowHighLevelClient(node);
                if (highLevelClient == null) {
                        logger.log(Level.WARNING, "no client for node: " + node);
@@ -156,8 +196,46 @@ public class RequestManager implements NodeListener {
        /**
         * {@inheritDoc}
         */
+       public void nodeAdded(Node node) {
+               HighLevelClient highLevelClient = nodeManager.borrowHighLevelClient(node);
+               if (highLevelClient == null) {
+                       return;
+               }
+               try {
+                       highLevelClient.addHighLevelProgressListener(this);
+               } finally {
+                       nodeManager.returnHighLevelClient(highLevelClient);
+               }
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void nodeRemoved(Node node) {
+               /* ignore. */
+       }
+
+       /**
+        * {@inheritDoc}
+        */
        public void nodeConnected(Node node) {
-               /* TODO - get all requests. */
+               HighLevelClient highLevelClient = nodeManager.borrowHighLevelClient(node);
+               if (highLevelClient == null) {
+                       logger.log(Level.WARNING, "got no high-level client for node " + node);
+                       return;
+               }
+               try {
+                       highLevelClient.setWatchGlobal(true);
+               } catch (IOException ioe1) {
+                       /* ignore exception, disconnects are handled elsewhere. */
+               } finally {
+                       nodeManager.returnHighLevelClient(highLevelClient);
+               }
+               try {
+                       getRequests(node);
+               } catch (IOException e) {
+                       /* ignore exception, disconnects are handled elsewhere. */
+               }
        }
 
        /**
@@ -167,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());
+       }
+
 }