X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fuk%2Fac%2Fvamsas%2Fclient%2Fpicking%2FPickEndPoint.java;h=2c3c1941d8468d9fa73656db48221cb81d0afcac;hb=2426fd4cc4177c5849336f2c54711114a1d957e4;hp=1864874e005b5514310aa7c8d98496a3d6f1c9ee;hpb=c8fdf290404aef51316aafd6422afdf49f5f025d;p=vamsas.git diff --git a/src/uk/ac/vamsas/client/picking/PickEndPoint.java b/src/uk/ac/vamsas/client/picking/PickEndPoint.java index 1864874..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) @@ -39,15 +41,15 @@ class PickEndPoint extends Thread in = new BufferedReader(new InputStreamReader(socket.getInputStream())); // Start the thread to listen for incoming messages -// logger.info("CLIENT: connection successful to port " -// + socket.getPort() + " via port " + socket.getLocalPort()); + logger.info("CLIENT: connection successful to port " + + socket.getPort() + " via port " + socket.getLocalPort()); start(); return true; } catch (Exception e) { - //logger.info("CLIENT: connection failed: " + e); + logger.info("CLIENT: connection failed: " + e); return false; } } @@ -69,44 +71,17 @@ 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(); } } - /** - * 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 (enabled) + while (manager!=null) { String str = in.readLine(); //logger.info("CLIENT: recv " + str + " from " + rPort); @@ -114,45 +89,47 @@ 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 - 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; + // 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(); // this may not be necessary - probably caused infinite loop on terminate() without null checks + } } - private void terminate() + void terminate() { - try { - if (socket.isConnected()) - 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) { - // TODO: generalize message class try { + if (str==null || str.length()==1) + return null; if (str.startsWith("CUSTOM")) return new CustomMessage(str.substring(6));