1 |
ajm |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
|
|
|
3 |
|
|
//---IMPORTS--- |
4 |
|
|
import javax.swing.*; |
5 |
|
|
import javax.swing.border.*; |
6 |
|
|
import java.awt.Color; |
7 |
|
|
|
8 |
|
|
import java.awt.*; |
9 |
|
|
import java.awt.event.*; |
10 |
|
|
import java.net.*; |
11 |
|
|
import java.io.*; |
12 |
ajm |
1.2 |
import java.util.Date; |
13 |
|
|
import java.text.DateFormat; |
14 |
|
|
import java.util.Locale; |
15 |
ajm |
1.4 |
import java.util.HashMap; |
16 |
ajm |
1.1 |
|
17 |
|
|
/** |
18 |
|
|
* NASTY AND BASIC, PLEASE DON'T COMPLAIN |
19 |
|
|
* |
20 |
|
|
* @author $Author: ajm4 $ |
21 |
ajm |
1.6 |
* @version $Id: SwingClient.java,v 1.5 2001/01/15 03:01:37 ajm4 Exp $ |
22 |
ajm |
1.1 |
*/ |
23 |
|
|
public class SwingClient extends JFrame implements Runnable { |
24 |
|
|
|
25 |
|
|
//---FINAL ATTRIBUTES--- |
26 |
|
|
|
27 |
|
|
/** |
28 |
|
|
* The current CVS revision of this class |
29 |
|
|
*/ |
30 |
ajm |
1.6 |
public final String REVISION = "$Revision: 1.5 $"; |
31 |
ajm |
1.1 |
|
32 |
ajm |
1.5 |
private final int width = 400; |
33 |
ajm |
1.1 |
private final int height = 700; |
34 |
|
|
|
35 |
|
|
//---STATIC METHODS--- |
36 |
|
|
|
37 |
|
|
public static void main(String[] args) { |
38 |
|
|
try { |
39 |
ajm |
1.5 |
String host = null; |
40 |
|
|
if (args.length != 1) { |
41 |
|
|
Object response = null; |
42 |
|
|
while(!(response instanceof String)) { |
43 |
|
|
response = JOptionPane.showInputDialog(null, "Please enter the name of a server running the I-Scream client interface:", "I-Scream Server", JOptionPane.INFORMATION_MESSAGE); |
44 |
|
|
} |
45 |
|
|
host = (String) response; |
46 |
|
|
} else { |
47 |
|
|
host = args[0]; |
48 |
|
|
} |
49 |
|
|
|
50 |
ajm |
1.1 |
int port = 4510; |
51 |
|
|
Socket socket = new Socket(host, port); |
52 |
|
|
BufferedReader inBound = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
53 |
|
|
PrintWriter outBound = new PrintWriter(socket.getOutputStream()); |
54 |
|
|
DataReader data = new DataReader(inBound); |
55 |
|
|
|
56 |
ajm |
1.5 |
SwingClient client = new SwingClient(data); |
57 |
|
|
Thread clientThread = new Thread(client); |
58 |
|
|
clientThread.start(); |
59 |
ajm |
1.1 |
} catch (Exception e) { |
60 |
ajm |
1.4 |
System.err.println("ERROR START: " + e); |
61 |
ajm |
1.5 |
System.exit(1); |
62 |
ajm |
1.1 |
} |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
//---CONSTRUCTORS--- |
66 |
|
|
|
67 |
|
|
/** |
68 |
|
|
* Creates a new Swing Client. |
69 |
|
|
*/ |
70 |
|
|
public SwingClient(DataReader data) { |
71 |
|
|
_data = data; |
72 |
|
|
// set up the Frame |
73 |
|
|
setTitle("I-Scream Client"); |
74 |
|
|
setSize(width, height); |
75 |
|
|
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
76 |
|
|
Box box = Box.createVerticalBox(); |
77 |
|
|
|
78 |
|
|
// create the i-scream logo at the top |
79 |
|
|
JLabel iscream = new JLabel(new ImageIcon("i-scream.gif")); |
80 |
|
|
JLabel comment = new JLabel(" I-Scream Client"); |
81 |
|
|
comment.setForeground( new Color(0, 0, 102)); |
82 |
|
|
Box header = Box.createHorizontalBox(); |
83 |
|
|
header.add(comment); |
84 |
|
|
header.add(Box.createHorizontalGlue()); |
85 |
|
|
header.add(iscream); |
86 |
|
|
|
87 |
ajm |
1.4 |
//_tabbedPane.setSelectedIndex(0); |
88 |
|
|
|
89 |
ajm |
1.1 |
// build the frame |
90 |
|
|
box.add(header); |
91 |
ajm |
1.4 |
box.add(_tabbedPane); |
92 |
|
|
box.add(_status); |
93 |
ajm |
1.1 |
getContentPane().add(box); |
94 |
|
|
|
95 |
|
|
// show the window |
96 |
|
|
show(); |
97 |
ajm |
1.4 |
_status.setText("Running..."); |
98 |
ajm |
1.1 |
} |
99 |
|
|
|
100 |
|
|
//---PUBLIC METHODS--- |
101 |
|
|
|
102 |
|
|
public void run() { |
103 |
ajm |
1.4 |
HashMap hostList = new HashMap(); |
104 |
ajm |
1.3 |
try { |
105 |
ajm |
1.6 |
_data.start(); |
106 |
|
|
while(true) { |
107 |
|
|
synchronized (_data) { |
108 |
|
|
_data.wait(); |
109 |
|
|
} |
110 |
|
|
String xml = _data.getXML(); |
111 |
|
|
if (xml == null) { |
112 |
|
|
// shouldn't really happen...but not sure |
113 |
|
|
_status.setText("No XML to update..."); |
114 |
|
|
} else { |
115 |
|
|
_status.setText("Got inbound data...handling"); |
116 |
|
|
// Get a string without any null characters in it. |
117 |
|
|
// -- maybe String.trim() would be better here ? |
118 |
|
|
if (xml.indexOf(0) != -1) { |
119 |
|
|
xml = xml.substring(0, xml.indexOf(0)); |
120 |
|
|
} |
121 |
|
|
else { |
122 |
|
|
xml = xml.substring(0, xml.length()); |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
// Use XMLPacketMaker to make an XMLPacket object. |
126 |
|
|
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml); |
127 |
|
|
XMLPacket packet = xmlPacketMaker.createXMLPacket(); |
128 |
|
|
String hostName = packet.getParam("packet.attributes.machine_name"); |
129 |
|
|
if(!hostList.containsKey(hostName)) { |
130 |
|
|
host = new HostDisplayPanel(); |
131 |
|
|
_tabbedPane.addTab(hostName, _serverIcon, host, "Monitor " + hostName); |
132 |
|
|
hostList.put(hostName, host); |
133 |
|
|
_status.setText("New Host added: " + hostName); |
134 |
|
|
} |
135 |
|
|
((HostDisplayPanel) hostList.get(hostName)).updateHost(packet); |
136 |
|
|
} |
137 |
ajm |
1.1 |
} |
138 |
ajm |
1.3 |
} catch (Exception e) { |
139 |
ajm |
1.4 |
System.err.println("ERROR RUN: " + e); |
140 |
ajm |
1.1 |
} |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
//---PRIVATE METHODS--- |
144 |
|
|
|
145 |
ajm |
1.2 |
protected JPanel makeTextPanel(String text, Component item) { |
146 |
ajm |
1.1 |
JPanel panel = new JPanel(false); |
147 |
|
|
JLabel label = new JLabel(text); |
148 |
|
|
label.setHorizontalAlignment(JLabel.RIGHT); |
149 |
ajm |
1.2 |
//item.setHorizontalAlignment(JLabel.LEFT); |
150 |
ajm |
1.1 |
panel.setLayout(new GridLayout(1, 2)); |
151 |
|
|
panel.add(label); |
152 |
|
|
panel.add(item); |
153 |
|
|
return panel; |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
|
157 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
158 |
|
|
|
159 |
|
|
//---ATTRIBUTES--- |
160 |
ajm |
1.5 |
|
161 |
ajm |
1.4 |
HostDisplayPanel host; |
162 |
|
|
JTabbedPane _tabbedPane = new JTabbedPane(); |
163 |
ajm |
1.1 |
DataReader _data; |
164 |
ajm |
1.4 |
JLabel _status = new JLabel(); |
165 |
|
|
ImageIcon _serverIcon = new ImageIcon("server.gif"); |
166 |
ajm |
1.1 |
|
167 |
|
|
//---STATIC ATTRIBUTES--- |
168 |
|
|
|
169 |
|
|
} |