root / trunk / code / projects / colonet / ColonetGUI / ColonetServerInterface.java @ 181
History | View | Annotate | Download (9.59 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 |
public static final String REQUEST_XBEE_IDS = "145"; |
47 |
|
48 |
public static final String COLONET_COMMAND = "13"; //0x0D |
49 |
public static final String COLONET_REQUEST = "14"; //0x0E |
50 |
public static final String CORONET_RESPONSE = "15"; //0x0F |
51 |
public static final String GLOBAL_DEST = "200"; |
52 |
|
53 |
//Queue instructions
|
54 |
public static final String COLONET_QUEUE = "100"; |
55 |
public static final String QUEUE_UPDATE = "101"; |
56 |
public static final String QUEUE_ADD = "102"; |
57 |
public static final String QUEUE_REMOVE = "103"; |
58 |
public static final String QUEUE_REORDER = "104"; |
59 |
|
60 |
//Use BATTERY to request battery level
|
61 |
public static final String BATTERY = "56"; //0x38 |
62 |
|
63 |
//MOTORS
|
64 |
public static final String MOTORS_INIT = "23"; //0x17 |
65 |
public static final String MOTOR1_SET = "24"; //0x18 |
66 |
public static final String MOTOR2_SET = "25"; //0x19 |
67 |
public static final String MOTORS_OFF = "26"; //0x1A |
68 |
public static final String MOVE = "27"; //0x1B |
69 |
public static final String MOVE_AVOID = "28"; //0x1C |
70 |
|
71 |
//BUZZER
|
72 |
public static final String BUZZER_INIT = "0"; //0x00 |
73 |
public static final String BUZZER_SET_VAL = "1"; //0x01 |
74 |
public static final String BUZZER_SET_FREQ = "2"; //0x02 |
75 |
public static final String BUZZER_CHIRP = "3"; //0x03 |
76 |
public static final String BUZZER_OFF = "4"; //0x04 |
77 |
|
78 |
//ORB
|
79 |
public static final String ORB_INIT = "12"; //0x0C |
80 |
public static final String ORB_SET = "13"; //0x0D |
81 |
public static final String ORB_SET_COLOR = "14"; //0x0E |
82 |
public static final String ORB_DISABLE = "15"; //0x0F |
83 |
public static final String ORB_ENABLE = "16"; //0x10 |
84 |
public static final String ORB_SET_DIO = "17"; //0x11 |
85 |
public static final String LED_INIT = "18"; //0x12 |
86 |
public static final String LED_USER = "19"; //0x13 |
87 |
public static final String ORB_SET_NUM_NS = "20"; //0x14 |
88 |
public static final String ORB_SET_NUM = "21"; //0x15 |
89 |
public static final String ORB_SEND = "22"; //0x16 |
90 |
|
91 |
|
92 |
Colonet colonet; //save the entire applet locally
|
93 |
Socket socket;
|
94 |
OutputStreamWriter out;
|
95 |
BufferedReader reader;
|
96 |
DataListener dataListener; |
97 |
JTextArea log, txtMatrix;
|
98 |
|
99 |
|
100 |
/*
|
101 |
* FUNCTION IMPLEMENTATIONS
|
102 |
*/
|
103 |
|
104 |
public ColonetServerInterface (Colonet colonet) {
|
105 |
this.colonet = colonet;
|
106 |
this.log = colonet.getLog();
|
107 |
this.txtMatrix = colonet.getMatrixInput();
|
108 |
dataListener = new DataListener();
|
109 |
} |
110 |
|
111 |
public Socket getSocket () { |
112 |
return socket;
|
113 |
} |
114 |
|
115 |
public OutputStreamWriter getOutputStreamWriter () { |
116 |
return out;
|
117 |
} |
118 |
|
119 |
public BufferedReader getBufferedReader () { |
120 |
return reader;
|
121 |
} |
122 |
|
123 |
public boolean isReady () { |
124 |
if (socket == null || out == null || reader == null) return false; |
125 |
if (!socket.isConnected() || socket.isClosed() || socket.isInputShutdown() || socket.isOutputShutdown()) return false; |
126 |
return true; |
127 |
} |
128 |
|
129 |
public boolean isInputReady () { |
130 |
try {
|
131 |
if (reader.ready()) return true; |
132 |
} catch (Exception e) { |
133 |
return false; |
134 |
} |
135 |
return false; |
136 |
} |
137 |
|
138 |
public String getLine () { |
139 |
if (this.isReady()) { |
140 |
try {
|
141 |
return reader.readLine();
|
142 |
} catch (IOException e) { |
143 |
return null; |
144 |
} |
145 |
} else {
|
146 |
return null; |
147 |
} |
148 |
} |
149 |
|
150 |
/**
|
151 |
* Create socket connection to Colonet server.
|
152 |
* If successful, start threads for loading the webcam image and listening for incoming data.
|
153 |
*/
|
154 |
public void connect (String strHost, String strPort) { |
155 |
//make sure hostname and port are valid
|
156 |
if (strHost.equals("") || strPort.equals("")) { |
157 |
err("Please enter a hostname and port.");
|
158 |
return;
|
159 |
} |
160 |
int port = 0; |
161 |
try {
|
162 |
port = Integer.parseInt(strPort);
|
163 |
} catch (Exception e) { |
164 |
err("Invalid port");
|
165 |
return;
|
166 |
} |
167 |
|
168 |
//make sure we aren't already connected.
|
169 |
if (socket != null && socket.isConnected()) { |
170 |
return;
|
171 |
} |
172 |
|
173 |
try {
|
174 |
socket = new Socket(strHost, port); |
175 |
} catch (UnknownHostException e) { |
176 |
log.append("Unknown host exception.\n");
|
177 |
err("Unknown Host Exception");
|
178 |
return;
|
179 |
} catch (IOException e) { |
180 |
log.append("IO Exception.\n");
|
181 |
err("IO Exception");
|
182 |
return;
|
183 |
} catch (java.security.AccessControlException e) {
|
184 |
log.append("Access Control Exception.\n");
|
185 |
err("Permission denied by java.security.AccessControlException.\n\n"
|
186 |
+"You may only connect to the server from which this applet was loaded.");
|
187 |
return;
|
188 |
} |
189 |
if (socket == null || !socket.isConnected()) { |
190 |
log.append("Connection is not ready. Try connecting again.");
|
191 |
return;
|
192 |
} |
193 |
try {
|
194 |
out = new OutputStreamWriter(socket.getOutputStream()); |
195 |
reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
196 |
} catch (IOException e) { |
197 |
warn("Could not get transfer streams from socket connection.");
|
198 |
} |
199 |
dataListener.start(); |
200 |
|
201 |
} |
202 |
|
203 |
/*
|
204 |
* sendString - only this method should actually write data to the output stream
|
205 |
*/
|
206 |
public void sendString (String s) { |
207 |
//make sure we can send
|
208 |
if (!this.isReady()) { |
209 |
log.setText("Could not send data.\n");
|
210 |
return;
|
211 |
} |
212 |
//send packet
|
213 |
try {
|
214 |
Thread.sleep(50); //pause to be safe |
215 |
out.write(s); |
216 |
out.flush(); |
217 |
log.setText("Sent: " + s);
|
218 |
} catch (IOException e) { |
219 |
log.setText("Could not send data.\n");
|
220 |
} catch (InterruptedException e) { |
221 |
log.setText("Thread InterruptedException in sendData\n");
|
222 |
} |
223 |
} |
224 |
|
225 |
public void sendData (String s, String robotNumber) { |
226 |
//create packet
|
227 |
String packet = ""; |
228 |
packet += ColonetServerInterface.SEND_TO_ROBOT; |
229 |
packet += " " + robotNumber;
|
230 |
packet += " " + ColonetServerInterface.COLONET_COMMAND;
|
231 |
packet += " " + s; //add the command code here |
232 |
packet += "\n";
|
233 |
sendString(packet); |
234 |
} |
235 |
|
236 |
public void sendRequest (String s, String robotNumber) { |
237 |
//create packet
|
238 |
String packet = ""; |
239 |
packet += ColonetServerInterface.REQUEST_FROM_SERVER; |
240 |
//packet += " " + robotNumber;
|
241 |
//packet += " " + ColonetServerInterface.COLONET_REQUEST;
|
242 |
packet += " " + s; //add the command code here |
243 |
packet += "\n";
|
244 |
sendString(packet); |
245 |
} |
246 |
|
247 |
public void sendSensorDataRequest () { |
248 |
sendRequest(ColonetServerInterface.REQUEST_BOM_MATRIX, "");
|
249 |
} |
250 |
|
251 |
public void sendXBeeIDRequest () { |
252 |
sendRequest(ColonetServerInterface.REQUEST_XBEE_IDS, "");
|
253 |
} |
254 |
|
255 |
/*
|
256 |
* Queue management
|
257 |
*/
|
258 |
public void sendQueueInstruction (String inst) { |
259 |
String packet = ""; |
260 |
packet += ColonetServerInterface.COLONET_QUEUE; |
261 |
packet += " " + inst;
|
262 |
packet += "\n";
|
263 |
sendString(packet); |
264 |
} |
265 |
|
266 |
public void sendQueueAdd (int pos, String data, String description) { |
267 |
String packet = ""; |
268 |
packet += ColonetServerInterface.QUEUE_ADD; |
269 |
packet += " " + pos;
|
270 |
packet += " " + data;
|
271 |
packet += " [" + description + "]"; |
272 |
packet += "\n";
|
273 |
sendQueueInstruction(packet); |
274 |
} |
275 |
|
276 |
public void sendQueueRemove (int pos) { |
277 |
String packet = ""; |
278 |
packet += ColonetServerInterface.QUEUE_REMOVE; |
279 |
packet += " " + pos;
|
280 |
packet += "\n";
|
281 |
sendQueueInstruction(packet); |
282 |
} |
283 |
|
284 |
public void sendQueueReorder (int pos1, int pos2) { |
285 |
String packet = ""; |
286 |
packet += ColonetServerInterface.QUEUE_REORDER; |
287 |
packet += " " + pos1;
|
288 |
packet += " " + pos2;
|
289 |
packet += "\n";
|
290 |
sendQueueInstruction(packet); |
291 |
} |
292 |
|
293 |
public void sendQueueUpdate () { |
294 |
sendQueueInstruction(ColonetServerInterface.QUEUE_UPDATE); |
295 |
} |
296 |
|
297 |
/**
|
298 |
* Display informational message box on the screen. Used for casual communicaton to the user.
|
299 |
* @param text Text to display
|
300 |
*/
|
301 |
public void msg (String text) { |
302 |
JOptionPane.showMessageDialog(null, text, "Colonet", JOptionPane.INFORMATION_MESSAGE); |
303 |
} |
304 |
|
305 |
/**
|
306 |
* Display warning message box on the screen. Used for minor alerts or exceptions.
|
307 |
* @param text Text to display
|
308 |
*/
|
309 |
public void warn (String text) { |
310 |
JOptionPane.showMessageDialog(null, text, "Colonet", JOptionPane.WARNING_MESSAGE); |
311 |
} |
312 |
|
313 |
/**
|
314 |
* Display error message box on the screen. Used for major errors or exceptions in the program.
|
315 |
* @param text Text to display
|
316 |
*/
|
317 |
public void err (String text) { |
318 |
JOptionPane.showMessageDialog(null, text, "Colonet", JOptionPane.ERROR_MESSAGE); |
319 |
} |
320 |
|
321 |
|
322 |
/*
|
323 |
* DataListener thread.
|
324 |
*
|
325 |
*/
|
326 |
class DataListener extends Thread { |
327 |
final int DATALISTENER_DELAY = 222; |
328 |
|
329 |
public DataListener () {
|
330 |
super("Colonet DataListener"); |
331 |
} |
332 |
|
333 |
public void run () { |
334 |
String line;
|
335 |
while (true) { |
336 |
try {
|
337 |
line = getLine(); |
338 |
if (line != null) { |
339 |
parseData(line); |
340 |
} |
341 |
Thread.sleep(DATALISTENER_DELAY);
|
342 |
} catch (InterruptedException e) { |
343 |
return;
|
344 |
} |
345 |
} |
346 |
} |
347 |
|
348 |
public void parseData (String line) { |
349 |
//log.append("Incoming data: [" + line + "]\n");
|
350 |
if (line.startsWith(ColonetServerInterface.RESPONSE_TO_CLIENT_REQUEST + " " + |
351 |
ColonetServerInterface.REQUEST_BOM_MATRIX)) |
352 |
colonet.parseMatrix(line); |
353 |
else if (line.startsWith(ColonetServerInterface.COLONET_QUEUE)) |
354 |
colonet.parseQueue(line); |
355 |
else if (line.startsWith(ColonetServerInterface.RESPONSE_TO_CLIENT_REQUEST + " " + |
356 |
ColonetServerInterface.REQUEST_XBEE_IDS)) |
357 |
colonet.parseXBeeIDs(line); |
358 |
} |
359 |
|
360 |
} |
361 |
|
362 |
} |