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$ |
16 |
* @version $Id$ |
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 |
_logger.write(toString(), Logger.SYSINIT, "created"); |
38 |
} |
39 |
|
40 |
//---PUBLIC METHODS--- |
41 |
|
42 |
public void run() { |
43 |
int qID = _queue.getQueue(); |
44 |
while(true) { |
45 |
String xml = ""; |
46 |
try { |
47 |
xml = (String) _queue.get(qID); |
48 |
} |
49 |
catch(InvalidQueueException e) { |
50 |
_logger.write(toString(), Logger.ERROR, "Queue failure: "+e); |
51 |
} |
52 |
|
53 |
// should be a neater way to do this |
54 |
XMLPacket packet = new XMLPacketMaker(xml).createXMLPacket(); |
55 |
String host = packet.getParam("packet.attributes.machine_name"); |
56 |
LinkedList list = (LinkedList) _hostMap.get(host); |
57 |
|
58 |
Iterator i = list.iterator(); |
59 |
while(i.hasNext()) { |
60 |
((Queue) i.next()).add(xml); |
61 |
} |
62 |
} |
63 |
} |
64 |
|
65 |
// MUST DEAL WITH hostList="" implying "all hosts" |
66 |
|
67 |
public void register(Queue dhQueue, String hostList) { |
68 |
StringTokenizer st = new StringTokenizer(hostList, ";"); |
69 |
while(st.hasMoreTokens()) { |
70 |
String host = st.nextToken(); |
71 |
if(_hostMap.containsKey(host)) { |
72 |
LinkedList list = (LinkedList) _hostMap.get(host); |
73 |
list.add(dhQueue); |
74 |
} |
75 |
else { |
76 |
LinkedList list = new LinkedList(); |
77 |
list.add(dhQueue); |
78 |
_hostMap.put(host, list); |
79 |
} |
80 |
} |
81 |
_logger.write(toString(), Logger.SYSMSG, "registered DataHandler - " + dhQueue.toString()); |
82 |
} |
83 |
|
84 |
public void deregister(Queue dhQueue, String hostList) { |
85 |
StringTokenizer st = new StringTokenizer(hostList, ";"); |
86 |
while(st.hasMoreTokens()) { |
87 |
String host = st.nextToken(); |
88 |
if(_hostMap.containsKey(host)) { |
89 |
LinkedList list = (LinkedList) _hostMap.get(host); |
90 |
list.remove(dhQueue); |
91 |
if(list.size()==0) { |
92 |
_hostMap.remove(host); |
93 |
} |
94 |
} |
95 |
} |
96 |
_logger.write(toString(), Logger.SYSMSG, "deregistered DataHandler - " + dhQueue.toString()); |
97 |
} |
98 |
|
99 |
/** |
100 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
101 |
* method to provide clean logging (every class should have this). |
102 |
* |
103 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
104 |
* to format the toString() |
105 |
* |
106 |
* @return the name of this class and its CVS revision |
107 |
*/ |
108 |
public String toString() { |
109 |
return FormatName.getName( |
110 |
_name, |
111 |
getClass().getName(), |
112 |
REVISION); |
113 |
} |
114 |
|
115 |
//---PRIVATE METHODS--- |
116 |
|
117 |
//---ACCESSOR/MUTATOR METHODS--- |
118 |
|
119 |
public Queue getQueue() { |
120 |
return _queue; |
121 |
} |
122 |
|
123 |
//---ATTRIBUTES--- |
124 |
|
125 |
/** |
126 |
* This is the friendly identifier of the |
127 |
* component this class is running in. |
128 |
* eg, a Filter may be called "filter1", |
129 |
* If this class does not have an owning |
130 |
* component, a name from the configuration |
131 |
* can be placed here. This name could also |
132 |
* be changed to null for utility classes. |
133 |
*/ |
134 |
private String _name = ClientInterfaceMain.NAME; |
135 |
|
136 |
/** |
137 |
* This holds a reference to the |
138 |
* system logger that is being used. |
139 |
*/ |
140 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
141 |
|
142 |
private Queue _queue; |
143 |
|
144 |
private HashMap _hostMap; |
145 |
|
146 |
//---STATIC ATTRIBUTES--- |
147 |
|
148 |
} |