implement node addition and removal events
[jSite2.git] / src / net / pterodactylus / jsite / core / RequestManager.java
index f2aa23f..925573d 100644 (file)
@@ -22,7 +22,10 @@ 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;
 
@@ -53,6 +56,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
        //
@@ -109,6 +116,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 +129,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 +167,38 @@ public class RequestManager implements NodeListener {
        /**
         * {@inheritDoc}
         */
+       public void nodeAdded(Node node) {
+               /* ignore. */
+       }
+
+       /**
+        * {@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. */
+               }
        }
 
        /**