1 |
tdb |
1.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 java.util.*; |
9 |
|
|
|
10 |
|
|
/** |
11 |
|
|
* Receives data from the incoming CORBA servant, places |
12 |
|
|
* it in a Queue, and then arranges distribution to the |
13 |
tdb |
1.4 |
* DataHandlers. |
14 |
|
|
* Has extra functionality to send data to DataHandlers |
15 |
|
|
* on a per-host basis - ie. the Client can request which |
16 |
|
|
* hosts it would like to listen for. |
17 |
tdb |
1.1 |
* |
18 |
tdb |
1.2 |
* @author $Author: tdb1 $ |
19 |
tdb |
1.10 |
* @version $Id: PacketSorter.java,v 1.9 2001/03/01 02:14:37 tdb1 Exp $ |
20 |
tdb |
1.1 |
*/ |
21 |
|
|
class PacketSorter extends Thread { |
22 |
|
|
|
23 |
|
|
//---FINAL ATTRIBUTES--- |
24 |
|
|
|
25 |
|
|
/** |
26 |
|
|
* The current CVS revision of this class |
27 |
|
|
*/ |
28 |
tdb |
1.10 |
public final String REVISION = "$Revision: 1.9 $"; |
29 |
tdb |
1.1 |
|
30 |
|
|
//---STATIC METHODS--- |
31 |
|
|
|
32 |
|
|
//---CONSTRUCTORS--- |
33 |
|
|
|
34 |
|
|
/** |
35 |
|
|
* Creates a new PacketSorter. |
36 |
tdb |
1.6 |
* |
37 |
|
|
* @param queueMonitorInterval The interval at which to monitor the Queue |
38 |
tdb |
1.1 |
*/ |
39 |
tdb |
1.6 |
public PacketSorter(int queueMonitorInterval) { |
40 |
tdb |
1.1 |
_queue = new Queue(); |
41 |
tdb |
1.5 |
// startup a monitor on this queue, every minute |
42 |
tdb |
1.6 |
String queueName = _name + " PacketSorterQueue"; |
43 |
|
|
_queue.startMonitor(queueMonitorInterval*1000, queueName); |
44 |
tdb |
1.7 |
_hostMap = Collections.synchronizedMap(new HashMap()); |
45 |
tdb |
1.10 |
_allHostDataList = Collections.synchronizedList(new LinkedList()); |
46 |
tdb |
1.7 |
_allHostsList = Collections.synchronizedList(new LinkedList()); |
47 |
tdb |
1.1 |
_logger.write(toString(), Logger.SYSINIT, "created"); |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
//---PUBLIC METHODS--- |
51 |
|
|
|
52 |
tdb |
1.4 |
/** |
53 |
|
|
* Method to start the PacketSorter running. This method will |
54 |
|
|
* loop forever processing and sending data. |
55 |
|
|
*/ |
56 |
tdb |
1.1 |
public void run() { |
57 |
|
|
int qID = _queue.getQueue(); |
58 |
|
|
while(true) { |
59 |
tdb |
1.4 |
// attempt to get some data from the Queue |
60 |
tdb |
1.1 |
String xml = ""; |
61 |
|
|
try { |
62 |
|
|
xml = (String) _queue.get(qID); |
63 |
|
|
} |
64 |
|
|
catch(InvalidQueueException e) { |
65 |
|
|
_logger.write(toString(), Logger.ERROR, "Queue failure: "+e); |
66 |
|
|
} |
67 |
|
|
|
68 |
tdb |
1.10 |
XMLPacket packet = null; |
69 |
|
|
|
70 |
|
|
try { |
71 |
|
|
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml); |
72 |
|
|
packet = xmlPacketMaker.createXMLPacket(); |
73 |
|
|
} catch(InvalidXMLException e) { |
74 |
|
|
_logger.write(toString(), Logger.ERROR, "Invalid XML: "+e); |
75 |
|
|
// skip the rest of this loop iteration |
76 |
|
|
continue; |
77 |
|
|
} |
78 |
tdb |
1.1 |
|
79 |
tdb |
1.8 |
String packetType = packet.getParam("packet.attributes.type"); |
80 |
|
|
// check if we need to send it regardless |
81 |
|
|
if(packetType.equals("data") || packetType.equals("heartbeat")) { |
82 |
|
|
String host = packet.getParam("packet.attributes.machine_name"); |
83 |
|
|
|
84 |
|
|
// look in the hostMap to see if anyone wants this data |
85 |
|
|
if(_hostMap.containsKey(host)) { |
86 |
tdb |
1.9 |
List list = (List) _hostMap.get(host); |
87 |
tdb |
1.8 |
Iterator i = list.iterator(); |
88 |
|
|
// push the data to the listening Handler's queue |
89 |
|
|
while(i.hasNext()) { |
90 |
|
|
((Queue) i.next()).add(xml); |
91 |
|
|
} |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
// any handler in this list wants all packets, so send |
95 |
|
|
// it on to them regardless |
96 |
tdb |
1.10 |
Iterator i = _allHostDataList.iterator(); |
97 |
tdb |
1.3 |
while(i.hasNext()) { |
98 |
|
|
((Queue) i.next()).add(xml); |
99 |
tdb |
1.8 |
} |
100 |
tdb |
1.1 |
} |
101 |
tdb |
1.8 |
else { |
102 |
tdb |
1.10 |
// always send this packet to all hosts, because it's |
103 |
|
|
// "extra" data, not host data |
104 |
tdb |
1.8 |
Iterator i = _allHostsList.iterator(); |
105 |
|
|
while(i.hasNext()) { |
106 |
|
|
((Queue) i.next()).add(xml); |
107 |
|
|
} |
108 |
tdb |
1.2 |
} |
109 |
tdb |
1.1 |
} |
110 |
|
|
} |
111 |
|
|
|
112 |
tdb |
1.4 |
/** |
113 |
|
|
* Register a DataHandler in the system. This method |
114 |
|
|
* actually takes a reference to a Queue, which should be |
115 |
|
|
* a Queue that the DataHandler is making use of. |
116 |
|
|
* It also takes a hostList, this being a semi-colon |
117 |
|
|
* seperated list of hosts that the Client the DataHandler |
118 |
|
|
* is serving has requested. If this list is simply an empty |
119 |
|
|
* String, it is assumed the Client wants to listen to all |
120 |
|
|
* host information. |
121 |
|
|
* |
122 |
|
|
* @param dhQueue a Queue being used by the DataHandler that is registering |
123 |
|
|
* @param hostList a semi-colon seperated list of hosts |
124 |
|
|
*/ |
125 |
tdb |
1.10 |
public synchronized void register(Queue dhQueue, String hostList) { |
126 |
tdb |
1.4 |
// check to see if we want all hosts |
127 |
tdb |
1.2 |
if(hostList.equals("")) { |
128 |
tdb |
1.10 |
_allHostDataList.add(dhQueue); |
129 |
tdb |
1.2 |
_logger.write(toString(), Logger.SYSMSG, "registered DataHandler for all hosts"); |
130 |
|
|
} |
131 |
|
|
else { |
132 |
tdb |
1.4 |
// go through the list of hosts |
133 |
tdb |
1.2 |
StringTokenizer st = new StringTokenizer(hostList, ";"); |
134 |
|
|
while(st.hasMoreTokens()) { |
135 |
|
|
String host = st.nextToken(); |
136 |
tdb |
1.4 |
// see if we already have a list in the map for this host |
137 |
tdb |
1.2 |
if(_hostMap.containsKey(host)) { |
138 |
tdb |
1.4 |
// we do, so add to it |
139 |
tdb |
1.9 |
List list = (List) _hostMap.get(host); |
140 |
tdb |
1.2 |
list.add(dhQueue); |
141 |
|
|
} |
142 |
|
|
else { |
143 |
tdb |
1.4 |
// we don't, so create a list and put it in the map |
144 |
tdb |
1.7 |
List list = Collections.synchronizedList(new LinkedList()); |
145 |
tdb |
1.2 |
list.add(dhQueue); |
146 |
|
|
_hostMap.put(host, list); |
147 |
|
|
} |
148 |
tdb |
1.1 |
} |
149 |
tdb |
1.2 |
_logger.write(toString(), Logger.SYSMSG, "registered DataHandler for hosts: "+hostList); |
150 |
tdb |
1.1 |
} |
151 |
tdb |
1.10 |
// always add host to our complete host list |
152 |
|
|
_allHostsList.add(dhQueue); |
153 |
tdb |
1.1 |
} |
154 |
|
|
|
155 |
tdb |
1.4 |
/** |
156 |
|
|
* Deregister a DataHandler. The DataHandler should give a reference |
157 |
|
|
* to the Queue it's using, and the *same* hostList it gave when it |
158 |
|
|
* register. It is imperative that the hostList is the same, otherwise |
159 |
|
|
* there will be all sorts of problems with lists getting out of sync. |
160 |
|
|
* |
161 |
|
|
* NB: Possible future addition would be recording of hostList's. |
162 |
|
|
* |
163 |
|
|
* @param dhQueue a Queue being used by the DataHandler that is deregistering |
164 |
|
|
* @param hostList a semi-colon seperated list of hosts |
165 |
|
|
*/ |
166 |
tdb |
1.10 |
public synchronized void deregister(Queue dhQueue, String hostList) { |
167 |
tdb |
1.4 |
// go through the list of hosts |
168 |
tdb |
1.2 |
if(hostList.equals("")) { |
169 |
tdb |
1.10 |
_allHostDataList.remove(dhQueue); |
170 |
tdb |
1.2 |
_logger.write(toString(), Logger.SYSMSG, "deregistered DataHandler for all hosts"); |
171 |
|
|
} |
172 |
|
|
else { |
173 |
|
|
StringTokenizer st = new StringTokenizer(hostList, ";"); |
174 |
|
|
while(st.hasMoreTokens()) { |
175 |
|
|
String host = st.nextToken(); |
176 |
tdb |
1.4 |
// this should in reality always be true, but best check |
177 |
tdb |
1.2 |
if(_hostMap.containsKey(host)) { |
178 |
tdb |
1.4 |
// get the list and remove the host in question |
179 |
tdb |
1.9 |
List list = (List) _hostMap.get(host); |
180 |
tdb |
1.2 |
list.remove(dhQueue); |
181 |
tdb |
1.4 |
// if the list is now empty, we might as well remove it |
182 |
tdb |
1.2 |
if(list.size()==0) { |
183 |
|
|
_hostMap.remove(host); |
184 |
|
|
} |
185 |
tdb |
1.1 |
} |
186 |
|
|
} |
187 |
tdb |
1.2 |
_logger.write(toString(), Logger.SYSMSG, "deregistered DataHandler for hosts: "+hostList); |
188 |
tdb |
1.1 |
} |
189 |
tdb |
1.10 |
// always remove host from our complete host list |
190 |
|
|
_allHostsList.remove(dhQueue); |
191 |
tdb |
1.1 |
} |
192 |
|
|
|
193 |
|
|
/** |
194 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
195 |
|
|
* method to provide clean logging (every class should have this). |
196 |
|
|
* |
197 |
|
|
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
198 |
|
|
* to format the toString() |
199 |
|
|
* |
200 |
|
|
* @return the name of this class and its CVS revision |
201 |
|
|
*/ |
202 |
|
|
public String toString() { |
203 |
|
|
return FormatName.getName( |
204 |
|
|
_name, |
205 |
|
|
getClass().getName(), |
206 |
|
|
REVISION); |
207 |
|
|
} |
208 |
|
|
|
209 |
|
|
//---PRIVATE METHODS--- |
210 |
|
|
|
211 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
212 |
tdb |
1.4 |
|
213 |
|
|
/** |
214 |
|
|
* Accessor to return a reference to the Queue object. This |
215 |
|
|
* is needed so the ClientInterfaceServant can get add data |
216 |
|
|
* easily. |
217 |
|
|
* |
218 |
|
|
* @return a reference to our Queue object. |
219 |
|
|
*/ |
220 |
tdb |
1.1 |
public Queue getQueue() { |
221 |
|
|
return _queue; |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
//---ATTRIBUTES--- |
225 |
|
|
|
226 |
|
|
/** |
227 |
|
|
* This is the friendly identifier of the |
228 |
|
|
* component this class is running in. |
229 |
|
|
* eg, a Filter may be called "filter1", |
230 |
|
|
* If this class does not have an owning |
231 |
|
|
* component, a name from the configuration |
232 |
|
|
* can be placed here. This name could also |
233 |
|
|
* be changed to null for utility classes. |
234 |
|
|
*/ |
235 |
|
|
private String _name = ClientInterfaceMain.NAME; |
236 |
|
|
|
237 |
|
|
/** |
238 |
|
|
* This holds a reference to the |
239 |
|
|
* system logger that is being used. |
240 |
|
|
*/ |
241 |
|
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
242 |
|
|
|
243 |
tdb |
1.4 |
/** |
244 |
|
|
* A reference to the Queue we're using. |
245 |
|
|
*/ |
246 |
tdb |
1.1 |
private Queue _queue; |
247 |
|
|
|
248 |
tdb |
1.4 |
/** |
249 |
|
|
* A HashMap to store lists of Queue's (in the DataHandlers) |
250 |
|
|
* in a way that can be easily accessed when data comes in. |
251 |
|
|
*/ |
252 |
tdb |
1.7 |
private Map _hostMap; |
253 |
tdb |
1.4 |
|
254 |
|
|
/** |
255 |
|
|
* A list specifically for a Queue's associated with DataHandlers |
256 |
|
|
* that want all host information. |
257 |
tdb |
1.10 |
*/ |
258 |
|
|
private List _allHostDataList; |
259 |
|
|
|
260 |
|
|
/** |
261 |
|
|
* A list of all hosts. |
262 |
tdb |
1.4 |
*/ |
263 |
tdb |
1.7 |
private List _allHostsList; |
264 |
tdb |
1.1 |
|
265 |
|
|
//---STATIC ATTRIBUTES--- |
266 |
|
|
|
267 |
|
|
} |