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