Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / ColonetServer / Command.cpp @ 129

History | View | Annotate | Download (6.9 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 (number_tokens < 2)
55
      return -1;
56

    
57
    end_pointer=NULL;
58
    int second_token = strtol(tokens[1], &end_pointer, 10);
59

    
60
    if (!end_pointer || *end_pointer != '\0') {
61
      printf("There was an error converting second token into a number.\n");
62
      return -1;
63
    }
64
    
65
    //TODO: move REQUEST_BOM_MATRIX stuff into a private function
66
    if (second_token == REQUEST_BOM_MATRIX) {
67
      //TODO: rename to indicate its actually the response message
68
      char bom_matrix_buffer[MAX_RESPONSE_LEN];
69
      char temp_bom_matrix_buffer[MAX_RESPONSE_LEN];
70
 
71
      //TODO: change after we start keeping track of bom matrix
72
      int number_robots = rand()%10;
73
      int bom_matrix[number_robots][number_robots];
74
      for (int ii = 0; ii < number_robots; ii++) {
75
        for (int j = 0; j < number_robots; j++) {
76
          //do this to generate some -1 values which mean they can't see each other
77
          int matrix_value = rand()%(number_robots+1)-1;
78
          bom_matrix[ii][j] = matrix_value;
79
        }
80
      }
81

    
82
      printf("number of robots is %d\n", number_robots);
83

    
84
      //TODO: separate parameters with spaces
85
      //TODO: make this better
86
      //TODO: make sure I don't need to do MAX_RESPONSE_LENGTH-1
87
      snprintf(bom_matrix_buffer,MAX_RESPONSE_LEN, "%d %d %d", RESPONSE_TO_CLIENT_REQUEST, REQUEST_BOM_MATRIX, number_robots);
88
      for (int ii = 0; ii < number_robots; ii++) {
89
        for (int j = 0; j < number_robots; j++) {
90
          //TODO: don't use strcpy
91
          strcpy(temp_bom_matrix_buffer, bom_matrix_buffer);
92
          //TODO: put length checking in here so array doesn't go out of bounds
93
          //TODO: maybe use strncat?
94
          strcat(temp_bom_matrix_buffer," %d");
95
          snprintf(bom_matrix_buffer, MAX_RESPONSE_LEN, temp_bom_matrix_buffer, bom_matrix[ii][j]);
96
        }
97
      }
98
      strcat(bom_matrix_buffer,"\n");
99
      connection_pool->write_to_client(pool_index, bom_matrix_buffer, strlen(bom_matrix_buffer));
100
      printf("Sending %s", bom_matrix_buffer);
101
    } else if (second_token == REQUEST_XBEE_IDS) {
102
      //TODO: move this into private function
103
      //TODO: make this better
104
      int number_robots = rand() % 10;
105

    
106
      char xbee_id_buffer[MAX_RESPONSE_LEN];
107
      char temp_xbee_id_buffer[MAX_RESPONSE_LEN];
108
      
109
      snprintf(xbee_id_buffer, MAX_RESPONSE_LEN, "%d %d %d", RESPONSE_TO_CLIENT_REQUEST, REQUEST_XBEE_IDS, number_robots);
110
      
111
      printf("number of robots is %d\n", number_robots);
112

    
113
      for (int ii = 0; ii < number_robots; ii++) {
114
        strcpy(temp_xbee_id_buffer, xbee_id_buffer);
115
        //TODO: put length checking in here so array doesn't go out of bounds
116
        //TODO: maybe use strncat?
117
        strcat(temp_xbee_id_buffer, " %d");
118
        snprintf(xbee_id_buffer, MAX_RESPONSE_LEN, temp_xbee_id_buffer, ii);
119
      }
120
      strcat(xbee_id_buffer, "\n");
121
      connection_pool->write_to_client(pool_index, xbee_id_buffer, strlen(xbee_id_buffer));
122
      printf("Sending %s", xbee_id_buffer);
123
    } else {
124
      char * my_current_message = "Hi, how are you?\n";
125
      printf("Sending %s\n", my_current_message);
126
      connection_pool->write_to_client(pool_index, my_current_message, strlen(my_current_message));
127
    }
128
  }
129

    
130
  return 0;
131
}
132

    
133
/**
134
 * @brief Breaks a command up into tokens
135
 *
136
 * @param command The command to tokenize
137
 * @param tokens A two dimensional character array to store the tokens in
138
 *
139
 * @return 0 on success, negative error code on failure
140
 */
141
int Command::tokenize_command(char* command, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE]) {
142
  char* next_token = command;
143
  char* end_token = NULL;
144
  int number_tokens = 0;
145

    
146
  if (!command) {
147
    return -1;
148
  }
149

    
150
  while ((end_token = strstr(next_token, " "))) {
151
    *end_token = '\0';
152

    
153
    if (strlen(next_token) > MAX_TOKEN_SIZE-1) {
154
      return -1;
155
    }
156

    
157
    strcpy(tokens[number_tokens], next_token);
158

    
159
    number_tokens++;
160
    next_token = end_token + 1;
161

    
162
    while (isspace(*next_token) && *next_token != '\0') {
163
      next_token++;
164
    }
165
  }
166

    
167
  if (end_token == NULL && *next_token != '\0') {
168
    if (strlen(next_token) > MAX_TOKEN_SIZE-1) {
169
      return -1;
170
    }
171

    
172
    strcpy(tokens[number_tokens], next_token);
173

    
174
    number_tokens++;
175
  }
176

    
177
  return number_tokens;
178
}
179

    
180

    
181
/** 
182
 * @brief checks a list of tokens to see if it's valid
183
 *
184
 * @param tokens The tokens to check
185
 * @param number_tokens The number of tokens contained in the tokens parameter
186
 *
187
 * @return 0 if tokens is valid
188
 */
189
int Command::check_tokens(unsigned char* tokens, int number_tokens) {
190
  if (number_tokens > 3 + PACKET_DATA_LEN) {
191
    /* Too many tokens */
192
    return -1;
193
  }
194

    
195
  if (number_tokens < 3) {
196
    /* Not enough tokens */
197
    return -1;
198
  }
199
  
200
  if (tokens[1] != COLONET_REQUEST && tokens[1] != COLONET_COMMAND) {
201
    /* Invalid message type */
202
    return -1;
203
  }
204

    
205
  return 0;
206
}
207

    
208

    
209

    
210

    
211

    
212

    
213
int Command::parse_send_to_robot(int number_tokens, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE]) {
214
  int i;
215
  unsigned char int_tokens[MAX_TOKENS];
216
  int number_int_tokens = number_tokens;
217
  unsigned char arguments[PACKET_DATA_LEN];
218

    
219
  memset(arguments, 1, PACKET_DATA_LEN);
220

    
221
  // Convert tokens to ints 
222
  for (i = ROBOT_COMMAND_OFFSET; i < number_int_tokens; i++) {
223
    int_tokens[i-ROBOT_COMMAND_OFFSET] = atoi(tokens[i]);
224
  }
225

    
226
  // Fill arguments buffer with arguments 
227
  for (i = ROBOT_COMMAND_LEN; i < number_int_tokens-ROBOT_COMMAND_OFFSET;
228
       i++) {
229
    arguments[i-ROBOT_COMMAND_LEN] = int_tokens[i];
230
  }
231

    
232
  // Check the tokens 
233
  if (check_tokens(int_tokens, number_int_tokens) < 0) {
234
    fprintf(stderr, "%s: Error - Invalid command/request.\n", __FUNCTION__);
235
    return -1;
236
  }
237

    
238
  /*  
239
  // Send packet to robot 
240
  fprintf(stderr, "Calling colonet_wl_send(%d, %d, %d, arguments)\n", 
241
  int_tokens[0], int_tokens[1], int_tokens[2]);
242
  colonet_wl_send(pool_index, int_tokens[0],
243
  (ColonetMessageType)int_tokens[1], int_tokens[2],
244
  arguments);
245
  */
246
  return 0;
247
}