ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/conient/uk/org/iscream/cms/conient/DataPanel.java
Revision: 1.17
Committed: Thu Mar 1 02:00:02 2001 UTC (23 years, 3 months ago) by ajm
Branch: MAIN
Changes since 1.16: +8 -5 lines
Log Message:
Now all configuration support is in place.  Full 1.1 support and configuration for it.
Still a few configuration bugs to iron out, but all the major construction and implementation is done.
Added debug messages to ConnectionHandler.
Fixed bug in the datapanel.

File Contents

# Content
1 //---PACKAGE DECLARATION---
2 package uk.ac.ukc.iscream.conient;
3
4 //---IMPORTS---
5 import uk.ac.ukc.iscream.util.*;
6 import java.util.HashMap;
7 import javax.swing.ImageIcon;
8 import javax.swing.JTabbedPane;
9 import javax.swing.JPanel;
10 import java.util.StringTokenizer;
11 import javax.swing.SwingUtilities;
12
13 /**
14 * This thread reads data from the DataReader
15 * it then asks the appropriate HostDisplayPanel
16 * to update its data.
17 *
18 * @author $Author: ajm4 $
19 * @version $Id: DataPanel.java,v 1.16 2001/02/28 23:57:17 ajm4 Exp $
20 */
21 public class DataPanel extends JPanel implements Runnable {
22
23 //---FINAL ATTRIBUTES---
24
25 /**
26 * The current CVS revision of this class
27 */
28 public final String REVISION = "$Revision: 1.16 $";
29
30 //---STATIC METHODS---
31
32 //---CONSTRUCTORS---
33
34 /**
35 * Constructs the data panel
36 */
37 public DataPanel() {
38 add(_tabbedPane);
39 }
40
41 //---PUBLIC METHODS---
42
43 /**
44 * Starts the DataPanel running
45 */
46 public void run() {
47 // setup the host list we will be using
48 refreshHostList();
49 if(Configuration.getInstance().getProperty("displayQueueInformation").equals("1")) {
50 if (_qFrame == null) {
51 _qFrame = new QueueFrame();
52 }
53 if (!_qFrame.isVisible()) {
54 _qFrame.setVisible(true);
55 }
56 }
57
58 try {
59 while(_running) {
60
61 String xml = (String) _dataQueue.get(_myQueue);
62 Conient.setQueueStatus(_dataQueue.queueSize(_myQueue), _dataQueue.elementCount());
63 if (xml == null) {
64 // shouldn't really happen...but not sure
65 //_status.insert("No XML to update...",0);
66 } else {
67
68 // Get a string without any null characters in it.
69 // -- maybe String.trim() would be better ?
70 if (xml.indexOf(0) != -1) {
71 xml = xml.substring(0, xml.indexOf(0));
72 }
73 else {
74 xml = xml.substring(0, xml.length());
75 }
76
77 // Use XMLPacketMaker to make an XMLPacket object.
78 XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml);
79 XMLPacket packet = xmlPacketMaker.createXMLPacket();
80
81 // if we want to debug the packets
82 if(Configuration.getInstance().getProperty("packetDump").equals("1")) {
83 System.out.println("[PACKET DUMP]\n" + packet.printAll());
84 }
85
86 String packetType = packet.getParam("packet.attributes.type");
87 if (packetType.equals("heartbeat") || packetType.equals("data")) {
88 String hostName = packet.getParam("packet.attributes.machine_name");
89 // if we're not using a fixed list
90 if(!_usingConfiguredList) {
91 // if we don't know about this host
92 // add it.
93 if(!_hostList.containsKey(hostName)) {
94 addHostPanel(hostName);
95 // if we want to remember new hosts (ie, in "discovery" mode)
96 if(_config.getProperty("hostDiscoveryMode").equals("1")) {
97 addToKnownHosts(hostName);
98 }
99 }
100 }
101 // one final check that we know about the host
102 // if we don't by now, then the server must have sent something odd
103 if(_hostList.containsKey(hostName)) {
104 if (!((HostDisplayPanel) _hostList.get(hostName)).updateHost(packet)) {
105 //throw new Exception(hostName + " sent an invalid data packet stopping data update!");
106 Conient.addMessage("WARNING{data panel}: " + hostName + " sent an invalid data or heartbeat packet");
107 }
108 } else {
109 Conient.addMessage("WARNING{data panel}: server sent data for an unexpected host - " + hostName);
110 }
111 } else if (packetType.equals("queueStat")) {
112 // check to config to see if we want queueStat packets to be processed or not
113 if(Configuration.getInstance().getProperty("displayQueueInformation").equals("1") && _qFrame != null) {
114 if (_qFrame.isVisible()) _qFrame.update(packet);
115 }
116 } else {
117 Conient.addMessage("WARNING{data panel}: and unknown packet type was received - " + packetType);
118 }
119 }
120 }
121 } catch (Exception e) {
122 Conient.addMessage("ERROR{data panel}: +" + e);
123 e.printStackTrace();
124 }
125 }
126
127 /**
128 * This method allows other classes
129 * to shutdown this data panel.
130 */
131 public void shutdown() {
132 _running = false;
133 }
134
135 //---PRIVATE METHODS---
136
137 /**
138 * Build the host list according to the configuration.
139 * If we're set to use one, it populates the display.
140 * If we're using one and its empty, or if we're not using
141 * one, we set that we're not so that we use all host we detect.
142 *
143 * See the run method for information on host discovery if we're
144 * getting all the hosts.
145 */
146 private void refreshHostList() {
147 // reset the list
148 _hostList = new HashMap();
149 // check we're using a set list
150 if (_config.getProperty("useHostList").equals("1")) {
151 // if we are get it and set up the display
152 String hostList = _config.getProperty("hostList");
153 if (!hostList.equals("")) {
154 StringTokenizer st = new StringTokenizer(hostList, ";");
155 while(st.hasMoreTokens()) {
156 String host = st.nextToken();
157 addHostPanel(host);
158 }
159 _usingConfiguredList = true;
160 // if we've got no list setup, we set that we're taking all
161 // hosts that might come in.
162 } else {
163 _usingConfiguredList = false;
164 }
165 // we're not using a list
166 } else {
167 _usingConfiguredList = false;
168 }
169 }
170
171 /**
172 * Adds a new Host to the hostList and adds its
173 * display to the tabbed pane.
174 *
175 * @param host the host to add
176 */
177 private void addHostPanel(String host) {
178 HostDisplayPanel hostPanel = new HostDisplayPanel(host);
179 SwingSafeAddTab task = new SwingSafeAddTab(_tabbedPane, hostPanel, host, "Monitor " + host, _serverIcon);
180 SwingUtilities.invokeLater(task);
181 _hostList.put(host, hostPanel);
182 Conient.addMessage("New Host added: " + host);
183 }
184
185 /**
186 * If we are in "discovery" mode, we want to keep a list
187 * of all new hosts we find, so we can add it to our list.
188 * This method simply adds the host to the end of the
189 * "knownHostList" property.
190 *
191 * @param host the host to add
192 */
193 private void addToKnownHosts(String host) {
194 String knownHosts = _config.getProperty("knownHostsList");
195 if (knownHosts.indexOf(host) == -1) {
196 _config.setProperty("knownHostsList", knownHosts + host + ";");
197 }
198 }
199
200 //---ACCESSOR/MUTATOR METHODS---
201
202 //---ATTRIBUTES---
203
204 /**
205 * The state of this thread.
206 */
207 boolean _running = true;
208
209 /**
210 * Assigns the queue that this panel
211 * will use to obtain data
212 *
213 * @param queue the queue
214 */
215 public void setQueue(Queue queue) {
216 _dataQueue = queue;
217 _myQueue = _dataQueue.getQueue();
218 }
219
220 /**
221 * Removes all the tabs on display
222 * Used to tidy up when a new data
223 * channel is opened.
224 */
225 public void cleanUpTabs() {
226 _tabbedPane.removeAll();
227 }
228
229 /**
230 * The tabbed pane is where HostDisplayPanel's
231 * are placed
232 */
233 JTabbedPane _tabbedPane = new JTabbedPane();
234
235 /**
236 * The queue new data will be read from
237 */
238 Queue _dataQueue;
239
240 /**
241 * Our queue number
242 */
243 int _myQueue;
244
245 /**
246 * An icon to represent a host
247 */
248 ImageIcon _serverIcon = new ImageIcon("./uk/ac/ukc/iscream/conient/server.gif");
249
250 /**
251 * A frame to display Queue information
252 * may not always be used - loaded according to config
253 */
254 QueueFrame _qFrame = null;
255
256 /**
257 * A reference to the configuraton object
258 */
259 Configuration _config = Configuration.getInstance();
260
261 /**
262 * Contains a list of hosts that the data panel will use
263 * or build during its operation.
264 */
265 HashMap _hostList = null;
266
267 /**
268 * If we should be using a configured list or just accepting
269 * all the hosts we get
270 */
271 boolean _usingConfiguredList = false;
272 }