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