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

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