Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / server / Command.cpp @ 629

History | View | Annotate | Download (9.9 KB)

1 128 jknichel
/**
2
 * @file Command.cpp
3
 *
4
 * @author Jason Knichel
5
 * @date 10/9/07
6 144 jknichel
 *
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 128 jknichel
 */
12
13 156 emarinel
#include <ctype.h>
14
#include <stdio.h>
15 128 jknichel
#include <stdlib.h>
16
#include <string.h>
17 156 emarinel
18 161 emarinel
#include <colonet_wireless.h>
19
20 391 emarinel
#include <Command.h>
21
#include <ConnectionPool.h>
22 447 emarinel
#include <map>
23
#include <vision.h>
24 128 jknichel
25 447 emarinel
using namespace std;
26
27 629 jknichel
/**
28
 * @brief Constructor for the Command class
29
 *
30
 * @param connection_pool_temp The connection pool this instance of the Command class is supposed to use
31
 * @param cs The ColonetServer this instance of the Command class is supposed to use
32
 */
33 453 emarinel
Command::Command(ConnectionPool * connection_pool_temp, ColonetServer* cs) {
34 139 jknichel
  connection_pool = connection_pool_temp;
35 453 emarinel
  colonet_server = cs;
36 128 jknichel
}
37
38 629 jknichel
/**
39
 * @brief Destructor for the Command class
40
 */
41 424 emarinel
Command::~Command() {}
42 128 jknichel
43 424 emarinel
/**
44 629 jknichel
 * @brief Called by connection pool to parse command from client.
45
 *
46
 * @param command The command to parse
47
 * @param pool_index The index in the connection pool of the client who generated this command
48
 *
49
 * @return 0 on success, negative error code on failure
50
 */
51 139 jknichel
int Command::parse_command(char* command, int pool_index) {
52 128 jknichel
  char tokens[MAX_TOKENS][MAX_TOKEN_SIZE];
53
  int number_tokens = 0;
54
  char* end_pointer = NULL;
55
  int command_id;
56
57 424 emarinel
  if (!connection_pool || !command || pool_index < 0) {
58 139 jknichel
    return -1;
59
  }
60
61 128 jknichel
  if ((number_tokens = tokenize_command(command, tokens)) < 0) {
62
    return -1;
63
  }
64
65
  //the 10 in the function call indicates number is base 10
66
  command_id = strtol(tokens[0], &end_pointer, 10);
67
68
  if (!end_pointer || *end_pointer != '\0') {
69
    printf("There was an error converting first token into a number.\n");
70
    return -1;
71
  }
72
73
  if (command_id == SEND_TO_ROBOT) {
74 156 emarinel
    if (parse_send_to_robot(number_tokens, tokens, pool_index)) {
75 424 emarinel
      fprintf(stderr, "parse_send_to_robot failed.\n");
76 129 jknichel
      return -1;
77 128 jknichel
    }
78
  } else if (command_id == REQUEST_FROM_SERVER) {
79 139 jknichel
    if (parse_request_from_server(number_tokens, tokens, pool_index)) {
80 128 jknichel
      return -1;
81
    }
82
  }
83
84
  return 0;
85
}
86
87
/**
88
 * @brief Breaks a command up into tokens
89
 *
90
 * @param command The command to tokenize
91
 * @param tokens A two dimensional character array to store the tokens in
92
 *
93
 * @return 0 on success, negative error code on failure
94
 */
95
int Command::tokenize_command(char* command, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE]) {
96
  char* next_token = command;
97
  char* end_token = NULL;
98
  int number_tokens = 0;
99
100
  if (!command) {
101
    return -1;
102
  }
103
104 629 jknichel
  //look for the end of the next token (tokens are separated by spaces)
105 128 jknichel
  while ((end_token = strstr(next_token, " "))) {
106 629 jknichel
    //set the location of the space to a null terminator
107 128 jknichel
    *end_token = '\0';
108
109 629 jknichel
    //make sure the token isn't too long
110 128 jknichel
    if (strlen(next_token) > MAX_TOKEN_SIZE-1) {
111
      return -1;
112
    }
113
114 629 jknichel
    //copy the next token into the token array
115 128 jknichel
    strcpy(tokens[number_tokens], next_token);
116
117
    number_tokens++;
118 629 jknichel
119 128 jknichel
    next_token = end_token + 1;
120
121 629 jknichel
    //skip over extra whitespace if there is any between tokens
122 128 jknichel
    while (isspace(*next_token) && *next_token != '\0') {
123
      next_token++;
124
    }
125
  }
126
127 629 jknichel
  //get the last token if there were no spaces at the end of the string to indicate an end of token
128 128 jknichel
  if (end_token == NULL && *next_token != '\0') {
129
    if (strlen(next_token) > MAX_TOKEN_SIZE-1) {
130
      return -1;
131
    }
132
133
    strcpy(tokens[number_tokens], next_token);
134
135
    number_tokens++;
136
  }
137
138
  return number_tokens;
139
}
140
141 161 emarinel
/**
142 128 jknichel
 * @brief checks a list of tokens to see if it's valid
143
 *
144
 * @param tokens The tokens to check
145
 * @param number_tokens The number of tokens contained in the tokens parameter
146
 *
147
 * @return 0 if tokens is valid
148
 */
149
int Command::check_tokens(unsigned char* tokens, int number_tokens) {
150
  if (number_tokens > 3 + PACKET_DATA_LEN) {
151
    /* Too many tokens */
152
    return -1;
153
  }
154
155
  if (number_tokens < 3) {
156
    /* Not enough tokens */
157
    return -1;
158
  }
159 161 emarinel
160 128 jknichel
  if (tokens[1] != COLONET_REQUEST && tokens[1] != COLONET_COMMAND) {
161
    /* Invalid message type */
162
    return -1;
163
  }
164
165
  return 0;
166
}
167 129 jknichel
168 629 jknichel
//TODO: fill in the doxygen comments for this
169 424 emarinel
/**
170
* @brief Sends parsed command from server to robot(s).
171 629 jknichel
*
172
* @param number_int_tokens
173
* @param tokens
174
* @param pool_index
175
*
176
* @return 0 on success, negative error code on failure
177 424 emarinel
*/
178
int Command::parse_send_to_robot(int number_int_tokens, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE], int pool_index) {
179
  unsigned char int_tokens[MAX_TOKENS], arguments[PACKET_DATA_LEN];
180 129 jknichel
181
  memset(arguments, 1, PACKET_DATA_LEN);
182
183 161 emarinel
  // Convert tokens to ints
184 424 emarinel
  for (int i = ROBOT_COMMAND_OFFSET; i < number_int_tokens; i++) {
185 129 jknichel
    int_tokens[i-ROBOT_COMMAND_OFFSET] = atoi(tokens[i]);
186
  }
187
188 161 emarinel
  // Fill arguments buffer with arguments
189 424 emarinel
  for (int i = ROBOT_COMMAND_LEN; i < number_int_tokens-ROBOT_COMMAND_OFFSET; i++) {
190 129 jknichel
    arguments[i-ROBOT_COMMAND_LEN] = int_tokens[i];
191
  }
192
193 161 emarinel
  // Check the tokens
194 129 jknichel
  if (check_tokens(int_tokens, number_int_tokens) < 0) {
195 151 gtress
    fprintf(stderr, "%s: Error - Invalid robot command/request.\n", __FUNCTION__);
196 129 jknichel
    return -1;
197
  }
198
199 516 emarinel
  //printf("parsed command from internet client: ");
200 424 emarinel
  for (int i = 0; i < number_int_tokens - ROBOT_COMMAND_OFFSET; i++) {
201
    printf("%d ", int_tokens[i]);
202
  }
203
  printf("\n");
204
205 161 emarinel
  // Send packet to robot
206 424 emarinel
  if (colonet_wl_send((short)pool_index, int_tokens[0], (ColonetRobotMessageType)int_tokens[1], int_tokens[2],
207
    arguments) != 0) {
208 418 emarinel
    fprintf(stderr, "Error - Colonet_wl_send failed.\n");
209
    exit(1);
210 408 emarinel
  }
211 156 emarinel
212 129 jknichel
  return 0;
213
}
214 130 jknichel
215 424 emarinel
/**
216 629 jknichel
 *
217
 */
218 139 jknichel
int Command::parse_request_from_server(int number_tokens, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE], int pool_index) {
219 130 jknichel
  char* end_pointer = NULL;
220
221 139 jknichel
  if (!connection_pool) {
222
    return -1;
223
  }
224
225 156 emarinel
  if (number_tokens < 2) {
226 130 jknichel
    return -1;
227 156 emarinel
  }
228 130 jknichel
229
  end_pointer=NULL;
230
  int second_token = strtol(tokens[1], &end_pointer, 10);
231
232
  if (!end_pointer || *end_pointer != '\0') {
233
    printf("There was an error converting second token into a number.\n");
234
    return -1;
235
  }
236 161 emarinel
237 445 emarinel
  switch (second_token) {
238
  case REQUEST_BOM_MATRIX:
239 139 jknichel
    if (parse_request_bom_matrix(pool_index)) {
240 131 jknichel
      return -1;
241 130 jknichel
    }
242 445 emarinel
    break;
243
244
  case REQUEST_XBEE_IDS:
245 139 jknichel
    if (parse_request_xbee_ids(pool_index)) {
246 131 jknichel
      return -1;
247
    }
248 445 emarinel
    break;
249
250
  case CLIENT_REQUEST_ROBOT_POSITIONS:
251
    if (parse_request_robot_positions(pool_index)) {
252
      return -1;
253
    }
254
    break;
255
256 453 emarinel
  case CLIENT_ASSIGN_ROBOT_ID:
257 520 emarinel
    colonet_server->getPositionMonitor()->assignRealId(atoi(tokens[2]), atoi(tokens[3]));
258 450 emarinel
    break;
259
260 445 emarinel
  default:
261 516 emarinel
    char* my_current_message = "Invalid request!\n";
262
    //printf("Sending %s\n", my_current_message);
263 131 jknichel
    connection_pool->write_to_client(pool_index, my_current_message, strlen(my_current_message));
264 445 emarinel
    break;
265 131 jknichel
  }
266 130 jknichel
267 131 jknichel
  return 0;
268
}
269 130 jknichel
270 139 jknichel
int Command::parse_request_bom_matrix(int pool_index) {
271 135 jknichel
  char response_bom_matrix_buffer[MAX_RESPONSE_LEN];
272 131 jknichel
  char temp_bom_matrix_buffer[MAX_RESPONSE_LEN];
273 161 emarinel
274 139 jknichel
  if (!connection_pool) {
275
    return -1;
276
  }
277
278 297 emarinel
  int num_robots;
279 299 jknichel
  int * xbee_ids;
280
  int** bom_matrix = colonet_get_sensor_matrix(&num_robots, &xbee_ids);
281 130 jknichel
282 297 emarinel
  printf("number of robots is %d\n", num_robots);
283 161 emarinel
284 131 jknichel
  //TODO: make this better
285
  //TODO: make sure I don't need to do MAX_RESPONSE_LENGTH-1
286 297 emarinel
  snprintf(response_bom_matrix_buffer, MAX_RESPONSE_LEN, "%d %d %d", RESPONSE_TO_CLIENT_REQUEST, REQUEST_BOM_MATRIX,
287
           num_robots);
288
  for (int i = 0; i < num_robots; i++) {
289
    for (int j = 0; j < num_robots; j++) {
290 131 jknichel
      //TODO: don't use strcpy
291 135 jknichel
      strcpy(temp_bom_matrix_buffer, response_bom_matrix_buffer);
292 130 jknichel
      //TODO: put length checking in here so array doesn't go out of bounds
293
      //TODO: maybe use strncat?
294 131 jknichel
      strcat(temp_bom_matrix_buffer," %d");
295 297 emarinel
      snprintf(response_bom_matrix_buffer, MAX_RESPONSE_LEN, temp_bom_matrix_buffer, bom_matrix[i][j]);
296 130 jknichel
    }
297
  }
298 135 jknichel
  strcat(response_bom_matrix_buffer,"\n");
299
  connection_pool->write_to_client(pool_index, response_bom_matrix_buffer, strlen(response_bom_matrix_buffer));
300 516 emarinel
  //printf("Sending %s", response_bom_matrix_buffer);
301 297 emarinel
302
  for (int i = 0; i < num_robots; i++) {
303
    free(bom_matrix[i]);
304
  }
305
  free(bom_matrix);
306 130 jknichel
307 299 jknichel
  free(xbee_ids);
308
309 130 jknichel
  return 0;
310
}
311 131 jknichel
312 445 emarinel
int Command::parse_request_robot_positions(int pool_index) {
313 516 emarinel
  //printf("*****parse_request_robot_positions\n");
314 455 emarinel
315 453 emarinel
  map<int, VisionPosition> positions = colonet_server->getPositionMonitor()->getAllRobotPositions();
316 455 emarinel
  map<int, VisionPosition>::iterator iter;
317 447 emarinel
318 466 emarinel
  char position_buffer[256];
319
  position_buffer[0] = 0;
320
  sprintf(position_buffer, "%d %d", RESPONSE_TO_CLIENT_REQUEST, CLIENT_REQUEST_ROBOT_POSITIONS);
321
322 455 emarinel
  for (iter = positions.begin(); iter != positions.end(); iter++) {
323
    char tmpbuf[80];
324 466 emarinel
    sprintf(tmpbuf, " %d %d %d", iter->first, iter->second.x, iter->second.y);
325 455 emarinel
    strcat(position_buffer, tmpbuf);
326
  }
327
328 466 emarinel
  strcat(position_buffer, "\n");
329
330 516 emarinel
  //printf("position buffer is: %s\n", position_buffer);
331 466 emarinel
332 455 emarinel
  connection_pool->write_to_client(pool_index, position_buffer, strlen(position_buffer));
333
334 445 emarinel
  return 0;
335
}
336
337 139 jknichel
int Command::parse_request_xbee_ids(int pool_index) {
338 297 emarinel
  char temp_xbee_id_buffer[MAX_RESPONSE_LEN];
339 131 jknichel
  char xbee_id_buffer[MAX_RESPONSE_LEN];
340 161 emarinel
341 297 emarinel
  int num_robots;
342
  int* xbee_ids = colonet_get_xbee_ids(&num_robots);
343
344 516 emarinel
  //printf("num_robots: %d\n", num_robots);
345
  //printf("xbee_ids: ");
346
  //for (int i = 0; i < num_robots; i++) {
347
  //  printf("%d ", xbee_ids[i]);
348
  //}
349
  //printf("\n");
350 418 emarinel
351 139 jknichel
  if (!connection_pool) {
352
    return -1;
353
  }
354
355 297 emarinel
  snprintf(xbee_id_buffer, MAX_RESPONSE_LEN, "%d %d %d", RESPONSE_TO_CLIENT_REQUEST, REQUEST_XBEE_IDS, num_robots);
356 161 emarinel
357 297 emarinel
  for (int i = 0; i < num_robots; i++) {
358 131 jknichel
    strcpy(temp_xbee_id_buffer, xbee_id_buffer);
359
    //TODO: put length checking in here so array doesn't go out of bounds
360
    //TODO: maybe use strncat?
361
    strcat(temp_xbee_id_buffer, " %d");
362 297 emarinel
    snprintf(xbee_id_buffer, MAX_RESPONSE_LEN, temp_xbee_id_buffer, xbee_ids[i]);
363 131 jknichel
  }
364
  strcat(xbee_id_buffer, "\n");
365
  connection_pool->write_to_client(pool_index, xbee_id_buffer, strlen(xbee_id_buffer));
366 516 emarinel
  //printf("Sending %s", xbee_id_buffer);
367 131 jknichel
368 297 emarinel
  free(xbee_ids);
369
370 131 jknichel
  return 0;
371
}