root / trunk / code / projects / colonet / ColonetGUI / ColonetServerInterface.java @ 39
History | View | Annotate | Download (8.24 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 |
COMMAND PACKET STRUCTURE
|
22 |
1: SEND_TO_ROBOT
|
23 |
2: # of robot, or GLOBAL_DEST
|
24 |
3: COLONET_COMMMAND
|
25 |
4: message code (i.e. ORB_SET)
|
26 |
5: any data, as many that fit in the packet
|
27 |
|
28 |
REQUEST PACKET STRUCTURE
|
29 |
1: REQUEST_FROM_ROBOT
|
30 |
2: # of robot
|
31 |
3: COLONET_REQUEST
|
32 |
4: ???
|
33 |
|
34 |
9/12/07 New server interface structure
|
35 |
Client will no longer send full robot packets to the server.
|
36 |
Commands will be defined as necessary.
|
37 |
*/
|
38 |
|
39 |
public static final String SEND_TO_ROBOT = "0"; |
40 |
public static final String REQUEST_FROM_ROBOT = "1"; |
41 |
public static final String RESPONSE_TO_CLIENT_REQUEST = "2"; |
42 |
public static final String GLOBAL_DEST = "200"; |
43 |
|
44 |
//Message types
|
45 |
public static final String COLONET_COMMAND = "13"; //0x0D |
46 |
public static final String COLONET_REQUEST = "14"; //0x0E |
47 |
public static final String CORONET_RESPONSE = "15"; //0x0F |
48 |
|
49 |
//Use BATTERY to request battery level
|
50 |
public static final String BATTERY = "56"; //0x38 |
51 |
|
52 |
//MOTORS
|
53 |
public static final String MOTORS_INIT = "23"; //0x17 |
54 |
public static final String MOTOR1_SET = "24"; //0x18 |
55 |
public static final String MOTOR2_SET = "25"; //0x19 |
56 |
public static final String MOTORS_OFF = "26"; //0x1A |
57 |
public static final String MOVE = "27"; //0x1B |
58 |
public static final String MOVE_AVOID = "28"; //0x1C |
59 |
|
60 |
//BUZZER
|
61 |
public static final String BUZZER_INIT = "0"; //0x00 |
62 |
public static final String BUZZER_SET_VAL = "1"; //0x01 |
63 |
public static final String BUZZER_SET_FREQ = "2"; //0x02 |
64 |
public static final String BUZZER_CHIRP = "3"; //0x03 |
65 |
public static final String BUZZER_OFF = "4"; //0x04 |
66 |
|
67 |
//ORB
|
68 |
public static final String ORB_INIT = "12"; //0x0C |
69 |
public static final String ORB_SET = "13"; //0x0D |
70 |
public static final String ORB_SET_COLOR = "14"; //0x0E |
71 |
public static final String ORB_DISABLE = "15"; //0x0F |
72 |
public static final String ORB_ENABLE = "16"; //0x10 |
73 |
public static final String ORB_SET_DIO = "17"; //0x11 |
74 |
public static final String LED_INIT = "18"; //0x12 |
75 |
public static final String LED_USER = "19"; //0x13 |
76 |
public static final String ORB_SET_NUM_NS = "20"; //0x14 |
77 |
public static final String ORB_SET_NUM = "21"; //0x15 |
78 |
public static final String ORB_SEND = "22"; //0x16 |
79 |
|
80 |
|
81 |
Socket socket;
|
82 |
OutputStreamWriter out;
|
83 |
BufferedReader reader;
|
84 |
JTextArea log;
|
85 |
|
86 |
/*
|
87 |
* FUNCTION IMPLEMENTATIONS
|
88 |
*/
|
89 |
|
90 |
public ColonetServerInterface (JTextArea log) { |
91 |
this.log = log;
|
92 |
} |
93 |
|
94 |
public Socket getSocket () { |
95 |
return socket;
|
96 |
} |
97 |
|
98 |
public OutputStreamWriter getOutputStreamWriter () { |
99 |
return out;
|
100 |
} |
101 |
|
102 |
public BufferedReader getBufferedReader () { |
103 |
return reader;
|
104 |
} |
105 |
|
106 |
public boolean isReady () { |
107 |
if (socket == null || out == null || reader == null) return false; |
108 |
if (!socket.isConnected() || socket.isClosed() || socket.isInputShutdown() || socket.isOutputShutdown()) return false; |
109 |
return true; |
110 |
} |
111 |
|
112 |
public boolean isInputReady () { |
113 |
try {
|
114 |
if (reader.ready()) return true; |
115 |
} catch (Exception e) { |
116 |
return false; |
117 |
} |
118 |
return false; |
119 |
} |
120 |
|
121 |
public String getLine () { |
122 |
if (this.isReady()) { |
123 |
try {
|
124 |
return reader.readLine();
|
125 |
} catch (IOException e) { |
126 |
return null; |
127 |
} |
128 |
} else {
|
129 |
return null; |
130 |
} |
131 |
} |
132 |
|
133 |
/**
|
134 |
* Create socket connection to Colonet server.
|
135 |
* If successful, start threads for loading the webcam image and listening for incoming data.
|
136 |
*/
|
137 |
public void connect (String strHost, String strPort) { |
138 |
//make sure hostname and port are valid
|
139 |
if (strHost.equals("") || strPort.equals("")) { |
140 |
err("Please enter a hostname and port.");
|
141 |
return;
|
142 |
} |
143 |
int port = 0; |
144 |
try {
|
145 |
port = Integer.parseInt(strPort);
|
146 |
} catch (Exception e) { |
147 |
err("Invalid port");
|
148 |
return;
|
149 |
} |
150 |
|
151 |
//make sure we aren't already connected. if so, disconnect first.
|
152 |
if (socket != null && socket.isConnected()) { |
153 |
try {
|
154 |
out.close(); |
155 |
socket.close(); |
156 |
} catch (IOException e) { |
157 |
log.append("Error closing socket. Reconnecting...\n");
|
158 |
} |
159 |
} |
160 |
|
161 |
try {
|
162 |
log.append("Attempting to connect to " + strHost + "\n"); |
163 |
socket = new Socket(strHost, port); |
164 |
} catch (UnknownHostException e) { |
165 |
log.append("Unknown host exception.\n");
|
166 |
err("Unknown Host Exception");
|
167 |
return;
|
168 |
} catch (IOException e) { |
169 |
log.append("IO Exception.\n");
|
170 |
err("IO Exception");
|
171 |
return;
|
172 |
} catch (java.security.AccessControlException e) {
|
173 |
log.append("Access Control Exception.\n");
|
174 |
err("Permission denied by java.security.AccessControlException.\n\nYou may only connect to the server from which this applet was loaded.");
|
175 |
return;
|
176 |
} |
177 |
if (socket == null || !socket.isConnected()) { |
178 |
log.append("Connection is not ready. Try connecting again.");
|
179 |
return;
|
180 |
} |
181 |
log.append("Connected to " + strPort + " on port " + port + "\n"); |
182 |
try {
|
183 |
out = new OutputStreamWriter(socket.getOutputStream()); |
184 |
reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
185 |
} catch (IOException e) { |
186 |
warn("Could not get transfer streams from socket connection.");
|
187 |
} |
188 |
|
189 |
} |
190 |
|
191 |
/**
|
192 |
* Sends a command code to the colonet server so that it can be redirected to one or more robots.
|
193 |
* Assembles the given string into a packet and sends a full command string to the colonet server.
|
194 |
* The string sent from here should be the correct form for sending directly to the robot.
|
195 |
*/
|
196 |
public void sendData (String s, String robotNumber) { |
197 |
//make sure we can send
|
198 |
if (!this.isReady()) { |
199 |
warn("There was a problem with the connection. Data could not be sent.\nMake sure you are connected and try sending the command again.");
|
200 |
return;
|
201 |
} |
202 |
|
203 |
//create packet
|
204 |
String packet;
|
205 |
packet = ColonetServerInterface.SEND_TO_ROBOT; |
206 |
packet += " " + robotNumber;
|
207 |
packet += " " + ColonetServerInterface.COLONET_COMMAND;
|
208 |
packet += " " + s; //add the command code here |
209 |
packet += "\n";
|
210 |
|
211 |
//send packet
|
212 |
try {
|
213 |
log.append("Sending: " + packet);
|
214 |
out.write(packet); |
215 |
out.flush(); |
216 |
} catch (IOException e) { |
217 |
log.append("Could not send data.\n");
|
218 |
} |
219 |
} |
220 |
|
221 |
/**
|
222 |
* Sends a request to the colonet server so that it can be redirected to one or more robots.
|
223 |
* Assembles the given string into a packet and sends a full command string to the colonet server.
|
224 |
* The string sent from here should be the correct form for sending directly to the robot.
|
225 |
* @param s The request code to be sent
|
226 |
* @see #sendData(String)
|
227 |
*/
|
228 |
public void sendRequest (String s, String robotNumber) { |
229 |
//make sure we can send
|
230 |
if (!this.isReady()) { |
231 |
warn("There was a problem with the connection. Data could not be sent.\nMake sure you are connected and try sending the command again.");
|
232 |
return;
|
233 |
} |
234 |
|
235 |
//get robot code to send to.
|
236 |
/*
|
237 |
if (cmbRobotNumber.getSelectedIndex() == 0)
|
238 |
robotNumber = ColonetServerInterface.GLOBAL_DEST;
|
239 |
else
|
240 |
robotNumber = ( cmbRobotNumber.getSelectedIndex() - 1 ) + "";
|
241 |
*/
|
242 |
|
243 |
//create packet
|
244 |
String packet;
|
245 |
packet = ColonetServerInterface.REQUEST_FROM_ROBOT; |
246 |
packet += " " + robotNumber;
|
247 |
packet += " " + ColonetServerInterface.COLONET_REQUEST;
|
248 |
packet += " " + s; //add the command code here |
249 |
packet += "\n";
|
250 |
|
251 |
//send packet
|
252 |
try {
|
253 |
Thread.sleep(200); //paue to be safe |
254 |
out.write(packet); |
255 |
out.flush(); |
256 |
log.append("Sent: " + packet);
|
257 |
} catch (IOException e) { |
258 |
log.append("Could not send data.\n");
|
259 |
} catch (InterruptedException e) { |
260 |
log.append("Thread InterruptedException in sendData\n");
|
261 |
} |
262 |
|
263 |
} |
264 |
|
265 |
|
266 |
|
267 |
|
268 |
/**
|
269 |
* Display informational message box on the screen. Used for casual communicaton to the user.
|
270 |
* @param text Text to display
|
271 |
*/
|
272 |
public void msg (String text) { |
273 |
JOptionPane.showMessageDialog(null, text, "Colonet", JOptionPane.INFORMATION_MESSAGE); |
274 |
} |
275 |
|
276 |
/**
|
277 |
* Display warning message box on the screen. Used for minor alerts or exceptions.
|
278 |
* @param text Text to display
|
279 |
*/
|
280 |
public void warn (String text) { |
281 |
JOptionPane.showMessageDialog(null, text, "Colonet", JOptionPane.WARNING_MESSAGE); |
282 |
} |
283 |
|
284 |
/**
|
285 |
* Display error message box on the screen. Used for major errors or exceptions in the program.
|
286 |
* @param text Text to display
|
287 |
*/
|
288 |
public void err (String text) { |
289 |
JOptionPane.showMessageDialog(null, text, "Colonet", JOptionPane.ERROR_MESSAGE); |
290 |
} |
291 |
|
292 |
|
293 |
} |