removed infinite loop on server shutdown
authorjprocter <jprocter@compbio.dundee.ac.uk>
Thu, 15 Mar 2007 14:16:08 +0000 (14:16 +0000)
committerjprocter <jprocter@compbio.dundee.ac.uk>
Thu, 15 Mar 2007 14:16:08 +0000 (14:16 +0000)
git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@357 be28352e-c001-0410-b1a7-c7978e42abec

src/uk/ac/vamsas/client/picking/PickEndPoint.java
src/uk/ac/vamsas/client/picking/PickServer.java
src/uk/ac/vamsas/client/picking/SocketManager.java

index c8ebfec..2c3c194 100644 (file)
@@ -14,7 +14,9 @@ class PickEndPoint extends Thread
        private int rPort;\r
        private BufferedWriter os;\r
        private BufferedReader in;\r
-\r
+       /**\r
+   * reference to server or null if no comms session active\r
+   */\r
        private SocketManager manager;  \r
        \r
        PickEndPoint(SocketManager manager, Socket s)\r
@@ -69,7 +71,7 @@ class PickEndPoint extends Thread
                {\r
                        //logger.info("CLIENT: failed to send");\r
                        \r
-                       // TODO: terminate the connection on a failed send or retry?\r
+                       // TODO: terminate the connection on a failed send or retry? (Has this been done? JBP)\r
                        terminate();\r
                }\r
        }\r
@@ -79,7 +81,7 @@ class PickEndPoint extends Thread
        {\r
                try\r
                {\r
-                       while (true)\r
+                       while (manager!=null)\r
                        {\r
                                String str = in.readLine();                             \r
                                //logger.info("CLIENT: recv " + str + " from " + rPort);\r
@@ -100,17 +102,26 @@ class PickEndPoint extends Thread
                        // terminate this endpoint (if this is server side)\r
                        //logger.info("CLIENT: read failed: " + e);\r
                                                \r
-                       terminate();\r
+                       terminate(); // this may not be necessary - probably caused infinite loop on terminate() without null checks\r
                }\r
        }\r
        \r
        void terminate()\r
        {\r
-               try { socket.close(); }\r
-               catch (IOException e) {}\r
-               \r
-               //logger.info("CLIENT: closing connection to port " + socket.getPort());\r
-               manager.removeEndPoint(this);\r
+    SocketManager mgr=manager;\r
+    manager=null; // stops receive thread\r
+               if (socket!=null) {\r
+      try { socket.close(); }\r
+      catch (IOException e) {}\r
+      socket=null;\r
+    }\r
+    //logger.info("CLIENT: closing connection to port " + socket.getPort());\r
+               // terminate() can be called by the socket manager to shutdown the connection,\r
+    // or the receive thread after an exception has been received\r
+    // (which includes the above case when the socket is closed.\r
+    //  so we only want to removeEndPoint once - for the initial call to terminate()\r
+    if (mgr!=null)\r
+      mgr.removeEndPoint(this);\r
        }\r
        \r
        private Message strToMessage(String str)\r
index b3f54e5..3198427 100644 (file)
@@ -54,7 +54,7 @@ class PickServer extends Thread
                }\r
                catch (IOException e)\r
                {\r
-                       //logger.info("SERVER: " + e);\r
+                       logger.debug("SERVER: " + e);\r
                        return false;\r
                }\r
        }\r
@@ -66,7 +66,7 @@ class PickServer extends Thread
         */\r
        public void run()\r
        {\r
-               logger.info("SERVER: listening on " + PORT + " - SERVER");\r
+               logger.debug("SERVER: listening on " + PORT + " - SERVER");\r
                \r
                // Loop forever, accepting connectons from other clients\r
                // TODO: add in the ability to terminate the server if a VAMSAS session\r
@@ -86,12 +86,13 @@ class PickServer extends Thread
        \r
        void terminate()\r
        {\r
-               logger.info("Server shutting down...");\r
+               logger.debug("Server shutting down...");\r
                isAlive=false;\r
                try { serverSocket.close(); }\r
                catch (Exception e)\r
                {\r
                        logger.error(e);\r
                }\r
+    logger.debug("Server shut down complete.");\r
        }\r
 }
\ No newline at end of file
index ad12a71..bc9dd59 100644 (file)
@@ -35,7 +35,7 @@ public class SocketManager implements IPickManager
         */\r
        public SocketManager()\r
        {\r
-//             logger.setLevel(Level.OFF);\r
+       //logger.setLevel(Level.OFF);\r
                \r
                server = new PickServer(this);\r
                clients = new LinkedList();\r
@@ -149,15 +149,15 @@ public class SocketManager implements IPickManager
        {\r
                public void run()\r
                {\r
-                       logger.info("Initializing connection...");\r
+                       logger.debug("Initializing connection...");\r
                        boolean connected = false;\r
                        \r
                        // Loop until we can get a connection (one way or the other)\r
-                       while (!connected)\r
+                       while (!connected && isRunning)\r
                        {\r
                                // Sleep for a rnd time so we don't end up with all the VAMSAS\r
                                // apps trying to initialize servers at the same time\r
-                               try { Thread.sleep((int)Math.random()); }\r
+                               try { Thread.sleep((int)(10*Math.random())); }\r
                                catch (InterruptedException e) {}\r
                                        \r
                                // Attempt to open the server port...\r
@@ -168,20 +168,25 @@ public class SocketManager implements IPickManager
                                else if (addEndPoint(null))\r
                                        connected = true;\r
                        }\r
+      logger.debug("Completed initializing connection.");\r
                }\r
        }\r
        \r
        public void shutdown()\r
        {\r
-               if (server == null)\r
+               logger.debug("Shutting down socket manager.");\r
+    if (server == null)\r
                        throw new Error("Client Implementation Error: shutdown() called on uninitialized SocketManager.");\r
                \r
                isRunning = false;\r
                \r
                if (server.isServer())\r
                        server.terminate();\r
-\r
-               while (clients.size() > 0)\r
-                       ((PickEndPoint)clients.getFirst()).terminate();\r
-       }\r
+               \r
+               while (clients.size() > 0) {\r
+                       logger.debug("Closing endpoint.");\r
+      ((PickEndPoint)clients.getFirst()).terminate();\r
+               }\r
+    logger.debug("Shutdown of socketmanager completed.");\r
+  }\r
 }
\ No newline at end of file