From: jprocter Date: Fri, 12 Jan 2007 15:26:11 +0000 (+0000) Subject: added (broken) haltPickManager methods X-Git-Tag: Release_0.2~188 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=c8fdf290404aef51316aafd6422afdf49f5f025d;p=vamsas.git added (broken) haltPickManager methods git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@332 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 c9e8507..1864874 100644 --- a/src/uk/ac/vamsas/client/picking/PickEndPoint.java +++ b/src/uk/ac/vamsas/client/picking/PickEndPoint.java @@ -4,9 +4,11 @@ import java.io.*; import java.net.*; import java.util.logging.*; +import org.apache.commons.logging.Log; + class PickEndPoint extends Thread { - private Logger logger = Logger.getLogger("uk.ac.vamsas.client.picking"); + private Log logger = org.apache.commons.logging.LogFactory.getLog(uk.ac.vamsas.client.picking.PickEndPoint.class); private Socket socket; private int rPort; @@ -71,13 +73,40 @@ class PickEndPoint extends Thread terminate(); } } - + /** + * state of server thread - running or not running + */ + private boolean running=false; + /** + * condition for server thread to stop + */ + private boolean enabled=true; + public void haltEndpoint() { + // TODO: FIX THIS METHOD + if (!running || !enabled) { + logger.debug("PickEndpoint is not running or already requested to halt."); + return; + } + logger.debug("Disabling pick endpoint."); + enabled=false; + terminate(); + while (running) { + try { + Thread.sleep(1); + } catch (Exception e) { + } + } + logger.debug("Pick endpoint has stopped."); + } + // void receive() (threaded) public void run() { + enabled=true; + running=true; try { - while (true) + while (enabled) { String str = in.readLine(); //logger.info("CLIENT: recv " + str + " from " + rPort); @@ -85,26 +114,34 @@ class PickEndPoint extends Thread // TODO: Spawn this off into the GUI Event-Dispatch thread... // Convert the string back into something API friendly + if (str!=null) { Message message = strToMessage(str); // Ignore corrupted or unknown message types if (message != null) manager.processMessage(this, message); + } } } catch (Exception e) { - // Means the other end of the connection has (probably died) so we need - // terminate this endpoint (if this is server side) - //logger.info("CLIENT: read failed: " + e); - - terminate(); - } + // Means the other end of the connection has (probably) died + logger.debug("Exception in receiver loop: "+e.getMessage()); + } + // terminate this endpoint (if this is server side) + //logger.info("CLIENT: read failed: " + e); + + terminate(); + + running=false; } private void terminate() { - try { socket.close(); } + try { + if (socket.isConnected()) + socket.close(); + } catch (IOException e) {} //logger.info("CLIENT: closing connection to port " + socket.getPort()); @@ -113,6 +150,7 @@ class PickEndPoint extends Thread private Message strToMessage(String str) { + // TODO: generalize message class try { if (str.startsWith("CUSTOM")) diff --git a/src/uk/ac/vamsas/client/picking/PickServer.java b/src/uk/ac/vamsas/client/picking/PickServer.java index 2590613..ab2aaf8 100644 --- a/src/uk/ac/vamsas/client/picking/PickServer.java +++ b/src/uk/ac/vamsas/client/picking/PickServer.java @@ -4,12 +4,14 @@ import java.io.*; import java.net.*; import java.util.logging.*; +import org.apache.commons.logging.Log; + /** * Server class that listens for incoming connections on a predefined port. */ class PickServer extends Thread { - private Logger logger = Logger.getLogger("uk.ac.vamsas.client.picking"); + private Log logger = org.apache.commons.logging.LogFactory.getLog(uk.ac.vamsas.client.picking.PickServer.class); // The port number we'll listen on static final int PORT = 53782; @@ -55,7 +57,35 @@ class PickServer extends Thread return false; } } - + /** + * state of server thread - running or not running + */ + private boolean running=false; + /** + * condition for server thread to stop + */ + private boolean enabled=true; + public void haltServer() { + // TODO: FIX THIS METHOD + if (!running || !enabled) { + logger.debug("PickServer is not running or already requested to halt."); + return; + } + logger.debug("Disabling pick server."); + enabled=false; + while (running) { + try { + serverSocket.close(); + } catch (Exception e) { + + } + try { + Thread.sleep(5); + } catch (Exception e) { + } + } + logger.debug("Pick server has stopped."); + } /** * Thread listening method - loops indefinitely listening for connections. * When one is received, the socket object is passed to the manager so it @@ -63,12 +93,13 @@ class PickServer extends Thread */ public void run() { + enabled=true; + running=true; //logger.info("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 - // is ended - while (true) + // TODO: terminate the server when haltServer is called (fails tests currently) + while (enabled) { try { @@ -79,5 +110,8 @@ class PickServer extends Thread } catch (IOException e) {} } + running=false; + manager.haltManager(); + } } \ 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 edfd573..5df3c43 100644 --- a/src/uk/ac/vamsas/client/picking/SocketManager.java +++ b/src/uk/ac/vamsas/client/picking/SocketManager.java @@ -4,6 +4,8 @@ import java.net.*; import java.util.*; import java.util.logging.*; +import org.apache.commons.logging.Log; + /** * Concrete implementation of the IPickManager interface that uses sockets for * message communication. An instance of this class attempts to run the central @@ -12,7 +14,7 @@ import java.util.logging.*; */ public class SocketManager implements IPickManager { - private Logger logger = Logger.getLogger("uk.ac.vamsas.client.picking"); + private Log logger = org.apache.commons.logging.LogFactory.getLog(uk.ac.vamsas.client.picking.SocketManager.class); // Maintains a list of client communication objects - each object represents // a way of talking to either: @@ -161,4 +163,17 @@ public class SocketManager implements IPickManager } } } + public void haltManager() { + // TODO: FIX this method + if (server==null) + throw new Error("Client Implementation Error: haltManager called on uninitialized SocketManager."); + logger.debug("Halting PickManager threads..."); + while (clients.size()>0) { + ((PickEndPoint)clients.getFirst()).haltEndpoint(); + } + server.haltServer(); + + logger.debug("Halted PickManager threads."); + + } } \ No newline at end of file