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