Project

General

Profile

Statistics
| Revision:

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

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

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

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

    
367
        }
368

    
369
}