Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / ColonetServer / ColonetServer.cpp @ 334

History | View | Annotate | Download (7.03 KB)

1
/**
2
 * @file ColonetServer.cpp
3
 *
4
 * @brief colonet_server - primary server application for Colonet
5
 *
6
 * @author Jason Knichel
7
 * @author Eugene Marinelli
8
 * @date 10/31/06
9
 */
10

    
11
#include <arpa/inet.h>
12
#include <fcntl.h>
13
#include <errno.h>
14
#include <netinet/in.h>
15
#include <string.h>
16
#include <sys/select.h>
17
#include <sys/socket.h>
18

    
19
#include <colonet_wireless.h>
20

    
21
#include "includes/ColonetServer.h"
22
#include "includes/ConnectionPool.h"
23
#include "includes/wirelessMessageHandler.h"
24
#include "includes/options.h"
25
#include "includes/Log.h"
26

    
27
#define LISTEN_BACKLOG 5
28
#define LOG_BUFFER_LENGTH 128
29

    
30
ConnectionPool * connection_pool;
31

    
32
/**
33
 * @brief Default constructor for ColonetServer
34
 */
35
ColonetServer::ColonetServer(): logger("logFile.txt") {
36
  listen_socket = 0;
37
}
38

    
39
/**
40
 * @brief Destructor for ColonetServer
41
 */
42
ColonetServer::~ColonetServer() {
43
}
44

    
45
/**
46
 * @brief Initializes the various elements needed for the server to run
47
 *
48
 * @param argc The number of command line arguments passed to the program
49
 * @param argv The command line arguments passed to the program
50
 *
51
 * @return 0 on success, negative error code on failure
52
 */
53
int ColonetServer::initialize_server(int argc, char * argv[]) {
54
  printf("Initializing Server...\n");
55

    
56
  parseCmdLine(argc, argv);
57

    
58
  if (initialize_connection(optionsG.listen_port) < 0) {
59
    return -1;
60
  }
61

    
62
  if (initialize_wireless() < 0) {
63
    fprintf(stderr, "%s: initWireless failed\n", __FUNCTION__);
64
    return -1;
65
  }
66

    
67
  return 0;
68
}
69

    
70
/**
71
 * @brief Starts the server listening on the socket that was opened for listening
72
 *
73
 * @return 0 on success, negative error code on failure
74
 */
75
int ColonetServer::start_listening() {
76
  if (listen(listen_socket, LISTEN_BACKLOG) < 0) {
77
    logger.log_error("\t\nThere was an error telling the socket to "
78
                     "listen for connections from clients.  Terminating Server...\n");
79
    return -1;
80
  }
81
  return 0;
82
}
83

    
84

    
85
/**
86
 * @brief Starts the server running (starts an infinite loop)
87
 */
88
int ColonetServer::run_server() {
89
  connection_pool.add_new_socket_to_pool(listen_socket);
90

    
91
  int accept_socket = 0;
92
  struct sockaddr_in client_addr;
93
  socklen_t client_addr_size = sizeof(client_addr);
94

    
95
  logger.log_message("Server initialized.  About to start listening for connections");
96

    
97
  while(1) {
98
    connection_pool.perform_select(listen_socket);
99

    
100
    //either no descriptors are ready or there was an error
101
    if (connection_pool.get_number_clients_ready() <= 0) {
102
      continue;
103
    }
104

    
105
    if (connection_pool.is_socket_ready_to_read(listen_socket)) {
106
      printf("Something is trying to connect...\n");
107
      if ((accept_socket = accept(listen_socket, (struct sockaddr*) &client_addr, &client_addr_size)) < 0) {
108
        if (errno == EMFILE) {
109
          printf("\tWhen attempting to accept a connection, "
110
                 "reached the per process limit of file descriptors."
111
                 "  Dropping the new connection.\n");
112
          continue;
113
        } else {
114
          printf("\tThere was an error when attempting to accept a connection");
115
        }
116
        continue;
117
      }
118

    
119
      char log_buffer[LOG_BUFFER_LENGTH];
120
      snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s attempting to connect.",
121
               inet_ntoa(client_addr.sin_addr));
122
      logger.log_string(LOG_TYPE_CONNECT, log_buffer);
123

    
124
      if (connection_pool.add_client(accept_socket) < 0) {
125
        printf("\tThere was an error when trying to add a client to the connection pool.");
126
        continue;
127
      }
128

    
129
      snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s successfully added to connection pool.",
130
               inet_ntoa(client_addr.sin_addr));
131
      logger.log_string(LOG_TYPE_CONNECT, log_buffer);
132
    }
133

    
134
    if (connection_pool.check_clients() < 0) {
135
      printf("\tThere was an error trying to update the clients.");
136
      continue;
137
    }
138
  }
139

    
140
  return 0;
141
}
142

    
143
int ColonetServer::process_received_wireless_message(unsigned char type, short source, int dest,
144
                                                     unsigned char * data, int len) {
145
  if (type == COLONET_RESPONSE) {
146
    printf("response\n");
147

    
148
    /*
149
    //Eugene's code
150
    char buffer[WRITE_BUFFER_SIZE];
151

152
    int value = (int)(data[0]);
153
    snprintf(buffer, WRITE_BUFFER_SIZE, "%d\n", value);
154

155
    int buflen = strlen(buffer);
156
    */
157

    
158
    //Greg's code
159
    char * buffer = (char *) &(data[5]);
160
    int buflen = strlen(buffer);
161
    buffer[buflen] = '\n';
162
    buflen++;
163

    
164
    if (connection_pool.write_to_client(dest, buffer, buflen) == ERROR_INVALID_CLIENT_ID) {
165
      printf("The robot wanted to pass the data to a client not in the pool.\n");
166
      return -1;
167
    }
168

    
169
    printf("Put data in write buffer for client.\n");
170
  } else {
171
    printf("not a response\n");
172
    return -1;
173
  }
174

    
175
  return 0;
176
}
177

    
178

    
179
/**
180
 * @brief Initializes the wireless
181
 *
182
 * @return 0 on success, negative error code on error
183
 */
184
int ColonetServer::initialize_wireless() {
185
  char* log_filename = NULL;
186

    
187
  if (optionsG.logging_enabled) {
188
    log_filename = optionsG.log_filename;
189
  }
190

    
191
  colonet_wl_init(optionsG.wireless_port, wirelessMessageHandler,
192
                  log_filename);
193

    
194
  if (colonet_wl_run_listener_thread()) {
195
    fprintf(stderr, "%s: colonet_wl_run_listener_thread failed.\n",
196
            __FUNCTION__);
197
    return -1;
198
  }
199

    
200
  return 0;
201
}
202

    
203
/**
204
 * @brief Initialize a connection to listen on
205
 *
206
 * @port The port to try to open to listen on
207
 *
208
 * @return 0 on success, negative error code on error
209
 */
210
int ColonetServer::initialize_connection(int port) {
211
  printf("Initializing connection that will be used to listen for "
212
         "clients...\n");
213
  int options = 1;
214
  struct sockaddr_in my_address;
215

    
216
  //get a socket fd
217
  if ((listen_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
218
    printf("\tThere was an error creating a socket\n");
219
    return -1;
220
  }
221

    
222
  //set up the address struct
223
  memset(&my_address,'\0',sizeof(my_address));
224
  my_address.sin_family = AF_INET;
225
  my_address.sin_addr.s_addr = htonl(INADDR_ANY);
226
  my_address.sin_port = htons(port);
227

    
228
  setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &options, sizeof(options));
229

    
230
  //get the current socket options
231
  if ((options = fcntl(listen_socket, F_GETFL)) < 0) {
232
    printf("\tThere was an error getting the socket options.\n");
233
    return -1;
234
  }
235

    
236
  //set the socket to non blocking
237
  options = (options | O_NONBLOCK);
238
  if (fcntl(listen_socket, F_SETFL, options) < 0) {
239
    printf("\tThere was an error setting the socket to be non blocking.\n");
240
    return -1;
241
  }
242

    
243
  //bind the socket to listen on the specified port
244
  if (bind(listen_socket, (struct sockaddr *) &my_address, sizeof(my_address)) < 0) {
245
    printf("\tThere was an error binding the socket\n");
246
    return -1;
247
  }
248

    
249
  return 0;
250
}
251

    
252
/**
253
 * @brief The main function of the server
254
 *
255
 * @param argc The number of command line arguments passed to the program
256
 * @param argv The command line arguments passed to the program
257
 *
258
 * @return 0 on success, negative error code on error
259
 */
260
ColonetServer colonet_server;
261
int main(int argc, char** argv) {
262

    
263
  if (colonet_server.initialize_server(argc, argv) < 0) {
264
    printf("\t\nThere was an error initializing the server. "
265
           "Terminating server...\n");
266
    return -1;
267
  }
268

    
269
  if (colonet_server.start_listening() < 0) {
270
    return -1;
271
  }
272

    
273
  colonet_server.run_server();
274

    
275
  return 0;
276
}