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