Project

General

Profile

Revision 22

Added by Jason knichel over 16 years ago

moved around some more code

View differences:

trunk/code/projects/colonet/ColonetServer/client.cpp
19 19
#include "includes/ConnectionPool.h"
20 20

  
21 21
/* Globals */
22
static ColonetWireless* wireless;
22
extern ColonetWireless* wireless;
23 23
extern ConnectionPool * connection_pool;
24 24

  
25 25

  
......
38 38
    printf("response\n");
39 39

  
40 40
    int dest = (int)(pkt->msg_dest);
41
    if (dest < connection_pool->get_next_available_slot())
42
    {
43
      char buffer[WRITE_BUFFER_SIZE];
41

  
42
    char buffer[WRITE_BUFFER_SIZE];
44 43
          
45
      int value = (int)(pkt->data[0]);
46
      snprintf(buffer, WRITE_BUFFER_SIZE, "%d\n", value);
44
    int value = (int)(pkt->data[0]);
45
    snprintf(buffer, WRITE_BUFFER_SIZE, "%d\n", value);
47 46
          
48
      int len = strlen(buffer);
49
      connection_pool->write_to_client(dest, buffer, len);
50
      printf("Put data in write buffer for client.\n");
51
    }
52
    else
53
    {
54
      printf("The robot wanted to pass the data to a client not in our pool.\n");
47
    int len = strlen(buffer);
48
    if (connection_pool->write_to_client(dest, buffer, len) == ERROR_INVALID_CLIENT_ID) {
49
      printf("The robot wanted to pass the data to a client not in the pool.\n");
55 50
      return -1;
56 51
    }
52

  
53
    printf("Put data in write buffer for client.\n");
57 54
  }
58 55
  else
59 56
  {
......
64 61
  return 0;
65 62
}
66 63

  
67
int initWireless()
68
{
69
  char* log_filename = NULL;
70

  
71
  if (optionsG.logging_enabled) {
72
    log_filename = optionsG.log_filename;
73
  }
74

  
75
  wireless = new ColonetWireless(optionsG.wireless_port, 
76
                                 wirelessMessageHandler, log_filename, 
77
                                 /*!optionsG.listener_mode*/false, true);
78
  //Note: last arg set to true ignores token ring;  in general, this should
79
  //probably be false (changed for demo purposes)
80

  
81
  if (!wireless->run_listener_thread()) {
82
		return -1;
83
	}
84

  
85
	return 0;
86
}
87

  
88
void send_wireless_message(unsigned char msg_dest, unsigned char msg_type, 
89
                          unsigned char msg_code, unsigned char* data) {
90
  wireless->send(msg_dest, msg_type, msg_code, data);
91
}
trunk/code/projects/colonet/ColonetServer/includes/ConnectionPool.h
8 8

  
9 9
#include <sys/select.h>
10 10

  
11
#include <colonet_wireless.h>
12

  
11 13
#define MAX_TOKENS 15
12 14
#define MAX_TOKEN_SIZE 30
13 15

  
......
17 19
#define ERROR_INVALID_CLIENT_DESCRIPTOR -1
18 20
#define ERROR_TOO_MANY_CLIENTS          -2
19 21
#define ERROR_ALLOCATING_MEMORY         -3
22
#define ERROR_NOT_ENOUGH_ROOM           -4
23
#define ERROR_INVALID_COMMAND           -5
24
#define ERROR_INVALID_CLIENT_ID         -6
25
#define ERROR_INVALID_MESSAGE           -7
26
#define ERROR_INVALID_MESSAGE_LENGTH    -8
20 27

  
28

  
21 29
#define MAX_CONNECTIONS 250
22 30
#define READ_BUFFER_SIZE 1024
23 31
#define WRITE_BUFFER_SIZE 1024
......
30 38

  
31 39
  int add_client(int client_file_descriptor);
32 40
  int remove_client(int client_file_descriptor);
33
  int check_clients();
41
  int check_clients(ColonetWireless * wireless);
34 42
  int write_to_client(int pool_index, char * message, int length);
35 43
  void set_listen_socket_in_ready_set(int listen_socket);
36 44
  int perform_select(int listen_socket, struct timeval * select_timeout);
......
67 75
  char * write_buffer[MAX_CONNECTIONS];
68 76
  int write_buffer_size[MAX_CONNECTIONS];
69 77

  
70
  int parse_command(char* command, int pool_index);
78
  int parse_command(char* command, int pool_index, ColonetWireless * wireless);
71 79
  int tokenize_command(char* command, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE]);
72 80
  int check_tokens(unsigned char* tokens, int numTokens);
73 81
};
trunk/code/projects/colonet/ColonetServer/includes/ColonetServer.h
25 25
private:
26 26
  ConnectionPool connection_pool;
27 27
  Log logger;
28

  
29
  int initialize_wireless();
30

  
31

  
28 32
};
29 33

  
30 34
#endif
trunk/code/projects/colonet/ColonetServer/ConnectionPool.cpp
114 114
//TODO: test that it drops commands properly if it gets sent too much data
115 115
//      do we want it to drop the data or drop the connection?
116 116
//TODO: give variables a better name
117
int ConnectionPool::check_clients() {
117
int ConnectionPool::check_clients(ColonetWireless * wireless) {
118 118
  char temp[READ_BUFFER_SIZE];
119 119
  char tempCommand[READ_BUFFER_SIZE+1];
120 120
  int i;
......
197 197
            break;
198 198
          }
199 199

  
200
          if (parse_command(tempCommand, i) < 0) {
200
          if (parse_command(tempCommand, i, wireless) < 0) {
201 201
            printf("There was an error parsing command\n");
202 202
            break;
203 203
          }
......
220 220
}
221 221

  
222 222
int ConnectionPool::write_to_client(int pool_index, char * message, int length) {
223
  //TODO: put error checking on pool_index, message, length
223
  if (pool_index < 0 || pool_index >= next_available_slot) {
224
    return ERROR_INVALID_CLIENT_ID;
225
  }
226

  
227
  if (!message) {
228
    return ERROR_INVALID_MESSAGE;
229
  }
230

  
231
  if (length < 0) {
232
    return ERROR_INVALID_MESSAGE_LENGTH;
233
  }
234

  
224 235
  if (length > (WRITE_BUFFER_SIZE-write_buffer_size[pool_index]))
225 236
    {
226 237
      //TODO: make this a logging statement instead of a print statement
227 238
      printf("There is not enough room in the write buffer to send the data to the client.\n");
228
      return -1;
239
      return ERROR_NOT_ENOUGH_ROOM;
229 240
    }
230 241

  
231 242
  memcpy(write_buffer[pool_index], message, length);
......
303 314

  
304 315
//TODO: write a function to write data into the write buffers
305 316
//TODO: fix names of variables to obey new style
306
int ConnectionPool::parse_command(char* command, int pool_index) {
317
int ConnectionPool::parse_command(char* command, int pool_index, ColonetWireless * wireless) {
307 318
  char tokens[MAX_TOKENS][MAX_TOKEN_SIZE];
308 319
  unsigned char int_tokens[MAX_TOKENS];
309 320
  int numTokens = 0;
......
373 384
    /* Send packet to robot */
374 385
    fprintf(stderr, "Calling wireless->send(%d, %d, %d, args)\n", 
375 386
            int_tokens[0], int_tokens[1], int_tokens[2]);
376
    send_wireless_message(int_tokens[0], int_tokens[1], int_tokens[2], args);
387
    wireless->send(int_tokens[0], int_tokens[1], int_tokens[2], args);
377 388
  }
378 389

  
379 390
  return 0;
trunk/code/projects/colonet/ColonetServer/ColonetServer.cpp
27 27
#define LOG_BUFFER_LENGTH 128
28 28

  
29 29
ConnectionPool * connection_pool;
30
ColonetWireless* wireless;
30 31

  
31 32
ColonetServer::ColonetServer(): logger("logFile.txt") {
32 33
}
......
42 43
  if (initConnection(optionsG.listen_port) < 0)
43 44
    return -1;
44 45

  
45
  if (initWireless() < 0) {
46
  if (initialize_wireless() < 0) {
46 47
    fprintf(stderr, "%s: initWireless failed\n", __FUNCTION__);
47 48
    return -1;
48 49
  }
......
50 51
  return 0;
51 52
}
52 53

  
54
int ColonetServer::initialize_wireless()
55
{
56
  char* log_filename = NULL;
57

  
58
  if (optionsG.logging_enabled) {
59
    log_filename = optionsG.log_filename;
60
  }
61

  
62
  wireless = new ColonetWireless(optionsG.wireless_port, 
63
                                 wirelessMessageHandler, log_filename, 
64
                                 /*!optionsG.listener_mode*/false, true);
65
  //Note: last arg set to true ignores token ring;  in general, this should
66
  //probably be false (changed for demo purposes)
67

  
68
  if (!wireless->run_listener_thread()) {
69
		return -1;
70
	}
71

  
72
	return 0;
73
}
74

  
53 75
int ColonetServer::log_error(char * error_message) {
54 76
  return logger.logMessage(LOG_TYPE_ERROR, error_message);
55 77
}
......
145 167
        
146 168
      }
147 169

  
148
      if (connection_pool.check_clients() < 0) {
170
      if (connection_pool.check_clients(wireless) < 0) {
149 171
        printf("\tThere was an error trying to update the clients.");
150 172
        continue;
151 173
      }
......
158 180
ConnectionPool * ColonetServer::get_connection_pool_pointer() {
159 181
  return &connection_pool;
160 182
}
183

  
184

  

Also available in: Unified diff