Revision 131
moved more code in the Command class into its own private functions
trunk/code/projects/colonet/ColonetServer/Command.cpp | ||
---|---|---|
190 | 190 |
return -1; |
191 | 191 |
} |
192 | 192 |
|
193 |
//TODO: move REQUEST_BOM_MATRIX stuff into a private function |
|
194 | 193 |
if (second_token == REQUEST_BOM_MATRIX) { |
195 |
//TODO: rename to indicate its actually the response message |
|
196 |
char bom_matrix_buffer[MAX_RESPONSE_LEN]; |
|
197 |
char temp_bom_matrix_buffer[MAX_RESPONSE_LEN]; |
|
198 |
|
|
199 |
//TODO: change after we start keeping track of bom matrix |
|
200 |
int number_robots = rand()%10; |
|
201 |
int bom_matrix[number_robots][number_robots]; |
|
202 |
for (int ii = 0; ii < number_robots; ii++) { |
|
203 |
for (int j = 0; j < number_robots; j++) { |
|
204 |
//do this to generate some -1 values which mean they can't see each other |
|
205 |
int matrix_value = rand()%(number_robots+1)-1; |
|
206 |
bom_matrix[ii][j] = matrix_value; |
|
207 |
} |
|
194 |
if (parse_request_bom_matrix(connection_pool, pool_index)) { |
|
195 |
return -1; |
|
208 | 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 |
} |
|
209 | 206 |
|
210 |
printf("number of robots is %d\n", number_robots); |
|
207 |
return 0; |
|
208 |
} |
|
211 | 209 |
|
212 |
//TODO: separate parameters with spaces
|
|
213 |
//TODO: make this better
|
|
214 |
//TODO: make sure I don't need to do MAX_RESPONSE_LENGTH-1
|
|
215 |
snprintf(bom_matrix_buffer,MAX_RESPONSE_LEN, "%d %d %d", RESPONSE_TO_CLIENT_REQUEST, REQUEST_BOM_MATRIX, number_robots);
|
|
216 |
for (int ii = 0; ii < number_robots; ii++) { |
|
217 |
for (int j = 0; j < number_robots; j++) {
|
|
218 |
//TODO: don't use strcpy
|
|
219 |
strcpy(temp_bom_matrix_buffer, bom_matrix_buffer);
|
|
220 |
//TODO: put length checking in here so array doesn't go out of bounds
|
|
221 |
//TODO: maybe use strncat?
|
|
222 |
strcat(temp_bom_matrix_buffer," %d");
|
|
223 |
snprintf(bom_matrix_buffer, MAX_RESPONSE_LEN, temp_bom_matrix_buffer, bom_matrix[ii][j]);
|
|
224 |
}
|
|
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;
|
|
225 | 223 |
} |
226 |
strcat(bom_matrix_buffer,"\n"); |
|
227 |
connection_pool->write_to_client(pool_index, bom_matrix_buffer, strlen(bom_matrix_buffer)); |
|
228 |
printf("Sending %s", bom_matrix_buffer); |
|
229 |
} else if (second_token == REQUEST_XBEE_IDS) { |
|
230 |
//TODO: move this into private function |
|
231 |
//TODO: make this better |
|
232 |
int number_robots = rand() % 10; |
|
224 |
} |
|
233 | 225 |
|
234 |
char xbee_id_buffer[MAX_RESPONSE_LEN]; |
|
235 |
char temp_xbee_id_buffer[MAX_RESPONSE_LEN]; |
|
236 |
|
|
237 |
snprintf(xbee_id_buffer, MAX_RESPONSE_LEN, "%d %d %d", RESPONSE_TO_CLIENT_REQUEST, REQUEST_XBEE_IDS, number_robots); |
|
238 |
|
|
239 |
printf("number of robots is %d\n", number_robots); |
|
226 |
printf("number of robots is %d\n", number_robots); |
|
240 | 227 |
|
241 |
for (int ii = 0; ii < number_robots; ii++) { |
|
242 |
strcpy(temp_xbee_id_buffer, xbee_id_buffer); |
|
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); |
|
243 | 236 |
//TODO: put length checking in here so array doesn't go out of bounds |
244 | 237 |
//TODO: maybe use strncat? |
245 |
strcat(temp_xbee_id_buffer, " %d");
|
|
246 |
snprintf(xbee_id_buffer, MAX_RESPONSE_LEN, temp_xbee_id_buffer, ii);
|
|
238 |
strcat(temp_bom_matrix_buffer," %d");
|
|
239 |
snprintf(bom_matrix_buffer, MAX_RESPONSE_LEN, temp_bom_matrix_buffer, bom_matrix[ii][j]);
|
|
247 | 240 |
} |
248 |
strcat(xbee_id_buffer, "\n"); |
|
249 |
connection_pool->write_to_client(pool_index, xbee_id_buffer, strlen(xbee_id_buffer)); |
|
250 |
printf("Sending %s", xbee_id_buffer); |
|
251 |
} else { |
|
252 |
char * my_current_message = "Hi, how are you?\n"; |
|
253 |
printf("Sending %s\n", my_current_message); |
|
254 |
connection_pool->write_to_client(pool_index, my_current_message, strlen(my_current_message)); |
|
255 | 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); |
|
256 | 245 |
|
257 | 246 |
return 0; |
258 | 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 |
} |
trunk/code/projects/colonet/ColonetServer/includes/Command.h | ||
---|---|---|
23 | 23 |
int parse_send_to_robot(int number_tokens, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE]); |
24 | 24 |
int parse_request_from_server(int number_tokens, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE], ConnectionPool * connection_pool, int pool_index); |
25 | 25 |
|
26 |
int parse_request_bom_matrix(ConnectionPool * connection_pool, int pool_index); |
|
27 |
int parse_request_xbee_ids(ConnectionPool * connection_pool, int pool_index); |
|
26 | 28 |
}; |
27 | 29 |
|
28 | 30 |
|
Also available in: Unified diff