1 |
|
//---PACKAGE DECLARATION--- |
2 |
+ |
package uk.ac.ukc.iscream.conient; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
|
import javax.swing.*; |
6 |
|
import javax.swing.border.*; |
6 |
– |
import java.awt.Color; |
7 |
– |
|
7 |
|
import java.awt.*; |
8 |
|
import java.awt.event.*; |
10 |
– |
import java.net.*; |
11 |
– |
import java.io.*; |
12 |
– |
import java.util.Date; |
13 |
– |
import java.text.DateFormat; |
14 |
– |
import java.util.Locale; |
15 |
– |
import java.util.HashMap; |
9 |
|
|
10 |
|
/** |
11 |
< |
* NASTY AND BASIC, PLEASE DON'T COMPLAIN |
11 |
> |
* This is the main class of the Conient client. |
12 |
|
* |
13 |
+ |
* This sets up the control panel, the data panel |
14 |
+ |
* and its own display. |
15 |
+ |
* |
16 |
|
* @author $Author$ |
17 |
|
* @version $Id$ |
18 |
|
*/ |
19 |
< |
public class SwingClient extends JFrame implements Runnable { |
19 |
> |
public class Conient extends JFrame { |
20 |
|
|
21 |
|
//---FINAL ATTRIBUTES--- |
22 |
|
|
24 |
|
* The current CVS revision of this class |
25 |
|
*/ |
26 |
|
public final String REVISION = "$Revision$"; |
27 |
+ |
|
28 |
+ |
/** |
29 |
+ |
* The initial width of the window |
30 |
+ |
*/ |
31 |
+ |
private final int DEFAULT_WIDTH = 600; |
32 |
|
|
33 |
< |
private final int width = 300; |
34 |
< |
private final int height = 700; |
33 |
> |
/** |
34 |
> |
* The initial height of the window |
35 |
> |
*/ |
36 |
> |
private final int DEFAULT_HEIGHT = 600; |
37 |
|
|
38 |
+ |
/** |
39 |
+ |
* The default configuration fle |
40 |
+ |
* This can be specified on the command line |
41 |
+ |
*/ |
42 |
+ |
public static final String DEFAULT_CONFIG_FILE = "./etc/default.conf"; |
43 |
+ |
|
44 |
+ |
/** |
45 |
+ |
* The time in seconds to display the splash |
46 |
+ |
* screen for |
47 |
+ |
*/ |
48 |
+ |
public static final int DISPLAY_SPLASH_TIME_SECONDS = 3; |
49 |
+ |
|
50 |
|
//---STATIC METHODS--- |
51 |
|
|
52 |
+ |
/** |
53 |
+ |
* The first method that is called. |
54 |
+ |
* Sets up the various panels |
55 |
+ |
* |
56 |
+ |
* @param args the command line arguments |
57 |
+ |
*/ |
58 |
|
public static void main(String[] args) { |
59 |
< |
try { |
60 |
< |
String host = args[0]; |
61 |
< |
int port = 4510; |
62 |
< |
Socket socket = new Socket(host, port); |
63 |
< |
BufferedReader inBound = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
64 |
< |
PrintWriter outBound = new PrintWriter(socket.getOutputStream()); |
65 |
< |
DataReader data = new DataReader(inBound); |
59 |
> |
// get the host from the command line if they gave it |
60 |
> |
String host = null; |
61 |
> |
String configFile = DEFAULT_CONFIG_FILE; |
62 |
> |
if (args.length == 1) { |
63 |
> |
configFile = args[0]; |
64 |
> |
} |
65 |
> |
|
66 |
> |
// start the configuration system |
67 |
> |
Configuration.initialise(configFile); |
68 |
> |
|
69 |
> |
// the data display panel |
70 |
> |
DataPanel data = new DataPanel(); |
71 |
|
|
72 |
< |
SwingClient cli = new SwingClient(data); |
73 |
< |
Thread cliThread = new Thread(cli); |
74 |
< |
cliThread.start(); |
75 |
< |
} catch (Exception e) { |
76 |
< |
System.err.println("ERROR START: " + e); |
77 |
< |
} |
72 |
> |
// the control panel |
73 |
> |
ControlPanel control = new ControlPanel(data); |
74 |
> |
|
75 |
> |
// the main frame (passed the two panels) |
76 |
> |
Conient client = new Conient(data, control); |
77 |
> |
conientFrame = (Frame) client; |
78 |
> |
Conient.addMessage("Conient {an i-scream Client} © 2001 University of Kent & Project i-scream"); |
79 |
> |
|
80 |
> |
Conient.addMessage("Conient ready."); |
81 |
> |
|
82 |
|
} |
83 |
|
|
84 |
+ |
/** |
85 |
+ |
* A static accessor, allowing components of the system |
86 |
+ |
* to get hold of the root frame of the system. |
87 |
+ |
* |
88 |
+ |
* see conientFrame attribute for details. |
89 |
+ |
* |
90 |
+ |
* @return the root Conient frame |
91 |
+ |
*/ |
92 |
+ |
public static Frame getFrame() { |
93 |
+ |
return conientFrame; |
94 |
+ |
} |
95 |
+ |
|
96 |
|
//---CONSTRUCTORS--- |
97 |
|
|
98 |
|
/** |
99 |
< |
* Creates a new Swing Client. |
99 |
> |
* Creates a new Swing Client Frame |
100 |
|
*/ |
101 |
< |
public SwingClient(DataReader data) { |
60 |
< |
_data = data; |
101 |
> |
private Conient(JPanel data, ControlPanel control) { |
102 |
|
// set up the Frame |
103 |
< |
setTitle("I-Scream Client"); |
104 |
< |
setSize(width, height); |
105 |
< |
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
106 |
< |
Box box = Box.createVerticalBox(); |
103 |
> |
super("Conient {an i-scream Client}"); |
104 |
> |
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); |
105 |
> |
setJMenuBar(control.getMenuBar()); |
106 |
> |
|
107 |
> |
// set what happens when the X in the corner is clicked |
108 |
> |
addWindowListener(new WindowAdapter() { |
109 |
> |
public void windowClosing(WindowEvent e) {System.exit(0);} |
110 |
> |
}); |
111 |
|
|
112 |
< |
// create the i-scream logo at the top |
113 |
< |
JLabel iscream = new JLabel(new ImageIcon("i-scream.gif")); |
69 |
< |
JLabel comment = new JLabel(" I-Scream Client"); |
70 |
< |
comment.setForeground( new Color(0, 0, 102)); |
71 |
< |
Box header = Box.createHorizontalBox(); |
72 |
< |
header.add(comment); |
73 |
< |
header.add(Box.createHorizontalGlue()); |
74 |
< |
header.add(iscream); |
112 |
> |
// add the control pane to the top border |
113 |
> |
getContentPane().add(control, "North"); |
114 |
|
|
115 |
< |
|
116 |
< |
|
117 |
< |
//_tabbedPane.setSelectedIndex(0); |
79 |
< |
|
80 |
< |
// build the frame |
81 |
< |
box.add(header); |
82 |
< |
box.add(_tabbedPane); |
83 |
< |
box.add(_status); |
84 |
< |
getContentPane().add(box); |
115 |
> |
// create a scrollable pane for the data in the centre |
116 |
> |
JScrollPane scrollPane = new JScrollPane(data); |
117 |
> |
getContentPane().add(scrollPane, "Center"); |
118 |
|
|
119 |
< |
// show the window |
120 |
< |
show(); |
121 |
< |
_status.setText("Running..."); |
122 |
< |
} |
119 |
> |
// setup the status panel |
120 |
> |
JPanel bottom = new JPanel(); |
121 |
> |
bottom.setLayout(new BorderLayout()); |
122 |
> |
_messages.setEditable(false); |
123 |
> |
_messages.setRows(3); |
124 |
> |
JScrollPane messagesPane = new JScrollPane(_messages); |
125 |
> |
messagesPane.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 102)), " Messages ")); |
126 |
> |
bottom.add(messagesPane, "Center"); |
127 |
> |
JPanel statusPanel = new JPanel(); |
128 |
> |
JPanel linkPanel = new JPanel(); |
129 |
> |
linkPanel.setLayout(new GridLayout(1,2)); |
130 |
> |
linkPanel.add(_controlStatus); |
131 |
> |
linkPanel.add(_dataStatus); |
132 |
> |
statusPanel.setLayout(new GridLayout(2,1)); |
133 |
> |
statusPanel.add(linkPanel); |
134 |
> |
statusPanel.add(_queueStatus); |
135 |
> |
bottom.add(statusPanel, "South"); |
136 |
|
|
137 |
< |
//---PUBLIC METHODS--- |
137 |
> |
// add the status panel to the bottom border |
138 |
> |
getContentPane().add(bottom, "South"); |
139 |
> |
|
140 |
> |
// a nice icon for the window |
141 |
> |
setIconImage((new ImageIcon("./uk/ac/ukc/iscream/conient/server.gif")).getImage()); |
142 |
|
|
143 |
< |
public void run() { |
144 |
< |
HashMap hostList = new HashMap(); |
143 |
> |
// and just because we can, a silly splash screen |
144 |
> |
// of the dudes that did this funky gibble |
145 |
> |
Splash splash = new Splash(); |
146 |
> |
splash.show(); |
147 |
> |
// wait |
148 |
|
try { |
149 |
< |
_data.start(); |
150 |
< |
while(true) { |
151 |
< |
synchronized (_data) { |
99 |
< |
_data.wait(); |
100 |
< |
} |
101 |
< |
String xml = _data.getXML(); |
102 |
< |
if (xml == null) { |
103 |
< |
_status.setText("No XML to update..."); |
104 |
< |
} else { |
105 |
< |
|
106 |
< |
// Get a string without any null characters in it. |
107 |
< |
// -- maybe String.trim() would be better here ? |
108 |
< |
if (xml.indexOf(0) != -1) { |
109 |
< |
xml = xml.substring(0, xml.indexOf(0)); |
110 |
< |
} |
111 |
< |
else { |
112 |
< |
xml = xml.substring(0, xml.length()); |
113 |
< |
} |
114 |
< |
|
115 |
< |
// Use XMLPacketMaker to make an XMLPacket object. |
116 |
< |
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml); |
117 |
< |
XMLPacket packet = xmlPacketMaker.createXMLPacket(); |
118 |
< |
String hostName = packet.getParam("packet.attributes.machine_name"); |
119 |
< |
if(!hostList.containsKey(hostName)) { |
120 |
< |
host = new HostDisplayPanel(); |
121 |
< |
_tabbedPane.addTab(hostName, _serverIcon, host, "Monitor " + hostName); |
122 |
< |
hostList.put(hostName, host); |
123 |
< |
_status.setText("New Host added: " + hostName); |
124 |
< |
} |
125 |
< |
((HostDisplayPanel) hostList.get(hostName)).updateHost(packet); |
126 |
< |
} |
149 |
> |
Thread.sleep(DISPLAY_SPLASH_TIME_SECONDS * 1000); |
150 |
> |
} catch (InterruptedException e) { |
151 |
> |
// don't do anything, we don't care |
152 |
|
} |
153 |
< |
} catch (Exception e) { |
154 |
< |
System.err.println("ERROR RUN: " + e); |
155 |
< |
} |
153 |
> |
//loose the window |
154 |
> |
splash.dispose(); |
155 |
> |
splash = null; |
156 |
> |
// show the main window |
157 |
> |
show(); |
158 |
|
} |
159 |
|
|
160 |
+ |
//---PUBLIC METHODS--- |
161 |
+ |
|
162 |
|
//---PRIVATE METHODS--- |
163 |
+ |
|
164 |
+ |
//---ACCESSOR/MUTATOR METHODS--- |
165 |
+ |
|
166 |
+ |
/** |
167 |
+ |
* Sets the control link status. |
168 |
+ |
* |
169 |
+ |
* @param status the message |
170 |
+ |
*/ |
171 |
+ |
public static void setControlStatus(String status) { |
172 |
+ |
_controlStatus.setText("Control Link: " + status); |
173 |
+ |
_controlStatus.repaint(); |
174 |
+ |
} |
175 |
|
|
176 |
< |
protected JPanel makeTextPanel(String text, Component item) { |
177 |
< |
JPanel panel = new JPanel(false); |
178 |
< |
JLabel label = new JLabel(text); |
179 |
< |
label.setHorizontalAlignment(JLabel.RIGHT); |
180 |
< |
//item.setHorizontalAlignment(JLabel.LEFT); |
181 |
< |
panel.setLayout(new GridLayout(1, 2)); |
182 |
< |
panel.add(label); |
183 |
< |
panel.add(item); |
143 |
< |
return panel; |
176 |
> |
/** |
177 |
> |
* Sets the data link status. |
178 |
> |
* |
179 |
> |
* @param status the message |
180 |
> |
*/ |
181 |
> |
public static void setDataStatus(String status) { |
182 |
> |
_dataStatus.setText("Data Link: " + status); |
183 |
> |
_dataStatus.repaint(); |
184 |
|
} |
185 |
|
|
186 |
+ |
/** |
187 |
+ |
* Updates the queue status. |
188 |
+ |
* |
189 |
+ |
* @param currentQueue |
190 |
+ |
* @param numElements |
191 |
+ |
*/ |
192 |
+ |
public static void setQueueStatus(int currentQueue, int numElements) { |
193 |
+ |
_queueStatus.setText("Data Queue : " + currentQueue + " Packets Recieved : " + numElements); |
194 |
+ |
_queueStatus.repaint(); |
195 |
+ |
} |
196 |
|
|
197 |
< |
//---ACCESSOR/MUTATOR METHODS--- |
197 |
> |
/** |
198 |
> |
* Adds a system message to the messages list |
199 |
> |
* |
200 |
> |
* @param message the new message |
201 |
> |
*/ |
202 |
> |
public static void addMessage(String message) { |
203 |
> |
_messages.insert(message + "\n", 0); |
204 |
> |
} |
205 |
|
|
206 |
|
//---ATTRIBUTES--- |
150 |
– |
HostDisplayPanel host; |
151 |
– |
JTabbedPane _tabbedPane = new JTabbedPane(); |
152 |
– |
|
153 |
– |
JLabel _machine_name = new JLabel(); |
154 |
– |
JLabel _ip = new JLabel(); |
155 |
– |
JLabel _date = new JLabel(); |
156 |
– |
JLabel _seq_no = new JLabel(); |
207 |
|
|
208 |
< |
JLabel _sTime = new JLabel(); |
208 |
> |
//---STATIC ATTRIBUTES--- |
209 |
|
|
210 |
< |
JLabel _load_load1 = new JLabel(); |
211 |
< |
JLabel _load_load5 = new JLabel(); |
212 |
< |
JLabel _load_load15 = new JLabel(); |
213 |
< |
|
164 |
< |
JLabel _processes_total = new JLabel(); |
165 |
< |
JLabel _processes_sleeping = new JLabel(); |
166 |
< |
JLabel _processes_zombie = new JLabel(); |
167 |
< |
JLabel _processes_stopped = new JLabel(); |
168 |
< |
|
169 |
< |
int min = 0; |
170 |
< |
int max = 100; |
171 |
< |
JProgressBar _cpu_idle = new JProgressBar(JProgressBar.HORIZONTAL, min, max); |
210 |
> |
/** |
211 |
> |
* Displays information about the data link |
212 |
> |
*/ |
213 |
> |
static JLabel _controlStatus = new JLabel("Control Link: Disconnected", JLabel.LEFT); |
214 |
|
{ |
215 |
< |
_cpu_idle.setStringPainted(true); |
215 |
> |
_controlStatus.setBorder(new EtchedBorder(EtchedBorder.LOWERED)); |
216 |
|
} |
217 |
< |
JProgressBar _cpu_user = new JProgressBar(JProgressBar.HORIZONTAL, min, max); |
217 |
> |
|
218 |
> |
/** |
219 |
> |
* Displays information about the data link |
220 |
> |
*/ |
221 |
> |
static JLabel _dataStatus = new JLabel("Data Link: Disconnected", JLabel.LEFT); |
222 |
|
{ |
223 |
< |
_cpu_user.setStringPainted(true); |
223 |
> |
_dataStatus.setBorder(new EtchedBorder(EtchedBorder.LOWERED)); |
224 |
|
} |
225 |
< |
JProgressBar _cpu_kernel = new JProgressBar(JProgressBar.HORIZONTAL, min, max); |
225 |
> |
|
226 |
> |
/** |
227 |
> |
* Displays information about the inbound data queue. |
228 |
> |
*/ |
229 |
> |
static JLabel _queueStatus = new JLabel(" ", JLabel.LEFT); |
230 |
|
{ |
231 |
< |
_cpu_kernel.setStringPainted(true); |
231 |
> |
_queueStatus.setBorder(new EtchedBorder(EtchedBorder.LOWERED)); |
232 |
|
} |
233 |
< |
JProgressBar _cpu_iowait = new JProgressBar(JProgressBar.HORIZONTAL, min, max); |
233 |
> |
|
234 |
> |
/** |
235 |
> |
* The place where system messages are written. |
236 |
> |
*/ |
237 |
> |
static JTextArea _messages = new JTextArea(); |
238 |
|
{ |
239 |
< |
_cpu_iowait.setStringPainted(true); |
239 |
> |
_messages.setLineWrap(true); |
240 |
|
} |
187 |
– |
JProgressBar _cpu_swap = new JProgressBar(JProgressBar.HORIZONTAL, min, max); |
188 |
– |
{ |
189 |
– |
_cpu_swap.setStringPainted(true); |
190 |
– |
} |
241 |
|
|
242 |
< |
JLabel _memory_real = new JLabel(); |
243 |
< |
JLabel _memory_free = new JLabel(); |
244 |
< |
JLabel _memory_swapinuse = new JLabel(); |
245 |
< |
JLabel _memory_swapfree = new JLabel(); |
246 |
< |
DataReader _data; |
247 |
< |
JLabel _status = new JLabel(); |
198 |
< |
ImageIcon _serverIcon = new ImageIcon("server.gif"); |
242 |
> |
/** |
243 |
> |
* Holds a reference to the root frame for Conient |
244 |
> |
* This is only used by dialogs (specifically the configurationn |
245 |
> |
* dialog) so that it can be modal, please use the accessor. |
246 |
> |
*/ |
247 |
> |
private static Frame conientFrame; |
248 |
|
|
249 |
< |
//---STATIC ATTRIBUTES--- |
250 |
< |
|
251 |
< |
} |
249 |
> |
//---INNER CLASSES---- |
250 |
> |
|
251 |
> |
/** |
252 |
> |
* An inner class to display a splash screen |
253 |
> |
*/ |
254 |
> |
private class Splash extends JWindow { |
255 |
> |
|
256 |
> |
/** |
257 |
> |
* Constructs a new splash screen |
258 |
> |
*/ |
259 |
> |
public Splash() { |
260 |
> |
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); |
261 |
> |
JPanel splash = new JPanel(); |
262 |
> |
splash.setBackground(Color.black); |
263 |
> |
JLabel image = new JLabel((new ImageIcon("./uk/ac/ukc/iscream/conient/i-scream-splash.gif"))); |
264 |
> |
splash.add(image); |
265 |
> |
setContentPane(splash); |
266 |
> |
Dimension screen = getToolkit().getScreenSize(); |
267 |
> |
pack(); |
268 |
> |
setLocation((screen.width - getSize().width) / 2, |
269 |
> |
(screen.height - getSize().height) / 2); |
270 |
> |
|
271 |
> |
|
272 |
> |
} |
273 |
> |
} |
274 |
> |
} |