Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (12.4 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 <ctype.h>
14
#include <stdio.h>
15
#include <stdlib.h>
16
#include <string.h>
17

    
18
#include <colonet_wireless.h>
19
#include <wireless.h>
20

    
21
#include <Command.h>
22
#include <ConnectionPool.h>
23
#include <map>
24
#include <vision.h>
25

    
26
using namespace std;
27

    
28
/**
29
 * @brief Constructor for the Command class
30
 * 
31
 * @param connection_pool_temp The connection pool this instance of the Command class is supposed to use
32
 * @param cs The ColonetServer this instance of the Command class is supposed to use
33
 */
34
Command::Command(ConnectionPool * connection_pool_temp, ColonetServer* cs) {
35
  connection_pool = connection_pool_temp;
36
  colonet_server = cs;
37
}
38

    
39
/**
40
 * @brief Destructor for the Command class
41
 */
42
Command::~Command() {}
43

    
44
/**
45
 * @brief Called by connection pool to parse command from client.
46
 *
47
 * @param command The command to parse
48
 * @param pool_index The index in the connection pool of the client who generated this command
49
 *
50
 * @return 0 on success, negative error code on failure
51
 */
52
int Command::parse_command(char* command, int pool_index) {
53
  char tokens[MAX_TOKENS][MAX_TOKEN_SIZE];
54
  int number_tokens = 0;
55
  char* end_pointer = NULL;
56
  int command_id;
57

    
58
  if (!connection_pool || !command || pool_index < 0) {
59
    return -1;
60
  }
61

    
62
  if ((number_tokens = tokenize_command(command, tokens)) < 0) {
63
    return -1;
64
  }
65

    
66
  //the 10 in the function call indicates number is base 10
67
  command_id = strtol(tokens[0], &end_pointer, 10);
68

    
69
  if (!end_pointer || *end_pointer != '\0') {
70
    printf("There was an error converting first token into a number.\n");
71
    return -1;
72
  }
73

    
74
  if (command_id == SEND_TO_ROBOT) {
75
    if (parse_send_to_robot(number_tokens, tokens, pool_index)) {
76
      fprintf(stderr, "parse_send_to_robot failed.\n");
77
      return -1;
78
    }
79
  } else if (command_id == REQUEST_FROM_SERVER) {
80
    if (parse_request_from_server(number_tokens, tokens, pool_index)) {
81
      return -1;
82
    }
83
  }
84

    
85
  return 0;
86
}
87

    
88
/**
89
 * @brief Breaks a command up into tokens
90
 *
91
 * @param command The command to tokenize
92
 * @param tokens A two dimensional character array to store the tokens in
93
 *
94
 * @return 0 on success, negative error code on failure
95
 */
96
int Command::tokenize_command(char* command, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE]) {
97
  char* next_token = command;
98
  char* end_token = NULL;
99
  int number_tokens = 0;
100

    
101
  if (!command) {
102
    return -1;
103
  }
104

    
105
  //look for the end of the next token (tokens are separated by spaces)
106
  while ((end_token = strstr(next_token, " "))) {
107
    //set the location of the space to a null terminator
108
    *end_token = '\0';
109

    
110
    //make sure the token isn't too long
111
    if (strlen(next_token) > MAX_TOKEN_SIZE-1) {
112
      return -1;
113
    }
114

    
115
    //copy the next token into the token array
116
    strcpy(tokens[number_tokens], next_token);
117

    
118
    number_tokens++;
119

    
120
    next_token = end_token + 1;
121

    
122
    //skip over extra whitespace if there is any between tokens
123
    while (isspace(*next_token) && *next_token != '\0') {
124
      next_token++;
125
    }
126
  }
127

    
128
  //get the last token if there were no spaces at the end of the string to indicate an end of token
129
  if (end_token == NULL && *next_token != '\0') {
130
    if (strlen(next_token) > MAX_TOKEN_SIZE-1) {
131
      return -1;
132
    }
133

    
134
    strcpy(tokens[number_tokens], next_token);
135

    
136
    number_tokens++;
137
  }
138

    
139
  return number_tokens;
140
}
141

    
142
/**
143
 * @brief checks a list of tokens to see if it's valid
144
 *
145
 * @param tokens The tokens to check
146
 * @param number_tokens The number of tokens contained in the tokens parameter
147
 *
148
 * @return 0 if tokens is valid
149
 */
150
int Command::check_tokens(unsigned char* tokens, int number_tokens) {
151
  if (number_tokens > 3 + PACKET_DATA_LEN) {
152
    /* Too many tokens */
153
    return -1;
154
  }
155

    
156
  if (number_tokens < 3) {
157
    /* Not enough tokens */
158
    return -1;
159
  }
160

    
161
  if (tokens[1] != COLONET_REQUEST && tokens[1] != COLONET_COMMAND) {
162
    /* Invalid message type */
163
    return -1;
164
  }
165

    
166
  return 0;
167
}
168

    
169
//TODO: fill in the doxygen comments for this
170
/**
171
* @brief Sends parsed command from server to robot(s).
172
*
173
* @param number_int_tokens 
174
* @param tokens
175
* @param pool_index
176
*
177
* @return 0 on success, negative error code on failure
178
*/
179
int Command::parse_send_to_robot(int number_int_tokens, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE], int pool_index) {
180
  unsigned char int_tokens[MAX_TOKENS], arguments[PACKET_DATA_LEN];
181

    
182
  /* GREG'S FILTHY HACK */
183
  /*   PLEASE REMOVE    */
184
  int cmd = atoi(tokens[3]);
185
  int bot = atoi(tokens[1]);
186
  if (cmd == 96) {
187
  
188
    wl_send_robot_to_robot_global_packet(3, 9, NULL, 0, bot, 0);
189
    printf("Sent recharge request to %d\n", bot);
190
  }
191
  /* END OF HACK */
192

    
193
  memset(arguments, 1, PACKET_DATA_LEN);
194

    
195
  // Convert tokens to ints
196
  for (int i = ROBOT_COMMAND_OFFSET; i < number_int_tokens; i++) {
197
    int_tokens[i-ROBOT_COMMAND_OFFSET] = atoi(tokens[i]);
198
  }
199

    
200
  // Fill arguments buffer with arguments
201
  for (int i = ROBOT_COMMAND_LEN; i < number_int_tokens-ROBOT_COMMAND_OFFSET; i++) {
202
    arguments[i-ROBOT_COMMAND_LEN] = int_tokens[i];
203
  }
204

    
205
  // Check the tokens
206
  if (check_tokens(int_tokens, number_int_tokens) < 0) {
207
    fprintf(stderr, "%s: Error - Invalid robot command/request.\n", __FUNCTION__);
208
    return -1;
209
  }
210

    
211
  //printf("parsed command from internet client: ");
212
  for (int i = 0; i < number_int_tokens - ROBOT_COMMAND_OFFSET; i++) {
213
    printf("%d ", int_tokens[i]);
214
  }
215
  printf("\n");
216

    
217
  // Send packet to robot
218
  if (colonet_wl_send((short)pool_index, int_tokens[0], (ColonetRobotMessageType)int_tokens[1], int_tokens[2],
219
                      arguments) != 0) {
220
    fprintf(stderr, "Error - Colonet_wl_send failed.\n");
221
    exit(1);
222
  }
223

    
224
  return 0;
225
}
226

    
227
/**
228
 * @brief This function parses a request where a client is asking for something from the server
229
 *
230
 * @param number_tokens Number of tokens in the tokens array
231
 * @param tokens The parsed form of the command
232
 * @param pool_index The index in the connection pool of the client who sent the command
233
 *
234
 * @return 0 on success, negative error code on failure
235
 */
236
int Command::parse_request_from_server(int number_tokens, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE], int pool_index) {
237
  char* end_pointer = NULL;
238

    
239
        //make sure a connection pool is set
240
  if (!connection_pool) {
241
    return -1;
242
  }
243

    
244
        //make sure you enough enough tokens to be a valid command
245
  if (number_tokens < 2) {
246
    return -1;
247
  }
248

    
249
        //convert the second token to an integer
250
  end_pointer=NULL;
251
  int second_token = strtol(tokens[1], &end_pointer, 10);
252
  if (!end_pointer || *end_pointer != '\0') {
253
    printf("There was an error converting second token into a number.\n");
254
    return -1;
255
  }
256

    
257
  int up_x, up_y, low_x, low_y;
258

    
259
        //figure out which command was sent
260
  switch (second_token) {
261
        case REQUEST_BOM_MATRIX:
262
                if (parse_request_bom_matrix(pool_index)) {
263
      return -1;
264
    }
265
    break;
266

    
267
  case REQUEST_XBEE_IDS:
268
    if (parse_request_xbee_ids(pool_index)) {
269
      return -1;
270
    }
271
    break;
272

    
273
  case CLIENT_REQUEST_ROBOT_POSITIONS:
274
    if (parse_request_robot_positions(pool_index)) {
275
      return -1;
276
    }
277
    break;
278

    
279
  case CLIENT_ASSIGN_ROBOT_ID:
280
    colonet_server->getPositionMonitor()->assignRealId(atoi(tokens[2]), atoi(tokens[3]));
281
    break;
282

    
283
  case CLIENT_SET_VIRTUAL_WALL:
284
    up_x = atoi(tokens[2]);
285
    up_y = atoi(tokens[3]);
286
    low_x = atoi(tokens[4]);
287
    low_y = atoi(tokens[5]);
288

    
289
    if (colonet_server->getVirtualWall()->set_coordinates(up_x, up_y, low_x, low_y) == 0) {
290
      fprintf(stderr, "Wall set to ((%d, %d), (%d, %d)).\n", up_x, up_y, low_x, low_y);
291
    } else {
292
      fprintf(stderr, "Set wall failed ((%d, %d), (%d, %d)).\n", up_x, up_y, low_x, low_y);
293
    }
294
    break;
295

    
296
  default:
297
    char* my_current_message = "Invalid request!\n";
298
    //printf("Sending %s\n", my_current_message);
299
    connection_pool->write_to_client(pool_index, my_current_message, strlen(my_current_message));
300
    break;
301
  }
302

    
303
  return 0;
304
}
305

    
306
/**
307
 * @brief This function parses a client's request for the bom matrix as far as the wireless library knows it.
308
 *
309
 * @param pool_index The index in the connection pool of the client who sent the command
310
 *
311
 * @return 0 on success, negative error code on failure
312
 */
313
int Command::parse_request_bom_matrix(int pool_index) {
314
  char response_bom_matrix_buffer[MAX_RESPONSE_LEN];
315
  char temp_bom_matrix_buffer[MAX_RESPONSE_LEN];
316

    
317
  if (!connection_pool) {
318
    return -1;
319
  }
320

    
321
  int num_robots;
322
  int * xbee_ids;
323
        //get the sensor matrix from the wireless library
324
  int** bom_matrix = colonet_get_sensor_matrix(&num_robots, &xbee_ids);
325

    
326
  printf("number of robots is %d\n", num_robots);
327

    
328
        //copy the information over into a linear array to send to the client
329
  //TODO: make this better
330
  //TODO: make sure I don't need to do MAX_RESPONSE_LENGTH-1
331
  snprintf(response_bom_matrix_buffer, MAX_RESPONSE_LEN, "%d %d %d", RESPONSE_TO_CLIENT_REQUEST, REQUEST_BOM_MATRIX,
332
           num_robots);
333
  for (int i = 0; i < num_robots; i++) {
334
    for (int j = 0; j < num_robots; j++) {
335
      //TODO: don't use strcpy
336
      strcpy(temp_bom_matrix_buffer, response_bom_matrix_buffer);
337
      //TODO: put length checking in here so array doesn't go out of bounds
338
      //TODO: maybe use strncat?
339
      strcat(temp_bom_matrix_buffer," %d");
340
      snprintf(response_bom_matrix_buffer, MAX_RESPONSE_LEN, temp_bom_matrix_buffer, bom_matrix[i][j]);
341
    }
342
  }
343
  strcat(response_bom_matrix_buffer,"\n");
344

    
345
        //send the bom matrix to the client that requested it
346
  connection_pool->write_to_client(pool_index, response_bom_matrix_buffer, strlen(response_bom_matrix_buffer));
347
  //printf("Sending %s", response_bom_matrix_buffer);
348
  
349
  for (int i = 0; i < num_robots; i++) {
350
    free(bom_matrix[i]);
351
  }
352
  free(bom_matrix);
353

    
354
  free(xbee_ids);
355

    
356
  return 0;
357
}
358

    
359
/**
360
 * @brief This functions parses a client's request for the positions of robots
361
 * 
362
 * @param pool_index The index in the connection pool of the client that sent this command
363
 *
364
 * @return 0 on success, negative error code on failure
365
 */
366
int Command::parse_request_robot_positions(int pool_index) {
367
  //printf("*****parse_request_robot_positions\n");
368

    
369
        //ask the position monitor for where it sees robots
370
  map<int, VisionPosition> positions = colonet_server->getPositionMonitor()->getAllRobotPositions();
371

    
372
  map<int, VisionPosition>::iterator iter;
373

    
374
  char position_buffer[256];
375
  position_buffer[0] = 0;
376
  sprintf(position_buffer, "%d %d", RESPONSE_TO_CLIENT_REQUEST, CLIENT_REQUEST_ROBOT_POSITIONS);
377
 
378
  for (iter = positions.begin(); iter != positions.end(); iter++) {
379
    char tmpbuf[80];
380
    sprintf(tmpbuf, " %d %d %d", iter->first, iter->second.x, iter->second.y);
381
    strcat(position_buffer, tmpbuf);
382
  }
383

    
384
  strcat(position_buffer, "\n");
385

    
386
  //printf("position buffer is: %s\n", position_buffer);
387

    
388
        //send the positions of the robots to the client that requested them
389
  connection_pool->write_to_client(pool_index, position_buffer, strlen(position_buffer));
390

    
391
  return 0;
392
}
393

    
394
/**
395
 * @brief This functions parses a client's request for the ids of the xbees in the token ring
396
 *
397
 * @param pool_index The index in the connection pool of the client that requested the xbee ids
398
 *
399
 * @return 0 on success, negative error code on failure
400
 */
401
int Command::parse_request_xbee_ids(int pool_index) {
402
  char temp_xbee_id_buffer[MAX_RESPONSE_LEN];
403
  char xbee_id_buffer[MAX_RESPONSE_LEN];
404

    
405
  int num_robots;
406

    
407
        //get a list of the xbee ids
408
  int* xbee_ids = colonet_get_xbee_ids(&num_robots);
409

    
410
  //printf("num_robots: %d\n", num_robots);
411
  //printf("xbee_ids: ");
412
  //for (int i = 0; i < num_robots; i++) {
413
  //  printf("%d ", xbee_ids[i]);
414
  //}
415
  //printf("\n");
416

    
417
  if (!connection_pool) {
418
    return -1;
419
  }
420

    
421
  snprintf(xbee_id_buffer, MAX_RESPONSE_LEN, "%d %d %d", RESPONSE_TO_CLIENT_REQUEST, REQUEST_XBEE_IDS, num_robots);
422

    
423
        //copy over the ids into a response to send to the client
424
  for (int i = 0; i < num_robots; i++) {
425
    strcpy(temp_xbee_id_buffer, xbee_id_buffer);
426
    //TODO: put length checking in here so array doesn't go out of bounds
427
    //TODO: maybe use strncat?
428
    strcat(temp_xbee_id_buffer, " %d");
429
    snprintf(xbee_id_buffer, MAX_RESPONSE_LEN, temp_xbee_id_buffer, xbee_ids[i]);
430
  }
431
  strcat(xbee_id_buffer, "\n");
432

    
433
        //send the list of xbee ides to the client that requested them
434
  connection_pool->write_to_client(pool_index, xbee_id_buffer, strlen(xbee_id_buffer));
435
  //printf("Sending %s", xbee_id_buffer);
436

    
437
  free(xbee_ids);
438

    
439
  return 0;
440
}