9 |
|
|
10 |
|
|
11 |
|
/** |
12 |
< |
* Acts as a Handler to a CORBA based client. |
12 |
> |
* Acts as a Control Handler to a CORBA based client. |
13 |
|
* |
14 |
|
* !!! FUNDAMENTAL DESIGN PROBLEM !!! |
15 |
|
* !!! Need a way to "shutdown" this class !!! |
17 |
|
* @author $Author$ |
18 |
|
* @version $Id$ |
19 |
|
*/ |
20 |
< |
class CorbaHandlerServant extends Thread implements CorbaHandlerOperations { |
20 |
> |
class CorbaControlHandlerServant extends CorbaControlHandlerPOA { |
21 |
|
|
22 |
|
//---FINAL ATTRIBUTES--- |
23 |
|
|
31 |
|
//---CONSTRUCTORS--- |
32 |
|
|
33 |
|
/** |
34 |
< |
* Construct a new CorbaHandlerServant. |
34 |
> |
* Construct a new CorbaControlHandlerServant. |
35 |
|
* |
36 |
|
* @param packetSorter A reference to the PacketSorter in the component |
37 |
– |
* @param name The name of the client |
37 |
|
* @param client A reference to the "servant" part of the connecting client. |
38 |
|
*/ |
39 |
< |
public CorbaHandlerServant(PacketSorter packetSorter, String name, Client client) { |
39 |
> |
public CorbaControlHandlerServant(PacketSorter packetSorter, Client client) { |
40 |
|
_packetSorter = packetSorter; |
42 |
– |
_clientName = name; |
41 |
|
_hostList = ""; |
44 |
– |
_queue = new Queue(); |
42 |
|
_client = client; |
43 |
+ |
_dataHandler = null; |
44 |
|
_logger.write(toString(), Logger.SYSINIT, "created"); |
45 |
|
} |
46 |
|
|
47 |
|
//---PUBLIC METHODS--- |
48 |
|
|
49 |
|
/** |
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 |
– |
/** |
50 |
|
* Start sending data to the client. |
51 |
|
* |
52 |
|
* @return a boolean stating whether the attempt to start succeeded |
53 |
|
*/ |
54 |
|
public boolean startData() { |
55 |
< |
if(!_sendingData) { |
55 |
> |
if(_dataHandler == null) { |
56 |
> |
// create a new DataHandler |
57 |
> |
CorbaDataHandler dh = new CorbaDataHandler(_client); |
58 |
|
// register the Queue |
59 |
< |
_packetSorter.register(_queue, _hostList); |
60 |
< |
// mark the running flag |
61 |
< |
_sendingData = true; |
62 |
< |
// bump the loop |
63 |
< |
synchronized(this) { |
64 |
< |
try { notifyAll(); } catch(Exception e) {} |
65 |
< |
} |
59 |
> |
_packetSorter.register(dh.getQueue(), _hostList); |
60 |
> |
// startup a monitor on the DataHandler's queue, every minute |
61 |
> |
dh.getQueue().startMonitor(60*1000, _packetSorter.getQueue(), _name); |
62 |
> |
// start the DataHandler running |
63 |
> |
dh.start(); |
64 |
> |
// keep a reference |
65 |
> |
_dataHandler = dh; |
66 |
|
return true; |
67 |
|
} |
68 |
|
else { |
76 |
|
* @return a boolean stating whether the attempt to stop succeeded |
77 |
|
*/ |
78 |
|
public boolean stopData() { |
79 |
< |
if(_sendingData) { |
79 |
> |
if(_dataHandler != null) { |
80 |
|
// deregister the Queue |
81 |
< |
_packetSorter.deregister(_queue, _hostList); |
82 |
< |
// stop ourselves running |
83 |
< |
_sendingData = false; |
84 |
< |
// if the main loop is waiting for data it won't notice the |
85 |
< |
// above flag to stop. This bumps it out of the blocked get(). |
127 |
< |
_queue.releaseQueue(_queueID); |
81 |
> |
_packetSorter.deregister(_dataHandler.getQueue(), _hostList); |
82 |
> |
// stop the DataHandler |
83 |
> |
_dataHandler.shutdown(); |
84 |
> |
// destroy the reference |
85 |
> |
_dataHandler = null; |
86 |
|
return true; |
87 |
|
} |
88 |
|
else { |
98 |
|
* @return Whether the request succeeded. |
99 |
|
*/ |
100 |
|
public boolean setHostList(String hostList) { |
101 |
< |
if(!_sendingData) { |
101 |
> |
if(_dataHandler == null) { |
102 |
|
_hostList = hostList; |
103 |
|
return true; |
104 |
|
} |
147 |
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
148 |
|
|
149 |
|
/** |
192 |
– |
* A reference to the Configuration Manager the system is using |
193 |
– |
*/ |
194 |
– |
private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM(); |
195 |
– |
|
196 |
– |
/** |
150 |
|
* A reference to the PacketSorter. |
151 |
|
*/ |
152 |
|
private PacketSorter _packetSorter; |
157 |
|
private String _hostList; |
158 |
|
|
159 |
|
/** |
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 |
– |
/** |
160 |
|
* The "servant" part of the client we're connected to. |
161 |
|
*/ |
162 |
|
private Client _client; |
163 |
|
|
164 |
|
/** |
165 |
< |
* Our queue number within our Queue |
165 |
> |
* A reference to our DataHandler, if we have one |
166 |
|
*/ |
167 |
< |
private int _queueID; |
230 |
< |
|
231 |
< |
/** |
232 |
< |
* The flag that dictates whether the main loop should *completely* exit |
233 |
< |
*/ |
234 |
< |
private boolean run; |
167 |
> |
private CorbaDataHandler _dataHandler; |
168 |
|
|
169 |
|
//---STATIC ATTRIBUTES--- |
170 |
|
|