4 |
|
import javax.swing.*; |
5 |
|
import javax.swing.border.*; |
6 |
|
import java.awt.Color; |
7 |
+ |
import uk.ac.ukc.iscream.util.*; |
8 |
|
|
9 |
|
import java.awt.*; |
10 |
|
import java.awt.event.*; |
13 |
|
import java.util.Date; |
14 |
|
import java.text.DateFormat; |
15 |
|
import java.util.Locale; |
16 |
+ |
import java.util.HashMap; |
17 |
+ |
import javax.swing.border.*; |
18 |
|
|
19 |
|
/** |
20 |
|
* NASTY AND BASIC, PLEASE DON'T COMPLAIN |
22 |
|
* @author $Author$ |
23 |
|
* @version $Id$ |
24 |
|
*/ |
25 |
< |
public class SwingClient extends JFrame implements Runnable { |
25 |
> |
public class SwingClient extends JFrame { |
26 |
|
|
27 |
|
//---FINAL ATTRIBUTES--- |
28 |
|
|
30 |
|
* The current CVS revision of this class |
31 |
|
*/ |
32 |
|
public final String REVISION = "$Revision$"; |
33 |
+ |
|
34 |
+ |
private final int width = 600; |
35 |
+ |
private final int height = 600; |
36 |
|
|
37 |
< |
private final int width = 300; |
38 |
< |
private final int height = 700; |
37 |
> |
// the well known port |
38 |
> |
public static final int PORT = 4510; |
39 |
|
|
40 |
|
//---STATIC METHODS--- |
41 |
|
|
42 |
|
public static void main(String[] args) { |
43 |
< |
try { |
44 |
< |
String host = args[0]; |
45 |
< |
int port = 4510; |
46 |
< |
Socket socket = new Socket(host, port); |
47 |
< |
BufferedReader inBound = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
48 |
< |
PrintWriter outBound = new PrintWriter(socket.getOutputStream()); |
49 |
< |
DataReader data = new DataReader(inBound); |
43 |
> |
// get the host from the command line if they gave it |
44 |
> |
String host = null; |
45 |
> |
if (args.length == 1) { |
46 |
> |
host = args[0]; |
47 |
> |
} |
48 |
> |
|
49 |
> |
// the data display panel |
50 |
> |
DataPanel data = new DataPanel(); |
51 |
|
|
52 |
< |
SwingClient cli = new SwingClient(data); |
53 |
< |
Thread cliThread = new Thread(cli); |
54 |
< |
cliThread.start(); |
55 |
< |
} catch (Exception e) { |
56 |
< |
System.err.println("ERROR: " + e); |
57 |
< |
} |
52 |
> |
// the control panel |
53 |
> |
ControlPanel control = new ControlPanel(data); |
54 |
> |
|
55 |
> |
// the main frame (passed the two panels) |
56 |
> |
SwingClient client = new SwingClient(data, control); |
57 |
> |
SwingClient.addMessage("Client started"); |
58 |
> |
SwingClient.setStatus("Disconnected"); |
59 |
|
} |
60 |
+ |
|
61 |
|
|
62 |
|
//---CONSTRUCTORS--- |
63 |
|
|
64 |
|
/** |
65 |
< |
* Creates a new Swing Client. |
65 |
> |
* Creates a new Swing Client Frame |
66 |
|
*/ |
67 |
< |
public SwingClient(DataReader data) { |
59 |
< |
_data = data; |
67 |
> |
private SwingClient(JPanel data, JPanel control) { |
68 |
|
// set up the Frame |
69 |
< |
setTitle("I-Scream Client"); |
69 |
> |
super("I-Scream Client"); |
70 |
> |
|
71 |
|
setSize(width, height); |
72 |
< |
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
73 |
< |
Box box = Box.createVerticalBox(); |
72 |
> |
addWindowListener(new WindowAdapter() { |
73 |
> |
public void windowClosing(WindowEvent e) {System.exit(0);} |
74 |
> |
}); |
75 |
|
|
66 |
– |
// create the i-scream logo at the top |
67 |
– |
JLabel iscream = new JLabel(new ImageIcon("i-scream.gif")); |
68 |
– |
JLabel comment = new JLabel(" I-Scream Client"); |
69 |
– |
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); |
76 |
|
|
77 |
< |
ImageIcon icon = new ImageIcon("server.gif"); |
78 |
< |
JTabbedPane tabbedPane = new JTabbedPane(); |
79 |
< |
JPanel mainPanel = new JPanel(); |
78 |
< |
mainPanel.setLayout(new GridLayout(21, 1)); |
79 |
< |
|
80 |
< |
JPanel panel1 = makeTextPanel("Machine Name : ", _machine_name); |
81 |
< |
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); |
77 |
> |
// build the frame |
78 |
> |
getContentPane().add(control, "North"); |
79 |
> |
getContentPane().add(data, "Center"); |
80 |
|
|
81 |
< |
tabbedPane.addTab("Raptor", icon, mainPanel, "Monitor raptor.ukc.ac.uk"); |
82 |
< |
|
83 |
< |
tabbedPane.setSelectedIndex(0); |
81 |
> |
JPanel bottom = new JPanel(); |
82 |
> |
bottom.setLayout(new BorderLayout()); |
83 |
> |
_messages.setEditable(false); |
84 |
> |
_messages.setRows(3); |
85 |
> |
JScrollPane messagesPane = new JScrollPane(_messages); |
86 |
> |
messagesPane.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 102)), " Messages ")); |
87 |
|
|
88 |
< |
// build the frame |
89 |
< |
box.add(header); |
90 |
< |
box.add(tabbedPane); |
91 |
< |
getContentPane().add(box); |
131 |
< |
|
88 |
> |
bottom.add(messagesPane, "Center"); |
89 |
> |
bottom.add(_status, "South"); |
90 |
> |
getContentPane().add(bottom, "South"); |
91 |
> |
|
92 |
|
// show the window |
93 |
|
show(); |
94 |
|
} |
95 |
|
|
96 |
|
//---PUBLIC METHODS--- |
97 |
|
|
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 |
– |
|
98 |
|
//---PRIVATE METHODS--- |
202 |
– |
|
203 |
– |
protected JPanel makeTextPanel(String text, Component item) { |
204 |
– |
JPanel panel = new JPanel(false); |
205 |
– |
JLabel label = new JLabel(text); |
206 |
– |
label.setHorizontalAlignment(JLabel.RIGHT); |
207 |
– |
//item.setHorizontalAlignment(JLabel.LEFT); |
208 |
– |
panel.setLayout(new GridLayout(1, 2)); |
209 |
– |
panel.add(label); |
210 |
– |
panel.add(item); |
211 |
– |
return panel; |
212 |
– |
} |
99 |
|
|
214 |
– |
|
100 |
|
//---ACCESSOR/MUTATOR METHODS--- |
101 |
|
|
102 |
+ |
public static void setStatus(String status) { |
103 |
+ |
_status.setText(status); |
104 |
+ |
_status.repaint(); |
105 |
+ |
} |
106 |
+ |
|
107 |
+ |
public static void addMessage(String message) { |
108 |
+ |
_messages.insert(message + "\n", 0); |
109 |
+ |
} |
110 |
|
//---ATTRIBUTES--- |
111 |
|
|
112 |
< |
JLabel _machine_name = new JLabel(); |
220 |
< |
JLabel _ip = new JLabel(); |
221 |
< |
JLabel _date = new JLabel(); |
222 |
< |
JLabel _seq_no = new JLabel(); |
223 |
< |
|
224 |
< |
JLabel _sTime = new JLabel(); |
225 |
< |
|
226 |
< |
JLabel _load_load1 = new JLabel(); |
227 |
< |
JLabel _load_load5 = new JLabel(); |
228 |
< |
JLabel _load_load15 = new JLabel(); |
229 |
< |
|
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); |
112 |
> |
static JLabel _status = new JLabel("Initialising", JLabel.LEFT); |
113 |
|
{ |
114 |
< |
_cpu_idle.setStringPainted(true); |
115 |
< |
} |
241 |
< |
JProgressBar _cpu_user = new JProgressBar(JProgressBar.HORIZONTAL, min, max); |
242 |
< |
{ |
243 |
< |
_cpu_user.setStringPainted(true); |
244 |
< |
} |
245 |
< |
JProgressBar _cpu_kernel = new JProgressBar(JProgressBar.HORIZONTAL, min, max); |
246 |
< |
{ |
247 |
< |
_cpu_kernel.setStringPainted(true); |
248 |
< |
} |
249 |
< |
JProgressBar _cpu_iowait = new JProgressBar(JProgressBar.HORIZONTAL, min, max); |
250 |
< |
{ |
251 |
< |
_cpu_iowait.setStringPainted(true); |
252 |
< |
} |
253 |
< |
JProgressBar _cpu_swap = new JProgressBar(JProgressBar.HORIZONTAL, min, max); |
254 |
< |
{ |
255 |
< |
_cpu_swap.setStringPainted(true); |
256 |
< |
} |
114 |
> |
_status.setBorder(new EtchedBorder(EtchedBorder.LOWERED)); |
115 |
> |
} |
116 |
|
|
117 |
< |
JLabel _memory_real = new JLabel(); |
118 |
< |
JLabel _memory_free = new JLabel(); |
119 |
< |
JLabel _memory_swapinuse = new JLabel(); |
120 |
< |
JLabel _memory_swapfree = new JLabel(); |
262 |
< |
DataReader _data; |
117 |
> |
ImageIcon _serverIcon = new ImageIcon("server.gif"); |
118 |
> |
JLabel _serverCountLabel; |
119 |
> |
int _serverCount = 0; |
120 |
> |
static JTextArea _messages = new JTextArea(); |
121 |
|
|
122 |
|
//---STATIC ATTRIBUTES--- |
123 |
|
|
124 |
< |
} |
124 |
> |
} |