root / trunk / code / projects / colonet / server / Command.cpp @ 763
History | View | Annotate | Download (12.2 KB)
1 |
/**
|
---|---|
2 |
* @file Command.cpp
|
3 |
*
|
4 |
* @author Jason Knichel
|
5 |
* @date 10/9/07
|
6 |
*
|
7 |
* @todo make it so command doesn't rely on connection pool. have methods
|
8 |
* that may return a response return it by getting a response array passed in to them
|
9 |
* that they fill in and then colonet server takes the contents of that response array
|
10 |
* and sends it to the appropriate client
|
11 |
*/
|
12 |
|
13 |
#include <ctype.h> |
14 |
#include <stdio.h> |
15 |
#include <stdlib.h> |
16 |
#include <string.h> |
17 |
|
18 |
#include <colonet_wireless.h> |
19 |
|
20 |
#include <Command.h> |
21 |
#include <ConnectionPool.h> |
22 |
#include <map> |
23 |
#include <vision.h> |
24 |
|
25 |
using namespace std; |
26 |
|
27 |
/**
|
28 |
* @brief Constructor for the Command class
|
29 |
*
|
30 |
* @param connection_pool_temp The connection pool this instance of the Command class is supposed to use
|
31 |
* @param cs The ColonetServer this instance of the Command class is supposed to use
|
32 |
*/
|
33 |
Command::Command(ConnectionPool * connection_pool_temp, ColonetServer* cs) { |
34 |
connection_pool = connection_pool_temp; |
35 |
colonet_server = cs; |
36 |
} |
37 |
|
38 |
/**
|
39 |
* @brief Destructor for the Command class
|
40 |
*/
|
41 |
Command::~Command() {} |
42 |
|
43 |
/**
|
44 |
* @brief Called by connection pool to parse command from client.
|
45 |
*
|
46 |
* @param command The command to parse
|
47 |
* @param pool_index The index in the connection pool of the client who generated this command
|
48 |
*
|
49 |
* @return 0 on success, negative error code on failure
|
50 |
*/
|
51 |
int Command::parse_command(char* command, int pool_index) { |
52 |
char tokens[MAX_TOKENS][MAX_TOKEN_SIZE];
|
53 |
int number_tokens = 0; |
54 |
char* end_pointer = NULL; |
55 |
int command_id;
|
56 |
|
57 |
if (!connection_pool || !command || pool_index < 0) { |
58 |
return -1; |
59 |
} |
60 |
|
61 |
if ((number_tokens = tokenize_command(command, tokens)) < 0) { |
62 |
return -1; |
63 |
} |
64 |
|
65 |
//the 10 in the function call indicates number is base 10
|
66 |
command_id = strtol(tokens[0], &end_pointer, 10); |
67 |
|
68 |
if (!end_pointer || *end_pointer != '\0') { |
69 |
printf("There was an error converting first token into a number.\n");
|
70 |
return -1; |
71 |
} |
72 |
|
73 |
if (command_id == SEND_TO_ROBOT) {
|
74 |
if (parse_send_to_robot(number_tokens, tokens, pool_index)) {
|
75 |
fprintf(stderr, "parse_send_to_robot failed.\n");
|
76 |
return -1; |
77 |
} |
78 |
} else if (command_id == REQUEST_FROM_SERVER) { |
79 |
if (parse_request_from_server(number_tokens, tokens, pool_index)) {
|
80 |
return -1; |
81 |
} |
82 |
} |
83 |
|
84 |
return 0; |
85 |
} |
86 |
|
87 |
/**
|
88 |
* @brief Breaks a command up into tokens
|
89 |
*
|
90 |
* @param command The command to tokenize
|
91 |
* @param tokens A two dimensional character array to store the tokens in
|
92 |
*
|
93 |
* @return 0 on success, negative error code on failure
|
94 |
*/
|
95 |
int Command::tokenize_command(char* command, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE]) { |
96 |
char* next_token = command;
|
97 |
char* end_token = NULL; |
98 |
int number_tokens = 0; |
99 |
|
100 |
if (!command) {
|
101 |
return -1; |
102 |
} |
103 |
|
104 |
//look for the end of the next token (tokens are separated by spaces)
|
105 |
while ((end_token = strstr(next_token, " "))) { |
106 |
//set the location of the space to a null terminator
|
107 |
*end_token = '\0';
|
108 |
|
109 |
//make sure the token isn't too long
|
110 |
if (strlen(next_token) > MAX_TOKEN_SIZE-1) { |
111 |
return -1; |
112 |
} |
113 |
|
114 |
//copy the next token into the token array
|
115 |
strcpy(tokens[number_tokens], next_token); |
116 |
|
117 |
number_tokens++; |
118 |
|
119 |
next_token = end_token + 1;
|
120 |
|
121 |
//skip over extra whitespace if there is any between tokens
|
122 |
while (isspace(*next_token) && *next_token != '\0') { |
123 |
next_token++; |
124 |
} |
125 |
} |
126 |
|
127 |
//get the last token if there were no spaces at the end of the string to indicate an end of token
|
128 |
if (end_token == NULL && *next_token != '\0') { |
129 |
if (strlen(next_token) > MAX_TOKEN_SIZE-1) { |
130 |
return -1; |
131 |
} |
132 |
|
133 |
strcpy(tokens[number_tokens], next_token); |
134 |
|
135 |
number_tokens++; |
136 |
} |
137 |
|
138 |
return number_tokens;
|
139 |
} |
140 |
|
141 |
/**
|
142 |
* @brief checks a list of tokens to see if it's valid
|
143 |
*
|
144 |
* @param tokens The tokens to check
|
145 |
* @param number_tokens The number of tokens contained in the tokens parameter
|
146 |
*
|
147 |
* @return 0 if tokens is valid
|
148 |
*/
|
149 |
int Command::check_tokens(unsigned char* tokens, int number_tokens) { |
150 |
if (number_tokens > 3 + PACKET_DATA_LEN) { |
151 |
/* Too many tokens */
|
152 |
return -1; |
153 |
} |
154 |
|
155 |
if (number_tokens < 3) { |
156 |
/* Not enough tokens */
|
157 |
return -1; |
158 |
} |
159 |
|
160 |
if (tokens[1] != COLONET_REQUEST && tokens[1] != COLONET_COMMAND) { |
161 |
/* Invalid message type */
|
162 |
return -1; |
163 |
} |
164 |
|
165 |
return 0; |
166 |
} |
167 |
|
168 |
//TODO: fill in the doxygen comments for this
|
169 |
/**
|
170 |
* @brief Sends parsed command from server to robot(s).
|
171 |
*
|
172 |
* @param number_int_tokens
|
173 |
* @param tokens
|
174 |
* @param pool_index
|
175 |
*
|
176 |
* @return 0 on success, negative error code on failure
|
177 |
*/
|
178 |
int Command::parse_send_to_robot(int number_int_tokens, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE], int pool_index) { |
179 |
unsigned char int_tokens[MAX_TOKENS], arguments[PACKET_DATA_LEN]; |
180 |
|
181 |
/* GREG'S FILTHY HACK */
|
182 |
/* PLEASE REMOVE */
|
183 |
|
184 |
printf("Index 3 token: %s\n", tokens[3]); |
185 |
|
186 |
|
187 |
/* END OF HACK */
|
188 |
|
189 |
memset(arguments, 1, PACKET_DATA_LEN);
|
190 |
|
191 |
// Convert tokens to ints
|
192 |
for (int i = ROBOT_COMMAND_OFFSET; i < number_int_tokens; i++) { |
193 |
int_tokens[i-ROBOT_COMMAND_OFFSET] = atoi(tokens[i]); |
194 |
} |
195 |
|
196 |
// Fill arguments buffer with arguments
|
197 |
for (int i = ROBOT_COMMAND_LEN; i < number_int_tokens-ROBOT_COMMAND_OFFSET; i++) { |
198 |
arguments[i-ROBOT_COMMAND_LEN] = int_tokens[i]; |
199 |
} |
200 |
|
201 |
// Check the tokens
|
202 |
if (check_tokens(int_tokens, number_int_tokens) < 0) { |
203 |
fprintf(stderr, "%s: Error - Invalid robot command/request.\n", __FUNCTION__);
|
204 |
return -1; |
205 |
} |
206 |
|
207 |
//printf("parsed command from internet client: ");
|
208 |
for (int i = 0; i < number_int_tokens - ROBOT_COMMAND_OFFSET; i++) { |
209 |
printf("%d ", int_tokens[i]);
|
210 |
} |
211 |
printf("\n");
|
212 |
|
213 |
// Send packet to robot
|
214 |
if (colonet_wl_send((short)pool_index, int_tokens[0], (ColonetRobotMessageType)int_tokens[1], int_tokens[2], |
215 |
arguments) != 0) {
|
216 |
fprintf(stderr, "Error - Colonet_wl_send failed.\n");
|
217 |
exit(1);
|
218 |
} |
219 |
|
220 |
return 0; |
221 |
} |
222 |
|
223 |
/**
|
224 |
* @brief This function parses a request where a client is asking for something from the server
|
225 |
*
|
226 |
* @param number_tokens Number of tokens in the tokens array
|
227 |
* @param tokens The parsed form of the command
|
228 |
* @param pool_index The index in the connection pool of the client who sent the command
|
229 |
*
|
230 |
* @return 0 on success, negative error code on failure
|
231 |
*/
|
232 |
int Command::parse_request_from_server(int number_tokens, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE], int pool_index) { |
233 |
char* end_pointer = NULL; |
234 |
|
235 |
//make sure a connection pool is set
|
236 |
if (!connection_pool) {
|
237 |
return -1; |
238 |
} |
239 |
|
240 |
//make sure you enough enough tokens to be a valid command
|
241 |
if (number_tokens < 2) { |
242 |
return -1; |
243 |
} |
244 |
|
245 |
//convert the second token to an integer
|
246 |
end_pointer=NULL;
|
247 |
int second_token = strtol(tokens[1], &end_pointer, 10); |
248 |
if (!end_pointer || *end_pointer != '\0') { |
249 |
printf("There was an error converting second token into a number.\n");
|
250 |
return -1; |
251 |
} |
252 |
|
253 |
int up_x, up_y, low_x, low_y;
|
254 |
|
255 |
//figure out which command was sent
|
256 |
switch (second_token) {
|
257 |
case REQUEST_BOM_MATRIX:
|
258 |
if (parse_request_bom_matrix(pool_index)) {
|
259 |
return -1; |
260 |
} |
261 |
break;
|
262 |
|
263 |
case REQUEST_XBEE_IDS:
|
264 |
if (parse_request_xbee_ids(pool_index)) {
|
265 |
return -1; |
266 |
} |
267 |
break;
|
268 |
|
269 |
case CLIENT_REQUEST_ROBOT_POSITIONS:
|
270 |
if (parse_request_robot_positions(pool_index)) {
|
271 |
return -1; |
272 |
} |
273 |
break;
|
274 |
|
275 |
case CLIENT_ASSIGN_ROBOT_ID:
|
276 |
colonet_server->getPositionMonitor()->assignRealId(atoi(tokens[2]), atoi(tokens[3])); |
277 |
break;
|
278 |
|
279 |
case CLIENT_SET_VIRTUAL_WALL:
|
280 |
up_x = atoi(tokens[2]);
|
281 |
up_y = atoi(tokens[3]);
|
282 |
low_x = atoi(tokens[4]);
|
283 |
low_y = atoi(tokens[5]);
|
284 |
|
285 |
if (colonet_server->getVirtualWall()->set_coordinates(up_x, up_y, low_x, low_y) == 0) { |
286 |
fprintf(stderr, "Wall set to ((%d, %d), (%d, %d)).\n", up_x, up_y, low_x, low_y);
|
287 |
} else {
|
288 |
fprintf(stderr, "Set wall failed ((%d, %d), (%d, %d)).\n", up_x, up_y, low_x, low_y);
|
289 |
} |
290 |
break;
|
291 |
|
292 |
default:
|
293 |
char* my_current_message = "Invalid request!\n"; |
294 |
//printf("Sending %s\n", my_current_message);
|
295 |
connection_pool->write_to_client(pool_index, my_current_message, strlen(my_current_message)); |
296 |
break;
|
297 |
} |
298 |
|
299 |
return 0; |
300 |
} |
301 |
|
302 |
/**
|
303 |
* @brief This function parses a client's request for the bom matrix as far as the wireless library knows it.
|
304 |
*
|
305 |
* @param pool_index The index in the connection pool of the client who sent the command
|
306 |
*
|
307 |
* @return 0 on success, negative error code on failure
|
308 |
*/
|
309 |
int Command::parse_request_bom_matrix(int pool_index) { |
310 |
char response_bom_matrix_buffer[MAX_RESPONSE_LEN];
|
311 |
char temp_bom_matrix_buffer[MAX_RESPONSE_LEN];
|
312 |
|
313 |
if (!connection_pool) {
|
314 |
return -1; |
315 |
} |
316 |
|
317 |
int num_robots;
|
318 |
int * xbee_ids;
|
319 |
//get the sensor matrix from the wireless library
|
320 |
int** bom_matrix = colonet_get_sensor_matrix(&num_robots, &xbee_ids);
|
321 |
|
322 |
printf("number of robots is %d\n", num_robots);
|
323 |
|
324 |
//copy the information over into a linear array to send to the client
|
325 |
//TODO: make this better
|
326 |
//TODO: make sure I don't need to do MAX_RESPONSE_LENGTH-1
|
327 |
snprintf(response_bom_matrix_buffer, MAX_RESPONSE_LEN, "%d %d %d", RESPONSE_TO_CLIENT_REQUEST, REQUEST_BOM_MATRIX,
|
328 |
num_robots); |
329 |
for (int i = 0; i < num_robots; i++) { |
330 |
for (int j = 0; j < num_robots; j++) { |
331 |
//TODO: don't use strcpy
|
332 |
strcpy(temp_bom_matrix_buffer, response_bom_matrix_buffer); |
333 |
//TODO: put length checking in here so array doesn't go out of bounds
|
334 |
//TODO: maybe use strncat?
|
335 |
strcat(temp_bom_matrix_buffer," %d");
|
336 |
snprintf(response_bom_matrix_buffer, MAX_RESPONSE_LEN, temp_bom_matrix_buffer, bom_matrix[i][j]); |
337 |
} |
338 |
} |
339 |
strcat(response_bom_matrix_buffer,"\n");
|
340 |
|
341 |
//send the bom matrix to the client that requested it
|
342 |
connection_pool->write_to_client(pool_index, response_bom_matrix_buffer, strlen(response_bom_matrix_buffer)); |
343 |
//printf("Sending %s", response_bom_matrix_buffer);
|
344 |
|
345 |
for (int i = 0; i < num_robots; i++) { |
346 |
free(bom_matrix[i]); |
347 |
} |
348 |
free(bom_matrix); |
349 |
|
350 |
free(xbee_ids); |
351 |
|
352 |
return 0; |
353 |
} |
354 |
|
355 |
/**
|
356 |
* @brief This functions parses a client's request for the positions of robots
|
357 |
*
|
358 |
* @param pool_index The index in the connection pool of the client that sent this command
|
359 |
*
|
360 |
* @return 0 on success, negative error code on failure
|
361 |
*/
|
362 |
int Command::parse_request_robot_positions(int pool_index) { |
363 |
//printf("*****parse_request_robot_positions\n");
|
364 |
|
365 |
//ask the position monitor for where it sees robots
|
366 |
map<int, VisionPosition> positions = colonet_server->getPositionMonitor()->getAllRobotPositions();
|
367 |
|
368 |
map<int, VisionPosition>::iterator iter;
|
369 |
|
370 |
char position_buffer[256]; |
371 |
position_buffer[0] = 0; |
372 |
sprintf(position_buffer, "%d %d", RESPONSE_TO_CLIENT_REQUEST, CLIENT_REQUEST_ROBOT_POSITIONS);
|
373 |
|
374 |
for (iter = positions.begin(); iter != positions.end(); iter++) {
|
375 |
char tmpbuf[80]; |
376 |
sprintf(tmpbuf, " %d %d %d", iter->first, iter->second.x, iter->second.y);
|
377 |
strcat(position_buffer, tmpbuf); |
378 |
} |
379 |
|
380 |
strcat(position_buffer, "\n");
|
381 |
|
382 |
//printf("position buffer is: %s\n", position_buffer);
|
383 |
|
384 |
//send the positions of the robots to the client that requested them
|
385 |
connection_pool->write_to_client(pool_index, position_buffer, strlen(position_buffer)); |
386 |
|
387 |
return 0; |
388 |
} |
389 |
|
390 |
/**
|
391 |
* @brief This functions parses a client's request for the ids of the xbees in the token ring
|
392 |
*
|
393 |
* @param pool_index The index in the connection pool of the client that requested the xbee ids
|
394 |
*
|
395 |
* @return 0 on success, negative error code on failure
|
396 |
*/
|
397 |
int Command::parse_request_xbee_ids(int pool_index) { |
398 |
char temp_xbee_id_buffer[MAX_RESPONSE_LEN];
|
399 |
char xbee_id_buffer[MAX_RESPONSE_LEN];
|
400 |
|
401 |
int num_robots;
|
402 |
|
403 |
//get a list of the xbee ids
|
404 |
int* xbee_ids = colonet_get_xbee_ids(&num_robots);
|
405 |
|
406 |
//printf("num_robots: %d\n", num_robots);
|
407 |
//printf("xbee_ids: ");
|
408 |
//for (int i = 0; i < num_robots; i++) {
|
409 |
// printf("%d ", xbee_ids[i]);
|
410 |
//}
|
411 |
//printf("\n");
|
412 |
|
413 |
if (!connection_pool) {
|
414 |
return -1; |
415 |
} |
416 |
|
417 |
snprintf(xbee_id_buffer, MAX_RESPONSE_LEN, "%d %d %d", RESPONSE_TO_CLIENT_REQUEST, REQUEST_XBEE_IDS, num_robots);
|
418 |
|
419 |
//copy over the ids into a response to send to the client
|
420 |
for (int i = 0; i < num_robots; i++) { |
421 |
strcpy(temp_xbee_id_buffer, xbee_id_buffer); |
422 |
//TODO: put length checking in here so array doesn't go out of bounds
|
423 |
//TODO: maybe use strncat?
|
424 |
strcat(temp_xbee_id_buffer, " %d");
|
425 |
snprintf(xbee_id_buffer, MAX_RESPONSE_LEN, temp_xbee_id_buffer, xbee_ids[i]); |
426 |
} |
427 |
strcat(xbee_id_buffer, "\n");
|
428 |
|
429 |
//send the list of xbee ides to the client that requested them
|
430 |
connection_pool->write_to_client(pool_index, xbee_id_buffer, strlen(xbee_id_buffer)); |
431 |
//printf("Sending %s", xbee_id_buffer);
|
432 |
|
433 |
free(xbee_ids); |
434 |
|
435 |
return 0; |
436 |
} |