1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* Copyright (C) 2000-2002 i-scream |
4 |
+ |
* |
5 |
+ |
* This program is free software; you can redistribute it and/or |
6 |
+ |
* modify it under the terms of the GNU General Public License |
7 |
+ |
* as published by the Free Software Foundation; either version 2 |
8 |
+ |
* of the License, or (at your option) any later version. |
9 |
+ |
* |
10 |
+ |
* This program is distributed in the hope that it will be useful, |
11 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
+ |
* GNU General Public License for more details. |
14 |
+ |
* |
15 |
+ |
* You should have received a copy of the GNU General Public License |
16 |
+ |
* along with this program; if not, write to the Free Software |
17 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
+ |
*/ |
19 |
+ |
|
20 |
|
//---PACKAGE DECLARATION--- |
21 |
< |
package uk.ac.ukc.iscream.clientinterface; |
21 |
> |
package uk.org.iscream.cms.server.clientinterface; |
22 |
|
|
23 |
|
//---IMPORTS--- |
24 |
< |
import uk.ac.ukc.iscream.util.*; |
25 |
< |
import uk.ac.ukc.iscream.componentmanager.*; |
26 |
< |
import uk.ac.ukc.iscream.core.*; |
27 |
< |
import uk.ac.ukc.iscream.client.*; |
24 |
> |
import uk.org.iscream.cms.server.util.*; |
25 |
> |
import uk.org.iscream.cms.server.componentmanager.*; |
26 |
> |
import uk.org.iscream.cms.server.core.*; |
27 |
> |
import uk.org.iscream.cms.server.client.*; |
28 |
|
|
29 |
|
|
30 |
|
/** |
31 |
< |
* Acts as a Handler to a CORBA based client. |
31 |
> |
* Acts as a Control Handler to a CORBA based client. |
32 |
|
* |
14 |
– |
* !!! FUNDAMENTAL DESIGN PROBLEM !!! |
15 |
– |
* !!! Need a way to "shutdown" this class !!! |
16 |
– |
* |
33 |
|
* @author $Author$ |
34 |
|
* @version $Id$ |
35 |
|
*/ |
36 |
< |
class CorbaHandlerServant extends Thread implements CorbaHandlerOperations { |
36 |
> |
class CorbaControlHandlerServant extends CorbaControlHandlerPOA { |
37 |
|
|
38 |
|
//---FINAL ATTRIBUTES--- |
39 |
|
|
47 |
|
//---CONSTRUCTORS--- |
48 |
|
|
49 |
|
/** |
50 |
< |
* Construct a new CorbaHandlerServant. |
50 |
> |
* Construct a new CorbaControlHandlerServant. |
51 |
|
* |
52 |
|
* @param packetSorter A reference to the PacketSorter in the component |
37 |
– |
* @param name The name of the client |
53 |
|
* @param client A reference to the "servant" part of the connecting client. |
54 |
+ |
* @param queueMonitorInterval The interval at which to monitor our Queue. |
55 |
+ |
* @param clientname A name to identify the client. |
56 |
|
*/ |
57 |
< |
public CorbaHandlerServant(PacketSorter packetSorter, String name, Client client) { |
57 |
> |
public CorbaControlHandlerServant(PacketSorter packetSorter, Client client, String clientname) { |
58 |
|
_packetSorter = packetSorter; |
42 |
– |
_clientName = name; |
59 |
|
_hostList = ""; |
44 |
– |
_queue = new Queue(); |
60 |
|
_client = client; |
61 |
+ |
_clientname = clientname; |
62 |
+ |
_dataHandler = null; |
63 |
|
_logger.write(toString(), Logger.SYSINIT, "created"); |
64 |
|
} |
65 |
|
|
66 |
|
//---PUBLIC METHODS--- |
67 |
|
|
68 |
|
/** |
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 |
– |
/** |
69 |
|
* Start sending data to the client. |
70 |
|
* |
71 |
|
* @return a boolean stating whether the attempt to start succeeded |
72 |
|
*/ |
73 |
|
public boolean startData() { |
74 |
< |
if(!_sendingData) { |
74 |
> |
if(_dataHandler == null) { |
75 |
> |
// create a new DataHandler |
76 |
> |
CorbaDataHandler dh = new CorbaDataHandler(_client, this); |
77 |
|
// register the Queue |
78 |
< |
_packetSorter.register(_queue, _hostList); |
79 |
< |
// mark the running flag |
80 |
< |
_sendingData = true; |
81 |
< |
// bump the loop |
82 |
< |
synchronized(this) { |
83 |
< |
try { notifyAll(); } catch(Exception e) {} |
78 |
> |
_packetSorter.register(dh.getQueue(), _hostList); |
79 |
> |
try { |
80 |
> |
// startup a monitor on the DataHandler's queue |
81 |
> |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
82 |
> |
int queueMonitorInterval = Integer.parseInt(cp.getProperty("ClientInterface", "Queue.MonitorInterval")); |
83 |
> |
String queueName = _name + " CorbaHandler:"+_clientname; |
84 |
> |
dh.getQueue().startMonitor(queueMonitorInterval*1000, _packetSorter.getQueue(), queueName); |
85 |
> |
} catch (PropertyNotFoundException e) { |
86 |
> |
_logger.write(toString(), Logger.WARNING, "failed to find queue monitor config, disabling. " + e); |
87 |
|
} |
88 |
+ |
// start the DataHandler running |
89 |
+ |
dh.start(); |
90 |
+ |
// keep a reference |
91 |
+ |
_dataHandler = dh; |
92 |
|
return true; |
93 |
|
} |
94 |
|
else { |
102 |
|
* @return a boolean stating whether the attempt to stop succeeded |
103 |
|
*/ |
104 |
|
public boolean stopData() { |
105 |
< |
if(_sendingData) { |
105 |
> |
if(_dataHandler != null) { |
106 |
|
// deregister the Queue |
107 |
< |
_packetSorter.deregister(_queue, _hostList); |
108 |
< |
// stop ourselves running |
109 |
< |
_sendingData = false; |
110 |
< |
// if the main loop is waiting for data it won't notice the |
111 |
< |
// above flag to stop. This bumps it out of the blocked get(). |
127 |
< |
_queue.releaseQueue(_queueID); |
107 |
> |
_packetSorter.deregister(_dataHandler.getQueue(), _hostList); |
108 |
> |
// stop the DataHandler |
109 |
> |
_dataHandler.shutdown(); |
110 |
> |
// destroy the reference |
111 |
> |
_dataHandler = null; |
112 |
|
return true; |
113 |
|
} |
114 |
|
else { |
124 |
|
* @return Whether the request succeeded. |
125 |
|
*/ |
126 |
|
public boolean setHostList(String hostList) { |
127 |
< |
if(!_sendingData) { |
127 |
> |
if(_dataHandler == null) { |
128 |
|
_hostList = hostList; |
129 |
|
return true; |
130 |
|
} |
134 |
|
} |
135 |
|
|
136 |
|
/** |
137 |
+ |
* Disconnect, this will shutdown the data and unhook from |
138 |
+ |
* the CORBA ORB. |
139 |
+ |
*/ |
140 |
+ |
public void disconnect() { |
141 |
+ |
// close the data handler |
142 |
+ |
stopData(); |
143 |
+ |
// disconnect from the ORB |
144 |
+ |
try { |
145 |
+ |
byte[] oid = _refman.getRootPOA().servant_to_id(this); |
146 |
+ |
_refman.getRootPOA().deactivate_object(oid); |
147 |
+ |
} catch(Exception e) { |
148 |
+ |
_logger.write(this.toString(), Logger.ERROR, "disconnect failed: "+e); |
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 |
156 |
> |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
157 |
|
* to format the toString() |
158 |
|
* |
159 |
|
* @return the name of this class and its CVS revision |
167 |
|
|
168 |
|
//---PRIVATE METHODS--- |
169 |
|
|
170 |
+ |
/** |
171 |
+ |
* Overridden for debugging purposes |
172 |
+ |
* to see when an instance of this class |
173 |
+ |
* is destroyed |
174 |
+ |
*/ |
175 |
+ |
protected void finalize() throws Throwable { |
176 |
+ |
_logger.write(this.toString(), Logger.DEBUG, "finalized by GC"); |
177 |
+ |
} |
178 |
+ |
|
179 |
|
//---ACCESSOR/MUTATOR METHODS--- |
180 |
|
|
181 |
|
//---ATTRIBUTES--- |
196 |
|
* system logger that is being used. |
197 |
|
*/ |
198 |
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
199 |
< |
|
199 |
> |
|
200 |
|
/** |
201 |
< |
* A reference to the Configuration Manager the system is using |
201 |
> |
* A reference to the reference manager in use |
202 |
|
*/ |
203 |
< |
private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM(); |
203 |
> |
private ReferenceManager _refman = ReferenceManager.getInstance(); |
204 |
|
|
205 |
|
/** |
206 |
|
* A reference to the PacketSorter. |
213 |
|
private String _hostList; |
214 |
|
|
215 |
|
/** |
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 |
– |
/** |
216 |
|
* The "servant" part of the client we're connected to. |
217 |
|
*/ |
218 |
|
private Client _client; |
219 |
|
|
220 |
|
/** |
221 |
< |
* Our queue number within our Queue |
221 |
> |
* A reference to our DataHandler, if we have one |
222 |
|
*/ |
223 |
< |
private int _queueID; |
223 |
> |
private CorbaDataHandler _dataHandler; |
224 |
|
|
225 |
|
/** |
226 |
< |
* The flag that dictates whether the main loop should *completely* exit |
226 |
> |
* A name to identify the client |
227 |
|
*/ |
228 |
< |
private boolean run; |
228 |
> |
private String _clientname; |
229 |
|
|
230 |
|
//---STATIC ATTRIBUTES--- |
231 |
|
|