root / trunk / code / projects / colonet / ColonetGUI / ColonetServerInterface.java @ 76
History | View | Annotate | Download (9.96 KB)
1 |
/*
|
---|---|
2 |
* ColonetServerInterface.java
|
3 |
* Gregory Tress
|
4 |
*/
|
5 |
|
6 |
import java.net.*; |
7 |
import java.io.*; |
8 |
import javax.swing.JOptionPane; |
9 |
import javax.swing.JTextArea; |
10 |
|
11 |
public class ColonetServerInterface |
12 |
{ |
13 |
|
14 |
/* STATIC FIELDS
|
15 |
* Eugene Marinelli, Fan
|
16 |
* 10/27/06
|
17 |
*
|
18 |
* Colonet Definitions - common definitions and structs used in all colonet
|
19 |
* applications
|
20 |
|
21 |
Old packet structure:
|
22 |
|
23 |
COMMAND PACKET STRUCTURE
|
24 |
1: SEND_TO_ROBOT
|
25 |
2: # of robot, or GLOBAL_DEST
|
26 |
3: COLONET_COMMMAND
|
27 |
4: message code (i.e. ORB_SET)
|
28 |
5: any data, as many that fit in the packet
|
29 |
|
30 |
REQUEST PACKET STRUCTURE
|
31 |
1: REQUEST_FROM_SERVER
|
32 |
2: # of robot
|
33 |
3: COLONET_REQUEST
|
34 |
4: ???
|
35 |
|
36 |
9/12/07 New server interface structure
|
37 |
Client will no longer send full robot packets to the server.
|
38 |
Commands will be defined as necessary.
|
39 |
*/
|
40 |
|
41 |
//General Colonet Interface
|
42 |
public static final String SEND_TO_ROBOT = "0"; |
43 |
public static final String REQUEST_FROM_SERVER = "1"; |
44 |
public static final String RESPONSE_TO_CLIENT_REQUEST = "2"; |
45 |
public static final String REQUEST_BOM_MATRIX = "144"; |
46 |
|
47 |
public static final String COLONET_COMMAND = "13"; //0x0D |
48 |
public static final String COLONET_REQUEST = "14"; //0x0E |
49 |
public static final String CORONET_RESPONSE = "15"; //0x0F |
50 |
public static final String GLOBAL_DEST = "200"; |
51 |
|
52 |
//Use BATTERY to request battery level
|
53 |
public static final String BATTERY = "56"; //0x38 |
54 |
|
55 |
//MOTORS
|
56 |
public static final String MOTORS_INIT = "23"; //0x17 |
57 |
public static final String MOTOR1_SET = "24"; //0x18 |
58 |
public static final String MOTOR2_SET = "25"; //0x19 |
59 |
public static final String MOTORS_OFF = "26"; //0x1A |
60 |
public static final String MOVE = "27"; //0x1B |
61 |
public static final String MOVE_AVOID = "28"; //0x1C |
62 |
|
63 |
//BUZZER
|
64 |
public static final String BUZZER_INIT = "0"; //0x00 |
65 |
public static final String BUZZER_SET_VAL = "1"; //0x01 |
66 |
public static final String BUZZER_SET_FREQ = "2"; //0x02 |
67 |
public static final String BUZZER_CHIRP = "3"; //0x03 |
68 |
public static final String BUZZER_OFF = "4"; //0x04 |
69 |
|
70 |
//ORB
|
71 |
public static final String ORB_INIT = "12"; //0x0C |
72 |
public static final String ORB_SET = "13"; //0x0D |
73 |
public static final String ORB_SET_COLOR = "14"; //0x0E |
74 |
public static final String ORB_DISABLE = "15"; //0x0F |
75 |
public static final String ORB_ENABLE = "16"; //0x10 |
76 |
public static final String ORB_SET_DIO = "17"; //0x11 |
77 |
public static final String LED_INIT = "18"; //0x12 |
78 |
public static final String LED_USER = "19"; //0x13 |
79 |
public static final String ORB_SET_NUM_NS = "20"; //0x14 |
80 |
public static final String ORB_SET_NUM = "21"; //0x15 |
81 |
public static final String ORB_SEND = "22"; //0x16 |
82 |
|
83 |
|
84 |
Socket socket;
|
85 |
OutputStreamWriter out;
|
86 |
BufferedReader reader;
|
87 |
DataListener dataListener; |
88 |
JTextArea log, txtMatrix;
|
89 |
|
90 |
/*
|
91 |
* FUNCTION IMPLEMENTATIONS
|
92 |
*/
|
93 |
|
94 |
public ColonetServerInterface (JTextArea log, JTextArea txtMatrix) { |
95 |
this.log = log;
|
96 |
this.txtMatrix = txtMatrix;
|
97 |
dataListener = new DataListener();
|
98 |
} |
99 |
|
100 |
public Socket getSocket () { |
101 |
return socket;
|
102 |
} |
103 |
|
104 |
public OutputStreamWriter getOutputStreamWriter () { |
105 |
return out;
|
106 |
} |
107 |
|
108 |
public BufferedReader getBufferedReader () { |
109 |
return reader;
|
110 |
} |
111 |
|
112 |
public boolean isReady () { |
113 |
if (socket == null || out == null || reader == null) return false; |
114 |
if (!socket.isConnected() || socket.isClosed() || socket.isInputShutdown() || socket.isOutputShutdown()) return false; |
115 |
return true; |
116 |
} |
117 |
|
118 |
public boolean isInputReady () { |
119 |
try {
|
120 |
if (reader.ready()) return true; |
121 |
} catch (Exception e) { |
122 |
return false; |
123 |
} |
124 |
return false; |
125 |
} |
126 |
|
127 |
public String getLine () { |
128 |
if (this.isReady()) { |
129 |
try {
|
130 |
return reader.readLine();
|
131 |
} catch (IOException e) { |
132 |
return null; |
133 |
} |
134 |
} else {
|
135 |
return null; |
136 |
} |
137 |
} |
138 |
|
139 |
/**
|
140 |
* Create socket connection to Colonet server.
|
141 |
* If successful, start threads for loading the webcam image and listening for incoming data.
|
142 |
*/
|
143 |
public void connect (String strHost, String strPort) { |
144 |
//make sure hostname and port are valid
|
145 |
if (strHost.equals("") || strPort.equals("")) { |
146 |
err("Please enter a hostname and port.");
|
147 |
return;
|
148 |
} |
149 |
int port = 0; |
150 |
try {
|
151 |
port = Integer.parseInt(strPort);
|
152 |
} catch (Exception e) { |
153 |
err("Invalid port");
|
154 |
return;
|
155 |
} |
156 |
|
157 |
//make sure we aren't already connected. if so, disconnect first.
|
158 |
if (socket != null && socket.isConnected()) { |
159 |
try {
|
160 |
out.close(); |
161 |
socket.close(); |
162 |
} catch (IOException e) { |
163 |
log.append("Error closing socket. Reconnecting...\n");
|
164 |
} |
165 |
} |
166 |
|
167 |
try {
|
168 |
log.append("Attempting to connect to " + strHost + "\n"); |
169 |
socket = new Socket(strHost, port); |
170 |
} catch (UnknownHostException e) { |
171 |
log.append("Unknown host exception.\n");
|
172 |
err("Unknown Host Exception");
|
173 |
return;
|
174 |
} catch (IOException e) { |
175 |
log.append("IO Exception.\n");
|
176 |
err("IO Exception");
|
177 |
return;
|
178 |
} catch (java.security.AccessControlException e) {
|
179 |
log.append("Access Control Exception.\n");
|
180 |
err("Permission denied by java.security.AccessControlException.\n\n"
|
181 |
+"You may only connect to the server from which this applet was loaded.");
|
182 |
return;
|
183 |
} |
184 |
if (socket == null || !socket.isConnected()) { |
185 |
log.append("Connection is not ready. Try connecting again.");
|
186 |
return;
|
187 |
} |
188 |
log.append("Connected to " + strHost + " on port " + port + "\n"); |
189 |
try {
|
190 |
out = new OutputStreamWriter(socket.getOutputStream()); |
191 |
reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
192 |
} catch (IOException e) { |
193 |
warn("Could not get transfer streams from socket connection.");
|
194 |
} |
195 |
dataListener.start(); |
196 |
|
197 |
} |
198 |
|
199 |
/**
|
200 |
* Sends a command code to the colonet server so that it can be redirected to one or more robots.
|
201 |
* Assembles the given string into a packet and sends a full command string to the colonet server.
|
202 |
* The string sent from here should be the correct form for sending directly to the robot.
|
203 |
*/
|
204 |
public void sendData (String s, String robotNumber) { |
205 |
//make sure we can send
|
206 |
if (!this.isReady()) { |
207 |
warn("There was a problem with the connection. Data could not be sent.\n"
|
208 |
+"Make sure you are connected and try sending the command again.");
|
209 |
return;
|
210 |
} |
211 |
|
212 |
//create packet
|
213 |
String packet;
|
214 |
packet = ColonetServerInterface.SEND_TO_ROBOT; |
215 |
packet += " " + robotNumber;
|
216 |
packet += " " + ColonetServerInterface.COLONET_COMMAND;
|
217 |
packet += " " + s; //add the command code here |
218 |
packet += "\n";
|
219 |
|
220 |
//send packet
|
221 |
try {
|
222 |
log.append("Sending: " + packet);
|
223 |
out.write(packet); |
224 |
out.flush(); |
225 |
} catch (IOException e) { |
226 |
log.append("Could not send data.\n");
|
227 |
} |
228 |
} |
229 |
|
230 |
/**
|
231 |
* Sends a request to the colonet server so that it can be redirected to one or more robots.
|
232 |
* Assembles the given string into a packet and sends a full command string to the colonet server.
|
233 |
* The string sent from here should be the correct form for sending directly to the robot.
|
234 |
* @param s The request code to be sent
|
235 |
* @see #sendData(String)
|
236 |
*/
|
237 |
public void sendRequest (String s, String robotNumber) { |
238 |
//make sure we can send
|
239 |
if (!this.isReady()) { |
240 |
log.append("Could not send data.\n");
|
241 |
return;
|
242 |
} |
243 |
|
244 |
//get robot code to send to.
|
245 |
/*
|
246 |
if (cmbRobotNumber.getSelectedIndex() == 0)
|
247 |
robotNumber = ColonetServerInterface.GLOBAL_DEST;
|
248 |
else
|
249 |
robotNumber = ( cmbRobotNumber.getSelectedIndex() - 1 ) + "";
|
250 |
*/
|
251 |
|
252 |
//create packet
|
253 |
String packet;
|
254 |
packet = ColonetServerInterface.REQUEST_FROM_SERVER; |
255 |
//packet += " " + robotNumber;
|
256 |
//packet += " " + ColonetServerInterface.COLONET_REQUEST;
|
257 |
packet += " " + s; //add the command code here |
258 |
packet += "\n";
|
259 |
|
260 |
//send packet
|
261 |
try {
|
262 |
Thread.sleep(100); //pause to be safe |
263 |
out.write(packet); |
264 |
out.flush(); |
265 |
log.append("Sent: " + packet);
|
266 |
} catch (IOException e) { |
267 |
log.append("Could not send data.\n");
|
268 |
} catch (InterruptedException e) { |
269 |
log.append("Thread InterruptedException in sendData\n");
|
270 |
} |
271 |
|
272 |
} |
273 |
|
274 |
|
275 |
|
276 |
|
277 |
/**
|
278 |
* Display informational message box on the screen. Used for casual communicaton to the user.
|
279 |
* @param text Text to display
|
280 |
*/
|
281 |
public void msg (String text) { |
282 |
JOptionPane.showMessageDialog(null, text, "Colonet", JOptionPane.INFORMATION_MESSAGE); |
283 |
} |
284 |
|
285 |
/**
|
286 |
* Display warning message box on the screen. Used for minor alerts or exceptions.
|
287 |
* @param text Text to display
|
288 |
*/
|
289 |
public void warn (String text) { |
290 |
JOptionPane.showMessageDialog(null, text, "Colonet", JOptionPane.WARNING_MESSAGE); |
291 |
} |
292 |
|
293 |
/**
|
294 |
* Display error message box on the screen. Used for major errors or exceptions in the program.
|
295 |
* @param text Text to display
|
296 |
*/
|
297 |
public void err (String text) { |
298 |
JOptionPane.showMessageDialog(null, text, "Colonet", JOptionPane.ERROR_MESSAGE); |
299 |
} |
300 |
|
301 |
|
302 |
/*
|
303 |
* DataListener thread.
|
304 |
*
|
305 |
*/
|
306 |
class DataListener extends Thread { |
307 |
final int DATALISTENER_DELAY = 222; |
308 |
|
309 |
public DataListener () {
|
310 |
super("Colonet DataListener"); |
311 |
} |
312 |
|
313 |
public void run () { |
314 |
String line;
|
315 |
while (true) { |
316 |
try {
|
317 |
line = getLine(); |
318 |
if (line != null) { |
319 |
parseData(line); |
320 |
//TODO: parse incoming data here
|
321 |
|
322 |
//request more data
|
323 |
//csi.sendRequest("Get all data", "server");
|
324 |
|
325 |
/*
|
326 |
// update robot list
|
327 |
int numRobots = 5;
|
328 |
String [] robotList = new String[numRobots+1];
|
329 |
robotList[0] = "All";
|
330 |
for (int i = 1; i <= numRobots; i++)
|
331 |
robotList[i] = "Robot " + i;
|
332 |
cmbRobotNum = new JComboBox(robotList);
|
333 |
*/
|
334 |
} |
335 |
Thread.sleep(DATALISTENER_DELAY);
|
336 |
} catch (InterruptedException e) { |
337 |
return;
|
338 |
} |
339 |
} |
340 |
} |
341 |
|
342 |
public void parseData (String line) { |
343 |
log.append("Incoming data: [" + line + "]\n"); |
344 |
if (line.startsWith(ColonetServerInterface.RESPONSE_TO_CLIENT_REQUEST + " " + ColonetServerInterface.REQUEST_BOM_MATRIX)) |
345 |
parseMatrix(line); |
346 |
} |
347 |
|
348 |
public void parseMatrix (String line) { |
349 |
log.append("Parsing matrix\n");
|
350 |
txtMatrix.setText("");
|
351 |
String [] str = line.split(" "); |
352 |
int num = Integer.parseInt(str[2]); |
353 |
for (int i = 0; i < num; i++) { |
354 |
for (int j = 0; j < num; j++) { |
355 |
String next = str[3 + i*num + j]; |
356 |
if (next.equals("-1")) |
357 |
txtMatrix.append("-");
|
358 |
else
|
359 |
txtMatrix.append(next); |
360 |
if (j < num - 1) |
361 |
txtMatrix.append(" ");
|
362 |
} |
363 |
if (i < num - 1) |
364 |
txtMatrix.append("\n");
|
365 |
} |
366 |
} |
367 |
|
368 |
} |
369 |
|
370 |
} |