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.9
Committed: Thu Mar 1 02:14:37 2001 UTC (23 years, 3 months ago) by tdb
Branch: MAIN
Changes since 1.8: +5 -5 lines
Log Message:
Some ClassCastExceptions going on that only just came up when the client got
to 1.1 protocol :)

File Contents

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