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