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.20
Committed: Tue May 21 16:47:16 2002 UTC (22 years ago) by tdb
Branch: MAIN
Changes since 1.19: +3 -2 lines
Log Message:
Added URL to GPL headers.

File Contents

# User Rev Content
1 tdb 1.19 /*
2     * i-scream central monitoring system
3 tdb 1.20 * http://www.i-scream.org.uk
4 tdb 1.19 * Copyright (C) 2000-2002 i-scream
5     *
6     * This program is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU General Public License
8     * as published by the Free Software Foundation; either version 2
9     * of the License, or (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19     */
20    
21 tdb 1.1 //---PACKAGE DECLARATION---
22 tdb 1.18 package uk.org.iscream.cms.server.clientinterface;
23 tdb 1.1
24     //---IMPORTS---
25 tdb 1.18 import uk.org.iscream.cms.server.util.*;
26     import uk.org.iscream.cms.server.componentmanager.*;
27     import uk.org.iscream.cms.server.core.*;
28 tdb 1.1 import java.util.*;
29    
30     /**
31     * Receives data from the incoming CORBA servant, places
32     * it in a Queue, and then arranges distribution to the
33 tdb 1.4 * DataHandlers.
34     * Has extra functionality to send data to DataHandlers
35     * on a per-host basis - ie. the Client can request which
36     * hosts it would like to listen for.
37 tdb 1.1 *
38 tdb 1.19 * @author $Author: tdb $
39 tdb 1.20 * @version $Id: PacketSorter.java,v 1.19 2002/05/18 18:16:01 tdb Exp $
40 tdb 1.1 */
41     class PacketSorter extends Thread {
42    
43     //---FINAL ATTRIBUTES---
44    
45     /**
46     * The current CVS revision of this class
47     */
48 tdb 1.20 public final String REVISION = "$Revision: 1.19 $";
49 tdb 1.1
50     //---STATIC METHODS---
51    
52     //---CONSTRUCTORS---
53    
54     /**
55     * Creates a new PacketSorter.
56     */
57 tdb 1.15 public PacketSorter() {
58 tdb 1.14 // set the Thread name
59     setName("clientinterface.PacketSorter");
60    
61 tdb 1.17 ConfigurationProxy cp = ConfigurationProxy.getInstance();
62     String configName = "ClientInterface";
63    
64     // see if this Queue needs a size limit
65     try {
66     int queueSizeLimit = Integer.parseInt(cp.getProperty(configName, "Queue.SizeLimit"));
67     String queueRemoveAlgorithm = cp.getProperty(configName, "Queue.RemoveAlgorithm");
68     int algorithm = StringUtils.getStringPos(queueRemoveAlgorithm, Queue.algorithms);
69     if(algorithm != -1) {
70     _logger.write(toString(), Logger.DEBUG, "Starting Queue with size limit of "+queueSizeLimit+", using remove algorithm "+queueRemoveAlgorithm);
71     // we have valid values, so lets start it.
72     _queue = new Queue(queueSizeLimit, algorithm);
73     }
74     else {
75     _logger.write(toString(), Logger.WARNING, "Bad Queue Algorithm configuration, not known: "+queueRemoveAlgorithm);
76     // just don't activate a limit
77     _queue = new Queue();
78     }
79    
80     } catch (PropertyNotFoundException e) {
81     _logger.write(toString(), Logger.DEBUG, "Optional config not set: "+e);
82     // just don't activate a limit
83     _queue = new Queue();
84     } catch (NumberFormatException e) {
85     _logger.write(toString(), Logger.WARNING, "Bad Queue SizeLimit configuration: "+e);
86     // just don't activate a limit
87     _queue = new Queue();
88     }
89    
90 tdb 1.15 // startup a monitor on this queue
91     try {
92     // try to get the interval, if this fails, we won't start up the monitor
93 tdb 1.17 int queueMonitorInterval = Integer.parseInt(cp.getProperty(configName, "Queue.MonitorInterval"));
94 tdb 1.15 String queueName = _name + " PacketSorterQueue";
95     _queue.startMonitor(queueMonitorInterval*1000, queueName);
96     } catch (PropertyNotFoundException e) {
97     _logger.write(toString(), Logger.WARNING, "failed to find queue monitor config, disabling. " + e);
98     }
99    
100 tdb 1.11 _hostMap = new HashMap();
101     _allHostDataList = new LinkedList();
102     _allHostsList = new LinkedList();
103 tdb 1.1 _logger.write(toString(), Logger.SYSINIT, "created");
104     }
105    
106     //---PUBLIC METHODS---
107    
108 tdb 1.4 /**
109     * Method to start the PacketSorter running. This method will
110     * loop forever processing and sending data.
111     */
112 tdb 1.1 public void run() {
113 tdb 1.13 XMLPacketMaker xmlPacketMaker = new XMLPacketMaker();
114 tdb 1.1 int qID = _queue.getQueue();
115     while(true) {
116 tdb 1.4 // attempt to get some data from the Queue
117 tdb 1.1 String xml = "";
118     try {
119     xml = (String) _queue.get(qID);
120     }
121     catch(InvalidQueueException e) {
122     _logger.write(toString(), Logger.ERROR, "Queue failure: "+e);
123     }
124    
125 tdb 1.10 XMLPacket packet = null;
126    
127     try {
128 tdb 1.13 packet = xmlPacketMaker.createXMLPacket(xml);
129 tdb 1.10 } catch(InvalidXMLException e) {
130     _logger.write(toString(), Logger.ERROR, "Invalid XML: "+e);
131     // skip the rest of this loop iteration
132     continue;
133     }
134 tdb 1.1
135 tdb 1.8 String packetType = packet.getParam("packet.attributes.type");
136     // check if we need to send it regardless
137     if(packetType.equals("data") || packetType.equals("heartbeat")) {
138     String host = packet.getParam("packet.attributes.machine_name");
139    
140     // look in the hostMap to see if anyone wants this data
141 tdb 1.11 synchronized(this) {
142     if(_hostMap.containsKey(host)) {
143     LinkedList list = (LinkedList) _hostMap.get(host);
144     Iterator i = list.iterator();
145     // push the data to the listening Handler's queue
146     while(i.hasNext()) {
147     ((Queue) i.next()).add(xml);
148     }
149 tdb 1.8 }
150     }
151    
152     // any handler in this list wants all packets, so send
153     // it on to them regardless
154 tdb 1.11 synchronized(this) {
155     Iterator i = _allHostDataList.iterator();
156     while(i.hasNext()) {
157     ((Queue) i.next()).add(xml);
158     }
159     }
160 tdb 1.1 }
161 tdb 1.8 else {
162 tdb 1.10 // always send this packet to all hosts, because it's
163     // "extra" data, not host data
164 tdb 1.11 synchronized(this) {
165     Iterator i = _allHostsList.iterator();
166     while(i.hasNext()) {
167     ((Queue) i.next()).add(xml);
168     }
169     }
170 tdb 1.2 }
171 tdb 1.1 }
172     }
173    
174 tdb 1.4 /**
175     * Register a DataHandler in the system. This method
176     * actually takes a reference to a Queue, which should be
177     * a Queue that the DataHandler is making use of.
178     * It also takes a hostList, this being a semi-colon
179     * seperated list of hosts that the Client the DataHandler
180     * is serving has requested. If this list is simply an empty
181     * String, it is assumed the Client wants to listen to all
182     * host information.
183     *
184     * @param dhQueue a Queue being used by the DataHandler that is registering
185     * @param hostList a semi-colon seperated list of hosts
186     */
187 tdb 1.12 public void register(Queue dhQueue, String hostList) {
188 tdb 1.4 // check to see if we want all hosts
189 tdb 1.2 if(hostList.equals("")) {
190 tdb 1.12 synchronized(this) {
191     _allHostDataList.add(dhQueue);
192     }
193 tdb 1.2 _logger.write(toString(), Logger.SYSMSG, "registered DataHandler for all hosts");
194     }
195     else {
196 tdb 1.4 // go through the list of hosts
197 tdb 1.2 StringTokenizer st = new StringTokenizer(hostList, ";");
198     while(st.hasMoreTokens()) {
199     String host = st.nextToken();
200 tdb 1.12 synchronized(this) {
201     // see if we already have a list in the map for this host
202     if(_hostMap.containsKey(host)) {
203     // we do, so add to it
204     List list = (List) _hostMap.get(host);
205     list.add(dhQueue);
206     }
207     else {
208     // we don't, so create a list and put it in the map
209     LinkedList list = new LinkedList();
210     list.add(dhQueue);
211     _hostMap.put(host, list);
212     }
213 tdb 1.2 }
214 tdb 1.1 }
215 tdb 1.2 _logger.write(toString(), Logger.SYSMSG, "registered DataHandler for hosts: "+hostList);
216 tdb 1.1 }
217 tdb 1.10 // always add host to our complete host list
218 tdb 1.12 synchronized(this) {
219     _allHostsList.add(dhQueue);
220     }
221 tdb 1.1 }
222    
223 tdb 1.4 /**
224     * Deregister a DataHandler. The DataHandler should give a reference
225     * to the Queue it's using, and the *same* hostList it gave when it
226     * register. It is imperative that the hostList is the same, otherwise
227     * there will be all sorts of problems with lists getting out of sync.
228     *
229     * NB: Possible future addition would be recording of hostList's.
230     *
231     * @param dhQueue a Queue being used by the DataHandler that is deregistering
232     * @param hostList a semi-colon seperated list of hosts
233     */
234 tdb 1.12 public void deregister(Queue dhQueue, String hostList) {
235 tdb 1.4 // go through the list of hosts
236 tdb 1.2 if(hostList.equals("")) {
237 tdb 1.12 synchronized(this) {
238     _allHostDataList.remove(dhQueue);
239     }
240 tdb 1.2 _logger.write(toString(), Logger.SYSMSG, "deregistered DataHandler for all hosts");
241     }
242     else {
243     StringTokenizer st = new StringTokenizer(hostList, ";");
244     while(st.hasMoreTokens()) {
245     String host = st.nextToken();
246 tdb 1.12 synchronized(this) {
247     // this should in reality always be true, but best check
248     if(_hostMap.containsKey(host)) {
249     // get the list and remove the host in question
250     LinkedList list = (LinkedList) _hostMap.get(host);
251     list.remove(dhQueue);
252     // if the list is now empty, we might as well remove it
253     if(list.size()==0) {
254     _hostMap.remove(host);
255     }
256 tdb 1.2 }
257 tdb 1.1 }
258     }
259 tdb 1.2 _logger.write(toString(), Logger.SYSMSG, "deregistered DataHandler for hosts: "+hostList);
260 tdb 1.1 }
261 tdb 1.10 // always remove host from our complete host list
262 tdb 1.12 synchronized(this) {
263     _allHostsList.remove(dhQueue);
264     }
265 tdb 1.1 }
266    
267     /**
268     * Overrides the {@link java.lang.Object#toString() Object.toString()}
269     * method to provide clean logging (every class should have this).
270     *
271 tdb 1.18 * This uses the uk.org.iscream.cms.server.util.NameFormat class
272 tdb 1.1 * to format the toString()
273     *
274     * @return the name of this class and its CVS revision
275     */
276     public String toString() {
277     return FormatName.getName(
278     _name,
279     getClass().getName(),
280     REVISION);
281     }
282    
283     //---PRIVATE METHODS---
284    
285     //---ACCESSOR/MUTATOR METHODS---
286 tdb 1.4
287     /**
288     * Accessor to return a reference to the Queue object. This
289     * is needed so the ClientInterfaceServant can get add data
290     * easily.
291     *
292     * @return a reference to our Queue object.
293     */
294 tdb 1.1 public Queue getQueue() {
295     return _queue;
296     }
297    
298     //---ATTRIBUTES---
299    
300     /**
301     * This is the friendly identifier of the
302     * component this class is running in.
303     * eg, a Filter may be called "filter1",
304     * If this class does not have an owning
305     * component, a name from the configuration
306     * can be placed here. This name could also
307     * be changed to null for utility classes.
308     */
309     private String _name = ClientInterfaceMain.NAME;
310    
311     /**
312     * This holds a reference to the
313     * system logger that is being used.
314     */
315     private Logger _logger = ReferenceManager.getInstance().getLogger();
316    
317 tdb 1.4 /**
318     * A reference to the Queue we're using.
319     */
320 tdb 1.1 private Queue _queue;
321    
322 tdb 1.4 /**
323     * A HashMap to store lists of Queue's (in the DataHandlers)
324     * in a way that can be easily accessed when data comes in.
325     */
326 tdb 1.11 private HashMap _hostMap;
327 tdb 1.4
328     /**
329     * A list specifically for a Queue's associated with DataHandlers
330     * that want all host information.
331 tdb 1.10 */
332 tdb 1.11 private LinkedList _allHostDataList;
333 tdb 1.10
334     /**
335     * A list of all hosts.
336 tdb 1.4 */
337 tdb 1.11 private LinkedList _allHostsList;
338 tdb 1.1
339     //---STATIC ATTRIBUTES---
340    
341     }