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 |
+ |
* @param queueMonitorInterval The interval at which to monitor our Queue. |
39 |
|
*/ |
40 |
< |
public CorbaHandlerServant(PacketSorter packetSorter, String name, Client client) { |
40 |
> |
public CorbaControlHandlerServant(PacketSorter packetSorter, Client client, int queueMonitorInterval) { |
41 |
|
_packetSorter = packetSorter; |
42 |
< |
_clientName = name; |
42 |
> |
_queueMonitorInterval = queueMonitorInterval; |
43 |
|
_hostList = ""; |
44 |
– |
_queue = new Queue(); |
44 |
|
_client = client; |
45 |
+ |
_dataHandler = null; |
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 |
– |
/** |
52 |
|
* Start sending data to the client. |
53 |
|
* |
54 |
|
* @return a boolean stating whether the attempt to start succeeded |
55 |
|
*/ |
56 |
|
public boolean startData() { |
57 |
< |
if(!_sendingData) { |
57 |
> |
if(_dataHandler == null) { |
58 |
> |
// create a new DataHandler |
59 |
> |
CorbaDataHandler dh = new CorbaDataHandler(_client); |
60 |
|
// register the Queue |
61 |
< |
_packetSorter.register(_queue, _hostList); |
62 |
< |
// mark the running flag |
63 |
< |
_sendingData = true; |
64 |
< |
// bump the loop |
65 |
< |
synchronized(this) { |
66 |
< |
try { notifyAll(); } catch(Exception e) {} |
67 |
< |
} |
61 |
> |
_packetSorter.register(dh.getQueue(), _hostList); |
62 |
> |
// startup a monitor on the DataHandler's queue, every minute |
63 |
> |
String queueName = _name + " CorbaHandler"; |
64 |
> |
dh.getQueue().startMonitor(_queueMonitorInterval*1000, _packetSorter.getQueue(), queueName); |
65 |
> |
// start the DataHandler running |
66 |
> |
dh.start(); |
67 |
> |
// keep a reference |
68 |
> |
_dataHandler = dh; |
69 |
|
return true; |
70 |
|
} |
71 |
|
else { |
79 |
|
* @return a boolean stating whether the attempt to stop succeeded |
80 |
|
*/ |
81 |
|
public boolean stopData() { |
82 |
< |
if(_sendingData) { |
82 |
> |
if(_dataHandler != null) { |
83 |
|
// deregister the Queue |
84 |
< |
_packetSorter.deregister(_queue, _hostList); |
85 |
< |
// stop ourselves running |
86 |
< |
_sendingData = false; |
87 |
< |
// if the main loop is waiting for data it won't notice the |
88 |
< |
// above flag to stop. This bumps it out of the blocked get(). |
127 |
< |
_queue.releaseQueue(_queueID); |
84 |
> |
_packetSorter.deregister(_dataHandler.getQueue(), _hostList); |
85 |
> |
// stop the DataHandler |
86 |
> |
_dataHandler.shutdown(); |
87 |
> |
// destroy the reference |
88 |
> |
_dataHandler = null; |
89 |
|
return true; |
90 |
|
} |
91 |
|
else { |
101 |
|
* @return Whether the request succeeded. |
102 |
|
*/ |
103 |
|
public boolean setHostList(String hostList) { |
104 |
< |
if(!_sendingData) { |
104 |
> |
if(_dataHandler == null) { |
105 |
|
_hostList = hostList; |
106 |
|
return true; |
107 |
|
} |
150 |
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
151 |
|
|
152 |
|
/** |
192 |
– |
* A reference to the Configuration Manager the system is using |
193 |
– |
*/ |
194 |
– |
private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM(); |
195 |
– |
|
196 |
– |
/** |
153 |
|
* A reference to the PacketSorter. |
154 |
|
*/ |
155 |
|
private PacketSorter _packetSorter; |
160 |
|
private String _hostList; |
161 |
|
|
162 |
|
/** |
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 |
– |
/** |
163 |
|
* The "servant" part of the client we're connected to. |
164 |
|
*/ |
165 |
|
private Client _client; |
166 |
|
|
167 |
|
/** |
168 |
< |
* Our queue number within our Queue |
168 |
> |
* A reference to our DataHandler, if we have one |
169 |
|
*/ |
170 |
< |
private int _queueID; |
170 |
> |
private CorbaDataHandler _dataHandler; |
171 |
|
|
172 |
|
/** |
173 |
< |
* The flag that dictates whether the main loop should *completely* exit |
173 |
> |
* The interval at which to monitor our Queue |
174 |
|
*/ |
175 |
< |
private boolean run; |
175 |
> |
private int _queueMonitorInterval; |
176 |
|
|
177 |
|
//---STATIC ATTRIBUTES--- |
178 |
|
|