Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (6.7 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 <unistd.h>
17
#include <sys/select.h>
18
#include <sys/socket.h>
19

    
20
#include <colonet_wireless.h>
21

    
22
#include <ColonetServer.h>
23
#include <ConnectionPool.h>
24
#include <options.h>
25
#include <Log.h>
26

    
27
#define LISTEN_BACKLOG 5
28
#define LOG_BUFFER_LENGTH 128
29
#define MAX_MSG_BUFFER_LENGTH 128
30

    
31
#define u_int32_t unsigned
32

    
33
//TODO: is this needed anymore?  it still compiles when i comment it out
34
//static ConnectionPool* connection_pool;
35

    
36
/**
37
 * @brief Default constructor for ColonetServer
38
 */
39
ColonetServer::ColonetServer() {
40
  listen_socket = 0;
41
}
42

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

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

    
60
  if (optionsG.logging_enabled) {
61
    logger = new Log("colonet_server_log.txt");
62
  }
63

    
64
  if (initialize_connection(optionsG.listen_port) < 0) {
65
    return -1;
66
  }
67

    
68
  if (initialize_wireless() < 0) {
69
    fprintf(stderr, "%s: initWireless failed\n", __FUNCTION__);
70
    return -1;
71
  }
72

    
73
  return 0;
74
}
75

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

    
90
  return 0;
91
}
92

    
93
/**
94
 * @brief Starts the server running (starts an infinite loop)
95
 */
96
int ColonetServer::run_server() {
97
  connection_pool.add_new_socket_to_pool(listen_socket);
98

    
99
  int accept_socket = 0;
100
  struct sockaddr_in client_addr;
101
  socklen_t client_addr_size = sizeof(client_addr);
102

    
103
  if (optionsG.logging_enabled) {
104
    logger->log_message("Server initialized.  About to start listening for connections");
105
  }
106

    
107
  while(1) {
108

    
109
    connection_pool.perform_select(listen_socket);
110

    
111
    //either no descriptors are ready or there was an error
112
    if (connection_pool.get_number_clients_ready() <= 0) {
113
      continue;
114
    }
115

    
116
    if (connection_pool.is_socket_ready_to_read(listen_socket)) {
117
      printf("Something is trying to connect...\n");
118
      if ((accept_socket = accept(listen_socket, (struct sockaddr*) &client_addr, &client_addr_size)) < 0) {
119
        if (errno == EMFILE) {
120
          printf("\tWhen attempting to accept a connection, reached the per process limit of file descriptors."
121
            "  Dropping the new connection.\n");
122
          continue;
123
        } else {
124
          printf("\tThere was an error when attempting to accept a connection");
125
        }
126
        continue;
127
      }
128

    
129
      if (optionsG.logging_enabled) {
130
        char log_buffer[LOG_BUFFER_LENGTH];
131
        snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s attempting to connect.",
132
          inet_ntoa(client_addr.sin_addr));
133
        logger->log_string(LOG_TYPE_CONNECT, log_buffer);
134
      }
135

    
136
      if (connection_pool.add_client(accept_socket) < 0) {
137
        printf("\tThere was an error when trying to add a client to the connection pool.");
138
        continue;
139
      }
140

    
141
      if (optionsG.logging_enabled) {
142
        char log_buffer[LOG_BUFFER_LENGTH];
143
        snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s successfully added to connection pool.",
144
                 inet_ntoa(client_addr.sin_addr));
145
        logger->log_string(LOG_TYPE_CONNECT, log_buffer);
146
      }
147
    }
148

    
149
    if (connection_pool.check_clients() < 0) {
150
      printf("\tThere was an error trying to update the clients.");
151
      continue;
152
    }
153
  }
154

    
155
  return 0;
156
}
157

    
158
/**
159
* @param source - ID of robot that message is from.
160
* @param dest - ID of internet client to send message to.
161
* @param data - Data to send to internet client.
162
* @param len - Length of the data param.
163
*/
164
int ColonetServer::process_received_wireless_message(int dest, char* data, int len) {
165
  if (connection_pool.write_to_client(dest, data, len) == ERROR_INVALID_CLIENT_ID) {
166
    printf("The robot wanted to pass the data to a client not in the pool.\n");
167
    return -1;
168
  }
169

    
170
  return 0;
171
}
172

    
173
/**
174
 * @brief Initializes the wireless
175
 *
176
 * @return 0 on success, negative error code on error
177
 */
178
int ColonetServer::initialize_wireless() {
179
  char* log_filename = NULL;
180

    
181
  if (optionsG.logging_enabled) {
182
    printf("Logging enabled. Log filename: %s\n", optionsG.log_filename);
183
    log_filename = optionsG.log_filename;
184
  } else {
185
    printf("Logging disabled.\n");
186
  }
187

    
188
  if (colonet_wl_init(optionsG.wireless_port, log_filename) != 0) {
189
    fprintf(stderr, "ERROR - colonet_wl_init failed.\n");
190
    return -1;
191
  }
192

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

    
198
  return 0;
199
}
200

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

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

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

    
225
  setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &options, sizeof(options));
226

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

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

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

    
246
  return 0;
247
}
248

    
249
PositionMonitor* ColonetServer::getPositionMonitor() {
250
  return &position_monitor;
251
}