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