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 |
– |
import uk.ac.ukc.iscream.util.*; |
8 |
– |
|
7 |
|
import java.awt.*; |
8 |
|
import java.awt.event.*; |
11 |
– |
import java.net.*; |
12 |
– |
import java.io.*; |
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.*; |
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 = 400; |
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 |
+ |
/** |
45 |
+ |
* The time in seconds to display the splash |
46 |
+ |
* screen for |
47 |
+ |
*/ |
48 |
+ |
public static final int DISPLAY_SPLASH_TIME_SECONDS = 3; |
49 |
+ |
|
50 |
|
//---STATIC METHODS--- |
51 |
|
|
52 |
+ |
/** |
53 |
+ |
* The first method that is called. |
54 |
+ |
* Sets up the various panels |
55 |
+ |
* |
56 |
+ |
* @param args the command line arguments |
57 |
+ |
*/ |
58 |
|
public static void main(String[] args) { |
59 |
< |
try { |
60 |
< |
String host = null; |
61 |
< |
if (args.length != 1) { |
62 |
< |
Object response = null; |
63 |
< |
while(!(response instanceof String)) { |
64 |
< |
response = JOptionPane.showInputDialog(null, "Please enter the name of a server running the I-Scream client interface:", "I-Scream Server", JOptionPane.INFORMATION_MESSAGE); |
46 |
< |
} |
47 |
< |
host = (String) response; |
48 |
< |
} else { |
49 |
< |
host = args[0]; |
50 |
< |
} |
59 |
> |
// get the host from the command line if they gave it |
60 |
> |
String host = null; |
61 |
> |
String configFile = DEFAULT_CONFIG_FILE; |
62 |
> |
if (args.length == 1) { |
63 |
> |
configFile = args[0]; |
64 |
> |
} |
65 |
|
|
66 |
< |
int port = 4510; |
67 |
< |
Socket socket = new Socket(host, port); |
54 |
< |
BufferedReader inBound = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
55 |
< |
PrintWriter outBound = new PrintWriter(socket.getOutputStream()); |
66 |
> |
// start the configuration system |
67 |
> |
Configuration.initialise(configFile); |
68 |
|
|
69 |
< |
Queue dataQueue = new Queue(); |
70 |
< |
DataReader data = new DataReader(inBound, dataQueue); |
69 |
> |
// the data display panel |
70 |
> |
DataPanel data = new DataPanel(); |
71 |
|
|
72 |
< |
SwingClient client = new SwingClient(dataQueue); |
73 |
< |
Thread clientThread = new Thread(client); |
74 |
< |
|
75 |
< |
clientThread.start(); |
76 |
< |
data.start(); |
72 |
> |
// the control panel |
73 |
> |
ControlPanel control = new ControlPanel(data); |
74 |
> |
|
75 |
> |
// the main frame (passed the two panels) |
76 |
> |
Conient client = new Conient(data, control); |
77 |
> |
Conient.addMessage("Conient {an i-scream Client} © 2001 University of Kent & Project i-scream"); |
78 |
|
|
79 |
< |
} catch (Exception e) { |
80 |
< |
System.err.println("ERROR START: " + e); |
68 |
< |
System.exit(1); |
69 |
< |
} |
79 |
> |
Conient.addMessage("Conient ready."); |
80 |
> |
|
81 |
|
} |
82 |
+ |
|
83 |
|
|
84 |
|
//---CONSTRUCTORS--- |
85 |
|
|
86 |
|
/** |
87 |
< |
* Creates a new Swing Client. |
87 |
> |
* Creates a new Swing Client Frame |
88 |
|
*/ |
89 |
< |
public SwingClient(Queue dataQueue) { |
78 |
< |
|
79 |
< |
_dataQueue = dataQueue; |
80 |
< |
_myQueue = _dataQueue.getQueue(); |
89 |
> |
private Conient(JPanel data, JPanel control) { |
90 |
|
// set up the Frame |
91 |
< |
setTitle("I-Scream Client"); |
92 |
< |
//setSize(width, height); |
84 |
< |
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
85 |
< |
Box box = Box.createVerticalBox(); |
91 |
> |
super("Conient {an i-scream Client}"); |
92 |
> |
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); |
93 |
|
|
87 |
– |
// create the i-scream logo at the top |
88 |
– |
JLabel iscream = new JLabel(new ImageIcon("i-scream.gif")); |
89 |
– |
_serverCountLabel = new JLabel(" Monitoring 0 hosts..."); |
90 |
– |
_serverCountLabel.setForeground( new Color(0, 0, 102)); |
91 |
– |
Box header = Box.createHorizontalBox(); |
92 |
– |
header.add(_serverCountLabel); |
93 |
– |
header.add(Box.createHorizontalGlue()); |
94 |
– |
header.add(iscream); |
95 |
– |
Box main = Box.createHorizontalBox(); |
96 |
– |
JButton |
94 |
|
utton = new JButton("Connect"); |
95 |
< |
|
96 |
< |
// set up the text area |
97 |
< |
_status.setEditable(false); |
98 |
< |
JScrollPane statusPane = new JScrollPane(_status); |
102 |
< |
statusPane.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 102)), " Messages ")); |
103 |
< |
|
104 |
< |
Box controlPane = Box.createVerticalBox(); |
105 |
< |
controlPane.add(button); |
106 |
< |
//controlPane.add(Box.createVerticalGlue()); |
107 |
< |
controlPane.add(statusPane); |
108 |
< |
main.add(controlPane); |
109 |
< |
main.add(_tabbedPane); |
95 |
> |
// set what happens when the X in the corner is clicked |
96 |
> |
addWindowListener(new WindowAdapter() { |
97 |
> |
public void windowClosing(WindowEvent e) {System.exit(0);} |
98 |
> |
}); |
99 |
|
|
100 |
< |
//_tabbedPane.setSelectedIndex(0); |
100 |
> |
// add the control pane to the top border |
101 |
> |
getContentPane().add(control, "North"); |
102 |
|
|
103 |
< |
// build the frame |
104 |
< |
box.add(header); |
105 |
< |
box.add(main); |
116 |
< |
|
117 |
< |
getContentPane().add(box); |
103 |
> |
// create a scrollable pane for the data in the centre |
104 |
> |
JScrollPane scrollPane = new JScrollPane(data); |
105 |
> |
getContentPane().add(scrollPane, "Center"); |
106 |
|
|
107 |
< |
// show the window |
108 |
< |
show(); |
109 |
< |
_status.insert("Running...\n",0); |
110 |
< |
} |
107 |
> |
// setup the status panel |
108 |
> |
JPanel bottom = new JPanel(); |
109 |
> |
bottom.setLayout(new BorderLayout()); |
110 |
> |
_messages.setEditable(false); |
111 |
> |
_messages.setRows(3); |
112 |
> |
JScrollPane messagesPane = new JScrollPane(_messages); |
113 |
> |
messagesPane.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 102)), " Messages ")); |
114 |
> |
bottom.add(messagesPane, "Center"); |
115 |
> |
JPanel statusPanel = new JPanel(); |
116 |
> |
JPanel linkPanel = new JPanel(); |
117 |
> |
linkPanel.setLayout(new GridLayout(1,2)); |
118 |
> |
linkPanel.add(_controlStatus); |
119 |
> |
linkPanel.add(_dataStatus); |
120 |
> |
statusPanel.setLayout(new GridLayout(2,1)); |
121 |
> |
statusPanel.add(linkPanel); |
122 |
> |
statusPanel.add(_queueStatus); |
123 |
> |
bottom.add(statusPanel, "South"); |
124 |
|
|
125 |
< |
//---PUBLIC METHODS--- |
125 |
> |
// add the status panel to the bottom border |
126 |
> |
getContentPane().add(bottom, "South"); |
127 |
> |
|
128 |
> |
// a nice icon for the window |
129 |
> |
setIconImage((new ImageIcon("./uk/ac/ukc/iscream/conient/server.gif")).getImage()); |
130 |
|
|
131 |
< |
public void run() { |
132 |
< |
HashMap hostList = new HashMap(); |
131 |
> |
// and just because we can, a silly splash screen |
132 |
> |
// of the dudes that did this funky gibble |
133 |
> |
Splash splash = new Splash(); |
134 |
> |
splash.show(); |
135 |
> |
// wait |
136 |
|
try { |
137 |
< |
while(true) { |
138 |
< |
|
139 |
< |
String xml = (String) _dataQueue.get(_myQueue); |
132 |
< |
if (xml == null) { |
133 |
< |
// shouldn't really happen...but not sure |
134 |
< |
_status.insert("No XML to update...",0); |
135 |
< |
} else { |
136 |
< |
|
137 |
< |
// Get a string without any null characters in it. |
138 |
< |
// -- maybe String.trim() would be better ? |
139 |
< |
if (xml.indexOf(0) != -1) { |
140 |
< |
xml = xml.substring(0, xml.indexOf(0)); |
141 |
< |
} |
142 |
< |
else { |
143 |
< |
xml = xml.substring(0, xml.length()); |
144 |
< |
} |
145 |
< |
|
146 |
< |
// Use XMLPacketMaker to make an XMLPacket object. |
147 |
< |
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml); |
148 |
< |
XMLPacket packet = xmlPacketMaker.createXMLPacket(); |
149 |
< |
String hostName = packet.getParam("packet.attributes.machine_name"); |
150 |
< |
if(!hostList.containsKey(hostName)) { |
151 |
< |
_serverCount++; |
152 |
< |
_serverCountLabel.setText(" Monitoring " + _serverCount + " hosts..."); |
153 |
< |
host = new HostDisplayPanel(); |
154 |
< |
_tabbedPane.addTab(hostName, _serverIcon, host, "Monitor " + hostName); |
155 |
< |
hostList.put(hostName, host); |
156 |
< |
_status.insert("New Host added: " + hostName + "\n", 0); |
157 |
< |
} |
158 |
< |
((HostDisplayPanel) hostList.get(hostName)).updateHost(packet); |
159 |
< |
} |
160 |
< |
} |
161 |
< |
} catch (Exception e) { |
162 |
< |
System.err.println("ERROR RUN: " + e); |
137 |
> |
Thread.sleep(DISPLAY_SPLASH_TIME_SECONDS * 1000); |
138 |
> |
} catch (InterruptedException e) { |
139 |
> |
// don't do anything, we don't care |
140 |
|
} |
141 |
+ |
//loose the window |
142 |
+ |
splash.dispose(); |
143 |
+ |
splash = null; |
144 |
+ |
// show the main window |
145 |
+ |
show(); |
146 |
|
} |
147 |
|
|
148 |
+ |
//---PUBLIC METHODS--- |
149 |
+ |
|
150 |
|
//---PRIVATE METHODS--- |
151 |
+ |
|
152 |
+ |
//---ACCESSOR/MUTATOR METHODS--- |
153 |
+ |
|
154 |
+ |
/** |
155 |
+ |
* Sets the control link status. |
156 |
+ |
* |
157 |
+ |
* @param status the message |
158 |
+ |
*/ |
159 |
+ |
public static void setControlStatus(String status) { |
160 |
+ |
_controlStatus.setText("Control Link: " + status); |
161 |
+ |
_controlStatus.repaint(); |
162 |
+ |
} |
163 |
|
|
164 |
< |
protected JPanel makeTextPanel(String text, java.awt.Component item) { |
165 |
< |
JPanel panel = new JPanel(false); |
166 |
< |
JLabel label = new JLabel(text); |
167 |
< |
label.setHorizontalAlignment(JLabel.RIGHT); |
168 |
< |
//item.setHorizontalAlignment(JLabel.LEFT); |
169 |
< |
panel.setLayout(new GridLayout(1, 2)); |
170 |
< |
panel.add(label); |
171 |
< |
panel.add(item); |
176 |
< |
return panel; |
164 |
> |
/** |
165 |
> |
* Sets the data link status. |
166 |
> |
* |
167 |
> |
* @param status the message |
168 |
> |
*/ |
169 |
> |
public static void setDataStatus(String status) { |
170 |
> |
_dataStatus.setText("Data Link: " + status); |
171 |
> |
_dataStatus.repaint(); |
172 |
|
} |
173 |
|
|
174 |
+ |
/** |
175 |
+ |
* Updates the queue status. |
176 |
+ |
* |
177 |
+ |
* @param currentQueue |
178 |
+ |
* @param numElements |
179 |
+ |
*/ |
180 |
+ |
public static void setQueueStatus(int currentQueue, int numElements) { |
181 |
+ |
_queueStatus.setText("Data Queue : " + currentQueue + " Packets Recieved : " + numElements); |
182 |
+ |
_queueStatus.repaint(); |
183 |
+ |
} |
184 |
|
|
185 |
< |
//---ACCESSOR/MUTATOR METHODS--- |
185 |
> |
/** |
186 |
> |
* Adds a system message to the messages list |
187 |
> |
* |
188 |
> |
* @param message the new message |
189 |
> |
*/ |
190 |
> |
public static void addMessage(String message) { |
191 |
> |
_messages.insert(message + "\n", 0); |
192 |
> |
} |
193 |
|
|
194 |
|
//---ATTRIBUTES--- |
183 |
– |
|
184 |
– |
HostDisplayPanel host; |
185 |
– |
JTabbedPane _tabbedPane = new JTabbedPane(); |
186 |
– |
Queue _dataQueue; |
187 |
– |
int _myQueue; |
188 |
– |
JTextArea _status = new JTextArea(); |
189 |
– |
ImageIcon _serverIcon = new ImageIcon("server.gif"); |
190 |
– |
JLabel _serverCountLabel; |
191 |
– |
int _serverCount = 0; |
195 |
|
|
193 |
– |
|
196 |
|
//---STATIC ATTRIBUTES--- |
197 |
|
|
198 |
+ |
/** |
199 |
+ |
* Displays information about the data link |
200 |
+ |
*/ |
201 |
+ |
static JLabel _controlStatus = new JLabel("Control Link: Disconnected", JLabel.LEFT); |
202 |
+ |
{ |
203 |
+ |
_controlStatus.setBorder(new EtchedBorder(EtchedBorder.LOWERED)); |
204 |
+ |
} |
205 |
+ |
|
206 |
+ |
/** |
207 |
+ |
* Displays information about the data link |
208 |
+ |
*/ |
209 |
+ |
static JLabel _dataStatus = new JLabel("Data Link: Disconnected", JLabel.LEFT); |
210 |
+ |
{ |
211 |
+ |
_dataStatus.setBorder(new EtchedBorder(EtchedBorder.LOWERED)); |
212 |
+ |
} |
213 |
+ |
|
214 |
+ |
/** |
215 |
+ |
* Displays information about the inbound data queue. |
216 |
+ |
*/ |
217 |
+ |
static JLabel _queueStatus = new JLabel(" ", JLabel.LEFT); |
218 |
+ |
{ |
219 |
+ |
_queueStatus.setBorder(new EtchedBorder(EtchedBorder.LOWERED)); |
220 |
+ |
} |
221 |
+ |
|
222 |
+ |
/** |
223 |
+ |
* The place where system messages are written. |
224 |
+ |
*/ |
225 |
+ |
static JTextArea _messages = new JTextArea(); |
226 |
+ |
{ |
227 |
+ |
_messages.setLineWrap(true); |
228 |
+ |
} |
229 |
+ |
|
230 |
+ |
//---INNER CLASSES---- |
231 |
+ |
|
232 |
+ |
/** |
233 |
+ |
* An inner class to display a splash screen |
234 |
+ |
*/ |
235 |
+ |
private class Splash extends JWindow { |
236 |
+ |
|
237 |
+ |
/** |
238 |
+ |
* Constructs a new splash screen |
239 |
+ |
*/ |
240 |
+ |
public Splash() { |
241 |
+ |
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); |
242 |
+ |
JPanel splash = new JPanel(); |
243 |
+ |
splash.setBackground(Color.black); |
244 |
+ |
JLabel image = new JLabel((new ImageIcon("./uk/ac/ukc/iscream/conient/i-scream-splash.gif"))); |
245 |
+ |
splash.add(image); |
246 |
+ |
setContentPane(splash); |
247 |
+ |
Dimension screen = getToolkit().getScreenSize(); |
248 |
+ |
pack(); |
249 |
+ |
setLocation((screen.width - getSize().width) / 2, |
250 |
+ |
(screen.height - getSize().height) / 2); |
251 |
+ |
|
252 |
+ |
|
253 |
+ |
} |
254 |
+ |
} |
255 |
|
} |