3 |
|
//---IMPORTS--- |
4 |
|
import javax.swing.*; |
5 |
|
import javax.swing.border.*; |
6 |
– |
import java.awt.Color; |
7 |
– |
|
6 |
|
import java.awt.*; |
7 |
|
import java.awt.event.*; |
10 |
– |
import java.net.*; |
11 |
– |
import java.io.*; |
8 |
|
|
9 |
|
/** |
10 |
< |
* NASTY AND BASIC, PLEASE DON'T COMPLAIN |
10 |
> |
* Root for the SwingClient...starts the whole show off... |
11 |
|
* |
12 |
|
* @author $Author$ |
13 |
|
* @version $Id$ |
14 |
|
*/ |
15 |
< |
public class SwingClient extends JFrame implements Runnable { |
15 |
> |
public class SwingClient extends JFrame { |
16 |
|
|
17 |
|
//---FINAL ATTRIBUTES--- |
18 |
|
|
20 |
|
* The current CVS revision of this class |
21 |
|
*/ |
22 |
|
public final String REVISION = "$Revision$"; |
23 |
+ |
|
24 |
+ |
private final int width = 600; |
25 |
+ |
private final int height = 600; |
26 |
|
|
27 |
< |
private final int width = 300; |
28 |
< |
private final int height = 700; |
27 |
> |
// the well known port |
28 |
> |
public static final int PORT = 4510; |
29 |
|
|
30 |
|
//---STATIC METHODS--- |
31 |
|
|
32 |
|
public static void main(String[] args) { |
33 |
< |
try { |
34 |
< |
String host = args[0]; |
35 |
< |
int port = 4510; |
36 |
< |
Socket socket = new Socket(host, port); |
37 |
< |
BufferedReader inBound = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
38 |
< |
PrintWriter outBound = new PrintWriter(socket.getOutputStream()); |
39 |
< |
DataReader data = new DataReader(inBound); |
33 |
> |
// get the host from the command line if they gave it |
34 |
> |
String host = null; |
35 |
> |
if (args.length == 1) { |
36 |
> |
host = args[0]; |
37 |
> |
} |
38 |
> |
|
39 |
> |
// the data display panel |
40 |
> |
DataPanel data = new DataPanel(); |
41 |
|
|
42 |
< |
SwingClient cli = new SwingClient(data); |
43 |
< |
Thread cliThread = new Thread(cli); |
44 |
< |
cliThread.start(); |
45 |
< |
} catch (Exception e) { |
46 |
< |
System.err.println("ERROR: " + e); |
47 |
< |
} |
42 |
> |
// the control panel |
43 |
> |
ControlPanel control = new ControlPanel(data); |
44 |
> |
|
45 |
> |
// the main frame (passed the two panels) |
46 |
> |
SwingClient client = new SwingClient(data, control); |
47 |
> |
SwingClient.addMessage("Client started"); |
48 |
|
} |
49 |
+ |
|
50 |
|
|
51 |
|
//---CONSTRUCTORS--- |
52 |
|
|
53 |
|
/** |
54 |
< |
* Creates a new Swing Client. |
54 |
> |
* Creates a new Swing Client Frame |
55 |
|
*/ |
56 |
< |
public SwingClient(DataReader data) { |
56 |
< |
_data = data; |
56 |
> |
private SwingClient(JPanel data, JPanel control) { |
57 |
|
// set up the Frame |
58 |
< |
setTitle("I-Scream Client"); |
58 |
> |
super("I-Scream Client"); |
59 |
> |
|
60 |
|
setSize(width, height); |
61 |
< |
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
62 |
< |
Box box = Box.createVerticalBox(); |
61 |
> |
addWindowListener(new WindowAdapter() { |
62 |
> |
public void windowClosing(WindowEvent e) {System.exit(0);} |
63 |
> |
}); |
64 |
|
|
65 |
< |
// create the i-scream logo at the top |
64 |
< |
JLabel iscream = new JLabel(new ImageIcon("i-scream.gif")); |
65 |
< |
JLabel comment = new JLabel(" I-Scream Client"); |
66 |
< |
comment.setForeground( new Color(0, 0, 102)); |
67 |
< |
Box header = Box.createHorizontalBox(); |
68 |
< |
header.add(comment); |
69 |
< |
header.add(Box.createHorizontalGlue()); |
70 |
< |
header.add(iscream); |
65 |
> |
getContentPane().add(control, "North"); |
66 |
|
|
67 |
< |
ImageIcon icon = new ImageIcon("server.gif"); |
68 |
< |
JTabbedPane tabbedPane = new JTabbedPane(); |
74 |
< |
JPanel mainPanel = new JPanel(); |
75 |
< |
mainPanel.setLayout(new GridLayout(21, 1)); |
76 |
< |
|
77 |
< |
JPanel panel1 = makeTextPanel("Machine Name : ", _machine_name); |
78 |
< |
JPanel panel2 = makeTextPanel("IP : ", _ip); |
79 |
< |
JPanel panel3 = makeTextPanel("Date : ", _date); |
80 |
< |
JPanel panel4 = makeTextPanel("Sequence Number : ", _seq_no); |
81 |
< |
JPanel panel5 = makeTextPanel("sTime : ", _sTime); |
82 |
< |
JPanel panel6 = makeTextPanel("Load Average (1 min) : ", _load_load1); |
83 |
< |
JPanel panel7 = makeTextPanel("Load Average (5 min) : ", _load_load5); |
84 |
< |
JPanel panel8 = makeTextPanel("Load Average (15 min) : ", _load_load15); |
85 |
< |
JPanel panel9 = makeTextPanel("Processes Total : ", _processes_total); |
86 |
< |
JPanel panel10 = makeTextPanel("Processes Sleeping : ", _processes_sleeping); |
87 |
< |
JPanel panel11 = makeTextPanel("Processes Zombie : ", _processes_zombie); |
88 |
< |
JPanel panel12 = makeTextPanel("Processes Stopped : ", _processes_stopped); |
89 |
< |
JPanel panel13 = makeTextPanel("CPU % idle : ", _cpu_idle); |
90 |
< |
JPanel panel14 = makeTextPanel("CPU % user : ", _cpu_user); |
91 |
< |
JPanel panel15 = makeTextPanel("CPU % kernel : ", _cpu_kernel); |
92 |
< |
JPanel panel16 = makeTextPanel("CPU % i/o wait : ", _cpu_iowait); |
93 |
< |
JPanel panel17 = makeTextPanel("CPU % swapping : ", _cpu_swap); |
94 |
< |
JPanel panel18 = makeTextPanel("Real Memory : ", _memory_real); |
95 |
< |
JPanel panel19 = makeTextPanel("Real Free : ", _memory_free); |
96 |
< |
JPanel panel20 = makeTextPanel("Swap in use : ", _memory_swapinuse); |
97 |
< |
JPanel panel21 = makeTextPanel("Swap free : ", _memory_swapfree); |
98 |
< |
mainPanel.add(panel1); |
99 |
< |
mainPanel.add(panel2); |
100 |
< |
mainPanel.add(panel3); |
101 |
< |
mainPanel.add(panel4); |
102 |
< |
mainPanel.add(panel5); |
103 |
< |
mainPanel.add(panel6); |
104 |
< |
mainPanel.add(panel7); |
105 |
< |
mainPanel.add(panel8); |
106 |
< |
mainPanel.add(panel9); |
107 |
< |
mainPanel.add(panel10); |
108 |
< |
mainPanel.add(panel11); |
109 |
< |
mainPanel.add(panel12); |
110 |
< |
mainPanel.add(panel13); |
111 |
< |
mainPanel.add(panel14); |
112 |
< |
mainPanel.add(panel15); |
113 |
< |
mainPanel.add(panel16); |
114 |
< |
mainPanel.add(panel17); |
115 |
< |
mainPanel.add(panel18); |
116 |
< |
mainPanel.add(panel19); |
117 |
< |
mainPanel.add(panel20); |
118 |
< |
mainPanel.add(panel21); |
67 |
> |
JScrollPane scrollPane = new JScrollPane(data); |
68 |
> |
getContentPane().add(scrollPane, "Center"); |
69 |
|
|
120 |
– |
tabbedPane.addTab("Raptor", icon, mainPanel, "Monitor raptor.ukc.ac.uk"); |
70 |
|
|
71 |
< |
tabbedPane.setSelectedIndex(0); |
71 |
> |
JPanel bottom = new JPanel(); |
72 |
> |
bottom.setLayout(new BorderLayout()); |
73 |
> |
_messages.setEditable(false); |
74 |
> |
_messages.setRows(3); |
75 |
> |
JScrollPane messagesPane = new JScrollPane(_messages); |
76 |
> |
messagesPane.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 102)), " Messages ")); |
77 |
|
|
78 |
< |
// build the frame |
79 |
< |
box.add(header); |
80 |
< |
box.add(tabbedPane); |
81 |
< |
getContentPane().add(box); |
82 |
< |
|
78 |
> |
bottom.add(messagesPane, "Center"); |
79 |
> |
JPanel statusPanel = new JPanel(); |
80 |
> |
JPanel linkPanel = new JPanel(); |
81 |
> |
linkPanel.setLayout(new GridLayout(2,1)); |
82 |
> |
linkPanel.add(_controlStatus); |
83 |
> |
linkPanel.add(_dataStatus); |
84 |
> |
statusPanel.setLayout(new GridLayout(1,3)); |
85 |
> |
statusPanel.add(linkPanel); |
86 |
> |
statusPanel.add(_queueStatus); |
87 |
> |
|
88 |
> |
bottom.add(statusPanel, "South"); |
89 |
> |
getContentPane().add(bottom, "South"); |
90 |
> |
|
91 |
|
// show the window |
92 |
|
show(); |
93 |
|
} |
94 |
|
|
95 |
|
//---PUBLIC METHODS--- |
96 |
|
|
135 |
– |
public void run() { |
136 |
– |
_data.start(); |
137 |
– |
while(true) { |
138 |
– |
String xml = _data.getXML(); |
139 |
– |
if (xml == null) { |
140 |
– |
// nothing |
141 |
– |
} else { |
142 |
– |
|
143 |
– |
// Get a string without any null characters in it. |
144 |
– |
// -- maybe String.trim() would be better here ? |
145 |
– |
if (xml.indexOf(0) != -1) { |
146 |
– |
xml = xml.substring(0, xml.indexOf(0)); |
147 |
– |
} |
148 |
– |
else { |
149 |
– |
xml = xml.substring(0, xml.length()); |
150 |
– |
} |
151 |
– |
|
152 |
– |
// Use XMLPacketMaker to make an XMLPacket object. |
153 |
– |
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml); |
154 |
– |
XMLPacket packet = xmlPacketMaker.createXMLPacket(); |
155 |
– |
|
156 |
– |
_machine_name.setText(packet.getParam("packet.attributes.machine_name")); |
157 |
– |
_ip.setText(packet.getParam("packet.attributes.ip")); |
158 |
– |
_date.setText(packet.getParam("packet.attributes.date")); |
159 |
– |
_seq_no.setText(packet.getParam("packet.attributes.seq_no")); |
160 |
– |
|
161 |
– |
_sTime.setText(packet.getParam("packet.sTime")); |
162 |
– |
|
163 |
– |
_load_load1.setText(packet.getParam("packet.load.load1")); |
164 |
– |
_load_load5.setText(packet.getParam("packet.load.load5")); |
165 |
– |
_load_load15.setText(packet.getParam("packet.load.load15")); |
166 |
– |
|
167 |
– |
_processes_total.setText(packet.getParam("packet.processes.total")); |
168 |
– |
_processes_sleeping.setText(packet.getParam("packet.processes.sleeping")); |
169 |
– |
_processes_zombie.setText(packet.getParam("packet.processes.zombie")); |
170 |
– |
_processes_stopped.setText(packet.getParam("packet.processes.stopped")); |
171 |
– |
|
172 |
– |
_cpu_idle.setText(packet.getParam("packet.cpu.idle")); |
173 |
– |
_cpu_user.setText(packet.getParam("packet.cpu.user")); |
174 |
– |
_cpu_kernel.setText(packet.getParam("packet.cpu.kernel")); |
175 |
– |
_cpu_iowait.setText(packet.getParam("packet.cpu.iowait")); |
176 |
– |
_cpu_swap.setText(packet.getParam("packet.cpu.swap")); |
177 |
– |
|
178 |
– |
_memory_real.setText(packet.getParam("packet.memory.real")); |
179 |
– |
_memory_free.setText(packet.getParam("packet.memory.free")); |
180 |
– |
_memory_swapinuse.setText(packet.getParam("packet.memory.swapinuse")); |
181 |
– |
_memory_swapfree.setText(packet.getParam("packet.memory.swapfree")); |
182 |
– |
} |
183 |
– |
} |
184 |
– |
} |
185 |
– |
|
97 |
|
//---PRIVATE METHODS--- |
98 |
+ |
|
99 |
+ |
//---ACCESSOR/MUTATOR METHODS--- |
100 |
+ |
|
101 |
+ |
public static void setControlStatus(String status) { |
102 |
+ |
_controlStatus.setText("Control Link: " + status); |
103 |
+ |
_controlStatus.repaint(); |
104 |
+ |
} |
105 |
|
|
106 |
< |
protected JPanel makeTextPanel(String text, JLabel item) { |
107 |
< |
JPanel panel = new JPanel(false); |
108 |
< |
JLabel label = new JLabel(text); |
191 |
< |
label.setHorizontalAlignment(JLabel.RIGHT); |
192 |
< |
item.setHorizontalAlignment(JLabel.LEFT); |
193 |
< |
panel.setLayout(new GridLayout(1, 2)); |
194 |
< |
panel.add(label); |
195 |
< |
panel.add(item); |
196 |
< |
return panel; |
106 |
> |
public static void setDataStatus(String status) { |
107 |
> |
_dataStatus.setText("Data Link: " + status); |
108 |
> |
_dataStatus.repaint(); |
109 |
|
} |
110 |
|
|
111 |
+ |
public static void setQueueStatus(int currentQueue, int numElements) { |
112 |
+ |
_queueStatus.setText("Data Queue : " + currentQueue + " Packets Recieved : " + numElements); |
113 |
+ |
_queueStatus.repaint(); |
114 |
+ |
} |
115 |
|
|
116 |
< |
//---ACCESSOR/MUTATOR METHODS--- |
116 |
> |
public static void addMessage(String message) { |
117 |
> |
_messages.insert(message + "\n", 0); |
118 |
> |
} |
119 |
|
|
120 |
|
//---ATTRIBUTES--- |
203 |
– |
|
204 |
– |
JLabel _machine_name = new JLabel(); |
205 |
– |
JLabel _ip = new JLabel(); |
206 |
– |
JLabel _date = new JLabel(); |
207 |
– |
JLabel _seq_no = new JLabel(); |
121 |
|
|
122 |
< |
JLabel _sTime = new JLabel(); |
122 |
> |
ImageIcon _serverIcon = new ImageIcon("server.gif"); |
123 |
> |
JLabel _serverCountLabel; |
124 |
> |
int _serverCount = 0; |
125 |
|
|
126 |
< |
JLabel _load_load1 = new JLabel(); |
212 |
< |
JLabel _load_load5 = new JLabel(); |
213 |
< |
JLabel _load_load15 = new JLabel(); |
126 |
> |
//---STATIC ATTRIBUTES--- |
127 |
|
|
128 |
< |
JLabel _processes_total = new JLabel(); |
129 |
< |
JLabel _processes_sleeping = new JLabel(); |
130 |
< |
JLabel _processes_zombie = new JLabel(); |
131 |
< |
JLabel _processes_stopped = new JLabel(); |
128 |
> |
static JLabel _controlStatus = new JLabel("Control Link: Disconnected", JLabel.LEFT); |
129 |
> |
{ |
130 |
> |
_controlStatus.setBorder(new EtchedBorder(EtchedBorder.LOWERED)); |
131 |
> |
} |
132 |
|
|
133 |
< |
JLabel _cpu_idle = new JLabel(); |
134 |
< |
JLabel _cpu_user = new JLabel(); |
135 |
< |
JLabel _cpu_kernel = new JLabel(); |
136 |
< |
JLabel _cpu_iowait = new JLabel(); |
224 |
< |
JLabel _cpu_swap = new JLabel(); |
133 |
> |
static JLabel _dataStatus = new JLabel("Data Link: Disconnected", JLabel.LEFT); |
134 |
> |
{ |
135 |
> |
_dataStatus.setBorder(new EtchedBorder(EtchedBorder.LOWERED)); |
136 |
> |
} |
137 |
|
|
138 |
< |
JLabel _memory_real = new JLabel(); |
139 |
< |
JLabel _memory_free = new JLabel(); |
140 |
< |
JLabel _memory_swapinuse = new JLabel(); |
141 |
< |
JLabel _memory_swapfree = new JLabel(); |
142 |
< |
DataReader _data; |
143 |
< |
|
232 |
< |
//---STATIC ATTRIBUTES--- |
233 |
< |
|
234 |
< |
} |
138 |
> |
static JLabel _queueStatus = new JLabel(" ", JLabel.LEFT); |
139 |
> |
{ |
140 |
> |
_queueStatus.setBorder(new EtchedBorder(EtchedBorder.LOWERED)); |
141 |
> |
} |
142 |
> |
static JTextArea _messages = new JTextArea(); |
143 |
> |
} |