ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/clientinterface/PacketSorter.java
Revision: 1.4
Committed: Sat Jan 27 23:30:40 2001 UTC (23 years, 4 months ago) by tdb
Branch: MAIN
Changes since 1.3: +69 -11 lines
Log Message:
All of these classes have been javadoc'd and commented.

File Contents

# Content
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 * 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: tdb1 $
19 * @version $Id: PacketSorter.java,v 1.3 2001/01/23 18:23:03 tdb1 Exp $
20 */
21 class PacketSorter extends Thread {
22
23 //---FINAL ATTRIBUTES---
24
25 /**
26 * The current CVS revision of this class
27 */
28 public final String REVISION = "$Revision: 1.3 $";
29
30 //---STATIC METHODS---
31
32 //---CONSTRUCTORS---
33
34 /**
35 * Creates a new PacketSorter.
36 */
37 public PacketSorter() {
38 _queue = new Queue();
39 _hostMap = new HashMap();
40 _allHostsList = new LinkedList();
41 _logger.write(toString(), Logger.SYSINIT, "created");
42 }
43
44 //---PUBLIC METHODS---
45
46 /**
47 * Method to start the PacketSorter running. This method will
48 * loop forever processing and sending data.
49 */
50 public void run() {
51 int qID = _queue.getQueue();
52 while(true) {
53 // attempt to get some data from the Queue
54 String xml = "";
55 try {
56 xml = (String) _queue.get(qID);
57 }
58 catch(InvalidQueueException e) {
59 _logger.write(toString(), Logger.ERROR, "Queue failure: "+e);
60 }
61
62 // find out which host this packet is actually for
63 XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml);
64 XMLPacket packet = xmlPacketMaker.createXMLPacket();
65 String host = packet.getParam("packet.attributes.machine_name");
66
67 // look in the hostMap to see if anyone wants this data
68 if(_hostMap.containsKey(host)) {
69 LinkedList list = (LinkedList) _hostMap.get(host);
70 Iterator i = list.iterator();
71 // push the data to the listening Handler's queue
72 while(i.hasNext()) {
73 ((Queue) i.next()).add(xml);
74 }
75 }
76
77 // any handler in this list wants all packets, so send
78 // it on to them regardless
79 Iterator j = _allHostsList.iterator();
80 while(j.hasNext()) {
81 ((Queue) j.next()).add(xml);
82 }
83 }
84 }
85
86 /**
87 * Register a DataHandler in the system. This method
88 * actually takes a reference to a Queue, which should be
89 * a Queue that the DataHandler is making use of.
90 * It also takes a hostList, this being a semi-colon
91 * seperated list of hosts that the Client the DataHandler
92 * is serving has requested. If this list is simply an empty
93 * String, it is assumed the Client wants to listen to all
94 * host information.
95 *
96 * @param dhQueue a Queue being used by the DataHandler that is registering
97 * @param hostList a semi-colon seperated list of hosts
98 */
99 public void register(Queue dhQueue, String hostList) {
100 // check to see if we want all hosts
101 if(hostList.equals("")) {
102 _allHostsList.add(dhQueue);
103 _logger.write(toString(), Logger.SYSMSG, "registered DataHandler for all hosts");
104 }
105 else {
106 // go through the list of hosts
107 StringTokenizer st = new StringTokenizer(hostList, ";");
108 while(st.hasMoreTokens()) {
109 String host = st.nextToken();
110 // see if we already have a list in the map for this host
111 if(_hostMap.containsKey(host)) {
112 // we do, so add to it
113 LinkedList list = (LinkedList) _hostMap.get(host);
114 list.add(dhQueue);
115 }
116 else {
117 // we don't, so create a list and put it in the map
118 LinkedList list = new LinkedList();
119 list.add(dhQueue);
120 _hostMap.put(host, list);
121 }
122 }
123 _logger.write(toString(), Logger.SYSMSG, "registered DataHandler for hosts: "+hostList);
124 }
125 }
126
127 /**
128 * Deregister a DataHandler. The DataHandler should give a reference
129 * to the Queue it's using, and the *same* hostList it gave when it
130 * register. It is imperative that the hostList is the same, otherwise
131 * there will be all sorts of problems with lists getting out of sync.
132 *
133 * NB: Possible future addition would be recording of hostList's.
134 *
135 * @param dhQueue a Queue being used by the DataHandler that is deregistering
136 * @param hostList a semi-colon seperated list of hosts
137 */
138 public void deregister(Queue dhQueue, String hostList) {
139 // go through the list of hosts
140 if(hostList.equals("")) {
141 _allHostsList.remove(dhQueue);
142 _logger.write(toString(), Logger.SYSMSG, "deregistered DataHandler for all hosts");
143 }
144 else {
145 StringTokenizer st = new StringTokenizer(hostList, ";");
146 while(st.hasMoreTokens()) {
147 String host = st.nextToken();
148 // this should in reality always be true, but best check
149 if(_hostMap.containsKey(host)) {
150 // get the list and remove the host in question
151 LinkedList list = (LinkedList) _hostMap.get(host);
152 list.remove(dhQueue);
153 // if the list is now empty, we might as well remove it
154 if(list.size()==0) {
155 _hostMap.remove(host);
156 }
157 }
158 }
159 _logger.write(toString(), Logger.SYSMSG, "deregistered DataHandler for hosts: "+hostList);
160 }
161 }
162
163 /**
164 * Overrides the {@link java.lang.Object#toString() Object.toString()}
165 * method to provide clean logging (every class should have this).
166 *
167 * This uses the uk.ac.ukc.iscream.util.NameFormat class
168 * to format the toString()
169 *
170 * @return the name of this class and its CVS revision
171 */
172 public String toString() {
173 return FormatName.getName(
174 _name,
175 getClass().getName(),
176 REVISION);
177 }
178
179 //---PRIVATE METHODS---
180
181 //---ACCESSOR/MUTATOR METHODS---
182
183 /**
184 * Accessor to return a reference to the Queue object. This
185 * is needed so the ClientInterfaceServant can get add data
186 * easily.
187 *
188 * @return a reference to our Queue object.
189 */
190 public Queue getQueue() {
191 return _queue;
192 }
193
194 //---ATTRIBUTES---
195
196 /**
197 * This is the friendly identifier of the
198 * component this class is running in.
199 * eg, a Filter may be called "filter1",
200 * If this class does not have an owning
201 * component, a name from the configuration
202 * can be placed here. This name could also
203 * be changed to null for utility classes.
204 */
205 private String _name = ClientInterfaceMain.NAME;
206
207 /**
208 * This holds a reference to the
209 * system logger that is being used.
210 */
211 private Logger _logger = ReferenceManager.getInstance().getLogger();
212
213 /**
214 * A reference to the Queue we're using.
215 */
216 private Queue _queue;
217
218 /**
219 * A HashMap to store lists of Queue's (in the DataHandlers)
220 * in a way that can be easily accessed when data comes in.
221 */
222 private HashMap _hostMap;
223
224 /**
225 * A list specifically for a Queue's associated with DataHandlers
226 * that want all host information.
227 */
228 private LinkedList _allHostsList;
229
230 //---STATIC ATTRIBUTES---
231
232 }