1 |
//---PACKAGE DECLARATION--- |
2 |
|
3 |
//---IMPORTS--- |
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.*; |
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.*; |
18 |
|
19 |
/** |
20 |
* NASTY AND BASIC, PLEASE DON'T COMPLAIN |
21 |
* |
22 |
* @author $Author: ajm4 $ |
23 |
* @version $Id: SwingClient.java,v 1.7 2001/01/15 03:26:26 ajm4 Exp $ |
24 |
*/ |
25 |
public class SwingClient extends JFrame implements Runnable { |
26 |
|
27 |
//---FINAL ATTRIBUTES--- |
28 |
|
29 |
/** |
30 |
* The current CVS revision of this class |
31 |
*/ |
32 |
public final String REVISION = "$Revision: 1.7 $"; |
33 |
|
34 |
private final int width = 400; |
35 |
private final int height = 700; |
36 |
|
37 |
//---STATIC METHODS--- |
38 |
|
39 |
public static void main(String[] args) { |
40 |
try { |
41 |
String host = null; |
42 |
if (args.length != 1) { |
43 |
Object response = null; |
44 |
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()); |
56 |
|
57 |
Queue dataQueue = new Queue(); |
58 |
DataReader data = new DataReader(inBound, dataQueue); |
59 |
|
60 |
SwingClient client = new SwingClient(dataQueue); |
61 |
Thread clientThread = new Thread(client); |
62 |
|
63 |
clientThread.start(); |
64 |
data.start(); |
65 |
|
66 |
} catch (Exception e) { |
67 |
System.err.println("ERROR START: " + e); |
68 |
System.exit(1); |
69 |
} |
70 |
} |
71 |
|
72 |
//---CONSTRUCTORS--- |
73 |
|
74 |
/** |
75 |
* Creates a new Swing Client. |
76 |
*/ |
77 |
public SwingClient(Queue dataQueue) { |
78 |
|
79 |
_dataQueue = dataQueue; |
80 |
_myQueue = _dataQueue.getQueue(); |
81 |
// 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 |
97 |
button = new JButton("Connect"); |
98 |
|
99 |
// set up the text area |
100 |
_status.setEditable(false); |
101 |
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); |
110 |
|
111 |
//_tabbedPane.setSelectedIndex(0); |
112 |
|
113 |
// build the frame |
114 |
box.add(header); |
115 |
box.add(main); |
116 |
|
117 |
getContentPane().add(box); |
118 |
|
119 |
// show the window |
120 |
show(); |
121 |
_status.insert("Running...\n",0); |
122 |
} |
123 |
|
124 |
//---PUBLIC METHODS--- |
125 |
|
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 |
|
166 |
//---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 |
} |
178 |
|
179 |
|
180 |
//---ACCESSOR/MUTATOR METHODS--- |
181 |
|
182 |
//---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; |
192 |
|
193 |
|
194 |
//---STATIC ATTRIBUTES--- |
195 |
|
196 |
} |