Start implementation of request loading when a node connects.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 23 May 2009 15:23:21 +0000 (17:23 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 23 May 2009 15:23:21 +0000 (17:23 +0200)
src/net/pterodactylus/jsite/core/RequestManager.java

index 8066a12..cc381cf 100644 (file)
 package net.pterodactylus.jsite.core;
 
 import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import net.pterodactylus.fcp.highlevel.FcpClient;
+import net.pterodactylus.fcp.highlevel.FcpException;
+import net.pterodactylus.fcp.highlevel.Request;
+import net.pterodactylus.jsite.util.IdGenerator;
+import net.pterodactylus.util.number.Hex;
 
 /**
  * Manages requests.
@@ -28,9 +40,15 @@ import java.io.IOException;
  */
 public class RequestManager implements NodeListener {
 
+       /** The logger. */
+       private static final Logger logger = Logger.getLogger(RequestManager.class.getName());
+
        /** The node manager. */
        private final NodeManager nodeManager;
 
+       /** Maps request IDs to requests. */
+       private final Map<String, Request> idRequests = Collections.synchronizedMap(new HashMap<String, Request>());
+
        /**
         * Creates a new request manager.
         *
@@ -46,13 +64,42 @@ public class RequestManager implements NodeListener {
        //
 
        public void load() throws IOException {
-
        }
 
        public void save() throws IOException {
        }
 
        //
+       // PRIVATE METHODS
+       //
+
+       /**
+        * Checks whether the given client token is a client token created by this
+        * request manager.
+        *
+        * @param clientToken
+        *            The client token to check
+        * @return {@code true} if the client token was created by this request
+        *         manager, {@code false} otherwise
+        */
+       private boolean isKnownClientToken(String clientToken) {
+               String[] clientTokenParts = clientToken.split("\\.");
+               if (clientTokenParts.length != 3) {
+                       return false;
+               }
+               String projectIdString = clientTokenParts[0];
+               if (projectIdString.length() != (IdGenerator.DEFAULT_LENGTH * 2)) {
+                       return false;
+               }
+               try {
+                       Hex.toByte(projectIdString);
+               } catch (NumberFormatException nfe1) {
+                       return false;
+               }
+               return true;
+       }
+
+       //
        // INTERFACE NodeListener
        //
 
@@ -60,21 +107,42 @@ public class RequestManager implements NodeListener {
         * {@inheritDoc}
         */
        public void nodeAdded(Node node) {
-               /* TODO */
+               /* ignore. */
        }
 
        /**
         * {@inheritDoc}
         */
        public void nodeConnected(Node node) {
-               /* TODO */
+               FcpClient fcpClient = nodeManager.getFcpClient(node);
+               if (fcpClient == null) {
+                       logger.log(Level.WARNING, "Got no FCP client for node (" + node + ")!");
+                       return;
+               }
+               try {
+                       Collection<Request> requests = fcpClient.getRequests(true);
+                       for (Request request : requests) {
+                               String clientToken = request.getClientToken();
+                               if ((clientToken == null) || (clientToken.trim().length() == 0)) {
+                                       continue;
+                               }
+                               if (!isKnownClientToken(clientToken)) {
+                                       continue;
+                               }
+                               /* TODO - process request. */
+                       }
+               } catch (IOException ioe1) {
+                       logger.log(Level.WARNING, "Could not get requests from node (" + node + ")!", ioe1);
+               } catch (FcpException fe1) {
+                       logger.log(Level.WARNING, "Could not get requests from node (" + node + ")!", fe1);
+               }
        }
 
        /**
         * {@inheritDoc}
         */
        public void nodeConnectionFailed(Node node, Throwable cause) {
-               /* TODO */
+               /* ignore. */
        }
 
        /**
@@ -88,7 +156,7 @@ public class RequestManager implements NodeListener {
         * {@inheritDoc}
         */
        public void nodeRemoved(Node node) {
-               /* TODO */
+               /* ignore. */
        }
 
 }