Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / server / ColonetServer.cpp @ 768

History | View | Annotate | Download (8.56 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 409 emarinel
#include <unistd.h>
17 11 emarinel
#include <sys/select.h>
18
#include <sys/socket.h>
19
20 20 jknichel
#include <colonet_wireless.h>
21
22 391 emarinel
#include <ColonetServer.h>
23
#include <ConnectionPool.h>
24
#include <options.h>
25
#include <Log.h>
26 11 emarinel
27
#define LISTEN_BACKLOG 5
28
#define LOG_BUFFER_LENGTH 128
29 424 emarinel
#define MAX_MSG_BUFFER_LENGTH 128
30 661 jknichel
#define REFRESH_VIRTUAL_WALL_PERIOD 400
31 11 emarinel
32 347 emarinel
#define u_int32_t unsigned
33
34 441 jknichel
//TODO: is this needed anymore?  it still compiles when i comment it out
35
//static ConnectionPool* connection_pool;
36 20 jknichel
37 29 jknichel
/**
38
 * @brief Default constructor for ColonetServer
39
 */
40 426 emarinel
ColonetServer::ColonetServer() {
41 24 jknichel
  listen_socket = 0;
42 453 emarinel
  connection_pool = new ConnectionPool(this);
43 20 jknichel
}
44
45 29 jknichel
/**
46
 * @brief Destructor for ColonetServer
47
 */
48 20 jknichel
ColonetServer::~ColonetServer() {
49
}
50
51 29 jknichel
/**
52
 * @brief Initializes the various elements needed for the server to run
53
 *
54
 * @param argc The number of command line arguments passed to the program
55
 * @param argv The command line arguments passed to the program
56
 *
57
 * @return 0 on success, negative error code on failure
58
 */
59 20 jknichel
int ColonetServer::initialize_server(int argc, char * argv[]) {
60 426 emarinel
  options_parseCmdLine(argc, argv);
61 20 jknichel
62 426 emarinel
  if (optionsG.logging_enabled) {
63
    logger = new Log("colonet_server_log.txt");
64
  }
65
66 27 jknichel
  if (initialize_connection(optionsG.listen_port) < 0) {
67 20 jknichel
    return -1;
68 27 jknichel
  }
69 20 jknichel
70 22 jknichel
  if (initialize_wireless() < 0) {
71 20 jknichel
    fprintf(stderr, "%s: initWireless failed\n", __FUNCTION__);
72
    return -1;
73
  }
74
75
  return 0;
76
}
77
78 29 jknichel
/**
79
 * @brief Starts the server listening on the socket that was opened for listening
80
 *
81
 * @return 0 on success, negative error code on failure
82
 */
83 25 jknichel
int ColonetServer::start_listening() {
84
  if (listen(listen_socket, LISTEN_BACKLOG) < 0) {
85 426 emarinel
    if (optionsG.logging_enabled) {
86
      logger->log_error("\t\nThere was an error telling the socket to listen for connections from clients.  "
87
                        "Terminating Server...\n");
88
    }
89 25 jknichel
    return -1;
90
  }
91 433 emarinel
92 25 jknichel
  return 0;
93
}
94 22 jknichel
95 618 jknichel
/**
96
 * @brief The function that is run in the new pthread in order to do the position monitoring
97
 *
98
 * @param arg the argument to give to the new thread
99
 */
100 508 emarinel
void* run_thread(void* arg) {
101
  PositionMonitor* p = (PositionMonitor*) arg;
102
  p->run();
103
  return NULL;
104
}
105
106 618 jknichel
/**
107
 * @brief This function spawns a new thread to do the position monitoring based off data
108
 *  retrieved from the library that parses the image from the camera
109
 */
110 508 emarinel
int ColonetServer::run_position_monitor() {
111
  pthread_t posmon_thread;
112
  pthread_create(&posmon_thread, NULL, run_thread, &position_monitor);
113
114
  return 0;
115
}
116
117 29 jknichel
/**
118
 * @brief Starts the server running (starts an infinite loop)
119
 */
120 24 jknichel
int ColonetServer::run_server() {
121 618 jknichel
  //add the socket that you will listen to connections on to the connection pool
122 453 emarinel
  connection_pool->add_new_socket_to_pool(listen_socket);
123 20 jknichel
124 143 jknichel
  int accept_socket = 0;
125
  struct sockaddr_in client_addr;
126
  socklen_t client_addr_size = sizeof(client_addr);
127 20 jknichel
128 426 emarinel
  if (optionsG.logging_enabled) {
129
    logger->log_message("Server initialized.  About to start listening for connections");
130
  }
131 11 emarinel
132 661 jknichel
  int refresh_virtual_wall_count = 0;
133
134 143 jknichel
  while(1) {
135 618 jknichel
    //sleep for a little bit so this program doesn't hog cpu
136 469 emarinel
    usleep(10000);
137
138 661 jknichel
    /* Refresh virtual wall. */
139 662 jknichel
    if (refresh_virtual_wall_count++ == REFRESH_VIRTUAL_WALL_PERIOD) {
140 661 jknichel
      fprintf(stderr, "Sending virtual wall info to robots.\n");
141
      refresh_virtual_wall_count = 0;
142
      virtual_wall.send_to_robots();
143
    }
144
145 618 jknichel
    //tell the connection pool to look at the file descriptors and see which ones are ready
146 453 emarinel
    connection_pool->perform_select(listen_socket);
147 409 emarinel
148 143 jknichel
    //either no descriptors are ready or there was an error
149 453 emarinel
    if (connection_pool->get_number_clients_ready() <= 0) {
150 143 jknichel
      continue;
151
    }
152 11 emarinel
153 618 jknichel
    //check to see if a new client is trying to connect
154 453 emarinel
    if (connection_pool->is_socket_ready_to_read(listen_socket)) {
155 143 jknichel
      printf("Something is trying to connect...\n");
156 618 jknichel
      //accept the connection
157 143 jknichel
      if ((accept_socket = accept(listen_socket, (struct sockaddr*) &client_addr, &client_addr_size)) < 0) {
158 424 emarinel
        if (errno == EMFILE) {
159
          printf("\tWhen attempting to accept a connection, reached the per process limit of file descriptors."
160 618 jknichel
                 "  Dropping the new connection.\n");
161 424 emarinel
          continue;
162
        } else {
163
          printf("\tThere was an error when attempting to accept a connection");
164
        }
165
        continue;
166 11 emarinel
      }
167
168 426 emarinel
      if (optionsG.logging_enabled) {
169 433 emarinel
        char log_buffer[LOG_BUFFER_LENGTH];
170
        snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s attempting to connect.",
171 648 emarinel
                                                                 inet_ntoa(client_addr.sin_addr));
172 433 emarinel
        logger->log_string(LOG_TYPE_CONNECT, log_buffer);
173 426 emarinel
      }
174 11 emarinel
175 618 jknichel
      //add the new client to the connection pool
176 453 emarinel
      if (connection_pool->add_client(accept_socket) < 0) {
177 424 emarinel
        printf("\tThere was an error when trying to add a client to the connection pool.");
178
        continue;
179 11 emarinel
      }
180
181 426 emarinel
      if (optionsG.logging_enabled) {
182 433 emarinel
        char log_buffer[LOG_BUFFER_LENGTH];
183 426 emarinel
        snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s successfully added to connection pool.",
184
                 inet_ntoa(client_addr.sin_addr));
185
        logger->log_string(LOG_TYPE_CONNECT, log_buffer);
186
      }
187 11 emarinel
    }
188 143 jknichel
189 618 jknichel
    //check all the clients in the connection pool
190 453 emarinel
    if (connection_pool->check_clients() < 0) {
191 143 jknichel
      printf("\tThere was an error trying to update the clients.");
192
    }
193 11 emarinel
  }
194
195 20 jknichel
  return 0;
196 11 emarinel
}
197 20 jknichel
198 424 emarinel
/**
199 618 jknichel
 * @brief when the server receives a new wireless message, it calls this function to process it
200
 *
201
 * @param source - ID of robot that message is from.
202
 * @param dest - ID of internet client to send message to.
203
 * @param data - Data to send to internet client.
204
 * @param len - Length of the data param.
205
 */
206 424 emarinel
int ColonetServer::process_received_wireless_message(int dest, char* data, int len) {
207 618 jknichel
  //try to pass the robot's response onto the client it wanted to respond to
208 453 emarinel
  if (connection_pool->write_to_client(dest, data, len) == ERROR_INVALID_CLIENT_ID) {
209 424 emarinel
    printf("The robot wanted to pass the data to a client not in the pool.\n");
210 140 jknichel
    return -1;
211
  }
212 574 gtress
  printf("Processing data from robot: %s\n", data);
213 140 jknichel
  return 0;
214 20 jknichel
}
215 22 jknichel
216 29 jknichel
/**
217
 * @brief Initializes the wireless
218
 *
219
 * @return 0 on success, negative error code on error
220
 */
221 27 jknichel
int ColonetServer::initialize_wireless() {
222 23 jknichel
  char* log_filename = NULL;
223 22 jknichel
224 23 jknichel
  if (optionsG.logging_enabled) {
225 348 emarinel
    printf("Logging enabled. Log filename: %s\n", optionsG.log_filename);
226 23 jknichel
    log_filename = optionsG.log_filename;
227 347 emarinel
  } else {
228
    printf("Logging disabled.\n");
229 23 jknichel
  }
230
231 618 jknichel
  //initialize the wireless library giving it the port and the log file name
232 424 emarinel
  if (colonet_wl_init(optionsG.wireless_port, log_filename) != 0) {
233 397 emarinel
    fprintf(stderr, "ERROR - colonet_wl_init failed.\n");
234
    return -1;
235
  }
236 347 emarinel
237 618 jknichel
  //call the function that will start the thread that will listen for wireless messages
238 164 emarinel
  if (colonet_wl_run_listener_thread()) {
239 348 emarinel
    fprintf(stderr, "%s: colonet_wl_run_listener_thread failed.\n", __FUNCTION__);
240 34 emarinel
    return -1;
241 161 emarinel
  }
242 23 jknichel
243 34 emarinel
  return 0;
244 23 jknichel
}
245
246 29 jknichel
/**
247
 * @brief Initialize a connection to listen on
248
 *
249
 * @port The port to try to open to listen on
250
 *
251
 * @return 0 on success, negative error code on error
252
 */
253 27 jknichel
int ColonetServer::initialize_connection(int port) {
254 348 emarinel
  printf("Initializing connection that will be used to listen for clients...\n");
255 27 jknichel
  int options = 1;
256
  struct sockaddr_in my_address;
257 23 jknichel
258
  //get a socket fd
259 24 jknichel
  if ((listen_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
260 408 emarinel
    printf("\tThere was an error creating socket\n");
261 23 jknichel
    return -1;
262
  }
263
264
  //set up the address struct
265 27 jknichel
  memset(&my_address,'\0',sizeof(my_address));
266
  my_address.sin_family = AF_INET;
267
  my_address.sin_addr.s_addr = htonl(INADDR_ANY);
268
  my_address.sin_port = htons(port);
269 161 emarinel
270 27 jknichel
  setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &options, sizeof(options));
271 23 jknichel
272
  //get the current socket options
273 27 jknichel
  if ((options = fcntl(listen_socket, F_GETFL)) < 0) {
274 23 jknichel
    printf("\tThere was an error getting the socket options.\n");
275
    return -1;
276
  }
277
278
  //set the socket to non blocking
279 27 jknichel
  options = (options | O_NONBLOCK);
280
  if (fcntl(listen_socket, F_SETFL, options) < 0) {
281 23 jknichel
    printf("\tThere was an error setting the socket to be non blocking.\n");
282
    return -1;
283
  }
284 161 emarinel
285 23 jknichel
  //bind the socket to listen on the specified port
286 27 jknichel
  if (bind(listen_socket, (struct sockaddr *) &my_address, sizeof(my_address)) < 0) {
287 23 jknichel
    printf("\tThere was an error binding the socket\n");
288
    return -1;
289
  }
290 161 emarinel
291 23 jknichel
  return 0;
292
}
293 443 emarinel
294 618 jknichel
/**
295
 * @brief returns the position monitor
296
 */
297 444 emarinel
PositionMonitor* ColonetServer::getPositionMonitor() {
298
  return &position_monitor;
299 443 emarinel
}
300 659 jknichel
301
VirtualWall* ColonetServer::getVirtualWall() {
302
  return &virtual_wall;
303
}