Project

General

Profile

Revision 79

Added by Chris Mar over 16 years ago

tried to merge lib_additions again...

View differences:

ConnectionPool.cpp
292 292
 *
293 293
 * @return void
294 294
 */
295
//TODO: put error checking on listen_socket to make sure its a valid socket
296 295
void ConnectionPool::set_listen_socket_in_ready_set(int listen_socket) {
296
  if (listen_socket < 0)
297
    return;
298

  
297 299
  FD_SET(listen_socket, &ready_set);
298 300
}
299 301

  
300 302
/**
301
 * @todo Since the listen socket is now a member of the class, the first parameter can be removed
302
 * @todo Does the select timeout need to be passed in?  or can it be specified in the method?
303
 *
304 303
 * @brief Find out what file descriptors are ready to write to and read from
305 304
 *
306 305
 * @param listen_socket The socket to listen on
......
308 307
 *
309 308
 * @return 0
310 309
 */
311
int ConnectionPool::perform_select(int listen_socket, struct timeval * select_timeout) {
310
int ConnectionPool::perform_select(int listen_socket) {
312 311
  read_set = ready_set;
313 312
  write_set = ready_set;
314 313

  
314
  struct timeval select_timeout;
315
  memset(&select_timeout, 0, sizeof(select_timeout));
316

  
315 317
  //TODO(Jason): think about why I put this there
316 318
  if (max_file_descriptor < listen_socket)
317 319
    max_file_descriptor = listen_socket;
318 320

  
319
  number_clients_ready = select(max_file_descriptor+1, &(read_set), &(write_set), NULL, select_timeout);
321
  number_clients_ready = select(max_file_descriptor+1, &(read_set), &(write_set), NULL, &select_timeout);
320 322

  
323
  if (number_clients_ready < 0) {
324
    perror(__FUNCTION__);
325
  }
326

  
321 327
  return 0;
322 328
}
323 329

  
......
369 375
  write_set = new_set;
370 376
}
371 377

  
372
//TODO: write a function to write data into the write buffers
373
//TODO: fix names of variables to obey new style
378
//TODO: write a function to write data into the write buffers (jason: what was this referring to?)
374 379
int ConnectionPool::parse_command(char* command, int pool_index, ColonetWireless * wireless) {
375 380
  char tokens[MAX_TOKENS][MAX_TOKEN_SIZE];
376
  unsigned cnhar int_tokens[MAX_TOKENS];
377
  int numTokens = 0;
378
  char* endptr = NULL;
379
  int commandID;
381
  unsigned char int_tokens[MAX_TOKENS];
382
  int number_tokens = 0;
383
  char* end_pointer = NULL;
384
  int command_id;
380 385
  int i;
381
  unsigned char args[PACKET_DATA_LEN];
386
  unsigned char arguments[PACKET_DATA_LEN];
382 387

  
383
  memset(args, 1, PACKET_DATA_LEN);
388
  memset(arguments, 1, PACKET_DATA_LEN);
384 389

  
385 390
  if (!command) {
386 391
    return -1;
......
394 399
    return -1;
395 400
  }
396 401

  
397
  if ((numTokens = tokenize_command(command, tokens)) < 0) {
402
  if ((number_tokens = tokenize_command(command, tokens)) < 0) {
398 403
    return -1;
399 404
  }
400 405

  
401 406
  //the 10 in the function call indicates number is base 10
402
  commandID = strtol(tokens[0], &endptr, 10);
407
  command_id = strtol(tokens[0], &end_pointer, 10);
403 408

  
404
  if (!endptr || *endptr != '\0') {
409
  if (!end_pointer || *end_pointer != '\0') {
405 410
    printf("There was an error converting first token into a number.\n");
406 411
    return -1;
407 412
  }
408 413

  
409
  if (commandID == SEND_TO_ROBOT || REQUEST_FROM_ROBOT) {
410
    int numIntTokens = numTokens;
414
  if (command_id == SEND_TO_ROBOT) {
415
    int number_int_tokens = number_tokens;
411 416

  
412
    /* Convert tokens to ints */
413
    for (i = ROBOT_COMMAND_OFFSET; i < numIntTokens; i++) {
417
    // Convert tokens to ints 
418
    for (i = ROBOT_COMMAND_OFFSET; i < number_int_tokens; i++) {
414 419
      int_tokens[i-ROBOT_COMMAND_OFFSET] = atoi(tokens[i]);
415 420
    }
416
    if (commandID == REQUEST_FROM_ROBOT) {
421
    /* we removed the request from robot constant
422
    if (command_id == REQUEST_FROM_ROBOT) {
417 423
      if (i < MAX_TOKENS) {
418 424
        for (;i >= 4; i--) {
419 425
          int_tokens[i] = int_tokens[i-1];
420 426
        }
421 427
        int_tokens[3] = pool_index;
422
        numIntTokens++;
428
        number_int_tokens++;
423 429
      } else {
424 430
        //TODO: send an error back to client here also
425 431
        fprintf(stderr, "Client attempted to request data from the robot but there was not enough room to put in the client's id.\n");
426 432
        return -1;
427 433
      }
428 434
    }
435
    */
429 436

  
430
    /* Fill args buffer with args */
431
    for (i = ROBOT_COMMAND_LEN; i < numIntTokens-ROBOT_COMMAND_OFFSET; i++) {
432
      args[i-ROBOT_COMMAND_LEN] = int_tokens[i];
437
    // Fill arguments buffer with arguments 
438
    for (i = ROBOT_COMMAND_LEN; i < number_int_tokens-ROBOT_COMMAND_OFFSET; i++) {
439
      arguments[i-ROBOT_COMMAND_LEN] = int_tokens[i];
433 440
    }
434 441

  
435
    /* Check the tokens */
436
    if (check_tokens(int_tokens, numIntTokens) < 0) {
442
    // Check the tokens 
443
    if (check_tokens(int_tokens, number_int_tokens) < 0) {
437 444
      fprintf(stderr, "%s: Error - Invalid command/request.\n", __FUNCTION__);
438 445
      return 0;
439 446
    }
440 447
  
441
    /* Send packet to robot */
442
    fprintf(stderr, "Calling wireless->send(%d, %d, %d, args)\n", 
448
    // Send packet to robot 
449
    fprintf(stderr, "Calling wireless->send(%d, %d, %d, arguments)\n", 
443 450
            int_tokens[0], int_tokens[1], int_tokens[2]);
444
    wireless->send(int_tokens[0], int_tokens[1], int_tokens[2], args);
451
    wireless->send(int_tokens[0], int_tokens[1], int_tokens[2], arguments);
452
  } else if (command_id == REQUEST_FROM_SERVER) {
453
    if (number_tokens < 2)
454
      return -1;
455

  
456
    end_pointer=NULL;
457
    int second_token = strtol(tokens[1], &end_pointer, 10);
458

  
459
    if (!end_pointer || *end_pointer != '\0') {
460
      printf("There was an error converting second token into a number.\n");
461
      return -1;
462
    }
463
    
464
    if (second_token == REQUEST_BOM_MATRIX) {
465
      //TODO: rename to indicate its actually the response message
466
      char bom_matrix_buffer[MAX_RESPONSE_LEN];
467
      char temp_bom_matrix_buffer[MAX_RESPONSE_LEN];
468
 
469
      //TODO: change after we start keeping track of bom matrix
470
      int number_robots = rand()%10;
471
      int bom_matrix[number_robots][number_robots];
472
      for (int ii = 0; ii < number_robots; ii++) {
473
        for (int j = 0; j < number_robots; j++) {
474
          //do this to generate some -1 values which mean they can't see each other
475
          int matrix_value = rand()%(number_robots+1)-1;
476
          bom_matrix[ii][j] = matrix_value;
477
        }
478
      }
479

  
480
      printf("number of robots is %d\n", number_robots);
481

  
482
      //TODO: separate parameters with spaces
483
      //TODO: make this better
484
      //TODO: make sure I don't need to do MAX_RESPONSE_LENGTH-1
485
      snprintf(bom_matrix_buffer,MAX_RESPONSE_LEN, "%d %d %d", RESPONSE_TO_CLIENT_REQUEST, REQUEST_BOM_MATRIX, number_robots);
486
      for (int ii = 0; ii < number_robots; ii++) {
487
        for (int j = 0; j < number_robots; j++) {
488
          //TODO: don't use strcpy
489
          strcpy(temp_bom_matrix_buffer, bom_matrix_buffer);
490
          //TODO: put length checking in here so array doesn't go out of bounds
491
          //TODO: maybe use strncat?
492
          printf("Buffer is %s\n", bom_matrix_buffer);
493
          strcat(temp_bom_matrix_buffer," %d");
494
          snprintf(bom_matrix_buffer, MAX_RESPONSE_LEN, temp_bom_matrix_buffer, bom_matrix[ii][j]);
495
        }
496
      }
497
      strcat(bom_matrix_buffer,"\n");
498
      write_to_client(pool_index, bom_matrix_buffer, strlen(bom_matrix_buffer));
499
      printf("Sending %s", bom_matrix_buffer);
500
    } else {
501
      char * my_current_message = "Hi, how are you?\n";
502
      printf("Sending %s\n", my_current_message);
503
      write_to_client(pool_index, my_current_message, strlen(my_current_message));
504
    }
445 505
  }
446 506

  
447 507
  return 0;
448 508
}
449 509

  
510
/**
511
 * @brief Breaks a command up into tokens
512
 *
513
 * @param command The command to tokenize
514
 * @param tokens A two dimensional character array to store the tokens in
515
 *
516
 * @return 0 on success, negative error code on failure
517
 */
450 518
int ConnectionPool::tokenize_command(char* command, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE]) {
451
  char* nextToken = command;
452
  char* endToken = NULL;
453
  int numTokens = 0;
519
  char* next_token = command;
520
  char* end_token = NULL;
521
  int number_tokens = 0;
454 522

  
455 523
  if (!command) {
456 524
    return -1;
457 525
  }
458 526

  
459
  while ((endToken = strstr(nextToken, " "))) {
460
    *endToken = '\0';
527
  while ((end_token = strstr(next_token, " "))) {
528
    *end_token = '\0';
461 529

  
462
    if (strlen(nextToken) > MAX_TOKEN_SIZE-1) {
530
    if (strlen(next_token) > MAX_TOKEN_SIZE-1) {
463 531
      return -1;
464 532
    }
465 533

  
466
    strcpy(tokens[numTokens], nextToken);
534
    strcpy(tokens[number_tokens], next_token);
467 535

  
468
    numTokens++;
469
    nextToken = endToken + 1;
536
    number_tokens++;
537
    next_token = end_token + 1;
470 538

  
471
    while (isspace(*nextToken) && *nextToken != '\0') {
472
      nextToken++;
539
    while (isspace(*next_token) && *next_token != '\0') {
540
      next_token++;
473 541
    }
474 542
  }
475 543

  
476
  if (endToken == NULL && *nextToken != '\0') {
477
    if (strlen(nextToken) > MAX_TOKEN_SIZE-1) {
544
  if (end_token == NULL && *next_token != '\0') {
545
    if (strlen(next_token) > MAX_TOKEN_SIZE-1) {
478 546
      return -1;
479 547
    }
480 548

  
481
    strcpy(tokens[numTokens], nextToken);
549
    strcpy(tokens[number_tokens], next_token);
482 550

  
483
    numTokens++;
551
    number_tokens++;
484 552
  }
485 553

  
486
  return numTokens;
554
  return number_tokens;
487 555
}
488 556

  
489
/** @brief checks a list of tokens to see if it's valid
557
/** 
558
 * @brief checks a list of tokens to see if it's valid
490 559
 *
560
 * @param tokens The tokens to check
561
 * @param number_tokens The number of tokens contained in the tokens parameter
562
 *
491 563
 * @return 0 if tokens is valid
492 564
 */
493
int ConnectionPool::check_tokens(unsigned char* tokens, int numTokens) {
494
  if (numTokens > 3 + PACKET_DATA_LEN) {
565
int ConnectionPool::check_tokens(unsigned char* tokens, int number_tokens) {
566
  if (number_tokens > 3 + PACKET_DATA_LEN) {
495 567
    /* Too many tokens */
496 568
    return -1;
497 569
  }
498 570

  
499
  if (numTokens < 3) {
571
  if (number_tokens < 3) {
500 572
    /* Not enough tokens */
501 573
    return -1;
502 574
  }

Also available in: Unified diff