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 |
|
|
31 |
|
*/ |
32 |
|
public final String REVISION = "$Revision$"; |
33 |
|
|
34 |
< |
private final int width = 400; |
35 |
< |
private final int height = 700; |
34 |
> |
private final int width = 600; |
35 |
> |
private final int height = 600; |
36 |
|
|
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 = null; |
45 |
< |
if (args.length != 1) { |
46 |
< |
Object response = null; |
47 |
< |
while(!(response instanceof String)) { |
45 |
< |
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 |
< |
} |
51 |
< |
|
52 |
< |
int port = 4510; |
53 |
< |
Socket socket = new Socket(host, port); |
54 |
< |
BufferedReader inBound = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
55 |
< |
PrintWriter outBound = new PrintWriter(socket.getOutputStream()); |
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 |
< |
Queue dataQueue = new Queue(); |
50 |
< |
DataReader data = new DataReader(inBound, dataQueue); |
49 |
> |
// the data display panel |
50 |
> |
DataPanel data = new DataPanel(); |
51 |
|
|
52 |
< |
SwingClient client = new SwingClient(dataQueue); |
53 |
< |
Thread clientThread = new Thread(client); |
54 |
< |
|
55 |
< |
clientThread.start(); |
56 |
< |
data.start(); |
57 |
< |
|
58 |
< |
} catch (Exception e) { |
67 |
< |
System.err.println("ERROR START: " + e); |
68 |
< |
System.exit(1); |
69 |
< |
} |
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(Queue dataQueue) { |
78 |
< |
|
79 |
< |
_dataQueue = dataQueue; |
80 |
< |
_myQueue = _dataQueue.getQueue(); |
67 |
> |
private SwingClient(JPanel data, JPanel control) { |
68 |
|
// set up the Frame |
82 |
– |
setTitle("I-Scream Client"); |
83 |
– |
//setSize(width, height); |
84 |
– |
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
85 |
– |
Box box = Box.createVerticalBox(); |
86 |
– |
|
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 |
69 |
|
utton = new JButton("Connect"); |
70 |
+ |
super("I-Scream Client"); |
71 |
|
|
72 |
< |
// set up the text area |
73 |
< |
_status.setEditable(false); |
74 |
< |
JScrollPane statusPane = new JScrollPane(_status); |
75 |
< |
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); |
72 |
> |
setSize(width, height); |
73 |
> |
addWindowListener(new WindowAdapter() { |
74 |
> |
public void windowClosing(WindowEvent e) {System.exit(0);} |
75 |
> |
}); |
76 |
|
|
111 |
– |
//_tabbedPane.setSelectedIndex(0); |
77 |
|
|
78 |
|
// build the frame |
79 |
< |
box.add(header); |
80 |
< |
box.add(main); |
79 |
> |
getContentPane().add(control, "North"); |
80 |
> |
getContentPane().add(data, "Center"); |
81 |
|
|
82 |
< |
getContentPane().add(box); |
82 |
> |
JPanel bottom = new JPanel(); |
83 |
> |
bottom.setLayout(new BorderLayout()); |
84 |
> |
_messages.setEditable(false); |
85 |
> |
_messages.setRows(3); |
86 |
> |
JScrollPane messagesPane = new JScrollPane(_messages); |
87 |
> |
messagesPane.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 102)), " Messages ")); |
88 |
|
|
89 |
+ |
bottom.add(messagesPane, "Center"); |
90 |
+ |
bottom.add(_status, "South"); |
91 |
+ |
getContentPane().add(bottom, "South"); |
92 |
+ |
|
93 |
|
// show the window |
94 |
|
show(); |
121 |
– |
_status.insert("Running...\n",0); |
95 |
|
} |
96 |
|
|
97 |
|
//---PUBLIC METHODS--- |
98 |
|
|
126 |
– |
public void run() { |
127 |
– |
HashMap hostList = new HashMap(); |
128 |
– |
try { |
129 |
– |
while(true) { |
130 |
– |
|
131 |
– |
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); |
163 |
– |
} |
164 |
– |
} |
165 |
– |
|
99 |
|
//---PRIVATE METHODS--- |
167 |
– |
|
168 |
– |
protected JPanel makeTextPanel(String text, java.awt.Component item) { |
169 |
– |
JPanel panel = new JPanel(false); |
170 |
– |
JLabel label = new JLabel(text); |
171 |
– |
label.setHorizontalAlignment(JLabel.RIGHT); |
172 |
– |
//item.setHorizontalAlignment(JLabel.LEFT); |
173 |
– |
panel.setLayout(new GridLayout(1, 2)); |
174 |
– |
panel.add(label); |
175 |
– |
panel.add(item); |
176 |
– |
return panel; |
177 |
– |
} |
100 |
|
|
179 |
– |
|
101 |
|
//---ACCESSOR/MUTATOR METHODS--- |
102 |
|
|
103 |
+ |
public static void setStatus(String status) { |
104 |
+ |
_status.setText(status); |
105 |
+ |
_status.repaint(); |
106 |
+ |
} |
107 |
+ |
|
108 |
+ |
public static void addMessage(String message) { |
109 |
+ |
_messages.insert(message + "\n", 0); |
110 |
+ |
} |
111 |
|
//---ATTRIBUTES--- |
112 |
|
|
113 |
< |
HostDisplayPanel host; |
114 |
< |
JTabbedPane _tabbedPane = new JTabbedPane(); |
115 |
< |
Queue _dataQueue; |
116 |
< |
int _myQueue; |
117 |
< |
JTextArea _status = new JTextArea(); |
113 |
> |
static JLabel _status = new JLabel("Initialising", JLabel.LEFT); |
114 |
> |
{ |
115 |
> |
_status.setBorder(new EtchedBorder(EtchedBorder.LOWERED)); |
116 |
> |
} |
117 |
> |
|
118 |
|
ImageIcon _serverIcon = new ImageIcon("server.gif"); |
119 |
|
JLabel _serverCountLabel; |
120 |
|
int _serverCount = 0; |
121 |
< |
|
121 |
> |
static JTextArea _messages = new JTextArea(); |
122 |
|
|
123 |
|
//---STATIC ATTRIBUTES--- |
124 |
|
|