X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fuk%2Fac%2Fvamsas%2Fclient%2Fpicking%2FSocketManager.java;h=bc9dd59a8455fbecf1580384f6c94055c5f29288;hb=57eddff1b5cec20440c4181f481c9adfbc5324b2;hp=f4f4ab7faa9063b0b91acc8bb97ba3221120cb1c;hpb=f46a9401190f34549d6c1bef0c2ce8a920cab83d;p=vamsas.git diff --git a/src/uk/ac/vamsas/client/picking/SocketManager.java b/src/uk/ac/vamsas/client/picking/SocketManager.java index f4f4ab7..bc9dd59 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: @@ -24,6 +26,8 @@ public class SocketManager implements IPickManager private IMessageHandler msgHandler; + private boolean isRunning = true; + /** * Constructs a new PickManager. This method will return immediately, while * a looping thread runs that attempts to run the server or connect to an @@ -31,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(); @@ -64,7 +68,7 @@ public class SocketManager implements IPickManager if (client.openConnection()) { clients.add(client); - logger.info("List now contains " + clients.size() + " client(s)"); + //logger.info("List now contains " + clients.size() + " client(s)"); return true; } @@ -90,13 +94,18 @@ public class SocketManager implements IPickManager */ private void forwardMessage(PickEndPoint origin, Message message) { - ListIterator itor = clients.listIterator(); - while (itor.hasNext()) + for (int i = clients.size()-1; i >= 0; i--) { - PickEndPoint client = (PickEndPoint) itor.next(); - - if (client != origin) - client.send(message); + try + { + PickEndPoint client = (PickEndPoint) clients.get(i); + if (client != origin) + client.send(message); + } + catch (Exception e) + { + System.out.println("FORWARD: " + e); + } } } @@ -113,8 +122,8 @@ public class SocketManager implements IPickManager if (msgHandler != null) msgHandler.handleMessage(message); - else - logger.info("No handler available to deal with incoming message"); +// else +// logger.info("No handler available to deal with incoming message"); } /** @@ -125,11 +134,11 @@ public class SocketManager implements IPickManager synchronized void removeEndPoint(PickEndPoint client) { clients.remove(client); - logger.info("List now contains " + clients.size() + " client(s)"); + //logger.info("List now contains " + clients.size() + " client(s)"); // If there's no endpoints left, then we've lost all connections and - // need to reinitialize - if (clients.size() == 0) + // need to reinitialize - but only if we've not been told to stop + if (clients.size() == 0 && isRunning) new InitializeThread().start(); } @@ -140,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... @@ -159,6 +168,25 @@ public class SocketManager implements IPickManager else if (addEndPoint(null)) connected = true; } + logger.debug("Completed initializing connection."); } } + + public void shutdown() + { + 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) { + logger.debug("Closing endpoint."); + ((PickEndPoint)clients.getFirst()).terminate(); + } + logger.debug("Shutdown of socketmanager completed."); + } } \ No newline at end of file