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

# 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.4 * @version $Id: PacketSorter.java,v 1.3 2001/01/23 18:23:03 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.4 public final String REVISION = "$Revision: 1.3 $";
29 tdb 1.1
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 tdb 1.2 _allHostsList = new LinkedList();
41 tdb 1.1 _logger.write(toString(), Logger.SYSINIT, "created");
42     }
43    
44     //---PUBLIC METHODS---
45    
46 tdb 1.4 /**
47     * Method to start the PacketSorter running. This method will
48     * loop forever processing and sending data.
49     */
50 tdb 1.1 public void run() {
51     int qID = _queue.getQueue();
52     while(true) {
53 tdb 1.4 // attempt to get some data from the Queue
54 tdb 1.1 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 tdb 1.4 // find out which host this packet is actually for
63 tdb 1.3 XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml);
64     XMLPacket packet = xmlPacketMaker.createXMLPacket();
65 tdb 1.1 String host = packet.getParam("packet.attributes.machine_name");
66    
67 tdb 1.4 // look in the hostMap to see if anyone wants this data
68 tdb 1.3 if(_hostMap.containsKey(host)) {
69     LinkedList list = (LinkedList) _hostMap.get(host);
70     Iterator i = list.iterator();
71 tdb 1.4 // push the data to the listening Handler's queue
72 tdb 1.3 while(i.hasNext()) {
73     ((Queue) i.next()).add(xml);
74     }
75 tdb 1.1 }
76 tdb 1.2
77 tdb 1.4 // any handler in this list wants all packets, so send
78     // it on to them regardless
79 tdb 1.2 Iterator j = _allHostsList.iterator();
80     while(j.hasNext()) {
81     ((Queue) j.next()).add(xml);
82     }
83 tdb 1.1 }
84     }
85    
86 tdb 1.4 /**
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 tdb 1.1 public void register(Queue dhQueue, String hostList) {
100 tdb 1.4 // check to see if we want all hosts
101 tdb 1.2 if(hostList.equals("")) {
102     _allHostsList.add(dhQueue);
103     _logger.write(toString(), Logger.SYSMSG, "registered DataHandler for all hosts");
104     }
105     else {
106 tdb 1.4 // go through the list of hosts
107 tdb 1.2 StringTokenizer st = new StringTokenizer(hostList, ";");
108     while(st.hasMoreTokens()) {
109     String host = st.nextToken();
110 tdb 1.4 // see if we already have a list in the map for this host
111 tdb 1.2 if(_hostMap.containsKey(host)) {
112 tdb 1.4 // we do, so add to it
113 tdb 1.2 LinkedList list = (LinkedList) _hostMap.get(host);
114     list.add(dhQueue);
115     }
116     else {
117 tdb 1.4 // we don't, so create a list and put it in the map
118 tdb 1.2 LinkedList list = new LinkedList();
119     list.add(dhQueue);
120     _hostMap.put(host, list);
121     }
122 tdb 1.1 }
123 tdb 1.2 _logger.write(toString(), Logger.SYSMSG, "registered DataHandler for hosts: "+hostList);
124 tdb 1.1 }
125     }
126    
127 tdb 1.4 /**
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 tdb 1.1 public void deregister(Queue dhQueue, String hostList) {
139 tdb 1.4 // go through the list of hosts
140 tdb 1.2 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 tdb 1.4 // this should in reality always be true, but best check
149 tdb 1.2 if(_hostMap.containsKey(host)) {
150 tdb 1.4 // get the list and remove the host in question
151 tdb 1.2 LinkedList list = (LinkedList) _hostMap.get(host);
152     list.remove(dhQueue);
153 tdb 1.4 // if the list is now empty, we might as well remove it
154 tdb 1.2 if(list.size()==0) {
155     _hostMap.remove(host);
156     }
157 tdb 1.1 }
158     }
159 tdb 1.2 _logger.write(toString(), Logger.SYSMSG, "deregistered DataHandler for hosts: "+hostList);
160 tdb 1.1 }
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 tdb 1.4
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 tdb 1.1 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 tdb 1.4 /**
214     * A reference to the Queue we're using.
215     */
216 tdb 1.1 private Queue _queue;
217    
218 tdb 1.4 /**
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 tdb 1.1 private HashMap _hostMap;
223 tdb 1.4
224     /**
225     * A list specifically for a Queue's associated with DataHandlers
226     * that want all host information.
227     */
228 tdb 1.2 private LinkedList _allHostsList;
229 tdb 1.1
230     //---STATIC ATTRIBUTES---
231    
232     }