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; |
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 |
|
//---STATIC METHODS--- |
45 |
|
|
46 |
+ |
/** |
47 |
+ |
* The first method that is called. |
48 |
+ |
* Sets up the various panels |
49 |
+ |
* |
50 |
+ |
* @param args the command line arguments |
51 |
+ |
*/ |
52 |
|
public static void main(String[] args) { |
53 |
< |
try { |
54 |
< |
String host = args[0]; |
55 |
< |
int port = 4510; |
56 |
< |
Socket socket = new Socket(host, port); |
57 |
< |
BufferedReader inBound = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
58 |
< |
PrintWriter outBound = new PrintWriter(socket.getOutputStream()); |
59 |
< |
DataReader data = new DataReader(inBound); |
53 |
> |
// get the host from the command line if they gave it |
54 |
> |
String host = null; |
55 |
> |
String configFile = DEFAULT_CONFIG_FILE; |
56 |
> |
if (args.length == 1) { |
57 |
> |
configFile = args[0]; |
58 |
> |
} |
59 |
> |
|
60 |
> |
// start the configuration system |
61 |
> |
Configuration.initialise(configFile); |
62 |
> |
|
63 |
> |
// the data display panel |
64 |
> |
DataPanel data = new DataPanel(); |
65 |
|
|
66 |
< |
SwingClient cli = new SwingClient(data); |
67 |
< |
Thread cliThread = new Thread(cli); |
68 |
< |
cliThread.start(); |
69 |
< |
} catch (Exception e) { |
70 |
< |
System.err.println("ERROR: " + e); |
71 |
< |
} |
66 |
> |
// the control panel |
67 |
> |
ControlPanel control = new ControlPanel(data); |
68 |
> |
|
69 |
> |
// the main frame (passed the two panels) |
70 |
> |
Conient client = new Conient(data, control); |
71 |
> |
Conient.addMessage("Conient {an i-scream Client} © 2001 University of Kent & Project i-scream"); |
72 |
> |
|
73 |
> |
Conient.addMessage("Conient ready."); |
74 |
|
} |
75 |
+ |
|
76 |
|
|
77 |
|
//---CONSTRUCTORS--- |
78 |
|
|
79 |
|
/** |
80 |
< |
* Creates a new Swing Client. |
80 |
> |
* Creates a new Swing Client Frame |
81 |
|
*/ |
82 |
< |
public SwingClient(DataReader data) { |
59 |
< |
_data = data; |
82 |
> |
private Conient(JPanel data, JPanel control) { |
83 |
|
// set up the Frame |
84 |
< |
setTitle("I-Scream Client"); |
85 |
< |
setSize(width, height); |
63 |
< |
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
64 |
< |
Box box = Box.createVerticalBox(); |
84 |
> |
super("Conient {an i-scream Client}"); |
85 |
> |
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); |
86 |
|
|
87 |
< |
// create the i-scream logo at the top |
88 |
< |
JLabel iscream = new JLabel(new ImageIcon("i-scream.gif")); |
89 |
< |
JLabel comment = new JLabel(" I-Scream Client"); |
90 |
< |
comment.setForeground( new Color(0, 0, 102)); |
70 |
< |
Box header = Box.createHorizontalBox(); |
71 |
< |
header.add(comment); |
72 |
< |
header.add(Box.createHorizontalGlue()); |
73 |
< |
header.add(iscream); |
74 |
< |
|
75 |
< |
ImageIcon icon = new ImageIcon("server.gif"); |
76 |
< |
JTabbedPane tabbedPane = new JTabbedPane(); |
77 |
< |
JPanel mainPanel = new JPanel(); |
78 |
< |
mainPanel.setLayout(new GridLayout(21, 1)); |
87 |
> |
// set what happens when the X in the corner is clicked |
88 |
> |
addWindowListener(new WindowAdapter() { |
89 |
> |
public void windowClosing(WindowEvent e) {System.exit(0);} |
90 |
> |
}); |
91 |
|
|
92 |
< |
JPanel panel1 = makeTextPanel("Machine Name : ", _machine_name); |
93 |
< |
JPanel panel2 = makeTextPanel("IP : ", _ip); |
82 |
< |
JPanel panel3 = makeTextPanel("Date : ", _date); |
83 |
< |
JPanel panel4 = makeTextPanel("Sequence Number : ", _seq_no); |
84 |
< |
JPanel panel5 = makeTextPanel("Host Time : ", _sTime); |
85 |
< |
JPanel panel6 = makeTextPanel("Load Average (1 min) : ", _load_load1); |
86 |
< |
JPanel panel7 = makeTextPanel("Load Average (5 min) : ", _load_load5); |
87 |
< |
JPanel panel8 = makeTextPanel("Load Average (15 min) : ", _load_load15); |
88 |
< |
JPanel panel9 = makeTextPanel("Processes Total : ", _processes_total); |
89 |
< |
JPanel panel10 = makeTextPanel("Processes Sleeping : ", _processes_sleeping); |
90 |
< |
JPanel panel11 = makeTextPanel("Processes Zombie : ", _processes_zombie); |
91 |
< |
JPanel panel12 = makeTextPanel("Processes Stopped : ", _processes_stopped); |
92 |
< |
JPanel panel13 = makeTextPanel("CPU % idle : ", _cpu_idle); |
93 |
< |
JPanel panel14 = makeTextPanel("CPU % user : ", _cpu_user); |
94 |
< |
JPanel panel15 = makeTextPanel("CPU % kernel : ", _cpu_kernel); |
95 |
< |
JPanel panel16 = makeTextPanel("CPU % i/o wait : ", _cpu_iowait); |
96 |
< |
JPanel panel17 = makeTextPanel("CPU % swapping : ", _cpu_swap); |
97 |
< |
JPanel panel18 = makeTextPanel("Real Memory : ", _memory_real); |
98 |
< |
JPanel panel19 = makeTextPanel("Real Free : ", _memory_free); |
99 |
< |
JPanel panel20 = makeTextPanel("Swap in use : ", _memory_swapinuse); |
100 |
< |
JPanel panel21 = makeTextPanel("Swap free : ", _memory_swapfree); |
101 |
< |
mainPanel.add(panel1); |
102 |
< |
mainPanel.add(panel2); |
103 |
< |
mainPanel.add(panel3); |
104 |
< |
mainPanel.add(panel4); |
105 |
< |
mainPanel.add(panel5); |
106 |
< |
mainPanel.add(panel6); |
107 |
< |
mainPanel.add(panel7); |
108 |
< |
mainPanel.add(panel8); |
109 |
< |
mainPanel.add(panel9); |
110 |
< |
mainPanel.add(panel10); |
111 |
< |
mainPanel.add(panel11); |
112 |
< |
mainPanel.add(panel12); |
113 |
< |
mainPanel.add(panel13); |
114 |
< |
mainPanel.add(panel14); |
115 |
< |
mainPanel.add(panel15); |
116 |
< |
mainPanel.add(panel16); |
117 |
< |
mainPanel.add(panel17); |
118 |
< |
mainPanel.add(panel18); |
119 |
< |
mainPanel.add(panel19); |
120 |
< |
mainPanel.add(panel20); |
121 |
< |
mainPanel.add(panel21); |
92 |
> |
// add the control pane to the top border |
93 |
> |
getContentPane().add(control, "North"); |
94 |
|
|
95 |
< |
tabbedPane.addTab("Raptor", icon, mainPanel, "Monitor raptor.ukc.ac.uk"); |
96 |
< |
|
97 |
< |
tabbedPane.setSelectedIndex(0); |
95 |
> |
// create a scrollable pane for the data in the centre |
96 |
> |
JScrollPane scrollPane = new JScrollPane(data); |
97 |
> |
getContentPane().add(scrollPane, "Center"); |
98 |
|
|
99 |
< |
// build the frame |
100 |
< |
box.add(header); |
101 |
< |
box.add(tabbedPane); |
102 |
< |
getContentPane().add(box); |
99 |
> |
// setup the status panel |
100 |
> |
JPanel bottom = new JPanel(); |
101 |
> |
bottom.setLayout(new BorderLayout()); |
102 |
> |
_messages.setEditable(false); |
103 |
> |
_messages.setRows(3); |
104 |
> |
JScrollPane messagesPane = new JScrollPane(_messages); |
105 |
> |
messagesPane.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 102)), " Messages ")); |
106 |
> |
bottom.add(messagesPane, "Center"); |
107 |
> |
JPanel statusPanel = new JPanel(); |
108 |
> |
JPanel linkPanel = new JPanel(); |
109 |
> |
linkPanel.setLayout(new GridLayout(1,2)); |
110 |
> |
linkPanel.add(_controlStatus); |
111 |
> |
linkPanel.add(_dataStatus); |
112 |
> |
statusPanel.setLayout(new GridLayout(2,1)); |
113 |
> |
statusPanel.add(linkPanel); |
114 |
> |
statusPanel.add(_queueStatus); |
115 |
> |
bottom.add(statusPanel, "South"); |
116 |
|
|
117 |
+ |
// add the status panel to the bottom border |
118 |
+ |
getContentPane().add(bottom, "South"); |
119 |
+ |
|
120 |
+ |
// a nice icon for the window |
121 |
+ |
setIconImage((new ImageIcon("./uk/ac/ukc/iscream/conient/server.gif")).getImage()); |
122 |
|
// show the window |
123 |
|
show(); |
124 |
|
} |
125 |
|
|
126 |
|
//---PUBLIC METHODS--- |
127 |
|
|
138 |
– |
public void run() { |
139 |
– |
try { |
140 |
– |
_data.start(); |
141 |
– |
while(true) { |
142 |
– |
synchronized (_data) { |
143 |
– |
_data.wait(); |
144 |
– |
} |
145 |
– |
String xml = _data.getXML(); |
146 |
– |
if (xml == null) { |
147 |
– |
// nothing |
148 |
– |
} else { |
149 |
– |
|
150 |
– |
// Get a string without any null characters in it. |
151 |
– |
// -- maybe String.trim() would be better here ? |
152 |
– |
if (xml.indexOf(0) != -1) { |
153 |
– |
xml = xml.substring(0, xml.indexOf(0)); |
154 |
– |
} |
155 |
– |
else { |
156 |
– |
xml = xml.substring(0, xml.length()); |
157 |
– |
} |
158 |
– |
|
159 |
– |
// Use XMLPacketMaker to make an XMLPacket object. |
160 |
– |
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml); |
161 |
– |
XMLPacket packet = xmlPacketMaker.createXMLPacket(); |
162 |
– |
|
163 |
– |
_machine_name.setText(packet.getParam("packet.attributes.machine_name")); |
164 |
– |
_ip.setText(packet.getParam("packet.attributes.ip")); |
165 |
– |
_date.setText(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(new Date(Long.parseLong(packet.getParam("packet.attributes.date"))))); |
166 |
– |
_seq_no.setText(packet.getParam("packet.attributes.seq_no")); |
167 |
– |
|
168 |
– |
_sTime.setText(packet.getParam("packet.sTime")); |
169 |
– |
|
170 |
– |
_load_load1.setText(packet.getParam("packet.load.load1")); |
171 |
– |
_load_load5.setText(packet.getParam("packet.load.load5")); |
172 |
– |
_load_load15.setText(packet.getParam("packet.load.load15")); |
173 |
– |
|
174 |
– |
_processes_total.setText(packet.getParam("packet.processes.total")); |
175 |
– |
_processes_sleeping.setText(packet.getParam("packet.processes.sleeping")); |
176 |
– |
_processes_zombie.setText(packet.getParam("packet.processes.zombie")); |
177 |
– |
_processes_stopped.setText(packet.getParam("packet.processes.stopped")); |
178 |
– |
|
179 |
– |
_cpu_idle.setString(packet.getParam("packet.cpu.idle") + "%"); |
180 |
– |
_cpu_idle.setValue(new Double(packet.getParam("packet.cpu.idle")).intValue()); |
181 |
– |
_cpu_user.setString(packet.getParam("packet.cpu.user") + "%"); |
182 |
– |
_cpu_user.setValue(new Double(packet.getParam("packet.cpu.user")).intValue()); |
183 |
– |
_cpu_kernel.setString(packet.getParam("packet.cpu.kernel") + "%"); |
184 |
– |
_cpu_kernel.setValue(new Double(packet.getParam("packet.cpu.kernel")).intValue()); |
185 |
– |
_cpu_iowait.setString(packet.getParam("packet.cpu.iowait") + "%"); |
186 |
– |
_cpu_iowait.setValue(new Double(packet.getParam("packet.cpu.iowait")).intValue()); |
187 |
– |
_cpu_swap.setString(packet.getParam("packet.cpu.swap") + "%"); |
188 |
– |
_cpu_swap.setValue(new Double(packet.getParam("packet.cpu.swap")).intValue()); |
189 |
– |
|
190 |
– |
_memory_real.setText(packet.getParam("packet.memory.real")); |
191 |
– |
_memory_free.setText(packet.getParam("packet.memory.free")); |
192 |
– |
_memory_swapinuse.setText(packet.getParam("packet.memory.swapinuse")); |
193 |
– |
_memory_swapfree.setText(packet.getParam("packet.memory.swapfree")); |
194 |
– |
} |
195 |
– |
} |
196 |
– |
} catch (Exception e) { |
197 |
– |
System.err.println("ERROR: " + e); |
198 |
– |
} |
199 |
– |
} |
200 |
– |
|
128 |
|
//---PRIVATE METHODS--- |
129 |
+ |
|
130 |
+ |
//---ACCESSOR/MUTATOR METHODS--- |
131 |
+ |
|
132 |
+ |
/** |
133 |
+ |
* Sets the control link status. |
134 |
+ |
* |
135 |
+ |
* @param status the message |
136 |
+ |
*/ |
137 |
+ |
public static void setControlStatus(String status) { |
138 |
+ |
_controlStatus.setText("Control Link: " + status); |
139 |
+ |
_controlStatus.repaint(); |
140 |
+ |
} |
141 |
|
|
142 |
< |
protected JPanel makeTextPanel(String text, Component item) { |
143 |
< |
JPanel panel = new JPanel(false); |
144 |
< |
JLabel label = new JLabel(text); |
145 |
< |
label.setHorizontalAlignment(JLabel.RIGHT); |
146 |
< |
//item.setHorizontalAlignment(JLabel.LEFT); |
147 |
< |
panel.setLayout(new GridLayout(1, 2)); |
148 |
< |
panel.add(label); |
149 |
< |
panel.add(item); |
211 |
< |
return panel; |
142 |
> |
/** |
143 |
> |
* Sets the data link status. |
144 |
> |
* |
145 |
> |
* @param status the message |
146 |
> |
*/ |
147 |
> |
public static void setDataStatus(String status) { |
148 |
> |
_dataStatus.setText("Data Link: " + status); |
149 |
> |
_dataStatus.repaint(); |
150 |
|
} |
151 |
|
|
152 |
+ |
/** |
153 |
+ |
* Updates the queue status. |
154 |
+ |
* |
155 |
+ |
* @param currentQueue |
156 |
+ |
* @param numElements |
157 |
+ |
*/ |
158 |
+ |
public static void setQueueStatus(int currentQueue, int numElements) { |
159 |
+ |
_queueStatus.setText("Data Queue : " + currentQueue + " Packets Recieved : " + numElements); |
160 |
+ |
_queueStatus.repaint(); |
161 |
+ |
} |
162 |
|
|
163 |
< |
//---ACCESSOR/MUTATOR METHODS--- |
163 |
> |
/** |
164 |
> |
* Adds a system message to the messages list |
165 |
> |
* |
166 |
> |
* @param message the new message |
167 |
> |
*/ |
168 |
> |
public static void addMessage(String message) { |
169 |
> |
_messages.insert(message + "\n", 0); |
170 |
> |
} |
171 |
|
|
172 |
|
//---ATTRIBUTES--- |
218 |
– |
|
219 |
– |
JLabel _machine_name = new JLabel(); |
220 |
– |
JLabel _ip = new JLabel(); |
221 |
– |
JLabel _date = new JLabel(); |
222 |
– |
JLabel _seq_no = new JLabel(); |
173 |
|
|
174 |
< |
JLabel _sTime = new JLabel(); |
174 |
> |
//---STATIC ATTRIBUTES--- |
175 |
|
|
176 |
< |
JLabel _load_load1 = new JLabel(); |
177 |
< |
JLabel _load_load5 = new JLabel(); |
178 |
< |
JLabel _load_load15 = new JLabel(); |
179 |
< |
|
230 |
< |
JLabel _processes_total = new JLabel(); |
231 |
< |
JLabel _processes_sleeping = new JLabel(); |
232 |
< |
JLabel _processes_zombie = new JLabel(); |
233 |
< |
JLabel _processes_stopped = new JLabel(); |
234 |
< |
|
235 |
< |
int min = 0; |
236 |
< |
int max = 100; |
237 |
< |
JProgressBar _cpu_idle = new JProgressBar(JProgressBar.HORIZONTAL, min, max); |
176 |
> |
/** |
177 |
> |
* Displays information about the data link |
178 |
> |
*/ |
179 |
> |
static JLabel _controlStatus = new JLabel("Control Link: Disconnected", JLabel.LEFT); |
180 |
|
{ |
181 |
< |
_cpu_idle.setStringPainted(true); |
181 |
> |
_controlStatus.setBorder(new EtchedBorder(EtchedBorder.LOWERED)); |
182 |
|
} |
183 |
< |
JProgressBar _cpu_user = new JProgressBar(JProgressBar.HORIZONTAL, min, max); |
183 |
> |
|
184 |
> |
/** |
185 |
> |
* Displays information about the data link |
186 |
> |
*/ |
187 |
> |
static JLabel _dataStatus = new JLabel("Data Link: Disconnected", JLabel.LEFT); |
188 |
|
{ |
189 |
< |
_cpu_user.setStringPainted(true); |
189 |
> |
_dataStatus.setBorder(new EtchedBorder(EtchedBorder.LOWERED)); |
190 |
|
} |
191 |
< |
JProgressBar _cpu_kernel = new JProgressBar(JProgressBar.HORIZONTAL, min, max); |
191 |
> |
|
192 |
> |
/** |
193 |
> |
* Displays information about the inbound data queue. |
194 |
> |
*/ |
195 |
> |
static JLabel _queueStatus = new JLabel(" ", JLabel.LEFT); |
196 |
|
{ |
197 |
< |
_cpu_kernel.setStringPainted(true); |
197 |
> |
_queueStatus.setBorder(new EtchedBorder(EtchedBorder.LOWERED)); |
198 |
|
} |
199 |
< |
JProgressBar _cpu_iowait = new JProgressBar(JProgressBar.HORIZONTAL, min, max); |
199 |
> |
|
200 |
> |
/** |
201 |
> |
* The place where system messages are written. |
202 |
> |
*/ |
203 |
> |
static JTextArea _messages = new JTextArea(); |
204 |
|
{ |
205 |
< |
_cpu_iowait.setStringPainted(true); |
205 |
> |
_messages.setLineWrap(true); |
206 |
|
} |
207 |
< |
JProgressBar _cpu_swap = new JProgressBar(JProgressBar.HORIZONTAL, min, max); |
254 |
< |
{ |
255 |
< |
_cpu_swap.setStringPainted(true); |
256 |
< |
} |
257 |
< |
|
258 |
< |
JLabel _memory_real = new JLabel(); |
259 |
< |
JLabel _memory_free = new JLabel(); |
260 |
< |
JLabel _memory_swapinuse = new JLabel(); |
261 |
< |
JLabel _memory_swapfree = new JLabel(); |
262 |
< |
DataReader _data; |
263 |
< |
|
264 |
< |
//---STATIC ATTRIBUTES--- |
265 |
< |
|
266 |
< |
} |
207 |
> |
} |