Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (7.5 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 <stdlib.h>
14
#include <stdio.h>
15
#include <string.h>
16
#include <ctype.h>
17
#include "includes/Command.h"
18
#include "includes/ConnectionPool.h"
19

    
20
Command::Command(ConnectionPool * connection_pool_temp) {
21
  connection_pool = connection_pool_temp;
22
}
23

    
24
Command::~Command() {
25
}
26

    
27
int Command::parse_command(char* command, int pool_index) {
28
  char tokens[MAX_TOKENS][MAX_TOKEN_SIZE];
29
  int number_tokens = 0;
30
  char* end_pointer = NULL;
31
  int command_id;
32

    
33
  if (!connection_pool) {
34
    return -1;
35
  }
36

    
37
  if (!command) {
38
    return -1;
39
  }
40

    
41
  if (pool_index < 0) {
42
    return -1;
43
  }
44

    
45
  if ((number_tokens = tokenize_command(command, tokens)) < 0) {
46
    return -1;
47
  }
48

    
49
  //the 10 in the function call indicates number is base 10
50
  command_id = strtol(tokens[0], &end_pointer, 10);
51

    
52
  if (!end_pointer || *end_pointer != '\0') {
53
    printf("There was an error converting first token into a number.\n");
54
    return -1;
55
  }
56

    
57
  if (command_id == SEND_TO_ROBOT) {
58
    if (parse_send_to_robot(number_tokens, tokens)) {
59
      return -1;
60
    }
61
  } else if (command_id == REQUEST_FROM_SERVER) {
62
    if (parse_request_from_server(number_tokens, tokens, pool_index)) {
63
      return -1;
64
    }
65
  }
66

    
67
  return 0;
68
}
69

    
70
/**
71
 * @brief Breaks a command up into tokens
72
 *
73
 * @param command The command to tokenize
74
 * @param tokens A two dimensional character array to store the tokens in
75
 *
76
 * @return 0 on success, negative error code on failure
77
 */
78
int Command::tokenize_command(char* command, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE]) {
79
  char* next_token = command;
80
  char* end_token = NULL;
81
  int number_tokens = 0;
82

    
83
  if (!command) {
84
    return -1;
85
  }
86

    
87
  while ((end_token = strstr(next_token, " "))) {
88
    *end_token = '\0';
89

    
90
    if (strlen(next_token) > MAX_TOKEN_SIZE-1) {
91
      return -1;
92
    }
93

    
94
    strcpy(tokens[number_tokens], next_token);
95

    
96
    number_tokens++;
97
    next_token = end_token + 1;
98

    
99
    while (isspace(*next_token) && *next_token != '\0') {
100
      next_token++;
101
    }
102
  }
103

    
104
  if (end_token == NULL && *next_token != '\0') {
105
    if (strlen(next_token) > MAX_TOKEN_SIZE-1) {
106
      return -1;
107
    }
108

    
109
    strcpy(tokens[number_tokens], next_token);
110

    
111
    number_tokens++;
112
  }
113

    
114
  return number_tokens;
115
}
116

    
117

    
118
/** 
119
 * @brief checks a list of tokens to see if it's valid
120
 *
121
 * @param tokens The tokens to check
122
 * @param number_tokens The number of tokens contained in the tokens parameter
123
 *
124
 * @return 0 if tokens is valid
125
 */
126
int Command::check_tokens(unsigned char* tokens, int number_tokens) {
127
  if (number_tokens > 3 + PACKET_DATA_LEN) {
128
    /* Too many tokens */
129
    return -1;
130
  }
131

    
132
  if (number_tokens < 3) {
133
    /* Not enough tokens */
134
    return -1;
135
  }
136
  
137
  if (tokens[1] != COLONET_REQUEST && tokens[1] != COLONET_COMMAND) {
138
    /* Invalid message type */
139
    return -1;
140
  }
141

    
142
  return 0;
143
}
144

    
145

    
146

    
147

    
148

    
149

    
150
int Command::parse_send_to_robot(int number_tokens, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE]) {
151
  int i;
152
  unsigned char int_tokens[MAX_TOKENS];
153
  int number_int_tokens = number_tokens;
154
  unsigned char arguments[PACKET_DATA_LEN];
155

    
156
  memset(arguments, 1, PACKET_DATA_LEN);
157

    
158
  // Convert tokens to ints 
159
  for (i = ROBOT_COMMAND_OFFSET; i < number_int_tokens; i++) {
160
    int_tokens[i-ROBOT_COMMAND_OFFSET] = atoi(tokens[i]);
161
  }
162

    
163
  // Fill arguments buffer with arguments 
164
  for (i = ROBOT_COMMAND_LEN; i < number_int_tokens-ROBOT_COMMAND_OFFSET;
165
       i++) {
166
    arguments[i-ROBOT_COMMAND_LEN] = int_tokens[i];
167
  }
168

    
169
  // Check the tokens 
170
  if (check_tokens(int_tokens, number_int_tokens) < 0) {
171
    fprintf(stderr, "%s: Error - Invalid command/request.\n", __FUNCTION__);
172
    return -1;
173
  }
174

    
175
  /*  
176
  // Send packet to robot 
177
  fprintf(stderr, "Calling colonet_wl_send(%d, %d, %d, arguments)\n", 
178
  int_tokens[0], int_tokens[1], int_tokens[2]);
179
  colonet_wl_send(pool_index, int_tokens[0],
180
  (ColonetMessageType)int_tokens[1], int_tokens[2],
181
  arguments);
182
  */
183
  return 0;
184
}
185

    
186

    
187
int Command::parse_request_from_server(int number_tokens, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE], int pool_index) {
188
  char* end_pointer = NULL;
189

    
190
  if (!connection_pool) {
191
    return -1;
192
  }
193

    
194
  if (number_tokens < 2)
195
    return -1;
196

    
197
  end_pointer=NULL;
198
  int second_token = strtol(tokens[1], &end_pointer, 10);
199

    
200
  if (!end_pointer || *end_pointer != '\0') {
201
    printf("There was an error converting second token into a number.\n");
202
    return -1;
203
  }
204
    
205
  if (second_token == REQUEST_BOM_MATRIX) {
206
    if (parse_request_bom_matrix(pool_index)) {
207
      return -1;
208
    }
209
  } else if (second_token == REQUEST_XBEE_IDS) {
210
    if (parse_request_xbee_ids(pool_index)) {
211
      return -1;
212
    }
213
  } else {
214
    char * my_current_message = "Hi, how are you?\n";
215
    printf("Sending %s\n", my_current_message);
216
    connection_pool->write_to_client(pool_index, my_current_message, strlen(my_current_message));
217
  }
218

    
219
  return 0;
220
}
221

    
222
int Command::parse_request_bom_matrix(int pool_index) {
223
  char response_bom_matrix_buffer[MAX_RESPONSE_LEN];
224
  char temp_bom_matrix_buffer[MAX_RESPONSE_LEN];
225
 
226
  if (!connection_pool) {
227
    return -1;
228
  }
229

    
230
  //TODO: change after we start keeping track of bom matrix
231
  int number_robots = rand()%10;
232
  int bom_matrix[number_robots][number_robots];
233
  for (int ii = 0; ii < number_robots; ii++) {
234
    for (int j = 0; j < number_robots; j++) {
235
      //do this to generate some -1 values which mean they can't see each other
236
      int matrix_value = rand()%(number_robots+1)-1;
237
      bom_matrix[ii][j] = matrix_value;
238
    }
239
  }
240

    
241
  printf("number of robots is %d\n", number_robots);
242

    
243
  //TODO: make this better
244
  //TODO: make sure I don't need to do MAX_RESPONSE_LENGTH-1
245
  snprintf(response_bom_matrix_buffer,MAX_RESPONSE_LEN, "%d %d %d", RESPONSE_TO_CLIENT_REQUEST, REQUEST_BOM_MATRIX, number_robots);
246
  for (int ii = 0; ii < number_robots; ii++) {
247
    for (int j = 0; j < number_robots; j++) {
248
      //TODO: don't use strcpy
249
      strcpy(temp_bom_matrix_buffer, response_bom_matrix_buffer);
250
      //TODO: put length checking in here so array doesn't go out of bounds
251
      //TODO: maybe use strncat?
252
      strcat(temp_bom_matrix_buffer," %d");
253
      snprintf(response_bom_matrix_buffer, MAX_RESPONSE_LEN, temp_bom_matrix_buffer, bom_matrix[ii][j]);
254
    }
255
  }
256
  strcat(response_bom_matrix_buffer,"\n");
257
  connection_pool->write_to_client(pool_index, response_bom_matrix_buffer, strlen(response_bom_matrix_buffer));
258
  printf("Sending %s", response_bom_matrix_buffer);
259

    
260
  return 0;
261
}
262

    
263
int Command::parse_request_xbee_ids(int pool_index) {
264
  //TODO: make this better
265
  int number_robots = rand() % 10;
266

    
267
  char xbee_id_buffer[MAX_RESPONSE_LEN];
268
  char temp_xbee_id_buffer[MAX_RESPONSE_LEN];
269
      
270
  if (!connection_pool) {
271
    return -1;
272
  }
273

    
274
  snprintf(xbee_id_buffer, MAX_RESPONSE_LEN, "%d %d %d", RESPONSE_TO_CLIENT_REQUEST, REQUEST_XBEE_IDS, number_robots);
275
      
276
  printf("number of robots is %d\n", number_robots);
277

    
278
  for (int ii = 0; ii < number_robots; ii++) {
279
    strcpy(temp_xbee_id_buffer, xbee_id_buffer);
280
    //TODO: put length checking in here so array doesn't go out of bounds
281
    //TODO: maybe use strncat?
282
    strcat(temp_xbee_id_buffer, " %d");
283
    snprintf(xbee_id_buffer, MAX_RESPONSE_LEN, temp_xbee_id_buffer, ii);
284
  }
285
  strcat(xbee_id_buffer, "\n");
286
  connection_pool->write_to_client(pool_index, xbee_id_buffer, strlen(xbee_id_buffer));
287
  printf("Sending %s", xbee_id_buffer);
288

    
289
  return 0;
290
}