1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.ac.ukc.iscream.clientinterface; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import uk.ac.ukc.iscream.util.*; |
6 |
import uk.ac.ukc.iscream.componentmanager.*; |
7 |
import uk.ac.ukc.iscream.core.*; |
8 |
import uk.ac.ukc.iscream.client.*; |
9 |
|
10 |
|
11 |
/** |
12 |
* Acts as a Handler to a CORBA based client. |
13 |
* |
14 |
* !!! FUNDAMENTAL DESIGN PROBLEM !!! |
15 |
* !!! Need a way to "shutdown" this class !!! |
16 |
* |
17 |
* @author $Author$ |
18 |
* @version $Id$ |
19 |
*/ |
20 |
class CorbaHandlerServant extends Thread implements CorbaHandlerOperations { |
21 |
|
22 |
//---FINAL ATTRIBUTES--- |
23 |
|
24 |
/** |
25 |
* The current CVS revision of this class |
26 |
*/ |
27 |
public final String REVISION = "$Revision: 1.1 $"; |
28 |
|
29 |
//---STATIC METHODS--- |
30 |
|
31 |
//---CONSTRUCTORS--- |
32 |
|
33 |
/** |
34 |
* Construct a new CorbaHandlerServant. |
35 |
* |
36 |
* @param packetSorter A reference to the PacketSorter in the component |
37 |
* @param name The name of the client |
38 |
* @param client A reference to the "servant" part of the connecting client. |
39 |
*/ |
40 |
public CorbaHandlerServant(PacketSorter packetSorter, String name, Client client) { |
41 |
_packetSorter = packetSorter; |
42 |
_clientName = name; |
43 |
_hostList = ""; |
44 |
_queue = new Queue(); |
45 |
_client = client; |
46 |
_logger.write(toString(), Logger.SYSINIT, "created"); |
47 |
} |
48 |
|
49 |
//---PUBLIC METHODS--- |
50 |
|
51 |
/** |
52 |
* This method loops round until we "shutdown" (not implemented yet). Inside |
53 |
* the main loop we will first wait until the startData() method is called, |
54 |
* then we'll send data constantly until stopData() is called. |
55 |
*/ |
56 |
public void run() { |
57 |
// get a queue |
58 |
_queueID = _queue.getQueue(); |
59 |
// we'll keep going until someone implements a "shutdown" :) |
60 |
run = true; |
61 |
while(run) { |
62 |
// initially lock until someone tells us to start |
63 |
synchronized(this) { |
64 |
try { wait(); } catch(Exception e) {} |
65 |
} |
66 |
// loop sending data |
67 |
while(_sendingData) { |
68 |
try { |
69 |
String xml = (String) _queue.get(_queueID); |
70 |
// if it's not null (which could happen if we're "released") |
71 |
// send it on to the client that we're connected to. |
72 |
if(xml != null) { |
73 |
_client.receiveXML(xml); |
74 |
} |
75 |
} |
76 |
catch(InvalidQueueException e) { |
77 |
// lets stop sending - maybe stop altogether ? |
78 |
stopData(); |
79 |
_logger.write(toString(), Logger.ERROR, "Queue failure: "+e); |
80 |
} |
81 |
} |
82 |
// now, we've obviously been stopped, so we'll head back round this |
83 |
// while loop until someone starts us again. |
84 |
} |
85 |
// if we get here we've been told to stop |
86 |
_logger.write(toString(), Logger.SYSMSG, "Shutting Down"); |
87 |
// remove ourselves from the queue |
88 |
_queue.removeQueue(_queueID); |
89 |
} |
90 |
|
91 |
/** |
92 |
* Start sending data to the client. |
93 |
* |
94 |
* @return a boolean stating whether the attempt to start succeeded |
95 |
*/ |
96 |
public boolean startData() { |
97 |
if(!_sendingData) { |
98 |
// register the Queue |
99 |
_packetSorter.register(_queue, _hostList); |
100 |
// mark the running flag |
101 |
_sendingData = true; |
102 |
// bump the loop |
103 |
synchronized(this) { |
104 |
try { notifyAll(); } catch(Exception e) {} |
105 |
} |
106 |
return true; |
107 |
} |
108 |
else { |
109 |
return false; |
110 |
} |
111 |
} |
112 |
|
113 |
/** |
114 |
* Stop sending data to the client. |
115 |
* |
116 |
* @return a boolean stating whether the attempt to stop succeeded |
117 |
*/ |
118 |
public boolean stopData() { |
119 |
if(_sendingData) { |
120 |
// deregister the Queue |
121 |
_packetSorter.deregister(_queue, _hostList); |
122 |
// stop ourselves running |
123 |
_sendingData = false; |
124 |
// if the main loop is waiting for data it won't notice the |
125 |
// above flag to stop. This bumps it out of the blocked get(). |
126 |
_queue.releaseQueue(_queueID); |
127 |
return true; |
128 |
} |
129 |
else { |
130 |
return false; |
131 |
} |
132 |
} |
133 |
|
134 |
/** |
135 |
* Sets the host list for the Client to the requested semi-colon separated |
136 |
* list of hostnames. This will only succeed if we are not sending data. |
137 |
* |
138 |
* @param hostList A semi-colon separated list of hostnames to use. |
139 |
* @return Whether the request succeeded. |
140 |
*/ |
141 |
public boolean setHostList(String hostList) { |
142 |
if(!_sendingData) { |
143 |
_hostList = hostList; |
144 |
return true; |
145 |
} |
146 |
else { |
147 |
return false; |
148 |
} |
149 |
} |
150 |
|
151 |
/** |
152 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
153 |
* method to provide clean logging (every class should have this). |
154 |
* |
155 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
156 |
* to format the toString() |
157 |
* |
158 |
* @return the name of this class and its CVS revision |
159 |
*/ |
160 |
public String toString() { |
161 |
return FormatName.getName( |
162 |
_name, |
163 |
getClass().getName(), |
164 |
REVISION); |
165 |
} |
166 |
|
167 |
//---PRIVATE METHODS--- |
168 |
|
169 |
//---ACCESSOR/MUTATOR METHODS--- |
170 |
|
171 |
//---ATTRIBUTES--- |
172 |
|
173 |
/** |
174 |
* This is the friendly identifier of the |
175 |
* component this class is running in. |
176 |
* eg, a Filter may be called "filter1", |
177 |
* If this class does not have an owning |
178 |
* component, a name from the configuration |
179 |
* can be placed here. This name could also |
180 |
* be changed to null for utility classes. |
181 |
*/ |
182 |
private String _name = ClientInterfaceMain.NAME; |
183 |
|
184 |
/** |
185 |
* This holds a reference to the |
186 |
* system logger that is being used. |
187 |
*/ |
188 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
189 |
|
190 |
/** |
191 |
* A reference to the Configuration Manager the system is using |
192 |
*/ |
193 |
private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM(); |
194 |
|
195 |
/** |
196 |
* A reference to the PacketSorter. |
197 |
*/ |
198 |
private PacketSorter _packetSorter; |
199 |
|
200 |
/** |
201 |
* The host list the Client has requested |
202 |
*/ |
203 |
private String _hostList; |
204 |
|
205 |
/** |
206 |
* The name of the connected client. |
207 |
*/ |
208 |
private String _clientName; |
209 |
|
210 |
/** |
211 |
* Whether we are active and sending data. |
212 |
*/ |
213 |
private boolean _sendingData; |
214 |
|
215 |
/** |
216 |
* The Queue we'll use for buffering data to the client. |
217 |
*/ |
218 |
private Queue _queue; |
219 |
|
220 |
/** |
221 |
* The "servant" part of the client we're connected to. |
222 |
*/ |
223 |
private Client _client; |
224 |
|
225 |
/** |
226 |
* Our queue number within our Queue |
227 |
*/ |
228 |
private int _queueID; |
229 |
|
230 |
/** |
231 |
* The flag that dictates whether the main loop should *completely* exit |
232 |
*/ |
233 |
private boolean run; |
234 |
|
235 |
//---STATIC ATTRIBUTES--- |
236 |
|
237 |
} |