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 |
* ClientHandlers. |
14 |
* |
15 |
* @author $Author: tdb1 $ |
16 |
* @version $Id: PacketSorter.java,v 1.2 2001/01/23 17:22:01 tdb1 Exp $ |
17 |
*/ |
18 |
class PacketSorter extends Thread { |
19 |
|
20 |
//---FINAL ATTRIBUTES--- |
21 |
|
22 |
/** |
23 |
* The current CVS revision of this class |
24 |
*/ |
25 |
public final String REVISION = "$Revision: 1.2 $"; |
26 |
|
27 |
//---STATIC METHODS--- |
28 |
|
29 |
//---CONSTRUCTORS--- |
30 |
|
31 |
/** |
32 |
* Creates a new PacketSorter. |
33 |
*/ |
34 |
public PacketSorter() { |
35 |
_queue = new Queue(); |
36 |
_hostMap = new HashMap(); |
37 |
_allHostsList = new LinkedList(); |
38 |
_logger.write(toString(), Logger.SYSINIT, "created"); |
39 |
} |
40 |
|
41 |
//---PUBLIC METHODS--- |
42 |
|
43 |
public void run() { |
44 |
int qID = _queue.getQueue(); |
45 |
while(true) { |
46 |
String xml = ""; |
47 |
try { |
48 |
xml = (String) _queue.get(qID); |
49 |
} |
50 |
catch(InvalidQueueException e) { |
51 |
_logger.write(toString(), Logger.ERROR, "Queue failure: "+e); |
52 |
} |
53 |
|
54 |
// look at host map |
55 |
|
56 |
// should be a neater way to do this |
57 |
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml); |
58 |
XMLPacket packet = xmlPacketMaker.createXMLPacket(); |
59 |
String host = packet.getParam("packet.attributes.machine_name"); |
60 |
|
61 |
if(_hostMap.containsKey(host)) { |
62 |
LinkedList list = (LinkedList) _hostMap.get(host); |
63 |
Iterator i = list.iterator(); |
64 |
while(i.hasNext()) { |
65 |
((Queue) i.next()).add(xml); |
66 |
} |
67 |
} |
68 |
|
69 |
// look at all hosts |
70 |
|
71 |
Iterator j = _allHostsList.iterator(); |
72 |
while(j.hasNext()) { |
73 |
((Queue) j.next()).add(xml); |
74 |
} |
75 |
} |
76 |
} |
77 |
|
78 |
// MUST DEAL WITH hostList="" implying "all hosts" |
79 |
|
80 |
public void register(Queue dhQueue, String hostList) { |
81 |
if(hostList.equals("")) { |
82 |
_allHostsList.add(dhQueue); |
83 |
_logger.write(toString(), Logger.SYSMSG, "registered DataHandler for all hosts"); |
84 |
} |
85 |
else { |
86 |
StringTokenizer st = new StringTokenizer(hostList, ";"); |
87 |
while(st.hasMoreTokens()) { |
88 |
String host = st.nextToken(); |
89 |
if(_hostMap.containsKey(host)) { |
90 |
LinkedList list = (LinkedList) _hostMap.get(host); |
91 |
list.add(dhQueue); |
92 |
} |
93 |
else { |
94 |
LinkedList list = new LinkedList(); |
95 |
list.add(dhQueue); |
96 |
_hostMap.put(host, list); |
97 |
} |
98 |
} |
99 |
_logger.write(toString(), Logger.SYSMSG, "registered DataHandler for hosts: "+hostList); |
100 |
} |
101 |
} |
102 |
|
103 |
public void deregister(Queue dhQueue, String hostList) { |
104 |
if(hostList.equals("")) { |
105 |
_allHostsList.remove(dhQueue); |
106 |
_logger.write(toString(), Logger.SYSMSG, "deregistered DataHandler for all hosts"); |
107 |
} |
108 |
else { |
109 |
StringTokenizer st = new StringTokenizer(hostList, ";"); |
110 |
while(st.hasMoreTokens()) { |
111 |
String host = st.nextToken(); |
112 |
if(_hostMap.containsKey(host)) { |
113 |
LinkedList list = (LinkedList) _hostMap.get(host); |
114 |
list.remove(dhQueue); |
115 |
if(list.size()==0) { |
116 |
_hostMap.remove(host); |
117 |
} |
118 |
} |
119 |
} |
120 |
_logger.write(toString(), Logger.SYSMSG, "deregistered DataHandler for hosts: "+hostList); |
121 |
} |
122 |
} |
123 |
|
124 |
/** |
125 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
126 |
* method to provide clean logging (every class should have this). |
127 |
* |
128 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
129 |
* to format the toString() |
130 |
* |
131 |
* @return the name of this class and its CVS revision |
132 |
*/ |
133 |
public String toString() { |
134 |
return FormatName.getName( |
135 |
_name, |
136 |
getClass().getName(), |
137 |
REVISION); |
138 |
} |
139 |
|
140 |
//---PRIVATE METHODS--- |
141 |
|
142 |
//---ACCESSOR/MUTATOR METHODS--- |
143 |
|
144 |
public Queue getQueue() { |
145 |
return _queue; |
146 |
} |
147 |
|
148 |
//---ATTRIBUTES--- |
149 |
|
150 |
/** |
151 |
* This is the friendly identifier of the |
152 |
* component this class is running in. |
153 |
* eg, a Filter may be called "filter1", |
154 |
* If this class does not have an owning |
155 |
* component, a name from the configuration |
156 |
* can be placed here. This name could also |
157 |
* be changed to null for utility classes. |
158 |
*/ |
159 |
private String _name = ClientInterfaceMain.NAME; |
160 |
|
161 |
/** |
162 |
* This holds a reference to the |
163 |
* system logger that is being used. |
164 |
*/ |
165 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
166 |
|
167 |
private Queue _queue; |
168 |
|
169 |
private HashMap _hostMap; |
170 |
private LinkedList _allHostsList; |
171 |
|
172 |
//---STATIC ATTRIBUTES--- |
173 |
|
174 |
} |