Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / ColonetGUI / ColonetServerInterface.java @ 172

History | View | Annotate | Download (9.74 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. if so, disconnect first.
169
                if (socket != null && socket.isConnected()) {
170
                        try {
171
                                out.close();
172
                                socket.close();
173
                        } catch (IOException e) {
174
                                log.append("Error closing socket. Reconnecting...\n");
175
                        }
176
                }
177
                
178
                try {
179
                        socket = new Socket(strHost, port);
180
                } catch (UnknownHostException e) {
181
                        log.append("Unknown host exception.\n");
182
                        err("Unknown Host Exception");
183
                        return;
184
                } catch (IOException e) {
185
                        log.append("IO Exception.\n");
186
                        err("IO Exception");
187
                        return;
188
                } catch (java.security.AccessControlException e) {
189
                        log.append("Access Control Exception.\n");
190
                        err("Permission denied by java.security.AccessControlException.\n\n"
191
                                +"You may only connect to the server from which this applet was loaded.");
192
                        return;
193
                }
194
                if (socket == null || !socket.isConnected()) {
195
                        log.append("Connection is not ready. Try connecting again.");
196
                        return;
197
                }
198
                try {
199
                        out = new OutputStreamWriter(socket.getOutputStream());
200
                        reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
201
                } catch (IOException e) {
202
                        warn("Could not get transfer streams from socket connection.");
203
                }
204
                dataListener.start();
205
        
206
        }
207
        
208
        /*
209
        *        sendString - only this method should actually write data to the output stream
210
        */
211
        public void sendString (String s) {
212
                //make sure we can send
213
                if (!this.isReady()) {
214
                        log.setText("Could not send data.\n");
215
                        return;
216
                }
217
                //send packet
218
                try {
219
                        Thread.sleep(10);  //pause to be safe
220
                        out.write(s);
221
                        out.flush();
222
                        log.setText("Sent: " + s);
223
                } catch (IOException e) {
224
                        log.setText("Could not send data.\n");
225
                } catch (InterruptedException e) {
226
                        log.setText("Thread InterruptedException in sendData\n");
227
                }
228
        }
229
        
230
        public void sendData (String s, String robotNumber) {        
231
                //create packet
232
                String packet = "";
233
                packet += ColonetServerInterface.SEND_TO_ROBOT;
234
                packet += " " + robotNumber;
235
                packet += " " + ColonetServerInterface.COLONET_COMMAND;
236
                packet += " " + s;  //add  the command code here
237
                packet += "\n";
238
                sendString(packet);
239
        }
240
        
241
        public void sendRequest (String s, String robotNumber) {
242
                //create packet
243
                String packet = "";
244
                packet += ColonetServerInterface.REQUEST_FROM_SERVER;
245
                //packet += " " + robotNumber;
246
                //packet += " " + ColonetServerInterface.COLONET_REQUEST;
247
                packet += " " + s;  //add  the command code here
248
                packet += "\n";
249
                sendString(packet);
250
        }
251
        
252
        public void sendSensorDataRequest () {
253
                sendRequest(ColonetServerInterface.REQUEST_BOM_MATRIX, "");
254
        }
255
        
256
        public void sendXBeeIDRequest () {
257
                sendRequest(ColonetServerInterface.REQUEST_XBEE_IDS, "");
258
        }
259
        
260
        /*
261
        *        Queue management
262
        */
263
        public void sendQueueInstruction (String inst) {
264
                String packet = "";
265
                packet += ColonetServerInterface.COLONET_QUEUE;
266
                packet += " " + inst;
267
                packet += "\n";
268
                sendString(packet);
269
        }
270
        
271
        public void sendQueueAdd (int pos, String data, String description) {
272
                String packet = "";
273
                packet += ColonetServerInterface.QUEUE_ADD;
274
                packet += " " + pos;
275
                packet += " " + data;
276
                packet += " [" + description + "]";
277
                packet += "\n";
278
                sendQueueInstruction(packet);
279
        }
280
        
281
        public void sendQueueRemove (int pos) {
282
                String packet = "";
283
                packet += ColonetServerInterface.QUEUE_REMOVE;
284
                packet += " " + pos;
285
                packet += "\n";
286
                sendQueueInstruction(packet);
287
        }
288

    
289
        public void sendQueueReorder (int pos1, int pos2) {
290
                String packet = "";
291
                packet += ColonetServerInterface.QUEUE_REORDER;
292
                packet += " " + pos1;
293
                packet += " " + pos2;
294
                packet += "\n";
295
                sendQueueInstruction(packet);
296
        }
297

    
298
        public void sendQueueUpdate () {
299
                sendQueueInstruction(ColonetServerInterface.QUEUE_UPDATE);
300
        }
301
        
302
        /**
303
         * Display informational message box on the screen. Used for casual communicaton to the user.
304
         * @param text Text to display
305
         */
306
        public void msg (String text) {
307
                JOptionPane.showMessageDialog(null, text, "Colonet", JOptionPane.INFORMATION_MESSAGE);
308
        }
309
        
310
        /**
311
         * Display warning message box on the screen. Used for minor alerts or exceptions.
312
         * @param text Text to display
313
         */
314
        public void warn (String text) {
315
                JOptionPane.showMessageDialog(null, text, "Colonet", JOptionPane.WARNING_MESSAGE);
316
        }
317
        
318
        /**
319
         * Display error message box on the screen. Used for major errors or exceptions in the program.
320
         * @param text Text to display
321
         */
322
        public void err (String text) {
323
                JOptionPane.showMessageDialog(null, text, "Colonet", JOptionPane.ERROR_MESSAGE);
324
        }
325
        
326
                
327
        /*
328
        *        DataListener thread.
329
        *
330
        */
331
        class DataListener extends Thread {
332
                final int DATALISTENER_DELAY = 222;
333
                
334
                public DataListener () {
335
                        super("Colonet DataListener");
336
                }
337
                
338
                public void run () {
339
                        String line;
340
                        while (true) { 
341
                                try {
342
                                        line = getLine();
343
                                        if (line != null) {
344
                                                parseData(line);
345
                                        }
346
                                        Thread.sleep(DATALISTENER_DELAY);
347
                                } catch (InterruptedException e) {
348
                                        return;
349
                                } 
350
                        }
351
                }
352
                
353
                public void parseData (String line) {
354
                        //log.append("Incoming data: [" + line + "]\n");
355
                        if (line.startsWith(ColonetServerInterface.RESPONSE_TO_CLIENT_REQUEST + " " +
356
                                ColonetServerInterface.REQUEST_BOM_MATRIX))
357
                                colonet.parseMatrix(line);
358
                        else if (line.startsWith(ColonetServerInterface.COLONET_QUEUE))
359
                                colonet.parseQueue(line);
360
                        else if (line.startsWith(ColonetServerInterface.RESPONSE_TO_CLIENT_REQUEST + " " +
361
                                ColonetServerInterface.REQUEST_XBEE_IDS))
362
                                colonet.parseXBeeIDs(line);
363
                }
364

    
365
        }
366

    
367
}