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
{\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
}\r
catch (IOException e)\r
{\r
- //logger.info("SERVER: " + e);\r
+ logger.debug("SERVER: " + e);\r
return false;\r
}\r
}\r
*/\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
\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
*/\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
{\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
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