ensured backups are only made when backup==true
[vamsas.git] / src / uk / ac / vamsas / client / picking / PickEndPoint.java
index 1864874..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
@@ -39,15 +41,15 @@ class PickEndPoint extends Thread
                        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));\r
                \r
                        // Start the thread to listen for incoming messages\r
-//                     logger.info("CLIENT: connection successful to port "\r
-//                             + socket.getPort() + " via port " + socket.getLocalPort());\r
+                       logger.info("CLIENT: connection successful to port "\r
+                               + socket.getPort() + " via port " + socket.getLocalPort());\r
                        start();\r
                        \r
                        return true;\r
                }\r
                catch (Exception e)\r
                {\r
-                       //logger.info("CLIENT: connection failed: " + e);\r
+                       logger.info("CLIENT: connection failed: " + e);\r
                        return false;\r
                }\r
        }\r
@@ -69,44 +71,17 @@ 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
-  /**\r
-   * state of server thread - running or not running\r
-   */\r
-  private boolean running=false;\r
-  /**\r
-   * condition for server thread to stop\r
-   */\r
-  private boolean enabled=true;\r
-  public void haltEndpoint() {\r
-  // TODO: FIX THIS METHOD\r
-    if (!running || !enabled) {\r
-      logger.debug("PickEndpoint is not running or already requested to halt.");\r
-      return;\r
-    }\r
-    logger.debug("Disabling pick endpoint.");\r
-    enabled=false;\r
-    terminate();\r
-    while (running) {\r
-      try {\r
-        Thread.sleep(1);\r
-      } catch (Exception e) {\r
-      }\r
-    }\r
-    logger.debug("Pick endpoint has stopped.");\r
-  }\r
-\r
+       \r
        // void receive() (threaded)\r
        public void run()\r
        {\r
-    enabled=true;\r
-    running=true;\r
                try\r
                {\r
-                       while (enabled)\r
+                       while (manager!=null)\r
                        {\r
                                String str = in.readLine();                             \r
                                //logger.info("CLIENT: recv " + str + " from " + rPort);\r
@@ -114,45 +89,47 @@ class PickEndPoint extends Thread
                                // TODO: Spawn this off into the GUI Event-Dispatch thread...\r
                                \r
                                // Convert the string back into something API friendly\r
-        if (str!=null) {\r
                                Message message = strToMessage(str);\r
                                \r
                                // Ignore corrupted or unknown message types\r
                                if (message != null)                            \r
                                        manager.processMessage(this, message);\r
-        }\r
                        }\r
                }\r
                catch (Exception e)\r
                {\r
-                       // Means the other end of the connection has (probably) died\r
-      logger.debug("Exception in receiver loop: "+e.getMessage());\r
-    }\r
-               // terminate this endpoint (if this is server side)\r
-    //logger.info("CLIENT: read failed: " + e);\r
-          \r
-    terminate();\r
-  \r
-               running=false;\r
+                       // Means the other end of the connection has (probably died) so we need\r
+                       // terminate this endpoint (if this is server side)\r
+                       //logger.info("CLIENT: read failed: " + e);\r
+                                               \r
+                       terminate(); // this may not be necessary - probably caused infinite loop on terminate() without null checks\r
+               }\r
        }\r
        \r
-       private void terminate()\r
+       void terminate()\r
        {\r
-               try { \r
-      if (socket.isConnected())\r
-        socket.close(); \r
-      }\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
        {\r
-    // TODO: generalize message class \r
                try\r
                {\r
+      if (str==null || str.length()==1)\r
+        return null;\r
                        if (str.startsWith("CUSTOM"))\r
                                return new CustomMessage(str.substring(6));\r
                \r