1 |
ajm |
1.5 |
//---PACKAGE DECLARATION--- |
2 |
|
|
package uk.ac.ukc.iscream.conient; |
3 |
|
|
|
4 |
|
|
//---IMPORTS--- |
5 |
ajm |
1.1 |
import uk.ac.ukc.iscream.util.*; |
6 |
ajm |
1.5 |
import java.util.HashMap; |
7 |
|
|
import javax.swing.ImageIcon; |
8 |
|
|
import javax.swing.JTabbedPane; |
9 |
|
|
import javax.swing.JPanel; |
10 |
ajm |
1.15 |
import java.util.StringTokenizer; |
11 |
|
|
import javax.swing.SwingUtilities; |
12 |
ajm |
1.5 |
|
13 |
|
|
/** |
14 |
|
|
* This thread reads data from the DataReader |
15 |
|
|
* it then asks the appropriate HostDisplayPanel |
16 |
|
|
* to update its data. |
17 |
|
|
* |
18 |
ajm |
1.6 |
* @author $Author: ajm4 $ |
19 |
ajm |
1.17 |
* @version $Id: DataPanel.java,v 1.16 2001/02/28 23:57:17 ajm4 Exp $ |
20 |
ajm |
1.5 |
*/ |
21 |
|
|
public class DataPanel extends JPanel implements Runnable { |
22 |
ajm |
1.1 |
|
23 |
ajm |
1.5 |
//---FINAL ATTRIBUTES--- |
24 |
ajm |
1.2 |
|
25 |
ajm |
1.5 |
/** |
26 |
|
|
* The current CVS revision of this class |
27 |
|
|
*/ |
28 |
ajm |
1.17 |
public final String REVISION = "$Revision: 1.16 $"; |
29 |
ajm |
1.5 |
|
30 |
|
|
//---STATIC METHODS--- |
31 |
|
|
|
32 |
|
|
//---CONSTRUCTORS--- |
33 |
|
|
|
34 |
ajm |
1.15 |
/** |
35 |
|
|
* Constructs the data panel |
36 |
|
|
*/ |
37 |
|
|
public DataPanel() { |
38 |
|
|
add(_tabbedPane); |
39 |
|
|
} |
40 |
|
|
|
41 |
ajm |
1.5 |
//---PUBLIC METHODS--- |
42 |
|
|
|
43 |
|
|
/** |
44 |
|
|
* Starts the DataPanel running |
45 |
|
|
*/ |
46 |
ajm |
1.1 |
public void run() { |
47 |
ajm |
1.15 |
// setup the host list we will be using |
48 |
|
|
refreshHostList(); |
49 |
ajm |
1.16 |
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 |
ajm |
1.5 |
|
58 |
ajm |
1.1 |
try { |
59 |
ajm |
1.5 |
while(_running) { |
60 |
ajm |
1.1 |
|
61 |
|
|
String xml = (String) _dataQueue.get(_myQueue); |
62 |
ajm |
1.6 |
Conient.setQueueStatus(_dataQueue.queueSize(_myQueue), _dataQueue.elementCount()); |
63 |
ajm |
1.1 |
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 |
ajm |
1.13 |
|
81 |
|
|
// if we want to debug the packets |
82 |
ajm |
1.15 |
if(Configuration.getInstance().getProperty("packetDump").equals("1")) { |
83 |
|
|
System.out.println("[PACKET DUMP]\n" + packet.printAll()); |
84 |
ajm |
1.13 |
} |
85 |
|
|
|
86 |
ajm |
1.11 |
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 |
ajm |
1.15 |
// 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 |
ajm |
1.11 |
} |
101 |
ajm |
1.15 |
// 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 |
ajm |
1.11 |
} |
111 |
ajm |
1.12 |
} else if (packetType.equals("queueStat")) { |
112 |
|
|
// check to config to see if we want queueStat packets to be processed or not |
113 |
ajm |
1.17 |
if(Configuration.getInstance().getProperty("displayQueueInformation").equals("1") && _qFrame != null) { |
114 |
|
|
if (_qFrame.isVisible()) _qFrame.update(packet); |
115 |
ajm |
1.12 |
} |
116 |
ajm |
1.11 |
} else { |
117 |
|
|
Conient.addMessage("WARNING{data panel}: and unknown packet type was received - " + packetType); |
118 |
ajm |
1.5 |
} |
119 |
ajm |
1.1 |
} |
120 |
|
|
} |
121 |
|
|
} catch (Exception e) { |
122 |
ajm |
1.8 |
Conient.addMessage("ERROR{data panel}: +" + e); |
123 |
ajm |
1.10 |
e.printStackTrace(); |
124 |
ajm |
1.1 |
} |
125 |
|
|
} |
126 |
|
|
|
127 |
ajm |
1.5 |
/** |
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 |
ajm |
1.15 |
/** |
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 |
ajm |
1.17 |
String knownHosts = _config.getProperty("knownHostsList"); |
195 |
|
|
if (knownHosts.indexOf(host) == -1) { |
196 |
|
|
_config.setProperty("knownHostsList", knownHosts + host + ";"); |
197 |
|
|
} |
198 |
ajm |
1.15 |
} |
199 |
|
|
|
200 |
ajm |
1.5 |
//---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 |
ajm |
1.1 |
public void setQueue(Queue queue) { |
216 |
|
|
_dataQueue = queue; |
217 |
|
|
_myQueue = _dataQueue.getQueue(); |
218 |
tdb |
1.4 |
} |
219 |
|
|
|
220 |
ajm |
1.5 |
/** |
221 |
|
|
* Removes all the tabs on display |
222 |
|
|
* Used to tidy up when a new data |
223 |
|
|
* channel is opened. |
224 |
|
|
*/ |
225 |
tdb |
1.4 |
public void cleanUpTabs() { |
226 |
|
|
_tabbedPane.removeAll(); |
227 |
ajm |
1.1 |
} |
228 |
|
|
|
229 |
ajm |
1.5 |
/** |
230 |
|
|
* The tabbed pane is where HostDisplayPanel's |
231 |
|
|
* are placed |
232 |
|
|
*/ |
233 |
ajm |
1.1 |
JTabbedPane _tabbedPane = new JTabbedPane(); |
234 |
ajm |
1.5 |
|
235 |
|
|
/** |
236 |
|
|
* The queue new data will be read from |
237 |
|
|
*/ |
238 |
ajm |
1.1 |
Queue _dataQueue; |
239 |
ajm |
1.5 |
|
240 |
|
|
/** |
241 |
|
|
* Our queue number |
242 |
|
|
*/ |
243 |
ajm |
1.1 |
int _myQueue; |
244 |
ajm |
1.5 |
|
245 |
|
|
/** |
246 |
|
|
* An icon to represent a host |
247 |
|
|
*/ |
248 |
ajm |
1.7 |
ImageIcon _serverIcon = new ImageIcon("./uk/ac/ukc/iscream/conient/server.gif"); |
249 |
ajm |
1.12 |
|
250 |
|
|
/** |
251 |
|
|
* A frame to display Queue information |
252 |
|
|
* may not always be used - loaded according to config |
253 |
|
|
*/ |
254 |
ajm |
1.15 |
QueueFrame _qFrame = null; |
255 |
ajm |
1.12 |
|
256 |
ajm |
1.15 |
/** |
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 |
ajm |
1.7 |
} |