1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.clientinterface; |
2 |
> |
package uk.org.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.*; |
5 |
> |
import uk.org.iscream.util.*; |
6 |
> |
import uk.org.iscream.componentmanager.*; |
7 |
> |
import uk.org.iscream.core.*; |
8 |
|
import java.util.*; |
9 |
|
|
10 |
|
/** |
33 |
|
|
34 |
|
/** |
35 |
|
* Creates a new PacketSorter. |
36 |
– |
* |
37 |
– |
* @param queueMonitorInterval The interval at which to monitor the Queue |
36 |
|
*/ |
37 |
< |
public PacketSorter(int queueMonitorInterval) { |
38 |
< |
_queue = new Queue(); |
39 |
< |
// startup a monitor on this queue, every minute |
40 |
< |
String queueName = _name + " PacketSorterQueue"; |
41 |
< |
_queue.startMonitor(queueMonitorInterval*1000, queueName); |
37 |
> |
public PacketSorter() { |
38 |
> |
// set the Thread name |
39 |
> |
setName("clientinterface.PacketSorter"); |
40 |
> |
|
41 |
> |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
42 |
> |
String configName = "ClientInterface"; |
43 |
> |
|
44 |
> |
// see if this Queue needs a size limit |
45 |
> |
try { |
46 |
> |
int queueSizeLimit = Integer.parseInt(cp.getProperty(configName, "Queue.SizeLimit")); |
47 |
> |
String queueRemoveAlgorithm = cp.getProperty(configName, "Queue.RemoveAlgorithm"); |
48 |
> |
int algorithm = StringUtils.getStringPos(queueRemoveAlgorithm, Queue.algorithms); |
49 |
> |
if(algorithm != -1) { |
50 |
> |
_logger.write(toString(), Logger.DEBUG, "Starting Queue with size limit of "+queueSizeLimit+", using remove algorithm "+queueRemoveAlgorithm); |
51 |
> |
// we have valid values, so lets start it. |
52 |
> |
_queue = new Queue(queueSizeLimit, algorithm); |
53 |
> |
} |
54 |
> |
else { |
55 |
> |
_logger.write(toString(), Logger.WARNING, "Bad Queue Algorithm configuration, not known: "+queueRemoveAlgorithm); |
56 |
> |
// just don't activate a limit |
57 |
> |
_queue = new Queue(); |
58 |
> |
} |
59 |
> |
|
60 |
> |
} catch (PropertyNotFoundException e) { |
61 |
> |
_logger.write(toString(), Logger.DEBUG, "Optional config not set: "+e); |
62 |
> |
// just don't activate a limit |
63 |
> |
_queue = new Queue(); |
64 |
> |
} catch (NumberFormatException e) { |
65 |
> |
_logger.write(toString(), Logger.WARNING, "Bad Queue SizeLimit configuration: "+e); |
66 |
> |
// just don't activate a limit |
67 |
> |
_queue = new Queue(); |
68 |
> |
} |
69 |
> |
|
70 |
> |
// startup a monitor on this queue |
71 |
> |
try { |
72 |
> |
// try to get the interval, if this fails, we won't start up the monitor |
73 |
> |
int queueMonitorInterval = Integer.parseInt(cp.getProperty(configName, "Queue.MonitorInterval")); |
74 |
> |
String queueName = _name + " PacketSorterQueue"; |
75 |
> |
_queue.startMonitor(queueMonitorInterval*1000, queueName); |
76 |
> |
} catch (PropertyNotFoundException e) { |
77 |
> |
_logger.write(toString(), Logger.WARNING, "failed to find queue monitor config, disabling. " + e); |
78 |
> |
} |
79 |
> |
|
80 |
|
_hostMap = new HashMap(); |
81 |
|
_allHostDataList = new LinkedList(); |
82 |
|
_allHostsList = new LinkedList(); |
90 |
|
* loop forever processing and sending data. |
91 |
|
*/ |
92 |
|
public void run() { |
93 |
+ |
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(); |
94 |
|
int qID = _queue.getQueue(); |
95 |
|
while(true) { |
96 |
|
// attempt to get some data from the Queue |
105 |
|
XMLPacket packet = null; |
106 |
|
|
107 |
|
try { |
108 |
< |
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml); |
72 |
< |
packet = xmlPacketMaker.createXMLPacket(); |
108 |
> |
packet = xmlPacketMaker.createXMLPacket(xml); |
109 |
|
} catch(InvalidXMLException e) { |
110 |
|
_logger.write(toString(), Logger.ERROR, "Invalid XML: "+e); |
111 |
|
// skip the rest of this loop iteration |
164 |
|
* @param dhQueue a Queue being used by the DataHandler that is registering |
165 |
|
* @param hostList a semi-colon seperated list of hosts |
166 |
|
*/ |
167 |
< |
public synchronized void register(Queue dhQueue, String hostList) { |
167 |
> |
public void register(Queue dhQueue, String hostList) { |
168 |
|
// check to see if we want all hosts |
169 |
|
if(hostList.equals("")) { |
170 |
< |
_allHostDataList.add(dhQueue); |
170 |
> |
synchronized(this) { |
171 |
> |
_allHostDataList.add(dhQueue); |
172 |
> |
} |
173 |
|
_logger.write(toString(), Logger.SYSMSG, "registered DataHandler for all hosts"); |
174 |
|
} |
175 |
|
else { |
177 |
|
StringTokenizer st = new StringTokenizer(hostList, ";"); |
178 |
|
while(st.hasMoreTokens()) { |
179 |
|
String host = st.nextToken(); |
180 |
< |
// see if we already have a list in the map for this host |
181 |
< |
if(_hostMap.containsKey(host)) { |
182 |
< |
// we do, so add to it |
183 |
< |
List list = (List) _hostMap.get(host); |
184 |
< |
list.add(dhQueue); |
180 |
> |
synchronized(this) { |
181 |
> |
// see if we already have a list in the map for this host |
182 |
> |
if(_hostMap.containsKey(host)) { |
183 |
> |
// we do, so add to it |
184 |
> |
List list = (List) _hostMap.get(host); |
185 |
> |
list.add(dhQueue); |
186 |
> |
} |
187 |
> |
else { |
188 |
> |
// we don't, so create a list and put it in the map |
189 |
> |
LinkedList list = new LinkedList(); |
190 |
> |
list.add(dhQueue); |
191 |
> |
_hostMap.put(host, list); |
192 |
> |
} |
193 |
|
} |
148 |
– |
else { |
149 |
– |
// we don't, so create a list and put it in the map |
150 |
– |
LinkedList list = new LinkedList(); |
151 |
– |
list.add(dhQueue); |
152 |
– |
_hostMap.put(host, list); |
153 |
– |
} |
194 |
|
} |
195 |
|
_logger.write(toString(), Logger.SYSMSG, "registered DataHandler for hosts: "+hostList); |
196 |
|
} |
197 |
|
// always add host to our complete host list |
198 |
< |
_allHostsList.add(dhQueue); |
198 |
> |
synchronized(this) { |
199 |
> |
_allHostsList.add(dhQueue); |
200 |
> |
} |
201 |
|
} |
202 |
|
|
203 |
|
/** |
211 |
|
* @param dhQueue a Queue being used by the DataHandler that is deregistering |
212 |
|
* @param hostList a semi-colon seperated list of hosts |
213 |
|
*/ |
214 |
< |
public synchronized void deregister(Queue dhQueue, String hostList) { |
214 |
> |
public void deregister(Queue dhQueue, String hostList) { |
215 |
|
// go through the list of hosts |
216 |
|
if(hostList.equals("")) { |
217 |
< |
_allHostDataList.remove(dhQueue); |
217 |
> |
synchronized(this) { |
218 |
> |
_allHostDataList.remove(dhQueue); |
219 |
> |
} |
220 |
|
_logger.write(toString(), Logger.SYSMSG, "deregistered DataHandler for all hosts"); |
221 |
|
} |
222 |
|
else { |
223 |
|
StringTokenizer st = new StringTokenizer(hostList, ";"); |
224 |
|
while(st.hasMoreTokens()) { |
225 |
|
String host = st.nextToken(); |
226 |
< |
// this should in reality always be true, but best check |
227 |
< |
if(_hostMap.containsKey(host)) { |
228 |
< |
// get the list and remove the host in question |
229 |
< |
LinkedList list = (LinkedList) _hostMap.get(host); |
230 |
< |
list.remove(dhQueue); |
231 |
< |
// if the list is now empty, we might as well remove it |
232 |
< |
if(list.size()==0) { |
233 |
< |
_hostMap.remove(host); |
226 |
> |
synchronized(this) { |
227 |
> |
// this should in reality always be true, but best check |
228 |
> |
if(_hostMap.containsKey(host)) { |
229 |
> |
// get the list and remove the host in question |
230 |
> |
LinkedList list = (LinkedList) _hostMap.get(host); |
231 |
> |
list.remove(dhQueue); |
232 |
> |
// if the list is now empty, we might as well remove it |
233 |
> |
if(list.size()==0) { |
234 |
> |
_hostMap.remove(host); |
235 |
> |
} |
236 |
|
} |
237 |
|
} |
238 |
|
} |
239 |
|
_logger.write(toString(), Logger.SYSMSG, "deregistered DataHandler for hosts: "+hostList); |
240 |
|
} |
241 |
|
// always remove host from our complete host list |
242 |
< |
_allHostsList.remove(dhQueue); |
242 |
> |
synchronized(this) { |
243 |
> |
_allHostsList.remove(dhQueue); |
244 |
> |
} |
245 |
|
} |
246 |
|
|
247 |
|
/** |
248 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
249 |
|
* method to provide clean logging (every class should have this). |
250 |
|
* |
251 |
< |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
251 |
> |
* This uses the uk.org.iscream.util.NameFormat class |
252 |
|
* to format the toString() |
253 |
|
* |
254 |
|
* @return the name of this class and its CVS revision |