root / trunk / code / projects / colonet / ColonetServer / Command.cpp @ 447
History | View | Annotate | Download (8.1 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 |
Command::Command(ConnectionPool * connection_pool_temp) { |
28 |
connection_pool = connection_pool_temp; |
29 |
} |
30 |
|
31 |
Command::~Command() {} |
32 |
|
33 |
/**
|
34 |
* Called by connection pool to parse command from client.
|
35 |
*/
|
36 |
int Command::parse_command(char* command, int pool_index) { |
37 |
char tokens[MAX_TOKENS][MAX_TOKEN_SIZE];
|
38 |
int number_tokens = 0; |
39 |
char* end_pointer = NULL; |
40 |
int command_id;
|
41 |
|
42 |
if (!connection_pool || !command || pool_index < 0) { |
43 |
return -1; |
44 |
} |
45 |
|
46 |
if ((number_tokens = tokenize_command(command, tokens)) < 0) { |
47 |
return -1; |
48 |
} |
49 |
|
50 |
//the 10 in the function call indicates number is base 10
|
51 |
command_id = strtol(tokens[0], &end_pointer, 10); |
52 |
|
53 |
if (!end_pointer || *end_pointer != '\0') { |
54 |
printf("There was an error converting first token into a number.\n");
|
55 |
return -1; |
56 |
} |
57 |
|
58 |
if (command_id == SEND_TO_ROBOT) {
|
59 |
if (parse_send_to_robot(number_tokens, tokens, pool_index)) {
|
60 |
fprintf(stderr, "parse_send_to_robot failed.\n");
|
61 |
return -1; |
62 |
} |
63 |
} else if (command_id == REQUEST_FROM_SERVER) { |
64 |
if (parse_request_from_server(number_tokens, tokens, pool_index)) {
|
65 |
return -1; |
66 |
} |
67 |
} |
68 |
|
69 |
return 0; |
70 |
} |
71 |
|
72 |
/**
|
73 |
* @brief Breaks a command up into tokens
|
74 |
*
|
75 |
* @param command The command to tokenize
|
76 |
* @param tokens A two dimensional character array to store the tokens in
|
77 |
*
|
78 |
* @return 0 on success, negative error code on failure
|
79 |
*/
|
80 |
int Command::tokenize_command(char* command, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE]) { |
81 |
char* next_token = command;
|
82 |
char* end_token = NULL; |
83 |
int number_tokens = 0; |
84 |
|
85 |
if (!command) {
|
86 |
return -1; |
87 |
} |
88 |
|
89 |
while ((end_token = strstr(next_token, " "))) { |
90 |
*end_token = '\0';
|
91 |
|
92 |
if (strlen(next_token) > MAX_TOKEN_SIZE-1) { |
93 |
return -1; |
94 |
} |
95 |
|
96 |
strcpy(tokens[number_tokens], next_token); |
97 |
|
98 |
number_tokens++; |
99 |
next_token = end_token + 1;
|
100 |
|
101 |
while (isspace(*next_token) && *next_token != '\0') { |
102 |
next_token++; |
103 |
} |
104 |
} |
105 |
|
106 |
if (end_token == NULL && *next_token != '\0') { |
107 |
if (strlen(next_token) > MAX_TOKEN_SIZE-1) { |
108 |
return -1; |
109 |
} |
110 |
|
111 |
strcpy(tokens[number_tokens], next_token); |
112 |
|
113 |
number_tokens++; |
114 |
} |
115 |
|
116 |
return number_tokens;
|
117 |
} |
118 |
|
119 |
/**
|
120 |
* @brief checks a list of tokens to see if it's valid
|
121 |
*
|
122 |
* @param tokens The tokens to check
|
123 |
* @param number_tokens The number of tokens contained in the tokens parameter
|
124 |
*
|
125 |
* @return 0 if tokens is valid
|
126 |
*/
|
127 |
int Command::check_tokens(unsigned char* tokens, int number_tokens) { |
128 |
if (number_tokens > 3 + PACKET_DATA_LEN) { |
129 |
/* Too many tokens */
|
130 |
return -1; |
131 |
} |
132 |
|
133 |
if (number_tokens < 3) { |
134 |
/* Not enough tokens */
|
135 |
return -1; |
136 |
} |
137 |
|
138 |
if (tokens[1] != COLONET_REQUEST && tokens[1] != COLONET_COMMAND) { |
139 |
/* Invalid message type */
|
140 |
return -1; |
141 |
} |
142 |
|
143 |
return 0; |
144 |
} |
145 |
|
146 |
/**
|
147 |
* @brief Sends parsed command from server to robot(s).
|
148 |
*/
|
149 |
int Command::parse_send_to_robot(int number_int_tokens, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE], int pool_index) { |
150 |
unsigned char int_tokens[MAX_TOKENS], arguments[PACKET_DATA_LEN]; |
151 |
|
152 |
memset(arguments, 1, PACKET_DATA_LEN);
|
153 |
|
154 |
// Convert tokens to ints
|
155 |
for (int i = ROBOT_COMMAND_OFFSET; i < number_int_tokens; i++) { |
156 |
int_tokens[i-ROBOT_COMMAND_OFFSET] = atoi(tokens[i]); |
157 |
} |
158 |
|
159 |
// Fill arguments buffer with arguments
|
160 |
for (int i = ROBOT_COMMAND_LEN; i < number_int_tokens-ROBOT_COMMAND_OFFSET; i++) { |
161 |
arguments[i-ROBOT_COMMAND_LEN] = int_tokens[i]; |
162 |
} |
163 |
|
164 |
// Check the tokens
|
165 |
if (check_tokens(int_tokens, number_int_tokens) < 0) { |
166 |
fprintf(stderr, "%s: Error - Invalid robot command/request.\n", __FUNCTION__);
|
167 |
return -1; |
168 |
} |
169 |
|
170 |
printf("parsed command from internet client: ");
|
171 |
for (int i = 0; i < number_int_tokens - ROBOT_COMMAND_OFFSET; i++) { |
172 |
printf("%d ", int_tokens[i]);
|
173 |
} |
174 |
printf("\n");
|
175 |
|
176 |
// Send packet to robot
|
177 |
if (colonet_wl_send((short)pool_index, int_tokens[0], (ColonetRobotMessageType)int_tokens[1], int_tokens[2], |
178 |
arguments) != 0) {
|
179 |
fprintf(stderr, "Error - Colonet_wl_send failed.\n");
|
180 |
exit(1);
|
181 |
} |
182 |
|
183 |
return 0; |
184 |
} |
185 |
|
186 |
/**
|
187 |
*
|
188 |
*/
|
189 |
int Command::parse_request_from_server(int number_tokens, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE], int pool_index) { |
190 |
char* end_pointer = NULL; |
191 |
|
192 |
if (!connection_pool) {
|
193 |
return -1; |
194 |
} |
195 |
|
196 |
if (number_tokens < 2) { |
197 |
return -1; |
198 |
} |
199 |
|
200 |
end_pointer=NULL;
|
201 |
int second_token = strtol(tokens[1], &end_pointer, 10); |
202 |
|
203 |
if (!end_pointer || *end_pointer != '\0') { |
204 |
printf("There was an error converting second token into a number.\n");
|
205 |
return -1; |
206 |
} |
207 |
|
208 |
switch (second_token) {
|
209 |
case REQUEST_BOM_MATRIX:
|
210 |
if (parse_request_bom_matrix(pool_index)) {
|
211 |
return -1; |
212 |
} |
213 |
break;
|
214 |
|
215 |
case REQUEST_XBEE_IDS:
|
216 |
if (parse_request_xbee_ids(pool_index)) {
|
217 |
return -1; |
218 |
} |
219 |
break;
|
220 |
|
221 |
case CLIENT_REQUEST_ROBOT_POSITIONS:
|
222 |
if (parse_request_robot_positions(pool_index)) {
|
223 |
return -1; |
224 |
} |
225 |
break;
|
226 |
|
227 |
default:
|
228 |
char * my_current_message = "Hi, how are you?\n"; |
229 |
printf("Sending %s\n", my_current_message);
|
230 |
connection_pool->write_to_client(pool_index, my_current_message, strlen(my_current_message)); |
231 |
break;
|
232 |
} |
233 |
|
234 |
return 0; |
235 |
} |
236 |
|
237 |
int Command::parse_request_bom_matrix(int pool_index) { |
238 |
char response_bom_matrix_buffer[MAX_RESPONSE_LEN];
|
239 |
char temp_bom_matrix_buffer[MAX_RESPONSE_LEN];
|
240 |
|
241 |
if (!connection_pool) {
|
242 |
return -1; |
243 |
} |
244 |
|
245 |
int num_robots;
|
246 |
int * xbee_ids;
|
247 |
int** bom_matrix = colonet_get_sensor_matrix(&num_robots, &xbee_ids);
|
248 |
|
249 |
printf("number of robots is %d\n", num_robots);
|
250 |
|
251 |
//TODO: make this better
|
252 |
//TODO: make sure I don't need to do MAX_RESPONSE_LENGTH-1
|
253 |
snprintf(response_bom_matrix_buffer, MAX_RESPONSE_LEN, "%d %d %d", RESPONSE_TO_CLIENT_REQUEST, REQUEST_BOM_MATRIX,
|
254 |
num_robots); |
255 |
for (int i = 0; i < num_robots; i++) { |
256 |
for (int j = 0; j < num_robots; j++) { |
257 |
//TODO: don't use strcpy
|
258 |
strcpy(temp_bom_matrix_buffer, response_bom_matrix_buffer); |
259 |
//TODO: put length checking in here so array doesn't go out of bounds
|
260 |
//TODO: maybe use strncat?
|
261 |
strcat(temp_bom_matrix_buffer," %d");
|
262 |
snprintf(response_bom_matrix_buffer, MAX_RESPONSE_LEN, temp_bom_matrix_buffer, bom_matrix[i][j]); |
263 |
} |
264 |
} |
265 |
strcat(response_bom_matrix_buffer,"\n");
|
266 |
connection_pool->write_to_client(pool_index, response_bom_matrix_buffer, strlen(response_bom_matrix_buffer)); |
267 |
printf("Sending %s", response_bom_matrix_buffer);
|
268 |
|
269 |
for (int i = 0; i < num_robots; i++) { |
270 |
free(bom_matrix[i]); |
271 |
} |
272 |
free(bom_matrix); |
273 |
|
274 |
free(xbee_ids); |
275 |
|
276 |
return 0; |
277 |
} |
278 |
|
279 |
int Command::parse_request_robot_positions(int pool_index) { |
280 |
printf("TODO - parse_request_robot_positions\n");
|
281 |
|
282 |
map<int, VisionPosition> positions = colonet_server.getPositionMonitor()->getAllRobotPositions();
|
283 |
|
284 |
return 0; |
285 |
} |
286 |
|
287 |
int Command::parse_request_xbee_ids(int pool_index) { |
288 |
char temp_xbee_id_buffer[MAX_RESPONSE_LEN];
|
289 |
char xbee_id_buffer[MAX_RESPONSE_LEN];
|
290 |
|
291 |
int num_robots;
|
292 |
int* xbee_ids = colonet_get_xbee_ids(&num_robots);
|
293 |
|
294 |
printf("num_robots: %d\n", num_robots);
|
295 |
printf("xbee_ids: ");
|
296 |
for (int i = 0; i < num_robots; i++) { |
297 |
printf("%d ", xbee_ids[i]);
|
298 |
} |
299 |
printf("\n");
|
300 |
|
301 |
if (!connection_pool) {
|
302 |
return -1; |
303 |
} |
304 |
|
305 |
snprintf(xbee_id_buffer, MAX_RESPONSE_LEN, "%d %d %d", RESPONSE_TO_CLIENT_REQUEST, REQUEST_XBEE_IDS, num_robots);
|
306 |
|
307 |
for (int i = 0; i < num_robots; i++) { |
308 |
strcpy(temp_xbee_id_buffer, xbee_id_buffer); |
309 |
//TODO: put length checking in here so array doesn't go out of bounds
|
310 |
//TODO: maybe use strncat?
|
311 |
strcat(temp_xbee_id_buffer, " %d");
|
312 |
snprintf(xbee_id_buffer, MAX_RESPONSE_LEN, temp_xbee_id_buffer, xbee_ids[i]); |
313 |
} |
314 |
strcat(xbee_id_buffer, "\n");
|
315 |
connection_pool->write_to_client(pool_index, xbee_id_buffer, strlen(xbee_id_buffer)); |
316 |
printf("Sending %s", xbee_id_buffer);
|
317 |
|
318 |
free(xbee_ids); |
319 |
|
320 |
return 0; |
321 |
} |