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