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.*; |
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 START: " + 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) { |
60 |
< |
_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 |
|
|
67 |
– |
// create the i-scream logo at the top |
68 |
– |
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); |
76 |
|
|
76 |
– |
|
77 |
– |
|
78 |
– |
//_tabbedPane.setSelectedIndex(0); |
79 |
– |
|
77 |
|
// build the frame |
78 |
< |
box.add(header); |
79 |
< |
box.add(_tabbedPane); |
80 |
< |
box.add(_status); |
81 |
< |
getContentPane().add(box); |
78 |
> |
getContentPane().add(control, "North"); |
79 |
> |
getContentPane().add(data, "Center"); |
80 |
> |
|
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 |
+ |
bottom.add(messagesPane, "Center"); |
89 |
+ |
bottom.add(_status, "South"); |
90 |
+ |
getContentPane().add(bottom, "South"); |
91 |
+ |
|
92 |
|
// show the window |
93 |
|
show(); |
88 |
– |
_status.setText("Running..."); |
94 |
|
} |
95 |
|
|
96 |
|
//---PUBLIC METHODS--- |
97 |
|
|
93 |
– |
public void run() { |
94 |
– |
HashMap hostList = new HashMap(); |
95 |
– |
try { |
96 |
– |
_data.start(); |
97 |
– |
while(true) { |
98 |
– |
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 |
– |
} |
127 |
– |
} |
128 |
– |
} catch (Exception e) { |
129 |
– |
System.err.println("ERROR RUN: " + e); |
130 |
– |
} |
131 |
– |
} |
132 |
– |
|
98 |
|
//---PRIVATE METHODS--- |
134 |
– |
|
135 |
– |
protected JPanel makeTextPanel(String text, Component item) { |
136 |
– |
JPanel panel = new JPanel(false); |
137 |
– |
JLabel label = new JLabel(text); |
138 |
– |
label.setHorizontalAlignment(JLabel.RIGHT); |
139 |
– |
//item.setHorizontalAlignment(JLabel.LEFT); |
140 |
– |
panel.setLayout(new GridLayout(1, 2)); |
141 |
– |
panel.add(label); |
142 |
– |
panel.add(item); |
143 |
– |
return panel; |
144 |
– |
} |
99 |
|
|
146 |
– |
|
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--- |
150 |
– |
HostDisplayPanel host; |
151 |
– |
JTabbedPane _tabbedPane = new JTabbedPane(); |
111 |
|
|
112 |
< |
JLabel _machine_name = new JLabel(); |
154 |
< |
JLabel _ip = new JLabel(); |
155 |
< |
JLabel _date = new JLabel(); |
156 |
< |
JLabel _seq_no = new JLabel(); |
157 |
< |
|
158 |
< |
JLabel _sTime = new JLabel(); |
159 |
< |
|
160 |
< |
JLabel _load_load1 = new JLabel(); |
161 |
< |
JLabel _load_load5 = new JLabel(); |
162 |
< |
JLabel _load_load15 = new JLabel(); |
163 |
< |
|
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); |
112 |
> |
static JLabel _status = new JLabel("Initialising", JLabel.LEFT); |
113 |
|
{ |
114 |
< |
_cpu_idle.setStringPainted(true); |
115 |
< |
} |
175 |
< |
JProgressBar _cpu_user = new JProgressBar(JProgressBar.HORIZONTAL, min, max); |
176 |
< |
{ |
177 |
< |
_cpu_user.setStringPainted(true); |
178 |
< |
} |
179 |
< |
JProgressBar _cpu_kernel = new JProgressBar(JProgressBar.HORIZONTAL, min, max); |
180 |
< |
{ |
181 |
< |
_cpu_kernel.setStringPainted(true); |
182 |
< |
} |
183 |
< |
JProgressBar _cpu_iowait = new JProgressBar(JProgressBar.HORIZONTAL, min, max); |
184 |
< |
{ |
185 |
< |
_cpu_iowait.setStringPainted(true); |
186 |
< |
} |
187 |
< |
JProgressBar _cpu_swap = new JProgressBar(JProgressBar.HORIZONTAL, min, max); |
188 |
< |
{ |
189 |
< |
_cpu_swap.setStringPainted(true); |
190 |
< |
} |
114 |
> |
_status.setBorder(new EtchedBorder(EtchedBorder.LOWERED)); |
115 |
> |
} |
116 |
|
|
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; |
197 |
– |
JLabel _status = new JLabel(); |
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 |
> |
} |