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