Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (6.37 KB)

1 161 emarinel
/**
2 29 jknichel
 * @file ColonetServer.cpp
3 11 emarinel
 *
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 118 emarinel
#include <arpa/inet.h>
12
#include <fcntl.h>
13
#include <errno.h>
14
#include <netinet/in.h>
15
#include <string.h>
16 11 emarinel
#include <sys/select.h>
17
#include <sys/socket.h>
18
19 20 jknichel
#include <colonet_wireless.h>
20
21 391 emarinel
#include <ColonetServer.h>
22
#include <ConnectionPool.h>
23
#include <wirelessMessageHandler.h>
24
#include <options.h>
25
#include <Log.h>
26 11 emarinel
27
#define LISTEN_BACKLOG 5
28
#define LOG_BUFFER_LENGTH 128
29
30 347 emarinel
#define u_int32_t unsigned
31
32 391 emarinel
static ConnectionPool * connection_pool;
33 20 jknichel
34 29 jknichel
/**
35
 * @brief Default constructor for ColonetServer
36
 */
37 20 jknichel
ColonetServer::ColonetServer(): logger("logFile.txt") {
38 24 jknichel
  listen_socket = 0;
39 20 jknichel
}
40
41 29 jknichel
/**
42
 * @brief Destructor for ColonetServer
43
 */
44 20 jknichel
ColonetServer::~ColonetServer() {
45
}
46
47 29 jknichel
/**
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 20 jknichel
int ColonetServer::initialize_server(int argc, char * argv[]) {
56
  parseCmdLine(argc, argv);
57
58 27 jknichel
  if (initialize_connection(optionsG.listen_port) < 0) {
59 20 jknichel
    return -1;
60 27 jknichel
  }
61 20 jknichel
62 22 jknichel
  if (initialize_wireless() < 0) {
63 20 jknichel
    fprintf(stderr, "%s: initWireless failed\n", __FUNCTION__);
64
    return -1;
65
  }
66
67
  return 0;
68
}
69
70 29 jknichel
/**
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 25 jknichel
int ColonetServer::start_listening() {
76
  if (listen(listen_socket, LISTEN_BACKLOG) < 0) {
77 132 jknichel
    logger.log_error("\t\nThere was an error telling the socket to "
78 140 jknichel
                     "listen for connections from clients.  Terminating Server...\n");
79 25 jknichel
    return -1;
80
  }
81
  return 0;
82
}
83 22 jknichel
84 20 jknichel
85 29 jknichel
/**
86
 * @brief Starts the server running (starts an infinite loop)
87
 */
88 24 jknichel
int ColonetServer::run_server() {
89 143 jknichel
  connection_pool.add_new_socket_to_pool(listen_socket);
90 20 jknichel
91 143 jknichel
  int accept_socket = 0;
92
  struct sockaddr_in client_addr;
93
  socklen_t client_addr_size = sizeof(client_addr);
94 20 jknichel
95 143 jknichel
  logger.log_message("Server initialized.  About to start listening for connections");
96 11 emarinel
97 143 jknichel
  while(1) {
98
    connection_pool.perform_select(listen_socket);
99 11 emarinel
100 143 jknichel
    //either no descriptors are ready or there was an error
101
    if (connection_pool.get_number_clients_ready() <= 0) {
102
      continue;
103
    }
104 11 emarinel
105 143 jknichel
    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 11 emarinel
      }
118
119 143 jknichel
      char log_buffer[LOG_BUFFER_LENGTH];
120 161 emarinel
      snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s attempting to connect.",
121 143 jknichel
               inet_ntoa(client_addr.sin_addr));
122
      logger.log_string(LOG_TYPE_CONNECT, log_buffer);
123 11 emarinel
124 143 jknichel
      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 11 emarinel
      }
128
129 161 emarinel
      snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s successfully added to connection pool.",
130 143 jknichel
               inet_ntoa(client_addr.sin_addr));
131
      logger.log_string(LOG_TYPE_CONNECT, log_buffer);
132 11 emarinel
    }
133 143 jknichel
134
    if (connection_pool.check_clients() < 0) {
135
      printf("\tThere was an error trying to update the clients.");
136
      continue;
137
    }
138 11 emarinel
  }
139
140 20 jknichel
  return 0;
141 11 emarinel
}
142 20 jknichel
143 140 jknichel
int ColonetServer::process_received_wireless_message(unsigned char type, short source, int dest,
144 297 emarinel
                                                     unsigned char * data, int len) {
145 140 jknichel
  if (type == COLONET_RESPONSE) {
146
    printf("response\n");
147
148 334 gtress
    //Greg's code
149
    char * buffer = (char *) &(data[5]);
150
    int buflen = strlen(buffer);
151
    buffer[buflen] = '\n';
152
    buflen++;
153
154 297 emarinel
    if (connection_pool.write_to_client(dest, buffer, buflen) == ERROR_INVALID_CLIENT_ID) {
155
      printf("The robot wanted to pass the data to a client not in the pool.\n");
156 140 jknichel
      return -1;
157
    }
158
159
    printf("Put data in write buffer for client.\n");
160
  } else {
161
    printf("not a response\n");
162
    return -1;
163
  }
164
165
  return 0;
166 20 jknichel
}
167 22 jknichel
168 140 jknichel
169 29 jknichel
/**
170
 * @brief Initializes the wireless
171
 *
172
 * @return 0 on success, negative error code on error
173
 */
174 27 jknichel
int ColonetServer::initialize_wireless() {
175 23 jknichel
  char* log_filename = NULL;
176 22 jknichel
177 23 jknichel
  if (optionsG.logging_enabled) {
178 348 emarinel
    printf("Logging enabled. Log filename: %s\n", optionsG.log_filename);
179 23 jknichel
    log_filename = optionsG.log_filename;
180 347 emarinel
  } else {
181
    printf("Logging disabled.\n");
182 23 jknichel
  }
183
184 397 emarinel
  if (colonet_wl_init(optionsG.wireless_port, wirelessMessageHandler, log_filename) != 0) {
185
    fprintf(stderr, "ERROR - colonet_wl_init failed.\n");
186
    return -1;
187
  }
188 347 emarinel
189 164 emarinel
  if (colonet_wl_run_listener_thread()) {
190 348 emarinel
    fprintf(stderr, "%s: colonet_wl_run_listener_thread failed.\n", __FUNCTION__);
191 34 emarinel
    return -1;
192 161 emarinel
  }
193 23 jknichel
194 34 emarinel
  return 0;
195 23 jknichel
}
196
197 29 jknichel
/**
198
 * @brief Initialize a connection to listen on
199
 *
200
 * @port The port to try to open to listen on
201
 *
202
 * @return 0 on success, negative error code on error
203
 */
204 27 jknichel
int ColonetServer::initialize_connection(int port) {
205 348 emarinel
  printf("Initializing connection that will be used to listen for clients...\n");
206 27 jknichel
  int options = 1;
207
  struct sockaddr_in my_address;
208 23 jknichel
209
  //get a socket fd
210 24 jknichel
  if ((listen_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
211 408 emarinel
    printf("\tThere was an error creating socket\n");
212 23 jknichel
    return -1;
213
  }
214
215
  //set up the address struct
216 27 jknichel
  memset(&my_address,'\0',sizeof(my_address));
217
  my_address.sin_family = AF_INET;
218
  my_address.sin_addr.s_addr = htonl(INADDR_ANY);
219
  my_address.sin_port = htons(port);
220 161 emarinel
221 27 jknichel
  setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &options, sizeof(options));
222 23 jknichel
223
  //get the current socket options
224 27 jknichel
  if ((options = fcntl(listen_socket, F_GETFL)) < 0) {
225 23 jknichel
    printf("\tThere was an error getting the socket options.\n");
226
    return -1;
227
  }
228
229
  //set the socket to non blocking
230 27 jknichel
  options = (options | O_NONBLOCK);
231
  if (fcntl(listen_socket, F_SETFL, options) < 0) {
232 23 jknichel
    printf("\tThere was an error setting the socket to be non blocking.\n");
233
    return -1;
234
  }
235 161 emarinel
236 23 jknichel
  //bind the socket to listen on the specified port
237 27 jknichel
  if (bind(listen_socket, (struct sockaddr *) &my_address, sizeof(my_address)) < 0) {
238 23 jknichel
    printf("\tThere was an error binding the socket\n");
239
    return -1;
240
  }
241 161 emarinel
242 23 jknichel
  return 0;
243
}