X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fuk%2Fac%2Fvamsas%2Fclient%2Fsimpleclient%2FSimplePickManager.java;h=1eabeac97f7a077d4216eb5a7206485ec53779c1;hb=844ccad5a3fcbedec17b2af66d460f31abc7cff1;hp=376662c6e978cd691353570ca107a4527ebc31ca;hpb=f900bd84927e08acb2c47527da9cc4efe3a2b968;p=vamsas.git diff --git a/src/uk/ac/vamsas/client/simpleclient/SimplePickManager.java b/src/uk/ac/vamsas/client/simpleclient/SimplePickManager.java index 376662c..1eabeac 100644 --- a/src/uk/ac/vamsas/client/simpleclient/SimplePickManager.java +++ b/src/uk/ac/vamsas/client/simpleclient/SimplePickManager.java @@ -1,3 +1,24 @@ +/* + * This file is part of the Vamsas Client version 0.1. + * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, + * Andrew Waterhouse and Dominik Lindner. + * + * Earlier versions have also been incorporated into Jalview version 2.4 + * since 2008, and TOPALi version 2 since 2007. + * + * The Vamsas Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Vamsas Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with the Vamsas Client. If not, see . + */ package uk.ac.vamsas.client.simpleclient; import org.apache.commons.logging.*; @@ -6,18 +27,21 @@ import uk.ac.vamsas.client.picking.IMessageHandler; import uk.ac.vamsas.client.picking.IPickManager; import uk.ac.vamsas.client.picking.Message; import uk.ac.vamsas.client.picking.SocketManager; + /** * Blocking message handler + * * @author - * + * */ public class SimplePickManager implements IPickManager { private Log log = LogFactory.getLog(SimplePickManager.class); - - SocketManager manager=null; + + SocketManager manager = null; + SimplePickManager(SocketManager manager) { - this.manager=manager; - final SimplePickManager me=this; + this.manager = manager; + final SimplePickManager me = this; /** * add a handler that calls the SimplePickManager message handler */ @@ -26,36 +50,54 @@ public class SimplePickManager implements IPickManager { me.handleMessage(message); } }); - + } + /** * when false, messages are queued in a FIFO until thread can restart. */ - private boolean passThru=true; + private boolean passThru = true; + + /** + * internal flag - set to true to sleep until passThru is true before passing + * on a message + */ + private boolean qUEUE = false; + /** * the client apps message handler */ - IMessageHandler pickHandler=null; + IMessageHandler pickHandler = null; + public void registerMessageHandler(IMessageHandler handler) { - pickHandler=handler; - + pickHandler = handler; + } public synchronized void sendMessage(Message message) { - manager.sendMessage(message); + // throw away messages whilst we block + if (passThru && manager != null) + manager.sendMessage(message); } + /** - * pass message onto the Apps handler, or wait until - * passThru is true before passing message on. + * pass message onto the Apps handler, or wait until passThru is true before + * passing message on. + * * @param message */ protected synchronized void handleMessage(Message message) { - while (!passThru) { - try { - Thread.sleep(5); - } catch (InterruptedException e) {}; + if (qUEUE) { + while (!passThru && manager != null) { + log.debug("Not passing through."); + try { + Thread.sleep(5); + } catch (InterruptedException e) { + } + ; + } } - if (passThru) + if (passThru && manager != null) pickHandler.handleMessage(message); } @@ -67,13 +109,21 @@ public class SimplePickManager implements IPickManager { } /** - * @param true to pass messages on to handlers, false to hold messages in queue + * @param true to pass messages on to handlers, false to hold messages in + * queue */ public void setPassThru(boolean passThru) { this.passThru = passThru; } - + + /** + * shutdown the pickmanager and remove all references to it + */ public void shutdown() { - manager.shutdown(); + passThru = false; + manager.registerMessageHandler(null); + SocketManager dying = manager; + manager = null; + dying.shutdown(); } }