applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / SimplePickManager.java
1 /*\r
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
5  * \r
6  * Earlier versions have also been incorporated into Jalview version 2.4 \r
7  * since 2008, and TOPALi version 2 since 2007.\r
8  * \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
13  *  \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
18  * \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
21  */\r
22 package uk.ac.vamsas.client.simpleclient;\r
23 \r
24 import org.apache.commons.logging.*;\r
25 \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
30 \r
31 /**\r
32  * Blocking message handler\r
33  * \r
34  * @author\r
35  * \r
36  */\r
37 public class SimplePickManager implements IPickManager {\r
38   private Log log = LogFactory.getLog(SimplePickManager.class);\r
39 \r
40   SocketManager manager = null;\r
41 \r
42   SimplePickManager(SocketManager manager) {\r
43     this.manager = manager;\r
44     final SimplePickManager me = this;\r
45     /**\r
46      * add a handler that calls the SimplePickManager message handler\r
47      */\r
48     manager.registerMessageHandler(new IMessageHandler() {\r
49       public void handleMessage(Message message) {\r
50         me.handleMessage(message);\r
51       }\r
52     });\r
53 \r
54   }\r
55 \r
56   /**\r
57    * when false, messages are queued in a FIFO until thread can restart.\r
58    */\r
59   private boolean passThru = true;\r
60 \r
61   /**\r
62    * internal flag - set to true to sleep until passThru is true before passing\r
63    * on a message\r
64    */\r
65   private boolean qUEUE = false;\r
66 \r
67   /**\r
68    * the client apps message handler\r
69    */\r
70   IMessageHandler pickHandler = null;\r
71 \r
72   public void registerMessageHandler(IMessageHandler handler) {\r
73     pickHandler = handler;\r
74 \r
75   }\r
76 \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
81   }\r
82 \r
83   /**\r
84    * pass message onto the Apps handler, or wait until passThru is true before\r
85    * passing message on.\r
86    * \r
87    * @param message\r
88    */\r
89   protected synchronized void handleMessage(Message message) {\r
90     if (qUEUE) {\r
91       while (!passThru && manager != null) {\r
92         log.debug("Not passing through.");\r
93         try {\r
94           Thread.sleep(5);\r
95         } catch (InterruptedException e) {\r
96         }\r
97         ;\r
98       }\r
99     }\r
100     if (passThru && manager != null)\r
101       pickHandler.handleMessage(message);\r
102   }\r
103 \r
104   /**\r
105    * @return true if messages are being passed to handlers\r
106    */\r
107   public boolean isPassThru() {\r
108     return passThru;\r
109   }\r
110 \r
111   /**\r
112    * @param true to pass messages on to handlers, false to hold messages in\r
113    *        queue\r
114    */\r
115   public void setPassThru(boolean passThru) {\r
116     this.passThru = passThru;\r
117   }\r
118 \r
119   /**\r
120    * shutdown the pickmanager and remove all references to it\r
121    */\r
122   public void shutdown() {\r
123     passThru = false;\r
124     manager.registerMessageHandler(null);\r
125     SocketManager dying = manager;\r
126     manager = null;\r
127     dying.shutdown();\r
128   }\r
129 }\r