Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (8.69 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
         
22
        /*  COMMAND PACKET STRUCTURE
23
        1:  SEND_TO_ROBOT
24
        2:  # of robot, or GLOBAL_DEST
25
        3:  COLONET_COMMMAND
26
        4:  message code (i.e. ORB_SET)
27
        5:  any data, as many that fit in the packet
28
        
29
        REQUEST PACKET STRUCTURE
30
        1:  REQUEST_FROM_ROBOT
31
        2:  # of robot
32
        3:  COLONET_REQUEST
33
        4:  ???
34
        */
35
         
36
        public static final String SEND_TO_ROBOT = "0";
37
        public static final String REQUEST_FROM_ROBOT = "1";
38
        public static final String RESPONSE_TO_CLIENT_REQUEST = "2";
39
        public static final String GLOBAL_DEST = "200";
40

    
41
        //Message types
42
        public static final String COLONET_COMMAND = "13"; //0x0D
43
        public static final String COLONET_REQUEST = "14"; //0x0E
44
        public static final String CORONET_RESPONSE = "15"; //0x0F
45

    
46
        //Packet properties
47
        public static final int PACKET_DATA_LEN = 7;
48
        public static final int PACKET_SIZE = 16;
49

    
50
        //Packet error codes
51
        /** For timeouts. */                                                public static final String WL_ERRCODE_ROBOTDEATH = "123";     
52
        /** For pinging charging station. */                public static final String WL_ERRCODE_NEEDTOCHARGE = "250";  
53
        /** Charging station response to ping. */        public static final String WL_ERRCODE_CHARGESTATION = "251";  
54
        public static final String WL_ERR_DEAD_LOC = "5";
55
        public static final String WL_ERROR_DEST = "255";
56
        public static final String WL_ERROR_LOC = "4";
57
        
58
        public static final String WL_DEFAULT_PAN = "3332";
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
        *        INSTANCE VARIBLES
93
        */
94
        
95
        Socket socket;
96
        OutputStreamWriter out;
97
        BufferedReader reader;
98
        JTextArea log;
99
        
100
        /*
101
        *        FUNCTION IMPLEMENTATIONS
102
        */
103

    
104
        public ColonetServerInterface (JTextArea log) {
105
                this.log = log;
106
        }
107

    
108
        public Socket getSocket () {
109
                return socket;
110
        }
111
        
112
        public OutputStreamWriter getOutputStreamWriter () {
113
                return out;
114
        }
115
        
116
        public BufferedReader getBufferedReader () {
117
                return reader;
118
        }
119
        
120
        public boolean isReady () {
121
                if (socket == null || out == null || reader == null) return false;
122
                if (!socket.isConnected() || socket.isClosed() || socket.isInputShutdown() || socket.isOutputShutdown()) return false;
123
                return true;
124
        }
125
        
126
        public boolean isInputReady () {
127
                try {
128
                        if (reader.ready()) return true;
129
                } catch (Exception e) {
130
                        return false;
131
                }
132
                return false;
133
        }
134
        
135
        /**
136
         * Create socket connection to Colonet server.
137
         * If successful, start threads for loading the webcam image and listening for incoming data.
138
         */
139
        public void connect (String strHost, String strPort) {
140
                //make sure hostname and port are valid
141
                if (strHost.equals("") || strPort.equals("")) {
142
                        err("Please enter a hostname and port.");
143
                        return;
144
                }
145
                int port = 0;
146
                try {
147
                        port = Integer.parseInt(strPort);
148
                } catch (Exception e) {
149
                        err("Invalid port");
150
                        return;
151
                }
152
                
153
                //make sure we aren't already connected. if so, disconnect first.
154
                if (socket != null && socket.isConnected()) {
155
                        try {
156
                                out.close();
157
                                socket.close();
158
                        } catch (IOException e) {
159
                                log.append("Error closing socket. Reconnecting...\n");
160
                        }
161
                }
162
                
163
                try {
164
                        log.append("Attempting to connect to " + strHost + "\n");
165
                        socket = new Socket(strHost, port);
166
                } catch (UnknownHostException e) {
167
                        log.append("Unknown host exception.\n");
168
                        err("Unknown Host Exception");
169
                        return;
170
                } catch (IOException e) {
171
                        log.append("IO Exception.\n");
172
                        err("IO Exception");
173
                        return;
174
                } catch (java.security.AccessControlException e) {
175
                        log.append("Access Control Exception.\n");
176
                        err("Permission denied by java.security.AccessControlException.\n\nYou may only connect to the server from which this applet was loaded.");
177
                        return;
178
                }
179
                if (socket == null || !socket.isConnected()) {
180
                        log.append("Connection is not ready. Try connecting again.");
181
                        return;
182
                }
183
                log.append("Connected to " + strPort + " on port " + port + "\n");
184
                try {
185
                        out = new OutputStreamWriter(socket.getOutputStream());
186
                        reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
187
                } catch (IOException e) {
188
                        warn("Could not get transfer streams from socket connection.");
189
                }
190
        
191
        }
192
        
193
        /**
194
         * Sends a command code to the colonet server so that it can be redirected to one or more robots.
195
         * Assembles the given string into a packet and sends a full command string to the colonet server.
196
         * The string sent from here should be the correct form for sending directly to the robot.
197
         */
198
        public void sendData (String s, String robotNumber) {
199
                //make sure we can send
200
                if (!this.isReady()) {
201
                        warn("There was a problem with the connection. Data could not be sent.\nMake sure you are connected and try sending the command again.");
202
                        return;
203
                }
204
        
205
                //create packet
206
                String packet;
207
                packet = ColonetServerInterface.SEND_TO_ROBOT;
208
                packet += " " + robotNumber;
209
                packet += " " + ColonetServerInterface.COLONET_COMMAND;
210
                packet += " " + s;  //add  the command code here
211
                packet += "\n";
212
                
213
                //send packet
214
                try {
215
                        Thread.sleep(200);
216
                        out.write(packet);
217
                        out.flush();
218
                        log.append("Sending: " + packet);
219
                } catch (IOException e) {
220
                        log.append("Could not send data.\n");
221
                } catch (InterruptedException e) {
222
                        log.append("Thread InterruptedException in sendData\n");
223
                }
224
                
225
        }
226
        
227
        /**
228
         * Sends a request to the colonet server so that it can be redirected to one or more robots.
229
         * Assembles the given string into a packet and sends a full command string to the colonet server.
230
         * The string sent from here should be the correct form for sending directly to the robot.
231
         * @param s The request code to be sent
232
         * @see #sendData(String)
233
         */
234
        public void sendRequest (String s, String robotNumber) {
235
                //make sure we can send
236
                if (!this.isReady()) {
237
                        warn("There was a problem with the connection. Data could not be sent.\nMake sure you are connected and try sending the command again.");
238
                        return;
239
                }
240
        
241
                //get robot code to send to.
242
                /*
243
                if (cmbRobotNumber.getSelectedIndex() == 0)
244
                        robotNumber = ColonetServerInterface.GLOBAL_DEST;
245
                else
246
                        robotNumber = ( cmbRobotNumber.getSelectedIndex() - 1 ) + "";
247
                */
248
        
249
                //create packet
250
                String packet;
251
                packet = ColonetServerInterface.REQUEST_FROM_ROBOT;
252
                packet += " " + robotNumber;
253
                packet += " " + ColonetServerInterface.COLONET_REQUEST;
254
                packet += " " + s;  //add  the command code here
255
                packet += "\n";
256
                
257
                //send packet
258
                try {
259
                        Thread.sleep(200);  //paue to be safe
260
                        out.write(packet);
261
                        out.flush();
262
                        log.append("Sent: " + packet);
263
                } catch (IOException e) {
264
                        log.append("Could not send data.\n");
265
                } catch (InterruptedException e) {
266
                        log.append("Thread InterruptedException in sendData\n");
267
                }
268
                
269
        }
270

    
271

    
272
        
273
        
274
        /**
275
         * Display informational message box on the screen. Used for casual communicaton to the user.
276
         * @param text Text to display
277
         */
278
        private void msg (String text) {
279
                JOptionPane.showMessageDialog(null, text, "Colonet", JOptionPane.INFORMATION_MESSAGE);
280
        }
281
        
282
        /**
283
         * Display warning message box on the screen. Used for minor alerts or exceptions.
284
         * @param text Text to display
285
         */
286
        private void warn (String text) {
287
                JOptionPane.showMessageDialog(null, text, "Colonet", JOptionPane.WARNING_MESSAGE);
288
        }
289
        
290
        /**
291
         * Display error message box on the screen. Used for major errors or exceptions in the program.
292
         * @param text Text to display
293
         */
294
        private void err (String text) {
295
                JOptionPane.showMessageDialog(null, text, "Colonet", JOptionPane.ERROR_MESSAGE);
296
        }
297
        
298

    
299
}