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: tdb1 $ |
18 |
* @version $Id: CorbaHandlerServant.java,v 1.1 2001/02/03 00:42:54 tdb1 Exp $ |
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 |
// !!! OUGHT TO CHECK QUEUE START OF USE !!! |
58 |
// get a queue |
59 |
_queueID = _queue.getQueue(); |
60 |
// we'll keep going until someone implements a "shutdown" :) |
61 |
run = true; |
62 |
while(run) { |
63 |
// initially lock until someone tells us to start |
64 |
synchronized(this) { |
65 |
try { wait(); } catch(Exception e) {} |
66 |
} |
67 |
// loop sending data |
68 |
while(_sendingData) { |
69 |
try { |
70 |
String xml = (String) _queue.get(_queueID); |
71 |
// if it's not null (which could happen if we're "released") |
72 |
// send it on to the client that we're connected to. |
73 |
if(xml != null) { |
74 |
_client.receiveXML(xml); |
75 |
} |
76 |
} |
77 |
catch(InvalidQueueException e) { |
78 |
// lets stop sending - maybe stop altogether ? |
79 |
stopData(); |
80 |
_logger.write(toString(), Logger.ERROR, "Queue failure: "+e); |
81 |
} |
82 |
} |
83 |
// now, we've obviously been stopped, so we'll head back round this |
84 |
// while loop until someone starts us again. |
85 |
} |
86 |
// if we get here we've been told to stop |
87 |
_logger.write(toString(), Logger.SYSMSG, "Shutting Down"); |
88 |
// remove ourselves from the queue |
89 |
_queue.removeQueue(_queueID); |
90 |
} |
91 |
|
92 |
/** |
93 |
* Start sending data to the client. |
94 |
* |
95 |
* @return a boolean stating whether the attempt to start succeeded |
96 |
*/ |
97 |
public boolean startData() { |
98 |
if(!_sendingData) { |
99 |
// register the Queue |
100 |
_packetSorter.register(_queue, _hostList); |
101 |
// mark the running flag |
102 |
_sendingData = true; |
103 |
// bump the loop |
104 |
synchronized(this) { |
105 |
try { notifyAll(); } catch(Exception e) {} |
106 |
} |
107 |
return true; |
108 |
} |
109 |
else { |
110 |
return false; |
111 |
} |
112 |
} |
113 |
|
114 |
/** |
115 |
* Stop sending data to the client. |
116 |
* |
117 |
* @return a boolean stating whether the attempt to stop succeeded |
118 |
*/ |
119 |
public boolean stopData() { |
120 |
if(_sendingData) { |
121 |
// deregister the Queue |
122 |
_packetSorter.deregister(_queue, _hostList); |
123 |
// stop ourselves running |
124 |
_sendingData = false; |
125 |
// if the main loop is waiting for data it won't notice the |
126 |
// above flag to stop. This bumps it out of the blocked get(). |
127 |
_queue.releaseQueue(_queueID); |
128 |
return true; |
129 |
} |
130 |
else { |
131 |
return false; |
132 |
} |
133 |
} |
134 |
|
135 |
/** |
136 |
* Sets the host list for the Client to the requested semi-colon separated |
137 |
* list of hostnames. This will only succeed if we are not sending data. |
138 |
* |
139 |
* @param hostList A semi-colon separated list of hostnames to use. |
140 |
* @return Whether the request succeeded. |
141 |
*/ |
142 |
public boolean setHostList(String hostList) { |
143 |
if(!_sendingData) { |
144 |
_hostList = hostList; |
145 |
return true; |
146 |
} |
147 |
else { |
148 |
return false; |
149 |
} |
150 |
} |
151 |
|
152 |
/** |
153 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
154 |
* method to provide clean logging (every class should have this). |
155 |
* |
156 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
157 |
* to format the toString() |
158 |
* |
159 |
* @return the name of this class and its CVS revision |
160 |
*/ |
161 |
public String toString() { |
162 |
return FormatName.getName( |
163 |
_name, |
164 |
getClass().getName(), |
165 |
REVISION); |
166 |
} |
167 |
|
168 |
//---PRIVATE METHODS--- |
169 |
|
170 |
//---ACCESSOR/MUTATOR METHODS--- |
171 |
|
172 |
//---ATTRIBUTES--- |
173 |
|
174 |
/** |
175 |
* This is the friendly identifier of the |
176 |
* component this class is running in. |
177 |
* eg, a Filter may be called "filter1", |
178 |
* If this class does not have an owning |
179 |
* component, a name from the configuration |
180 |
* can be placed here. This name could also |
181 |
* be changed to null for utility classes. |
182 |
*/ |
183 |
private String _name = ClientInterfaceMain.NAME; |
184 |
|
185 |
/** |
186 |
* This holds a reference to the |
187 |
* system logger that is being used. |
188 |
*/ |
189 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
190 |
|
191 |
/** |
192 |
* A reference to the Configuration Manager the system is using |
193 |
*/ |
194 |
private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM(); |
195 |
|
196 |
/** |
197 |
* A reference to the PacketSorter. |
198 |
*/ |
199 |
private PacketSorter _packetSorter; |
200 |
|
201 |
/** |
202 |
* The host list the Client has requested |
203 |
*/ |
204 |
private String _hostList; |
205 |
|
206 |
/** |
207 |
* The name of the connected client. |
208 |
*/ |
209 |
private String _clientName; |
210 |
|
211 |
/** |
212 |
* Whether we are active and sending data. |
213 |
*/ |
214 |
private boolean _sendingData; |
215 |
|
216 |
/** |
217 |
* The Queue we'll use for buffering data to the client. |
218 |
*/ |
219 |
private Queue _queue; |
220 |
|
221 |
/** |
222 |
* The "servant" part of the client we're connected to. |
223 |
*/ |
224 |
private Client _client; |
225 |
|
226 |
/** |
227 |
* Our queue number within our Queue |
228 |
*/ |
229 |
private int _queueID; |
230 |
|
231 |
/** |
232 |
* The flag that dictates whether the main loop should *completely* exit |
233 |
*/ |
234 |
private boolean run; |
235 |
|
236 |
//---STATIC ATTRIBUTES--- |
237 |
|
238 |
} |