Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (7.14 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
#define u_int32_t unsigned
31

    
32
ConnectionPool * connection_pool;
33

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

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

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

    
58
  parseCmdLine(argc, argv);
59

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

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

    
69
  return 0;
70
}
71

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

    
86

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

    
93
  int accept_socket = 0;
94
  struct sockaddr_in client_addr;
95
  socklen_t client_addr_size = sizeof(client_addr);
96

    
97
  logger.log_message("Server initialized.  About to start listening for connections");
98

    
99
  while(1) {
100
    connection_pool.perform_select(listen_socket);
101

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

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

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

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

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

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

    
142
  return 0;
143
}
144

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

    
150
    /*
151
    //Eugene's code
152
    char buffer[WRITE_BUFFER_SIZE];
153

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

157
    int buflen = strlen(buffer);
158
    */
159

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

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

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

    
177
  return 0;
178
}
179

    
180

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

    
189
  if (optionsG.logging_enabled) {
190
    printf("Logging enabled. Log filename: %s\n", optionsG.log_filename);
191
    log_filename = optionsG.log_filename;
192
  } else {
193
    printf("Logging disabled.\n");
194
  }
195

    
196
  colonet_wl_init(optionsG.wireless_port, wirelessMessageHandler, log_filename);
197

    
198
  if (colonet_wl_run_listener_thread()) {
199
    fprintf(stderr, "%s: colonet_wl_run_listener_thread failed.\n", __FUNCTION__);
200
    return -1;
201
  }
202

    
203
  return 0;
204
}
205

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

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

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

    
230
  setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &options, sizeof(options));
231

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

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

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

    
251
  return 0;
252
}
253

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

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

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

    
275
  colonet_server.run_server();
276

    
277
  return 0;
278
}