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.1 2001/01/23 16:55:23 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.1 $"; |
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 |
XMLPacket packet = new XMLPacketMaker(xml).createXMLPacket(); |
58 |
String host = packet.getParam("packet.attributes.machine_name"); |
59 |
LinkedList list = (LinkedList) _hostMap.get(host); |
60 |
|
61 |
Iterator i = list.iterator(); |
62 |
while(i.hasNext()) { |
63 |
((Queue) i.next()).add(xml); |
64 |
} |
65 |
|
66 |
// look at all hosts |
67 |
|
68 |
Iterator j = _allHostsList.iterator(); |
69 |
while(j.hasNext()) { |
70 |
((Queue) j.next()).add(xml); |
71 |
} |
72 |
} |
73 |
} |
74 |
|
75 |
// MUST DEAL WITH hostList="" implying "all hosts" |
76 |
|
77 |
public void register(Queue dhQueue, String hostList) { |
78 |
if(hostList.equals("")) { |
79 |
_allHostsList.add(dhQueue); |
80 |
_logger.write(toString(), Logger.SYSMSG, "registered DataHandler for all hosts"); |
81 |
} |
82 |
else { |
83 |
StringTokenizer st = new StringTokenizer(hostList, ";"); |
84 |
while(st.hasMoreTokens()) { |
85 |
String host = st.nextToken(); |
86 |
if(_hostMap.containsKey(host)) { |
87 |
LinkedList list = (LinkedList) _hostMap.get(host); |
88 |
list.add(dhQueue); |
89 |
} |
90 |
else { |
91 |
LinkedList list = new LinkedList(); |
92 |
list.add(dhQueue); |
93 |
_hostMap.put(host, list); |
94 |
} |
95 |
} |
96 |
_logger.write(toString(), Logger.SYSMSG, "registered DataHandler for hosts: "+hostList); |
97 |
} |
98 |
} |
99 |
|
100 |
public void deregister(Queue dhQueue, String hostList) { |
101 |
if(hostList.equals("")) { |
102 |
_allHostsList.remove(dhQueue); |
103 |
_logger.write(toString(), Logger.SYSMSG, "deregistered DataHandler for all hosts"); |
104 |
} |
105 |
else { |
106 |
StringTokenizer st = new StringTokenizer(hostList, ";"); |
107 |
while(st.hasMoreTokens()) { |
108 |
String host = st.nextToken(); |
109 |
if(_hostMap.containsKey(host)) { |
110 |
LinkedList list = (LinkedList) _hostMap.get(host); |
111 |
list.remove(dhQueue); |
112 |
if(list.size()==0) { |
113 |
_hostMap.remove(host); |
114 |
} |
115 |
} |
116 |
} |
117 |
_logger.write(toString(), Logger.SYSMSG, "deregistered DataHandler for hosts: "+hostList); |
118 |
} |
119 |
} |
120 |
|
121 |
/** |
122 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
123 |
* method to provide clean logging (every class should have this). |
124 |
* |
125 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
126 |
* to format the toString() |
127 |
* |
128 |
* @return the name of this class and its CVS revision |
129 |
*/ |
130 |
public String toString() { |
131 |
return FormatName.getName( |
132 |
_name, |
133 |
getClass().getName(), |
134 |
REVISION); |
135 |
} |
136 |
|
137 |
//---PRIVATE METHODS--- |
138 |
|
139 |
//---ACCESSOR/MUTATOR METHODS--- |
140 |
|
141 |
public Queue getQueue() { |
142 |
return _queue; |
143 |
} |
144 |
|
145 |
//---ATTRIBUTES--- |
146 |
|
147 |
/** |
148 |
* This is the friendly identifier of the |
149 |
* component this class is running in. |
150 |
* eg, a Filter may be called "filter1", |
151 |
* If this class does not have an owning |
152 |
* component, a name from the configuration |
153 |
* can be placed here. This name could also |
154 |
* be changed to null for utility classes. |
155 |
*/ |
156 |
private String _name = ClientInterfaceMain.NAME; |
157 |
|
158 |
/** |
159 |
* This holds a reference to the |
160 |
* system logger that is being used. |
161 |
*/ |
162 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
163 |
|
164 |
private Queue _queue; |
165 |
|
166 |
private HashMap _hostMap; |
167 |
private LinkedList _allHostsList; |
168 |
|
169 |
//---STATIC ATTRIBUTES--- |
170 |
|
171 |
} |