Project

General

Profile

Revision 130

Added by Jason knichel over 16 years ago

extracted some code in the Command class into their own private methods

View differences:

Command.cpp
51 51
      return -1;
52 52
    }
53 53
  } else if (command_id == REQUEST_FROM_SERVER) {
54
    if (number_tokens < 2)
54
    if (parse_request_from_server(number_tokens, tokens, connection_pool, pool_index)) {
55 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 56
    }
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 57
  }
129 58

  
130 59
  return 0;
......
245 174
  */
246 175
  return 0;
247 176
}
177

  
178

  
179
int Command::parse_request_from_server(int number_tokens, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE], ConnectionPool * connection_pool, int pool_index) {
180
  char* end_pointer = NULL;
181

  
182
  if (number_tokens < 2)
183
    return -1;
184

  
185
  end_pointer=NULL;
186
  int second_token = strtol(tokens[1], &end_pointer, 10);
187

  
188
  if (!end_pointer || *end_pointer != '\0') {
189
    printf("There was an error converting second token into a number.\n");
190
    return -1;
191
  }
192
    
193
  //TODO: move REQUEST_BOM_MATRIX stuff into a private function
194
  if (second_token == REQUEST_BOM_MATRIX) {
195
    //TODO: rename to indicate its actually the response message
196
    char bom_matrix_buffer[MAX_RESPONSE_LEN];
197
    char temp_bom_matrix_buffer[MAX_RESPONSE_LEN];
198
 
199
    //TODO: change after we start keeping track of bom matrix
200
    int number_robots = rand()%10;
201
    int bom_matrix[number_robots][number_robots];
202
    for (int ii = 0; ii < number_robots; ii++) {
203
      for (int j = 0; j < number_robots; j++) {
204
	//do this to generate some -1 values which mean they can't see each other
205
	int matrix_value = rand()%(number_robots+1)-1;
206
	bom_matrix[ii][j] = matrix_value;
207
      }
208
    }
209

  
210
    printf("number of robots is %d\n", number_robots);
211

  
212
    //TODO: separate parameters with spaces
213
    //TODO: make this better
214
    //TODO: make sure I don't need to do MAX_RESPONSE_LENGTH-1
215
    snprintf(bom_matrix_buffer,MAX_RESPONSE_LEN, "%d %d %d", RESPONSE_TO_CLIENT_REQUEST, REQUEST_BOM_MATRIX, number_robots);
216
    for (int ii = 0; ii < number_robots; ii++) {
217
      for (int j = 0; j < number_robots; j++) {
218
	//TODO: don't use strcpy
219
	strcpy(temp_bom_matrix_buffer, bom_matrix_buffer);
220
	//TODO: put length checking in here so array doesn't go out of bounds
221
	//TODO: maybe use strncat?
222
	strcat(temp_bom_matrix_buffer," %d");
223
	snprintf(bom_matrix_buffer, MAX_RESPONSE_LEN, temp_bom_matrix_buffer, bom_matrix[ii][j]);
224
      }
225
    }
226
    strcat(bom_matrix_buffer,"\n");
227
    connection_pool->write_to_client(pool_index, bom_matrix_buffer, strlen(bom_matrix_buffer));
228
    printf("Sending %s", bom_matrix_buffer);
229
  } else if (second_token == REQUEST_XBEE_IDS) {
230
    //TODO: move this into private function
231
    //TODO: make this better
232
    int number_robots = rand() % 10;
233

  
234
    char xbee_id_buffer[MAX_RESPONSE_LEN];
235
    char temp_xbee_id_buffer[MAX_RESPONSE_LEN];
236
      
237
    snprintf(xbee_id_buffer, MAX_RESPONSE_LEN, "%d %d %d", RESPONSE_TO_CLIENT_REQUEST, REQUEST_XBEE_IDS, number_robots);
238
      
239
    printf("number of robots is %d\n", number_robots);
240

  
241
    for (int ii = 0; ii < number_robots; ii++) {
242
      strcpy(temp_xbee_id_buffer, xbee_id_buffer);
243
      //TODO: put length checking in here so array doesn't go out of bounds
244
      //TODO: maybe use strncat?
245
      strcat(temp_xbee_id_buffer, " %d");
246
      snprintf(xbee_id_buffer, MAX_RESPONSE_LEN, temp_xbee_id_buffer, ii);
247
    }
248
    strcat(xbee_id_buffer, "\n");
249
    connection_pool->write_to_client(pool_index, xbee_id_buffer, strlen(xbee_id_buffer));
250
    printf("Sending %s", xbee_id_buffer);
251
  } else {
252
    char * my_current_message = "Hi, how are you?\n";
253
    printf("Sending %s\n", my_current_message);
254
    connection_pool->write_to_client(pool_index, my_current_message, strlen(my_current_message));
255
  }
256

  
257
  return 0;
258
}

Also available in: Unified diff