root / trunk / code / projects / colonet / server / Command.cpp @ 763
History | View | Annotate | Download (12.2 KB)
1 | 128 | jknichel | /**
|
---|---|---|---|
2 | * @file Command.cpp
|
||
3 | *
|
||
4 | * @author Jason Knichel
|
||
5 | * @date 10/9/07
|
||
6 | 144 | jknichel | *
|
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 | 128 | jknichel | */
|
12 | |||
13 | 156 | emarinel | #include <ctype.h> |
14 | #include <stdio.h> |
||
15 | 128 | jknichel | #include <stdlib.h> |
16 | #include <string.h> |
||
17 | 156 | emarinel | |
18 | 161 | emarinel | #include <colonet_wireless.h> |
19 | |||
20 | 391 | emarinel | #include <Command.h> |
21 | #include <ConnectionPool.h> |
||
22 | 447 | emarinel | #include <map> |
23 | #include <vision.h> |
||
24 | 128 | jknichel | |
25 | 447 | emarinel | using namespace std; |
26 | |||
27 | 629 | jknichel | /**
|
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 | 453 | emarinel | Command::Command(ConnectionPool * connection_pool_temp, ColonetServer* cs) { |
34 | 139 | jknichel | connection_pool = connection_pool_temp; |
35 | 453 | emarinel | colonet_server = cs; |
36 | 128 | jknichel | } |
37 | |||
38 | 629 | jknichel | /**
|
39 | * @brief Destructor for the Command class
|
||
40 | */
|
||
41 | 424 | emarinel | Command::~Command() {} |
42 | 128 | jknichel | |
43 | 424 | emarinel | /**
|
44 | 629 | jknichel | * @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 | 139 | jknichel | int Command::parse_command(char* command, int pool_index) { |
52 | 128 | jknichel | char tokens[MAX_TOKENS][MAX_TOKEN_SIZE];
|
53 | int number_tokens = 0; |
||
54 | char* end_pointer = NULL; |
||
55 | int command_id;
|
||
56 | |||
57 | 424 | emarinel | if (!connection_pool || !command || pool_index < 0) { |
58 | 139 | jknichel | return -1; |
59 | } |
||
60 | |||
61 | 128 | jknichel | 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 | 156 | emarinel | if (parse_send_to_robot(number_tokens, tokens, pool_index)) {
|
75 | 424 | emarinel | fprintf(stderr, "parse_send_to_robot failed.\n");
|
76 | 129 | jknichel | return -1; |
77 | 128 | jknichel | } |
78 | } else if (command_id == REQUEST_FROM_SERVER) { |
||
79 | 139 | jknichel | if (parse_request_from_server(number_tokens, tokens, pool_index)) {
|
80 | 128 | jknichel | 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 | 629 | jknichel | //look for the end of the next token (tokens are separated by spaces)
|
105 | 128 | jknichel | while ((end_token = strstr(next_token, " "))) { |
106 | 629 | jknichel | //set the location of the space to a null terminator
|
107 | 128 | jknichel | *end_token = '\0';
|
108 | |||
109 | 629 | jknichel | //make sure the token isn't too long
|
110 | 128 | jknichel | if (strlen(next_token) > MAX_TOKEN_SIZE-1) { |
111 | return -1; |
||
112 | } |
||
113 | |||
114 | 629 | jknichel | //copy the next token into the token array
|
115 | 128 | jknichel | strcpy(tokens[number_tokens], next_token); |
116 | |||
117 | number_tokens++; |
||
118 | 629 | jknichel | |
119 | 128 | jknichel | next_token = end_token + 1;
|
120 | |||
121 | 629 | jknichel | //skip over extra whitespace if there is any between tokens
|
122 | 128 | jknichel | while (isspace(*next_token) && *next_token != '\0') { |
123 | next_token++; |
||
124 | } |
||
125 | } |
||
126 | |||
127 | 629 | jknichel | //get the last token if there were no spaces at the end of the string to indicate an end of token
|
128 | 128 | jknichel | 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 | 161 | emarinel | /**
|
142 | 128 | jknichel | * @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 | 161 | emarinel | |
160 | 128 | jknichel | if (tokens[1] != COLONET_REQUEST && tokens[1] != COLONET_COMMAND) { |
161 | /* Invalid message type */
|
||
162 | return -1; |
||
163 | } |
||
164 | |||
165 | return 0; |
||
166 | } |
||
167 | 129 | jknichel | |
168 | 629 | jknichel | //TODO: fill in the doxygen comments for this
|
169 | 424 | emarinel | /**
|
170 | * @brief Sends parsed command from server to robot(s).
|
||
171 | 629 | jknichel | *
|
172 | * @param number_int_tokens
|
||
173 | * @param tokens
|
||
174 | * @param pool_index
|
||
175 | *
|
||
176 | * @return 0 on success, negative error code on failure
|
||
177 | 424 | emarinel | */
|
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 | 129 | jknichel | |
181 | 763 | gtress | /* 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 | 129 | jknichel | memset(arguments, 1, PACKET_DATA_LEN);
|
190 | |||
191 | 161 | emarinel | // Convert tokens to ints
|
192 | 424 | emarinel | for (int i = ROBOT_COMMAND_OFFSET; i < number_int_tokens; i++) { |
193 | 129 | jknichel | int_tokens[i-ROBOT_COMMAND_OFFSET] = atoi(tokens[i]); |
194 | } |
||
195 | |||
196 | 161 | emarinel | // Fill arguments buffer with arguments
|
197 | 424 | emarinel | for (int i = ROBOT_COMMAND_LEN; i < number_int_tokens-ROBOT_COMMAND_OFFSET; i++) { |
198 | 129 | jknichel | arguments[i-ROBOT_COMMAND_LEN] = int_tokens[i]; |
199 | } |
||
200 | |||
201 | 161 | emarinel | // Check the tokens
|
202 | 129 | jknichel | if (check_tokens(int_tokens, number_int_tokens) < 0) { |
203 | 151 | gtress | fprintf(stderr, "%s: Error - Invalid robot command/request.\n", __FUNCTION__);
|
204 | 129 | jknichel | return -1; |
205 | } |
||
206 | |||
207 | 516 | emarinel | //printf("parsed command from internet client: ");
|
208 | 424 | emarinel | for (int i = 0; i < number_int_tokens - ROBOT_COMMAND_OFFSET; i++) { |
209 | printf("%d ", int_tokens[i]);
|
||
210 | } |
||
211 | printf("\n");
|
||
212 | |||
213 | 161 | emarinel | // Send packet to robot
|
214 | 424 | emarinel | if (colonet_wl_send((short)pool_index, int_tokens[0], (ColonetRobotMessageType)int_tokens[1], int_tokens[2], |
215 | 648 | emarinel | arguments) != 0) {
|
216 | 418 | emarinel | fprintf(stderr, "Error - Colonet_wl_send failed.\n");
|
217 | exit(1);
|
||
218 | 408 | emarinel | } |
219 | 156 | emarinel | |
220 | 129 | jknichel | return 0; |
221 | } |
||
222 | 130 | jknichel | |
223 | 424 | emarinel | /**
|
224 | 640 | jknichel | * @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 | 629 | jknichel | */
|
232 | 139 | jknichel | int Command::parse_request_from_server(int number_tokens, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE], int pool_index) { |
233 | 130 | jknichel | char* end_pointer = NULL; |
234 | |||
235 | 640 | jknichel | //make sure a connection pool is set
|
236 | 139 | jknichel | if (!connection_pool) {
|
237 | return -1; |
||
238 | } |
||
239 | |||
240 | 640 | jknichel | //make sure you enough enough tokens to be a valid command
|
241 | 156 | emarinel | if (number_tokens < 2) { |
242 | 130 | jknichel | return -1; |
243 | 156 | emarinel | } |
244 | 130 | jknichel | |
245 | 640 | jknichel | //convert the second token to an integer
|
246 | 130 | jknichel | 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 | 161 | emarinel | |
253 | 661 | jknichel | int up_x, up_y, low_x, low_y;
|
254 | |||
255 | 640 | jknichel | //figure out which command was sent
|
256 | 445 | emarinel | switch (second_token) {
|
257 | 640 | jknichel | case REQUEST_BOM_MATRIX:
|
258 | if (parse_request_bom_matrix(pool_index)) {
|
||
259 | 131 | jknichel | return -1; |
260 | 130 | jknichel | } |
261 | 445 | emarinel | break;
|
262 | |||
263 | case REQUEST_XBEE_IDS:
|
||
264 | 139 | jknichel | if (parse_request_xbee_ids(pool_index)) {
|
265 | 131 | jknichel | return -1; |
266 | } |
||
267 | 445 | emarinel | break;
|
268 | |||
269 | case CLIENT_REQUEST_ROBOT_POSITIONS:
|
||
270 | if (parse_request_robot_positions(pool_index)) {
|
||
271 | return -1; |
||
272 | } |
||
273 | break;
|
||
274 | |||
275 | 453 | emarinel | case CLIENT_ASSIGN_ROBOT_ID:
|
276 | 520 | emarinel | colonet_server->getPositionMonitor()->assignRealId(atoi(tokens[2]), atoi(tokens[3])); |
277 | 450 | emarinel | break;
|
278 | |||
279 | 659 | jknichel | case CLIENT_SET_VIRTUAL_WALL:
|
280 | 661 | jknichel | 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 | 659 | jknichel | break;
|
291 | |||
292 | 445 | emarinel | default:
|
293 | 516 | emarinel | char* my_current_message = "Invalid request!\n"; |
294 | //printf("Sending %s\n", my_current_message);
|
||
295 | 131 | jknichel | connection_pool->write_to_client(pool_index, my_current_message, strlen(my_current_message)); |
296 | 445 | emarinel | break;
|
297 | 131 | jknichel | } |
298 | 130 | jknichel | |
299 | 131 | jknichel | return 0; |
300 | } |
||
301 | 130 | jknichel | |
302 | 640 | jknichel | /**
|
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 | 139 | jknichel | int Command::parse_request_bom_matrix(int pool_index) { |
310 | 135 | jknichel | char response_bom_matrix_buffer[MAX_RESPONSE_LEN];
|
311 | 131 | jknichel | char temp_bom_matrix_buffer[MAX_RESPONSE_LEN];
|
312 | 161 | emarinel | |
313 | 139 | jknichel | if (!connection_pool) {
|
314 | return -1; |
||
315 | } |
||
316 | |||
317 | 297 | emarinel | int num_robots;
|
318 | 299 | jknichel | int * xbee_ids;
|
319 | 640 | jknichel | //get the sensor matrix from the wireless library
|
320 | 299 | jknichel | int** bom_matrix = colonet_get_sensor_matrix(&num_robots, &xbee_ids);
|
321 | 130 | jknichel | |
322 | 297 | emarinel | printf("number of robots is %d\n", num_robots);
|
323 | 161 | emarinel | |
324 | 640 | jknichel | //copy the information over into a linear array to send to the client
|
325 | 131 | jknichel | //TODO: make this better
|
326 | //TODO: make sure I don't need to do MAX_RESPONSE_LENGTH-1
|
||
327 | 297 | emarinel | 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 | 131 | jknichel | //TODO: don't use strcpy
|
332 | 135 | jknichel | strcpy(temp_bom_matrix_buffer, response_bom_matrix_buffer); |
333 | 130 | jknichel | //TODO: put length checking in here so array doesn't go out of bounds
|
334 | //TODO: maybe use strncat?
|
||
335 | 131 | jknichel | strcat(temp_bom_matrix_buffer," %d");
|
336 | 297 | emarinel | snprintf(response_bom_matrix_buffer, MAX_RESPONSE_LEN, temp_bom_matrix_buffer, bom_matrix[i][j]); |
337 | 130 | jknichel | } |
338 | } |
||
339 | 135 | jknichel | strcat(response_bom_matrix_buffer,"\n");
|
340 | 640 | jknichel | |
341 | //send the bom matrix to the client that requested it
|
||
342 | 135 | jknichel | connection_pool->write_to_client(pool_index, response_bom_matrix_buffer, strlen(response_bom_matrix_buffer)); |
343 | 516 | emarinel | //printf("Sending %s", response_bom_matrix_buffer);
|
344 | 297 | emarinel | |
345 | for (int i = 0; i < num_robots; i++) { |
||
346 | free(bom_matrix[i]); |
||
347 | } |
||
348 | free(bom_matrix); |
||
349 | 130 | jknichel | |
350 | 299 | jknichel | free(xbee_ids); |
351 | |||
352 | 130 | jknichel | return 0; |
353 | } |
||
354 | 131 | jknichel | |
355 | 640 | jknichel | /**
|
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 | 445 | emarinel | int Command::parse_request_robot_positions(int pool_index) { |
363 | 516 | emarinel | //printf("*****parse_request_robot_positions\n");
|
364 | 455 | emarinel | |
365 | 640 | jknichel | //ask the position monitor for where it sees robots
|
366 | 453 | emarinel | map<int, VisionPosition> positions = colonet_server->getPositionMonitor()->getAllRobotPositions();
|
367 | 640 | jknichel | |
368 | 455 | emarinel | map<int, VisionPosition>::iterator iter;
|
369 | 447 | emarinel | |
370 | 466 | emarinel | char position_buffer[256]; |
371 | position_buffer[0] = 0; |
||
372 | sprintf(position_buffer, "%d %d", RESPONSE_TO_CLIENT_REQUEST, CLIENT_REQUEST_ROBOT_POSITIONS);
|
||
373 | 640 | jknichel | |
374 | 455 | emarinel | for (iter = positions.begin(); iter != positions.end(); iter++) {
|
375 | char tmpbuf[80]; |
||
376 | 466 | emarinel | sprintf(tmpbuf, " %d %d %d", iter->first, iter->second.x, iter->second.y);
|
377 | 455 | emarinel | strcat(position_buffer, tmpbuf); |
378 | } |
||
379 | |||
380 | 466 | emarinel | strcat(position_buffer, "\n");
|
381 | |||
382 | 516 | emarinel | //printf("position buffer is: %s\n", position_buffer);
|
383 | 466 | emarinel | |
384 | 640 | jknichel | //send the positions of the robots to the client that requested them
|
385 | 455 | emarinel | connection_pool->write_to_client(pool_index, position_buffer, strlen(position_buffer)); |
386 | |||
387 | 445 | emarinel | return 0; |
388 | } |
||
389 | |||
390 | 640 | jknichel | /**
|
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 | 139 | jknichel | int Command::parse_request_xbee_ids(int pool_index) { |
398 | 297 | emarinel | char temp_xbee_id_buffer[MAX_RESPONSE_LEN];
|
399 | 131 | jknichel | char xbee_id_buffer[MAX_RESPONSE_LEN];
|
400 | 161 | emarinel | |
401 | 297 | emarinel | int num_robots;
|
402 | 640 | jknichel | |
403 | //get a list of the xbee ids
|
||
404 | 297 | emarinel | int* xbee_ids = colonet_get_xbee_ids(&num_robots);
|
405 | |||
406 | 516 | emarinel | //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 | 418 | emarinel | |
413 | 139 | jknichel | if (!connection_pool) {
|
414 | return -1; |
||
415 | } |
||
416 | |||
417 | 297 | emarinel | snprintf(xbee_id_buffer, MAX_RESPONSE_LEN, "%d %d %d", RESPONSE_TO_CLIENT_REQUEST, REQUEST_XBEE_IDS, num_robots);
|
418 | 161 | emarinel | |
419 | 640 | jknichel | //copy over the ids into a response to send to the client
|
420 | 297 | emarinel | for (int i = 0; i < num_robots; i++) { |
421 | 131 | jknichel | 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 | 297 | emarinel | snprintf(xbee_id_buffer, MAX_RESPONSE_LEN, temp_xbee_id_buffer, xbee_ids[i]); |
426 | 131 | jknichel | } |
427 | strcat(xbee_id_buffer, "\n");
|
||
428 | 640 | jknichel | |
429 | //send the list of xbee ides to the client that requested them
|
||
430 | 131 | jknichel | connection_pool->write_to_client(pool_index, xbee_id_buffer, strlen(xbee_id_buffer)); |
431 | 516 | emarinel | //printf("Sending %s", xbee_id_buffer);
|
432 | 131 | jknichel | |
433 | 297 | emarinel | free(xbee_ids); |
434 | |||
435 | 131 | jknichel | return 0; |
436 | } |