Picking API now has message classes and basic implementation of interfaces (for its...
[vamsas.git] / src / uk / ac / vamsas / client / picking / SocketManager.java
@@ -5,14 +5,14 @@ import java.util.*;
 import java.util.logging.*;\r
 \r
 /**\r
- * Manager class that maintains a list of connected clients in addition to\r
- * attempting to run a server to listen for client connections. If the server\r
- * initialization fails, then an attempt to connect (as a client) to another JVM\r
- * that is already a server happens instead.\r
+ * Concrete implementation of the IPickManager interface that uses sockets for\r
+ * message communication. An instance of this class attempts to run the central\r
+ * server for other clients; failing that, it attempts to connect to an existing\r
+ * server instead.\r
  */\r
-public class PickManager\r
+public class SocketManager implements IPickManager\r
 {\r
-       private static Logger logger = Logger.getLogger("uk.ac.vamsas.client.picking");\r
+       private Logger logger = Logger.getLogger("uk.ac.vamsas.client.picking");\r
        \r
        // Maintains a list of client communication objects - each object represents\r
        // a way of talking to either:\r
@@ -22,13 +22,17 @@ public class PickManager
        \r
        private PickServer server;\r
        \r
+       private IMessageHandler msgHandler;\r
+       \r
        /**\r
         * Constructs a new PickManager. This method will return immediately, while\r
         * a looping thread runs that attempts to run the server or connect to an\r
         * existing server.\r
         */\r
-       public PickManager()\r
+       public SocketManager()\r
        {\r
+//             logger.setLevel(Level.OFF);\r
+               \r
                server = new PickServer(this);\r
                clients = new LinkedList();\r
                \r
@@ -36,6 +40,16 @@ public class PickManager
        }\r
        \r
        /**\r
+        * Registers a message handler with the manager that allows the manager to\r
+        * perform a method callback on that object whenever a message is received.\r
+        * @param handler the message handler to register\r
+        */\r
+       public void registerMessageHandler(IMessageHandler handler)\r
+       {\r
+               msgHandler = handler;\r
+       }\r
+       \r
+       /**\r
         * Attempts to establish a connection between two client endpoints. This\r
         * method is called in two ways: 1) by the server when it receives a remote\r
         * request (in which case the socket will already be established) and 2) by\r
@@ -43,14 +57,14 @@ public class PickManager
         * @param socket a socket endpoint for the connection\r
         * @return true if the connection is successfully, false otherwise\r
         */\r
-       boolean addEndPoint(Socket socket)\r
+       synchronized boolean addEndPoint(Socket socket)\r
        {\r
                PickEndPoint client = new PickEndPoint(this, socket);\r
                \r
                if (client.openConnection())\r
                {\r
                        clients.add(client);\r
-                       logger.fine("List now contains " + clients.size() + " client(s)");\r
+                       logger.info("List now contains " + clients.size() + " client(s)");\r
                        return true;\r
                }\r
                \r
@@ -59,11 +73,11 @@ public class PickManager
        \r
        /**\r
         * Sends a message to other clients.\r
-        * @param str the message to send\r
+        * @param message the message to send\r
         */\r
-       public void sendMessage(String str)\r
+       public void sendMessage(Message message)\r
        {\r
-               forwardMessage(null, str);\r
+               forwardMessage(null, message);\r
        }\r
        \r
        /**\r
@@ -72,9 +86,9 @@ public class PickManager
         * but mustn't forward it *back* to client B.\r
         * @param origin the client endpoint that received the message (will be null\r
         * if the message originates from this instance\r
-        * @param str the message to send\r
+        * @param message the message to send\r
         */\r
-       private void forwardMessage(PickEndPoint origin, String str)\r
+       private void forwardMessage(PickEndPoint origin, Message message)\r
        {\r
                ListIterator itor = clients.listIterator();\r
                while (itor.hasNext())\r
@@ -82,7 +96,7 @@ public class PickManager
                        PickEndPoint client = (PickEndPoint) itor.next();\r
                        \r
                        if (client != origin)\r
-                               client.send(str);\r
+                               client.send(message);\r
                }\r
        }\r
        \r
@@ -90,14 +104,17 @@ public class PickManager
         * Handles a received message. If the manager is running in server mode,\r
         * then it must ensure the message is also forwarded to the other clients.\r
         * @param origin the client endpoint that received the message\r
-        * @str the message that was received\r
+        * @message the message that was received\r
         */\r
-       void handleMessage(PickEndPoint origin, String str)\r
-       {\r
+       void processMessage(PickEndPoint origin, Message message)\r
+       {       \r
                if (server.isServer())\r
-                       forwardMessage(origin, str);\r
-               \r
-               // TODO: pass message to VAMSAS API\r
+                       forwardMessage(origin, message);\r
+                       \r
+               if (msgHandler != null)\r
+                       msgHandler.handleMessage(message);\r
+               else\r
+                       logger.info("No handler available to deal with incoming message");\r
        }\r
        \r
        /**\r
@@ -105,10 +122,10 @@ public class PickManager
         * longer valid.\r
         * @param client the client endpoint to remove\r
         */\r
-       void removeEndPoint(PickEndPoint client)\r
+       synchronized void removeEndPoint(PickEndPoint client)\r
        {\r
                clients.remove(client);\r
-               logger.fine("List now contains " + clients.size() + " client(s)");\r
+               logger.info("List now contains " + clients.size() + " client(s)");\r
                \r
                // If there's no endpoints left, then we've lost all connections and\r
                // need to reinitialize\r
@@ -123,7 +140,7 @@ public class PickManager
        {\r
                public void run()\r
                {\r
-                       logger.fine("Initializing connection...");\r
+                       logger.info("Initializing connection...");\r
                        boolean connected = false;\r
                        \r
                        // Loop until we can get a connection (one way or the other)\r