2 * This file is part of the Vamsas Client version 0.1.
\r
3 * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite,
\r
4 * Andrew Waterhouse and Dominik Lindner.
\r
6 * Earlier versions have also been incorporated into Jalview version 2.4
\r
7 * since 2008, and TOPALi version 2 since 2007.
\r
9 * The Vamsas Client is free software: you can redistribute it and/or modify
\r
10 * it under the terms of the GNU Lesser General Public License as published by
\r
11 * the Free Software Foundation, either version 3 of the License, or
\r
12 * (at your option) any later version.
\r
14 * The Vamsas Client is distributed in the hope that it will be useful,
\r
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
17 * GNU Lesser General Public License for more details.
\r
19 * You should have received a copy of the GNU Lesser General Public License
\r
20 * along with the Vamsas Client. If not, see <http://www.gnu.org/licenses/>.
\r
22 package uk.ac.vamsas.client.simpleclient;
\r
24 import org.apache.commons.logging.*;
\r
26 import uk.ac.vamsas.client.picking.IMessageHandler;
\r
27 import uk.ac.vamsas.client.picking.IPickManager;
\r
28 import uk.ac.vamsas.client.picking.Message;
\r
29 import uk.ac.vamsas.client.picking.SocketManager;
\r
32 * Blocking message handler
\r
37 public class SimplePickManager implements IPickManager {
\r
38 private Log log = LogFactory.getLog(SimplePickManager.class);
\r
40 SocketManager manager = null;
\r
42 SimplePickManager(SocketManager manager) {
\r
43 this.manager = manager;
\r
44 final SimplePickManager me = this;
\r
46 * add a handler that calls the SimplePickManager message handler
\r
48 manager.registerMessageHandler(new IMessageHandler() {
\r
49 public void handleMessage(Message message) {
\r
50 me.handleMessage(message);
\r
57 * when false, messages are queued in a FIFO until thread can restart.
\r
59 private boolean passThru = true;
\r
62 * internal flag - set to true to sleep until passThru is true before passing
\r
65 private boolean qUEUE = false;
\r
68 * the client apps message handler
\r
70 IMessageHandler pickHandler = null;
\r
72 public void registerMessageHandler(IMessageHandler handler) {
\r
73 pickHandler = handler;
\r
77 public synchronized void sendMessage(Message message) {
\r
78 // throw away messages whilst we block
\r
79 if (passThru && manager != null)
\r
80 manager.sendMessage(message);
\r
84 * pass message onto the Apps handler, or wait until passThru is true before
\r
85 * passing message on.
\r
89 protected synchronized void handleMessage(Message message) {
\r
91 while (!passThru && manager != null) {
\r
92 log.debug("Not passing through.");
\r
95 } catch (InterruptedException e) {
\r
100 if (passThru && manager != null)
\r
101 pickHandler.handleMessage(message);
\r
105 * @return true if messages are being passed to handlers
\r
107 public boolean isPassThru() {
\r
112 * @param true to pass messages on to handlers, false to hold messages in
\r
115 public void setPassThru(boolean passThru) {
\r
116 this.passThru = passThru;
\r
120 * shutdown the pickmanager and remove all references to it
\r
122 public void shutdown() {
\r
124 manager.registerMessageHandler(null);
\r
125 SocketManager dying = manager;
\r