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.5
Committed: Mon Feb 12 02:23:52 2001 UTC (23 years, 3 months ago) by tdb
Branch: MAIN
Changes since 1.4: +4 -2 lines
Log Message:
Now monitor the Queue here every 60 seconds, reporting back to the Queue in
the PacketSorter class.

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.4 2001/01/27 23:30:40 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.4 $";
29
30 //---STATIC METHODS---
31
32 //---CONSTRUCTORS---
33
34 /**
35 * Creates a new PacketSorter.
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");
44 }
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);
59 }
60 catch(InvalidQueueException e) {
61 _logger.write(toString(), Logger.ERROR, "Queue failure: "+e);
62 }
63
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");
68
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 // 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);
84 }
85 }
86 }
87
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);
123 }
124 }
125 _logger.write(toString(), Logger.SYSMSG, "registered DataHandler for hosts: "+hostList);
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");
145 }
146 else {
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 }
159 }
160 }
161 _logger.write(toString(), Logger.SYSMSG, "deregistered DataHandler for hosts: "+hostList);
162 }
163 }
164
165 /**
166 * Overrides the {@link java.lang.Object#toString() Object.toString()}
167 * method to provide clean logging (every class should have this).
168 *
169 * This uses the uk.ac.ukc.iscream.util.NameFormat class
170 * to format the toString()
171 *
172 * @return the name of this class and its CVS revision
173 */
174 public String toString() {
175 return FormatName.getName(
176 _name,
177 getClass().getName(),
178 REVISION);
179 }
180
181 //---PRIVATE METHODS---
182
183 //---ACCESSOR/MUTATOR METHODS---
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 }
195
196 //---ATTRIBUTES---
197
198 /**
199 * This is the friendly identifier of the
200 * component this class is running in.
201 * eg, a Filter may be called "filter1",
202 * If this class does not have an owning
203 * component, a name from the configuration
204 * can be placed here. This name could also
205 * be changed to null for utility classes.
206 */
207 private String _name = ClientInterfaceMain.NAME;
208
209 /**
210 * This holds a reference to the
211 * system logger that is being used.
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---
233
234 }