root / trunk / code / projects / colonet / client / ColonetServerInterface.java @ 670
History | View | Annotate | Download (19.4 KB)
1 | 32 | gtress | /*
|
---|---|---|---|
2 | 539 | emarinel | * ColonetServerInterface.java
|
3 | * Gregory Tress
|
||
4 | 32 | gtress | */
|
5 | |||
6 | import java.net.*; |
||
7 | import java.io.*; |
||
8 | import javax.swing.JOptionPane; |
||
9 | import javax.swing.JTextArea; |
||
10 | |||
11 | 333 | gtress | /**
|
12 | * The Colonet server interface class. This class handles direct communication with the Colonet server. The general
|
||
13 | 527 | emarinel | * contract is that this class will encapsulate the process of sending commands and requests to the server and
|
14 | * receiving responses. This class should provide methods to send specific commands and requests so that the
|
||
15 | * Colonet can easily communicate with the server. When data is received from the server, the ColonetServerInterface
|
||
16 | * decides how to handle it and in many cases this means simply passing it to the Colonet.
|
||
17 | 333 | gtress | * <p>
|
18 | * The motivation is that any number of Colonet-type classes could be designed for different purposes if needed, with
|
||
19 | 527 | emarinel | * this class being used by each one for communication.
|
20 | 333 | gtress | * For instance, if an application is designed which only monitors sensor data, or that only has access to
|
21 | * the task queue, etc., the application could easily plug in to this interface.
|
||
22 | 527 | emarinel | * The Colonet should generally not contain communication-specific code or Strings of command and request data.
|
23 | 333 | gtress | * If additional commands and requests are needed for permanent implementation, it is suggested that convenience
|
24 | * classes be created in the ColonetServerInterface and called from the Colonet.
|
||
25 | *
|
||
26 | 527 | emarinel | * @author Gregory Tress
|
27 | 333 | gtress | */
|
28 | 527 | emarinel | public class ColonetServerInterface |
29 | 32 | gtress | { |
30 | 531 | emarinel | /* Old packet structure:
|
31 | 527 | emarinel | |
32 | 539 | emarinel | COMMAND PACKET STRUCTURE
|
33 | 1: SEND_TO_ROBOT
|
||
34 | 2: # of robot, or GLOBAL_DEST
|
||
35 | 3: COLONET_COMMMAND
|
||
36 | 4: message code (i.e. ORB_SET)
|
||
37 | 5: any data, as many that fit in the packet
|
||
38 | 527 | emarinel | |
39 | 32 | gtress | REQUEST PACKET STRUCTURE
|
40 | 539 | emarinel | 1: REQUEST_FROM_SERVER
|
41 | 2: # of robot
|
||
42 | 3: COLONET_REQUEST
|
||
43 | 4: ???
|
||
44 | 527 | emarinel | |
45 | 539 | emarinel | 9/12/07 New server interface structure
|
46 | Client will no longer send full robot packets to the server.
|
||
47 | Commands will be defined as necessary.
|
||
48 | 32 | gtress | */
|
49 | 527 | emarinel | |
50 | 76 | gtress | //General Colonet Interface
|
51 | 32 | gtress | public static final String SEND_TO_ROBOT = "0"; |
52 | 67 | gtress | public static final String REQUEST_FROM_SERVER = "1"; |
53 | 32 | gtress | public static final String RESPONSE_TO_CLIENT_REQUEST = "2"; |
54 | 76 | gtress | public static final String REQUEST_BOM_MATRIX = "144"; |
55 | 155 | gtress | public static final String REQUEST_XBEE_IDS = "145"; |
56 | 527 | emarinel | |
57 | 32 | gtress | public static final String COLONET_COMMAND = "13"; //0x0D |
58 | public static final String COLONET_REQUEST = "14"; //0x0E |
||
59 | public static final String CORONET_RESPONSE = "15"; //0x0F |
||
60 | 76 | gtress | public static final String GLOBAL_DEST = "200"; |
61 | 459 | gtress | public static final String CLIENT_REQUEST_ROBOT_POSITIONS = "86"; |
62 | public static final String CLIENT_ASSIGN_ROBOT_ID = "87"; |
||
63 | 665 | gtress | public static final String CLIENT_SET_VIRTUAL_WALL = "89"; |
64 | 470 | gtress | public static final String MOVE_TO_ABSOLUTE_POSITION = "83"; //0x53 |
65 | 665 | gtress | |
66 | 527 | emarinel | |
67 | 107 | gtress | //Queue instructions
|
68 | public static final String COLONET_QUEUE = "100"; |
||
69 | 527 | emarinel | public static final String QUEUE_UPDATE = "101"; |
70 | 107 | gtress | public static final String QUEUE_ADD = "102"; |
71 | public static final String QUEUE_REMOVE = "103"; |
||
72 | public static final String QUEUE_REORDER = "104"; |
||
73 | 527 | emarinel | |
74 | 32 | gtress | //Use BATTERY to request battery level
|
75 | public static final String BATTERY = "56"; //0x38 |
||
76 | 527 | emarinel | |
77 | 32 | gtress | //MOTORS
|
78 | public static final String MOTORS_INIT = "23"; //0x17 |
||
79 | public static final String MOTOR1_SET = "24"; //0x18 |
||
80 | public static final String MOTOR2_SET = "25"; //0x19 |
||
81 | public static final String MOTORS_OFF = "26"; //0x1A |
||
82 | public static final String MOVE = "27"; //0x1B |
||
83 | 39 | gtress | public static final String MOVE_AVOID = "28"; //0x1C |
84 | 586 | gtress | public static final String MOVE_R = "29"; //0x1D |
85 | public static final String MOVE_L = "30"; //0x1E |
||
86 | public static final String MOVE_F = "31"; //0x1F |
||
87 | public static final String MOVE_B = "32"; //0x1G |
||
88 | 527 | emarinel | |
89 | 32 | gtress | //BUZZER
|
90 | public static final String BUZZER_INIT = "0"; //0x00 |
||
91 | public static final String BUZZER_SET_VAL = "1"; //0x01 |
||
92 | public static final String BUZZER_SET_FREQ = "2"; //0x02 |
||
93 | public static final String BUZZER_CHIRP = "3"; //0x03 |
||
94 | public static final String BUZZER_OFF = "4"; //0x04 |
||
95 | |||
96 | //ORB
|
||
97 | public static final String ORB_INIT = "12"; //0x0C |
||
98 | public static final String ORB_SET = "13"; //0x0D |
||
99 | public static final String ORB_SET_COLOR = "14"; //0x0E |
||
100 | public static final String ORB_DISABLE = "15"; //0x0F |
||
101 | public static final String ORB_ENABLE = "16"; //0x10 |
||
102 | public static final String ORB_SET_DIO = "17"; //0x11 |
||
103 | public static final String LED_INIT = "18"; //0x12 |
||
104 | public static final String LED_USER = "19"; //0x13 |
||
105 | public static final String ORB_SET_NUM_NS = "20"; //0x14 |
||
106 | public static final String ORB_SET_NUM = "21"; //0x15 |
||
107 | public static final String ORB_SEND = "22"; //0x16 |
||
108 | |||
109 | 539 | emarinel | Colonet colonet; //save reference to the entire applet locally
|
110 | 32 | gtress | Socket socket;
|
111 | OutputStreamWriter out;
|
||
112 | BufferedReader reader;
|
||
113 | 76 | gtress | DataListener dataListener; |
114 | 527 | emarinel | |
115 | 32 | gtress | /*
|
116 | 539 | emarinel | * FUNCTION IMPLEMENTATIONS
|
117 | 32 | gtress | */
|
118 | |||
119 | 333 | gtress | /**
|
120 | * Constructs a new ColonetServerInterface. When constructing a ColonetServerInterface, a valid Colonet object
|
||
121 | 527 | emarinel | * reference must be provided to ensure that data is routed correctly.
|
122 | 333 | gtress | *
|
123 | 527 | emarinel | * @param colonet The Colonet object to save locally. This reference cannot be changed once the
|
124 | 539 | emarinel | * ColonetSreverInterface has been contsructed.
|
125 | 333 | gtress | * @throws NullPointerException if colonet is null
|
126 | *
|
||
127 | */
|
||
128 | 136 | gtress | public ColonetServerInterface (Colonet colonet) {
|
129 | this.colonet = colonet;
|
||
130 | 76 | gtress | dataListener = new DataListener();
|
131 | 32 | gtress | } |
132 | |||
133 | public Socket getSocket () { |
||
134 | return socket;
|
||
135 | } |
||
136 | 527 | emarinel | |
137 | 32 | gtress | public OutputStreamWriter getOutputStreamWriter () { |
138 | return out;
|
||
139 | } |
||
140 | 527 | emarinel | |
141 | 32 | gtress | public BufferedReader getBufferedReader () { |
142 | return reader;
|
||
143 | } |
||
144 | 527 | emarinel | |
145 | 32 | gtress | public boolean isReady () { |
146 | 333 | gtress | if (socket == null || out == null || reader == null) |
147 | return false; |
||
148 | if (!socket.isConnected() || socket.isClosed() || socket.isInputShutdown() || socket.isOutputShutdown())
|
||
149 | return false; |
||
150 | 32 | gtress | return true; |
151 | } |
||
152 | 527 | emarinel | |
153 | 32 | gtress | public boolean isInputReady () { |
154 | try {
|
||
155 | if (reader.ready()) return true; |
||
156 | } catch (Exception e) { |
||
157 | return false; |
||
158 | } |
||
159 | return false; |
||
160 | } |
||
161 | 527 | emarinel | |
162 | 420 | gtress | public String getLine () throws IOException { |
163 | return reader.readLine();
|
||
164 | 39 | gtress | } |
165 | 527 | emarinel | |
166 | 32 | gtress | /**
|
167 | * Create socket connection to Colonet server.
|
||
168 | 333 | gtress | * If successful, start thread for listening for incoming data.
|
169 | 32 | gtress | */
|
170 | public void connect (String strHost, String strPort) { |
||
171 | //make sure hostname and port are valid
|
||
172 | if (strHost.equals("") || strPort.equals("")) { |
||
173 | err("Please enter a hostname and port.");
|
||
174 | return;
|
||
175 | } |
||
176 | int port = 0; |
||
177 | try {
|
||
178 | port = Integer.parseInt(strPort);
|
||
179 | } catch (Exception e) { |
||
180 | err("Invalid port");
|
||
181 | return;
|
||
182 | } |
||
183 | 527 | emarinel | |
184 | 181 | gtress | //make sure we aren't already connected.
|
185 | 32 | gtress | if (socket != null && socket.isConnected()) { |
186 | 181 | gtress | return;
|
187 | 32 | gtress | } |
188 | 527 | emarinel | |
189 | 32 | gtress | try {
|
190 | socket = new Socket(strHost, port); |
||
191 | 420 | gtress | socket.setKeepAlive(true);
|
192 | 32 | gtress | } catch (UnknownHostException e) { |
193 | err("Unknown Host Exception");
|
||
194 | return;
|
||
195 | } catch (IOException e) { |
||
196 | 542 | emarinel | err("Failed to connect to " + strHost + ".\n"); |
197 | 32 | gtress | return;
|
198 | } catch (java.security.AccessControlException e) {
|
||
199 | 585 | gtress | err("Permission denied.\n\n"
|
200 | 542 | emarinel | + "You may only connect to the server from which this applet was loaded.");
|
201 | 32 | gtress | return;
|
202 | } |
||
203 | 542 | emarinel | |
204 | 32 | gtress | if (socket == null || !socket.isConnected()) { |
205 | return;
|
||
206 | } |
||
207 | try {
|
||
208 | out = new OutputStreamWriter(socket.getOutputStream()); |
||
209 | reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
||
210 | } catch (IOException e) { |
||
211 | warn("Could not get transfer streams from socket connection.");
|
||
212 | } |
||
213 | 542 | emarinel | |
214 | 76 | gtress | dataListener.start(); |
215 | 32 | gtress | } |
216 | 527 | emarinel | |
217 | 420 | gtress | public void disconnect () { |
218 | 542 | emarinel | try {
|
219 | if (reader != null) { |
||
220 | reader.close(); |
||
221 | 539 | emarinel | } |
222 | 542 | emarinel | if (out != null) { |
223 | out.close(); |
||
224 | } |
||
225 | 549 | gtress | colonet.disconnect(); |
226 | 542 | emarinel | } catch (IOException e) { |
227 | } |
||
228 | 420 | gtress | } |
229 | 527 | emarinel | |
230 | 107 | gtress | /*
|
231 | 517 | gtress | * Send a general String to the OutputStream.
|
232 | 107 | gtress | */
|
233 | 333 | gtress | private void sendString (String s) { |
234 | 32 | gtress | //make sure we can send
|
235 | if (!this.isReady()) { |
||
236 | 585 | gtress | colonet.getInfoPanel().append("Could not send data.\n");
|
237 | 32 | gtress | return;
|
238 | } |
||
239 | //send packet
|
||
240 | try {
|
||
241 | 590 | gtress | Thread.sleep(100); //pause to be safe |
242 | 107 | gtress | out.write(s); |
243 | 32 | gtress | out.flush(); |
244 | } catch (IOException e) { |
||
245 | 585 | gtress | colonet.getInfoPanel().setText("Could not send data.\n");
|
246 | 107 | gtress | } catch (InterruptedException e) { |
247 | 67 | gtress | } |
248 | 32 | gtress | } |
249 | 527 | emarinel | |
250 | 333 | gtress | /**
|
251 | * General send-to-server method. This method is used by other command methods, which are usually convenience
|
||
252 | 527 | emarinel | * methods that simply specify arguments to this method. A command consists of a String which holds integers
|
253 | * separated by spaces. This method should not be used directly unless you know the format of the particular
|
||
254 | * command you are sending. If implementing a particular command for permanent use, it is recommended that
|
||
255 | 333 | gtress | * you create a new wrapper method specific to that command in the ColonetServerInterface file.
|
256 | *
|
||
257 | * Note that no checking is performed in this method to ensure the correct formatting of the String arguments.
|
||
258 | * If malformed commands or robot numbers are specified, the behavior of the request at the server will be
|
||
259 | 527 | emarinel | * undefined and could result in server failure.
|
260 | 333 | gtress | *
|
261 | * @param s The command String in its correct format. The format of a command String is ultimately specified
|
||
262 | 527 | emarinel | * by the Colonet server application and may change.
|
263 | * @param robotNumber The number of the robot that is the subject of the command, if any. The robot number
|
||
264 | * is specified as a single integer in a String. If the command does not have a single robot subject, this
|
||
265 | * argument can be null or an empty String, whichever is convenient.
|
||
266 | 333 | gtress | */
|
267 | 527 | emarinel | public void sendData (String s, String robotNumber) { |
268 | 107 | gtress | //create packet
|
269 | String packet = ""; |
||
270 | packet += ColonetServerInterface.SEND_TO_ROBOT; |
||
271 | 333 | gtress | if (robotNumber != null) |
272 | 517 | gtress | packet += " " + robotNumber;
|
273 | 107 | gtress | packet += " " + ColonetServerInterface.COLONET_COMMAND;
|
274 | 539 | emarinel | packet += " " + s; //add the command code here |
275 | 107 | gtress | packet += "\n";
|
276 | sendString(packet); |
||
277 | 585 | gtress | //colonet.getInfoPanel().append("S:" + packet);
|
278 | 107 | gtress | } |
279 | 527 | emarinel | |
280 | 333 | gtress | /**
|
281 | * General request-from-server method. This method is used by other request methods, which are usually convenience
|
||
282 | 527 | emarinel | * methods that simply specify arguments to this method. A request consists of a String which holds integers
|
283 | * separated by spaces. This method should not be used directly unless you know the format of the particular
|
||
284 | 333 | gtress | * request you are making. If implementing a particular request, it is recommended that you create a new method
|
285 | * specific to that request in the ColonetServerInterface file.
|
||
286 | *
|
||
287 | */
|
||
288 | private void sendRequest (String s, String robotNumber) { |
||
289 | 32 | gtress | //create packet
|
290 | 107 | gtress | String packet = ""; |
291 | packet += ColonetServerInterface.REQUEST_FROM_SERVER; |
||
292 | 270 | gtress | packet += " " + robotNumber;
|
293 | 539 | emarinel | packet += " " + s; //add the command code here |
294 | 32 | gtress | packet += "\n";
|
295 | 107 | gtress | sendString(packet); |
296 | 32 | gtress | } |
297 | 527 | emarinel | |
298 | /**
|
||
299 | * Sends a request to the server to report the entire BOM sensor matrix. The server will reply at its convenience.
|
||
300 | 333 | gtress | * No guarantee is made (end-to-end or otherwise) that the server will respond in a timely manner or at all
|
301 | * to any individual request.
|
||
302 | */
|
||
303 | 107 | gtress | public void sendSensorDataRequest () { |
304 | sendRequest(ColonetServerInterface.REQUEST_BOM_MATRIX, "");
|
||
305 | } |
||
306 | 527 | emarinel | |
307 | 333 | gtress | /**
|
308 | 527 | emarinel | * Sends a request to the server to report a list of XBee IDs. The server will reply at its convenience.
|
309 | 333 | gtress | * The purpose of having this list is to ensure that robots are properly identified for control purposes.
|
310 | 527 | emarinel | * This keeps robot identification consistent between sessions and prevents arbitrary assignment.
|
311 | 333 | gtress | * No guarantee is made (end-to-end or otherwise) that the server will respond in a timely manner or at all
|
312 | * to any individual request.
|
||
313 | *
|
||
314 | * @see Colonet#parseXBeeIDs(String)
|
||
315 | */
|
||
316 | 136 | gtress | public void sendXBeeIDRequest () { |
317 | sendRequest(ColonetServerInterface.REQUEST_XBEE_IDS, "");
|
||
318 | } |
||
319 | 527 | emarinel | |
320 | 333 | gtress | /**
|
321 | 527 | emarinel | * Sends a battery request for a specific robot to the server. The server will reply at its convenience.
|
322 | * Behavior is undefined if an invalid robot number is specified. The server will probably not respond in
|
||
323 | 333 | gtress | * this case.
|
324 | * No guarantee is made (end-to-end or otherwise) that the server will respond in a timely manner or at all
|
||
325 | * to any individual request.
|
||
326 | *
|
||
327 | * @param robotNum The number of the robot for which we are requesting the battery. Note that this value
|
||
328 | * is sent as-is to the server. No mapping to or from XBee IDs is performed. The contract for this is
|
||
329 | * currently undefined.
|
||
330 | * @see Colonet#parseBattery(String)
|
||
331 | */
|
||
332 | 270 | gtress | public void sendBatteryRequest (int robotNum) { |
333 | 425 | gtress | //create packet
|
334 | String packet = ""; |
||
335 | packet += ColonetServerInterface.SEND_TO_ROBOT; |
||
336 | packet += " " + robotNum;
|
||
337 | packet += " " + ColonetServerInterface.COLONET_REQUEST;
|
||
338 | 539 | emarinel | packet += " " + ColonetServerInterface.BATTERY; //add the command code here |
339 | 425 | gtress | packet += "\n";
|
340 | sendString(packet); |
||
341 | 567 | gtress | //System.out.println("Sent battery request: " + packet);
|
342 | 270 | gtress | } |
343 | 527 | emarinel | |
344 | 459 | gtress | /**
|
345 | * Requests a list of all robot positions and correspondind robot IDs.
|
||
346 | */
|
||
347 | public void sendPositionRequest () { |
||
348 | sendRequest(ColonetServerInterface.CLIENT_REQUEST_ROBOT_POSITIONS, "");
|
||
349 | } |
||
350 | 527 | emarinel | |
351 | 459 | gtress | /**
|
352 | * Send a robot ID assignment update to store on the server.
|
||
353 | */
|
||
354 | public void sendIDAssignment (int oldID, int newID) { |
||
355 | String packet = ""; |
||
356 | packet += ColonetServerInterface.REQUEST_FROM_SERVER; |
||
357 | 522 | gtress | packet += " " + CLIENT_ASSIGN_ROBOT_ID;
|
358 | 459 | gtress | packet += " " + oldID + " " + newID; |
359 | packet += "\n";
|
||
360 | sendString(packet); |
||
361 | } |
||
362 | 527 | emarinel | |
363 | /**
|
||
364 | 665 | gtress | * Order a robot to move to an absolute coordinate point.
|
365 | */
|
||
366 | 470 | gtress | public void sendAbsoluteMove (int id, int x, int y) { |
367 | 648 | emarinel | int x_high = (x>>8) & 0xff; |
368 | int x_low = x & 0xff; |
||
369 | int y_high = (y>>8) & 0xff; |
||
370 | int y_low = y & 0xff; |
||
371 | |||
372 | sendData(MOVE_TO_ABSOLUTE_POSITION + " " + x_high + " " + x_low + " " + y_high + " " + y_low, "" + id); |
||
373 | 470 | gtress | } |
374 | 665 | gtress | |
375 | 670 | gtress | /**
|
376 | * Establish a boundary for robot motion.
|
||
377 | */
|
||
378 | public void sendBoundary (int x1, int y1, int x2, int y2) { |
||
379 | String packet = ""; |
||
380 | 665 | gtress | packet += ColonetServerInterface.REQUEST_FROM_SERVER; |
381 | packet += " " + CLIENT_SET_VIRTUAL_WALL;
|
||
382 | 670 | gtress | packet += " " + x1 + " " + y1 + " " + x2 + " " + y2; |
383 | packet += "\n";
|
||
384 | sendString(packet); |
||
385 | } |
||
386 | 527 | emarinel | |
387 | 107 | gtress | /*
|
388 | 539 | emarinel | * Queue management
|
389 | 107 | gtress | */
|
390 | 333 | gtress | private void sendQueueInstruction (String inst) { |
391 | 107 | gtress | String packet = ""; |
392 | packet += ColonetServerInterface.COLONET_QUEUE; |
||
393 | packet += " " + inst;
|
||
394 | packet += "\n";
|
||
395 | sendString(packet); |
||
396 | } |
||
397 | 527 | emarinel | |
398 | 333 | gtress | /**
|
399 | 527 | emarinel | * Notifies the Colonet server to add a task to the current task queue. The Colonet server holds the canonical
|
400 | 333 | gtress | * task queue. Any applet can send tasks to add to the queue. Local copies of the queue in the applet are for
|
401 | * display purposes only, and are not authoritative when adding tasks. All clients send additions asynchronously,
|
||
402 | 527 | emarinel | * and as a result no guarantee is made that a task will be added in the specificed location in the queue or at
|
403 | 333 | gtress | * all. If an invalid position is specified, the task will probably not be added to the queue and may cause
|
404 | * server failure. Due to the asynchronous nature of the queue, we cannot easily account for concurrent
|
||
405 | * modification failures on the client side.
|
||
406 | *
|
||
407 | 527 | emarinel | * @param pos The position in the queue at which we would like the task to be placed. Note that this is not
|
408 | 333 | gtress | * guaranteed. Invalid positions may cause the task to be discarded at the server.
|
409 | 527 | emarinel | * @param data The String containing the command code(s) for the task and any arguments necessary to fully
|
410 | 333 | gtress | * define the behavior of the task. This format is currently not specified. In the future, the canonical format
|
411 | * of the data String will ultimately be defined by the Colonet server.
|
||
412 | * @param description A String that contains a description of the task. This will be the message displayed to
|
||
413 | 527 | emarinel | * the user when information about the task is requested.
|
414 | 333 | gtress | */
|
415 | 107 | gtress | public void sendQueueAdd (int pos, String data, String description) { |
416 | String packet = ""; |
||
417 | packet += ColonetServerInterface.QUEUE_ADD; |
||
418 | packet += " " + pos;
|
||
419 | packet += " " + data;
|
||
420 | packet += " [" + description + "]"; |
||
421 | packet += "\n";
|
||
422 | sendQueueInstruction(packet); |
||
423 | } |
||
424 | 527 | emarinel | |
425 | 333 | gtress | /**
|
426 | 527 | emarinel | * Notifies the Colonet server to remove a task from the current task queue. The Colonet server holds the canonical
|
427 | 333 | gtress | * task queue. Any applet can remove a task from the queue. Local copies of the queue in the applet are for
|
428 | * display purposes only, and are not authoritative when removing tasks. All clients remove tasks asynchronously,
|
||
429 | 527 | emarinel | * and as a result no guarantee is made that the correct task will actually be removed.
|
430 | 333 | gtress | * If an invalid position is specified, the state of the queue is undefined and server failure may result.
|
431 | * Due to the asynchronous nature of the queue, we cannot easily account for concurrent
|
||
432 | * modification failures on the client side.
|
||
433 | *
|
||
434 | 527 | emarinel | * @param pos The position in the queue at which we would like the task to be removed. Note that this is not
|
435 | 333 | gtress | * guaranteed. Invalid positions may result in a corrupted queue.
|
436 | */
|
||
437 | 107 | gtress | public void sendQueueRemove (int pos) { |
438 | String packet = ""; |
||
439 | packet += ColonetServerInterface.QUEUE_REMOVE; |
||
440 | packet += " " + pos;
|
||
441 | packet += "\n";
|
||
442 | sendQueueInstruction(packet); |
||
443 | } |
||
444 | 32 | gtress | |
445 | 333 | gtress | /**
|
446 | 527 | emarinel | * Notifies the Colonet server to reorder tasks in the current task queue. The Colonet server holds the canonical
|
447 | 333 | gtress | * task queue. Any applet can reorder tasks in the queue. Local copies of the queue in the applet are for
|
448 | * display purposes only, and are not authoritative when reordering tasks. All clients reorder tasks asynchronously,
|
||
449 | 527 | emarinel | * and as a result no guarantee is made that the correct tasks will actually be reordered.
|
450 | 333 | gtress | * If an invalid position is specified, the state of the queue is undefined and server failure may result.
|
451 | * Due to the asynchronous nature of the queue, we cannot easily account for concurrent
|
||
452 | * modification failures on the client side.
|
||
453 | *
|
||
454 | * @param pos1 The queue position of a task which we would like to reorder.
|
||
455 | * @param pos2 The queue position of a task which we would like to reorder.
|
||
456 | */
|
||
457 | 107 | gtress | public void sendQueueReorder (int pos1, int pos2) { |
458 | String packet = ""; |
||
459 | packet += ColonetServerInterface.QUEUE_REORDER; |
||
460 | packet += " " + pos1;
|
||
461 | packet += " " + pos2;
|
||
462 | packet += "\n";
|
||
463 | sendQueueInstruction(packet); |
||
464 | } |
||
465 | 32 | gtress | |
466 | 136 | gtress | public void sendQueueUpdate () { |
467 | sendQueueInstruction(ColonetServerInterface.QUEUE_UPDATE); |
||
468 | } |
||
469 | 527 | emarinel | |
470 | 32 | gtress | /**
|
471 | * Display informational message box on the screen. Used for casual communicaton to the user.
|
||
472 | * @param text Text to display
|
||
473 | */
|
||
474 | 35 | gtress | public void msg (String text) { |
475 | 470 | gtress | JOptionPane.showMessageDialog(colonet, text, "Colonet", JOptionPane.INFORMATION_MESSAGE); |
476 | 32 | gtress | } |
477 | 527 | emarinel | |
478 | 32 | gtress | /**
|
479 | * Display warning message box on the screen. Used for minor alerts or exceptions.
|
||
480 | * @param text Text to display
|
||
481 | */
|
||
482 | 35 | gtress | public void warn (String text) { |
483 | 470 | gtress | JOptionPane.showMessageDialog(colonet, text, "Colonet", JOptionPane.WARNING_MESSAGE); |
484 | 32 | gtress | } |
485 | 527 | emarinel | |
486 | 32 | gtress | /**
|
487 | * Display error message box on the screen. Used for major errors or exceptions in the program.
|
||
488 | * @param text Text to display
|
||
489 | */
|
||
490 | 35 | gtress | public void err (String text) { |
491 | 470 | gtress | JOptionPane.showMessageDialog(colonet, text, "Colonet", JOptionPane.ERROR_MESSAGE); |
492 | 32 | gtress | } |
493 | 527 | emarinel | |
494 | |||
495 | 76 | gtress | /*
|
496 | 539 | emarinel | * DataListener thread.
|
497 | 76 | gtress | *
|
498 | */
|
||
499 | class DataListener extends Thread { |
||
500 | 545 | gtress | final int DATALISTENER_DELAY = 122; |
501 | 527 | emarinel | |
502 | 76 | gtress | public DataListener () {
|
503 | super("Colonet DataListener"); |
||
504 | } |
||
505 | 527 | emarinel | |
506 | 76 | gtress | public void run () { |
507 | String line;
|
||
508 | 527 | emarinel | while (true) { |
509 | 76 | gtress | try {
|
510 | 420 | gtress | line = reader.readLine(); |
511 | 539 | emarinel | if (line == null) { |
512 | throw new IOException(); |
||
513 | } |
||
514 | 420 | gtress | parseData(line); |
515 | 76 | gtress | Thread.sleep(DATALISTENER_DELAY);
|
516 | } catch (InterruptedException e) { |
||
517 | return;
|
||
518 | 420 | gtress | } catch (IOException e) { |
519 | 539 | emarinel | disconnect(); |
520 | return;
|
||
521 | 420 | gtress | } |
522 | 76 | gtress | } |
523 | } |
||
524 | 527 | emarinel | |
525 | 76 | gtress | public void parseData (String line) { |
526 | 333 | gtress | // Sensor Matrix
|
527 | 136 | gtress | if (line.startsWith(ColonetServerInterface.RESPONSE_TO_CLIENT_REQUEST + " " + |
528 | ColonetServerInterface.REQUEST_BOM_MATRIX)) |
||
529 | colonet.parseMatrix(line); |
||
530 | 270 | gtress | // Task Queue
|
531 | 155 | gtress | else if (line.startsWith(ColonetServerInterface.COLONET_QUEUE)) |
532 | 136 | gtress | colonet.parseQueue(line); |
533 | 270 | gtress | // XBee IDs
|
534 | 155 | gtress | else if (line.startsWith(ColonetServerInterface.RESPONSE_TO_CLIENT_REQUEST + " " + |
535 | 136 | gtress | ColonetServerInterface.REQUEST_XBEE_IDS)) |
536 | colonet.parseXBeeIDs(line); |
||
537 | 270 | gtress | // Battery
|
538 | else if (line.startsWith(ColonetServerInterface.RESPONSE_TO_CLIENT_REQUEST + " " + |
||
539 | ColonetServerInterface.BATTERY)) |
||
540 | colonet.parseBattery(line); |
||
541 | 459 | gtress | // Robot Positions
|
542 | else if (line.startsWith(ColonetServerInterface.RESPONSE_TO_CLIENT_REQUEST + " " + |
||
543 | ColonetServerInterface.CLIENT_REQUEST_ROBOT_POSITIONS)) |
||
544 | colonet.parsePositions(line); |
||
545 | 333 | gtress | // Unknown type
|
546 | 510 | gtress | else {
|
547 | 567 | gtress | System.out.println("Got unknown data: " + line + "\n"); |
548 | 510 | gtress | } |
549 | 76 | gtress | } |
550 | } |
||
551 | 420 | gtress | } |