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
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
+ logger.debug("CLIENT: connection successful to port "\r
+ socket.getPort() + " via port " + socket.getLocalPort());\r
start();\r
\r
{\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
try\r
{\r
- while (true)\r
+ while (manager!=null)\r
{\r
String str = in.readLine(); \r
//logger.info("CLIENT: recv " + str + " from " + rPort);\r
// 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