10 |
|
/** |
11 |
|
* Receives data from the incoming CORBA servant, places |
12 |
|
* it in a Queue, and then arranges distribution to the |
13 |
< |
* ClientHandlers. |
13 |
> |
* 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 |
|
* |
18 |
|
* @author $Author$ |
19 |
|
* @version $Id$ |
33 |
|
|
34 |
|
/** |
35 |
|
* Creates a new PacketSorter. |
36 |
+ |
* |
37 |
+ |
* @param queueMonitorInterval The interval at which to monitor the Queue |
38 |
|
*/ |
39 |
< |
public PacketSorter() { |
39 |
> |
public PacketSorter(int queueMonitorInterval) { |
40 |
|
_queue = new Queue(); |
41 |
+ |
// startup a monitor on this queue, every minute |
42 |
+ |
String queueName = _name + " PacketSorterQueue"; |
43 |
+ |
_queue.startMonitor(queueMonitorInterval*1000, queueName); |
44 |
|
_hostMap = new HashMap(); |
45 |
|
_allHostsList = new LinkedList(); |
46 |
|
_logger.write(toString(), Logger.SYSINIT, "created"); |
48 |
|
|
49 |
|
//---PUBLIC METHODS--- |
50 |
|
|
51 |
+ |
/** |
52 |
+ |
* Method to start the PacketSorter running. This method will |
53 |
+ |
* loop forever processing and sending data. |
54 |
+ |
*/ |
55 |
|
public void run() { |
56 |
|
int qID = _queue.getQueue(); |
57 |
|
while(true) { |
58 |
+ |
// attempt to get some data from the Queue |
59 |
|
String xml = ""; |
60 |
|
try { |
61 |
|
xml = (String) _queue.get(qID); |
64 |
|
_logger.write(toString(), Logger.ERROR, "Queue failure: "+e); |
65 |
|
} |
66 |
|
|
67 |
< |
// look at host map |
68 |
< |
|
69 |
< |
// should be a neater way to do this |
57 |
< |
XMLPacket packet = new XMLPacketMaker(xml).createXMLPacket(); |
67 |
> |
// find out which host this packet is actually for |
68 |
> |
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml); |
69 |
> |
XMLPacket packet = xmlPacketMaker.createXMLPacket(); |
70 |
|
String host = packet.getParam("packet.attributes.machine_name"); |
59 |
– |
LinkedList list = (LinkedList) _hostMap.get(host); |
71 |
|
|
72 |
< |
Iterator i = list.iterator(); |
73 |
< |
while(i.hasNext()) { |
74 |
< |
((Queue) i.next()).add(xml); |
72 |
> |
// look in the hostMap to see if anyone wants this data |
73 |
> |
if(_hostMap.containsKey(host)) { |
74 |
> |
LinkedList list = (LinkedList) _hostMap.get(host); |
75 |
> |
Iterator i = list.iterator(); |
76 |
> |
// push the data to the listening Handler's queue |
77 |
> |
while(i.hasNext()) { |
78 |
> |
((Queue) i.next()).add(xml); |
79 |
> |
} |
80 |
|
} |
81 |
|
|
82 |
< |
// look at all hosts |
83 |
< |
|
82 |
> |
// any handler in this list wants all packets, so send |
83 |
> |
// it on to them regardless |
84 |
|
Iterator j = _allHostsList.iterator(); |
85 |
|
while(j.hasNext()) { |
86 |
|
((Queue) j.next()).add(xml); |
88 |
|
} |
89 |
|
} |
90 |
|
|
91 |
< |
// MUST DEAL WITH hostList="" implying "all hosts" |
92 |
< |
|
91 |
> |
/** |
92 |
> |
* Register a DataHandler in the system. This method |
93 |
> |
* actually takes a reference to a Queue, which should be |
94 |
> |
* a Queue that the DataHandler is making use of. |
95 |
> |
* It also takes a hostList, this being a semi-colon |
96 |
> |
* seperated list of hosts that the Client the DataHandler |
97 |
> |
* is serving has requested. If this list is simply an empty |
98 |
> |
* String, it is assumed the Client wants to listen to all |
99 |
> |
* host information. |
100 |
> |
* |
101 |
> |
* @param dhQueue a Queue being used by the DataHandler that is registering |
102 |
> |
* @param hostList a semi-colon seperated list of hosts |
103 |
> |
*/ |
104 |
|
public void register(Queue dhQueue, String hostList) { |
105 |
+ |
// check to see if we want all hosts |
106 |
|
if(hostList.equals("")) { |
107 |
|
_allHostsList.add(dhQueue); |
108 |
|
_logger.write(toString(), Logger.SYSMSG, "registered DataHandler for all hosts"); |
109 |
|
} |
110 |
|
else { |
111 |
+ |
// go through the list of hosts |
112 |
|
StringTokenizer st = new StringTokenizer(hostList, ";"); |
113 |
|
while(st.hasMoreTokens()) { |
114 |
|
String host = st.nextToken(); |
115 |
+ |
// see if we already have a list in the map for this host |
116 |
|
if(_hostMap.containsKey(host)) { |
117 |
+ |
// we do, so add to it |
118 |
|
LinkedList list = (LinkedList) _hostMap.get(host); |
119 |
|
list.add(dhQueue); |
120 |
|
} |
121 |
|
else { |
122 |
+ |
// we don't, so create a list and put it in the map |
123 |
|
LinkedList list = new LinkedList(); |
124 |
|
list.add(dhQueue); |
125 |
|
_hostMap.put(host, list); |
129 |
|
} |
130 |
|
} |
131 |
|
|
132 |
+ |
/** |
133 |
+ |
* Deregister a DataHandler. The DataHandler should give a reference |
134 |
+ |
* to the Queue it's using, and the *same* hostList it gave when it |
135 |
+ |
* register. It is imperative that the hostList is the same, otherwise |
136 |
+ |
* there will be all sorts of problems with lists getting out of sync. |
137 |
+ |
* |
138 |
+ |
* NB: Possible future addition would be recording of hostList's. |
139 |
+ |
* |
140 |
+ |
* @param dhQueue a Queue being used by the DataHandler that is deregistering |
141 |
+ |
* @param hostList a semi-colon seperated list of hosts |
142 |
+ |
*/ |
143 |
|
public void deregister(Queue dhQueue, String hostList) { |
144 |
+ |
// go through the list of hosts |
145 |
|
if(hostList.equals("")) { |
146 |
|
_allHostsList.remove(dhQueue); |
147 |
|
_logger.write(toString(), Logger.SYSMSG, "deregistered DataHandler for all hosts"); |
150 |
|
StringTokenizer st = new StringTokenizer(hostList, ";"); |
151 |
|
while(st.hasMoreTokens()) { |
152 |
|
String host = st.nextToken(); |
153 |
+ |
// this should in reality always be true, but best check |
154 |
|
if(_hostMap.containsKey(host)) { |
155 |
+ |
// get the list and remove the host in question |
156 |
|
LinkedList list = (LinkedList) _hostMap.get(host); |
157 |
|
list.remove(dhQueue); |
158 |
+ |
// if the list is now empty, we might as well remove it |
159 |
|
if(list.size()==0) { |
160 |
|
_hostMap.remove(host); |
161 |
|
} |
184 |
|
//---PRIVATE METHODS--- |
185 |
|
|
186 |
|
//---ACCESSOR/MUTATOR METHODS--- |
187 |
< |
|
187 |
> |
|
188 |
> |
/** |
189 |
> |
* Accessor to return a reference to the Queue object. This |
190 |
> |
* is needed so the ClientInterfaceServant can get add data |
191 |
> |
* easily. |
192 |
> |
* |
193 |
> |
* @return a reference to our Queue object. |
194 |
> |
*/ |
195 |
|
public Queue getQueue() { |
196 |
|
return _queue; |
197 |
|
} |
215 |
|
*/ |
216 |
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
217 |
|
|
218 |
+ |
/** |
219 |
+ |
* A reference to the Queue we're using. |
220 |
+ |
*/ |
221 |
|
private Queue _queue; |
222 |
|
|
223 |
+ |
/** |
224 |
+ |
* A HashMap to store lists of Queue's (in the DataHandlers) |
225 |
+ |
* in a way that can be easily accessed when data comes in. |
226 |
+ |
*/ |
227 |
|
private HashMap _hostMap; |
228 |
+ |
|
229 |
+ |
/** |
230 |
+ |
* A list specifically for a Queue's associated with DataHandlers |
231 |
+ |
* that want all host information. |
232 |
+ |
*/ |
233 |
|
private LinkedList _allHostsList; |
234 |
|
|
235 |
|
//---STATIC ATTRIBUTES--- |