Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (8.28 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
        public static final String SEND_TO_ROBOT = "0";
42
        public static final String REQUEST_FROM_SERVER = "1";
43
        public static final String RESPONSE_TO_CLIENT_REQUEST = "2";
44
        public static final String GLOBAL_DEST = "200";
45

    
46
        //Message types
47
        public static final String COLONET_COMMAND = "13"; //0x0D
48
        public static final String COLONET_REQUEST = "14"; //0x0E
49
        public static final String CORONET_RESPONSE = "15"; //0x0F
50
        
51
        //Use BATTERY to request battery level
52
        public static final String BATTERY = "56"; //0x38
53
        
54
        //MOTORS
55
        public static final String MOTORS_INIT = "23"; //0x17
56
        public static final String MOTOR1_SET = "24"; //0x18
57
        public static final String MOTOR2_SET = "25"; //0x19
58
        public static final String MOTORS_OFF = "26"; //0x1A
59
        public static final String MOVE = "27"; //0x1B
60
        public static final String MOVE_AVOID = "28"; //0x1C
61
        
62
        //BUZZER
63
        public static final String BUZZER_INIT = "0"; //0x00
64
        public static final String BUZZER_SET_VAL = "1"; //0x01
65
        public static final String BUZZER_SET_FREQ = "2"; //0x02
66
        public static final String BUZZER_CHIRP = "3"; //0x03
67
        public static final String BUZZER_OFF = "4"; //0x04
68

    
69
        //ORB
70
        public static final String ORB_INIT = "12"; //0x0C
71
        public static final String ORB_SET = "13"; //0x0D
72
        public static final String ORB_SET_COLOR = "14"; //0x0E
73
        public static final String ORB_DISABLE = "15"; //0x0F
74
        public static final String ORB_ENABLE = "16"; //0x10
75
        public static final String ORB_SET_DIO = "17"; //0x11
76
        public static final String LED_INIT = "18"; //0x12
77
        public static final String LED_USER = "19"; //0x13
78
        public static final String ORB_SET_NUM_NS = "20"; //0x14
79
        public static final String ORB_SET_NUM = "21"; //0x15
80
        public static final String ORB_SEND = "22"; //0x16
81

    
82
        
83
        Socket socket;
84
        OutputStreamWriter out;
85
        BufferedReader reader;
86
        JTextArea log;
87
        
88
        /*
89
        *        FUNCTION IMPLEMENTATIONS
90
        */
91

    
92
        public ColonetServerInterface (JTextArea log) {
93
                this.log = log;
94
        }
95

    
96
        public Socket getSocket () {
97
                return socket;
98
        }
99
        
100
        public OutputStreamWriter getOutputStreamWriter () {
101
                return out;
102
        }
103
        
104
        public BufferedReader getBufferedReader () {
105
                return reader;
106
        }
107
        
108
        public boolean isReady () {
109
                if (socket == null || out == null || reader == null) return false;
110
                if (!socket.isConnected() || socket.isClosed() || socket.isInputShutdown() || socket.isOutputShutdown()) return false;
111
                return true;
112
        }
113
        
114
        public boolean isInputReady () {
115
                try {
116
                        if (reader.ready()) return true;
117
                } catch (Exception e) {
118
                        return false;
119
                }
120
                return false;
121
        }
122
        
123
        public String getLine () {
124
                if (this.isReady()) {
125
                        try {
126
                                return reader.readLine();
127
                        } catch (IOException e) {
128
                                return null;
129
                        }
130
                } else {
131
                  return null;
132
                }
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\n"
177
                                +"You may only connect to the server from which this applet was loaded.");
178
                        return;
179
                }
180
                if (socket == null || !socket.isConnected()) {
181
                        log.append("Connection is not ready. Try connecting again.");
182
                        return;
183
                }
184
                log.append("Connected to " + strPort + " on port " + port + "\n");
185
                try {
186
                        out = new OutputStreamWriter(socket.getOutputStream());
187
                        reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
188
                } catch (IOException e) {
189
                        warn("Could not get transfer streams from socket connection.");
190
                }
191
        
192
        }
193
        
194
        /**
195
         * Sends a command code to the colonet server so that it can be redirected to one or more robots.
196
         * Assembles the given string into a packet and sends a full command string to the colonet server.
197
         * The string sent from here should be the correct form for sending directly to the robot.
198
         */
199
        public void sendData (String s, String robotNumber) {
200
                //make sure we can send
201
                if (!this.isReady()) {
202
                        warn("There was a problem with the connection. Data could not be sent.\n"
203
                                +"Make sure you are connected and try sending the command again.");
204
                        return;
205
                }
206
        
207
                //create packet
208
                String packet;
209
                packet = ColonetServerInterface.SEND_TO_ROBOT;
210
                packet += " " + robotNumber;
211
                packet += " " + ColonetServerInterface.COLONET_COMMAND;
212
                packet += " " + s;  //add  the command code here
213
                packet += "\n";
214
                
215
                //send packet
216
                try {
217
                        log.append("Sending: " + packet);
218
                        out.write(packet);
219
                        out.flush();
220
                } catch (IOException e) {
221
                        log.append("Could not send data.\n");
222
                }
223
        }
224
        
225
        /**
226
         * Sends a request to the colonet server so that it can be redirected to one or more robots.
227
         * Assembles the given string into a packet and sends a full command string to the colonet server.
228
         * The string sent from here should be the correct form for sending directly to the robot.
229
         * @param s The request code to be sent
230
         * @see #sendData(String)
231
         */
232
        public void sendRequest (String s, String robotNumber) {
233
                //make sure we can send
234
                if (!this.isReady()) {
235
                        warn("There was a problem with the connection. Data could not be sent.\nMake sure you are connected and try sending the command again.");
236
                        return;
237
                }
238
        
239
                //get robot code to send to.
240
                /*
241
                if (cmbRobotNumber.getSelectedIndex() == 0)
242
                        robotNumber = ColonetServerInterface.GLOBAL_DEST;
243
                else
244
                        robotNumber = ( cmbRobotNumber.getSelectedIndex() - 1 ) + "";
245
                */
246
        
247
                //create packet
248
                String packet;
249
                packet = ColonetServerInterface.REQUEST_FROM_SERVER;
250
                packet += " " + robotNumber;
251
                packet += " " + ColonetServerInterface.COLONET_REQUEST;
252
                packet += " " + s;  //add  the command code here
253
                packet += "\n";
254
                
255
                //send packet
256
                try {
257
                        Thread.sleep(200);  //pause to be safe
258
                        out.write(packet);
259
                        out.flush();
260
                        log.append("Sent: " + packet);
261
                } catch (IOException e) {
262
                        log.append("Could not send data.\n");
263
                } catch (InterruptedException e) {
264
                        log.append("Thread InterruptedException in sendData\n");
265
                }
266
                
267
        }
268

    
269

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

    
297
}