root / trunk / code / projects / colonet / ColonetServer / ColonetServer.cpp @ 57
History | View | Annotate | Download (9.18 KB)
1 | 29 | jknichel | /**
|
---|---|---|---|
2 | * @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 | #include <colonet_wireless.h> |
||
12 | #include <sys/select.h> |
||
13 | #include <sys/socket.h> |
||
14 | #include <netinet/in.h> |
||
15 | #include <errno.h> |
||
16 | #include <arpa/inet.h> |
||
17 | |||
18 | 23 | jknichel | #include <fcntl.h> |
19 | |||
20 | 20 | jknichel | #include <colonet_wireless.h> |
21 | |||
22 | #include "includes/ColonetServer.h" |
||
23 | 11 | emarinel | #include "includes/ConnectionPool.h" |
24 | #include "includes/client.h" |
||
25 | #include "includes/options.h" |
||
26 | 57 | jknichel | #include "includes/Log.h" |
27 | 11 | emarinel | |
28 | #define LISTEN_BACKLOG 5 |
||
29 | #define LOG_BUFFER_LENGTH 128 |
||
30 | |||
31 | 20 | jknichel | ConnectionPool * connection_pool; |
32 | |||
33 | 29 | jknichel | /**
|
34 | * @brief Default constructor for ColonetServer
|
||
35 | */
|
||
36 | 20 | jknichel | ColonetServer::ColonetServer(): logger("logFile.txt") {
|
37 | 24 | jknichel | listen_socket = 0;
|
38 | 20 | jknichel | } |
39 | |||
40 | 29 | jknichel | /**
|
41 | * @brief Destructor for ColonetServer
|
||
42 | */
|
||
43 | 20 | jknichel | ColonetServer::~ColonetServer() { |
44 | } |
||
45 | |||
46 | 29 | jknichel | /**
|
47 | * @brief Initializes the various elements needed for the server to run
|
||
48 | *
|
||
49 | * @param argc The number of command line arguments passed to the program
|
||
50 | * @param argv The command line arguments passed to the program
|
||
51 | *
|
||
52 | * @return 0 on success, negative error code on failure
|
||
53 | */
|
||
54 | 20 | jknichel | int ColonetServer::initialize_server(int argc, char * argv[]) { |
55 | printf("Initializing Server...\n");
|
||
56 | |||
57 | parseCmdLine(argc, argv); |
||
58 | |||
59 | 27 | jknichel | if (initialize_connection(optionsG.listen_port) < 0) { |
60 | 20 | jknichel | return -1; |
61 | 27 | jknichel | } |
62 | 20 | jknichel | |
63 | 22 | jknichel | if (initialize_wireless() < 0) { |
64 | 20 | jknichel | fprintf(stderr, "%s: initWireless failed\n", __FUNCTION__);
|
65 | return -1; |
||
66 | } |
||
67 | |||
68 | return 0; |
||
69 | } |
||
70 | |||
71 | 29 | jknichel | /**
|
72 | * @brief Starts the server listening on the socket that was opened for listening
|
||
73 | *
|
||
74 | * @return 0 on success, negative error code on failure
|
||
75 | */
|
||
76 | 25 | jknichel | int ColonetServer::start_listening() {
|
77 | if (listen(listen_socket, LISTEN_BACKLOG) < 0) { |
||
78 | log_error("\t\nThere was an error telling the socket to "
|
||
79 | 27 | jknichel | "listen for connections from clients. Terminating Server...\n");
|
80 | 25 | jknichel | return -1; |
81 | } |
||
82 | return 0; |
||
83 | } |
||
84 | 22 | jknichel | |
85 | 29 | jknichel | /**
|
86 | * @brief Logs an error message to the log file
|
||
87 | */
|
||
88 | 20 | jknichel | int ColonetServer::log_error(char * error_message) { |
89 | return logger.logMessage(LOG_TYPE_ERROR, error_message);
|
||
90 | } |
||
91 | |||
92 | 29 | jknichel | /**
|
93 | * @brief Logs a message to the log file
|
||
94 | */
|
||
95 | 20 | jknichel | int ColonetServer::log_message(char * message) { |
96 | return logger.logMessage(LOG_TYPE_MESSAGE, message);
|
||
97 | } |
||
98 | |||
99 | 29 | jknichel | /**
|
100 | * @brief Starts the server running (starts an infinite loop)
|
||
101 | */
|
||
102 | 24 | jknichel | int ColonetServer::run_server() {
|
103 | 20 | jknichel | connection_pool.set_listen_socket_in_ready_set(listen_socket); |
104 | |||
105 | //TODO: why is all of this in that if statement and not just conn_pool.maxfd = listen_socket ?
|
||
106 | if (listen_socket > connection_pool.get_max_file_descriptor()) {
|
||
107 | connection_pool.set_max_file_descriptor(listen_socket); |
||
108 | |||
109 | 27 | jknichel | int accept_socket = 0; |
110 | struct sockaddr_in client_addr;
|
||
111 | socklen_t client_addr_size = sizeof(client_addr);
|
||
112 | struct timeval select_timeout;
|
||
113 | 11 | emarinel | |
114 | 34 | emarinel | memset(&select_timeout, 0, sizeof(select_timeout)); |
115 | 11 | emarinel | |
116 | 27 | jknichel | log_message("Server initialized. About to start listening for connections");
|
117 | 11 | emarinel | |
118 | while(1) { |
||
119 | 27 | jknichel | connection_pool.perform_select(listen_socket, &select_timeout); |
120 | 11 | emarinel | |
121 | //either no descriptors are ready or there was an error
|
||
122 | //TODO: check for specific errors
|
||
123 | if (connection_pool.get_number_clients_ready() <= 0) { |
||
124 | continue;
|
||
125 | } |
||
126 | |||
127 | 20 | jknichel | if (connection_pool.is_socket_ready_to_read(listen_socket)) {
|
128 | 11 | emarinel | printf("Something is trying to connect...\n");
|
129 | 27 | jknichel | if ((accept_socket = accept(listen_socket, (struct sockaddr*) &client_addr, &client_addr_size)) < 0) { |
130 | 11 | emarinel | if (errno == EMFILE) {
|
131 | printf("\tWhen attempting to accept a connection, "
|
||
132 | "reached the per process limit of file descriptors."
|
||
133 | " Dropping the new connection.\n");
|
||
134 | continue;
|
||
135 | } else {
|
||
136 | 55 | jknichel | printf("\tThere was an error when attempting to accept a connection");
|
137 | 11 | emarinel | } |
138 | continue;
|
||
139 | } |
||
140 | |||
141 | 27 | jknichel | char log_buffer[LOG_BUFFER_LENGTH];
|
142 | snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s attempting to connect.",
|
||
143 | inet_ntoa(client_addr.sin_addr)); |
||
144 | logger.logMessage(LOG_TYPE_CONNECT, log_buffer); |
||
145 | 11 | emarinel | |
146 | 27 | jknichel | if (connection_pool.add_client(accept_socket) < 0) { |
147 | printf("\tThere was an error when trying to add a client to the connection pool.");
|
||
148 | 11 | emarinel | continue;
|
149 | } |
||
150 | |||
151 | 27 | jknichel | snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s successfully added to connection pool.",
|
152 | inet_ntoa(client_addr.sin_addr)); |
||
153 | logger.logMessage(LOG_TYPE_CONNECT, log_buffer); |
||
154 | 11 | emarinel | } |
155 | |||
156 | 22 | jknichel | if (connection_pool.check_clients(wireless) < 0) { |
157 | 11 | emarinel | printf("\tThere was an error trying to update the clients.");
|
158 | continue;
|
||
159 | } |
||
160 | } |
||
161 | } |
||
162 | |||
163 | 20 | jknichel | return 0; |
164 | 11 | emarinel | } |
165 | 20 | jknichel | |
166 | 29 | jknichel | /**
|
167 | * @todo This method is here because the wireless message handler has a dependency on the connection_pool
|
||
168 | * This should be removed when that dependency is broken
|
||
169 | *
|
||
170 | * @return A pointer to the connection pool
|
||
171 | */
|
||
172 | 20 | jknichel | ConnectionPool * ColonetServer::get_connection_pool_pointer() { |
173 | return &connection_pool;
|
||
174 | } |
||
175 | 22 | jknichel | |
176 | 29 | jknichel | /**
|
177 | * @brief Initializes the wireless
|
||
178 | *
|
||
179 | * @return 0 on success, negative error code on error
|
||
180 | */
|
||
181 | 27 | jknichel | int ColonetServer::initialize_wireless() {
|
182 | 23 | jknichel | char* log_filename = NULL; |
183 | 22 | jknichel | |
184 | 23 | jknichel | if (optionsG.logging_enabled) {
|
185 | log_filename = optionsG.log_filename; |
||
186 | } |
||
187 | |||
188 | wireless = new ColonetWireless(optionsG.wireless_port,
|
||
189 | wirelessMessageHandler, log_filename, |
||
190 | /*!optionsG.listener_mode*/false, true); |
||
191 | //Note: last arg set to true ignores token ring; in general, this should
|
||
192 | //probably be false (changed for demo purposes)
|
||
193 | |||
194 | if (!wireless->run_listener_thread()) {
|
||
195 | 34 | emarinel | return -1; |
196 | } |
||
197 | 23 | jknichel | |
198 | 34 | emarinel | return 0; |
199 | 23 | jknichel | } |
200 | |||
201 | 29 | jknichel | /**
|
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 | 27 | jknichel | int ColonetServer::initialize_connection(int port) { |
209 | 23 | jknichel | printf("Initializing connection that will be used to listen for "
|
210 | "clients...\n");
|
||
211 | 27 | jknichel | int options = 1; |
212 | struct sockaddr_in my_address;
|
||
213 | 23 | jknichel | |
214 | //get a socket fd
|
||
215 | 24 | jknichel | if ((listen_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { |
216 | 23 | jknichel | printf("\tThere was an error creating a socket\n");
|
217 | return -1; |
||
218 | } |
||
219 | |||
220 | //set up the address struct
|
||
221 | 27 | jknichel | memset(&my_address,'\0',sizeof(my_address)); |
222 | my_address.sin_family = AF_INET; |
||
223 | my_address.sin_addr.s_addr = htonl(INADDR_ANY); |
||
224 | my_address.sin_port = htons(port); |
||
225 | 23 | jknichel | |
226 | 27 | jknichel | setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &options, sizeof(options));
|
227 | 23 | jknichel | |
228 | //get the current socket options
|
||
229 | 27 | jknichel | if ((options = fcntl(listen_socket, F_GETFL)) < 0) { |
230 | 23 | jknichel | printf("\tThere was an error getting the socket options.\n");
|
231 | return -1; |
||
232 | } |
||
233 | |||
234 | //set the socket to non blocking
|
||
235 | 27 | jknichel | options = (options | O_NONBLOCK); |
236 | if (fcntl(listen_socket, F_SETFL, options) < 0) { |
||
237 | 23 | jknichel | printf("\tThere was an error setting the socket to be non blocking.\n");
|
238 | return -1; |
||
239 | } |
||
240 | |||
241 | //bind the socket to listen on the specified port
|
||
242 | 27 | jknichel | if (bind(listen_socket, (struct sockaddr *) &my_address, sizeof(my_address)) < 0) { |
243 | 23 | jknichel | printf("\tThere was an error binding the socket\n");
|
244 | return -1; |
||
245 | } |
||
246 | |||
247 | return 0; |
||
248 | } |
||
249 | |||
250 | 29 | jknichel | /**
|
251 | * @brief The main function of the server
|
||
252 | *
|
||
253 | * @param argc The number of command line arguments passed to the program
|
||
254 | * @param argv The command line arguments passed to the program
|
||
255 | *
|
||
256 | * @return 0 on success, negative error code on error
|
||
257 | */
|
||
258 | 24 | jknichel | int main(int argc, char** argv) { |
259 | ColonetServer colonet_server; |
||
260 | |||
261 | connection_pool = colonet_server.get_connection_pool_pointer(); |
||
262 | |||
263 | if (colonet_server.initialize_server(argc, argv) < 0) { |
||
264 | colonet_server.log_error("\t\nThere was an error initializing the server. "
|
||
265 | "Terminating server...\n");
|
||
266 | return -1; |
||
267 | } |
||
268 | |||
269 | 25 | jknichel | if (colonet_server.start_listening() < 0) { |
270 | 24 | jknichel | return -1; |
271 | } |
||
272 | 25 | jknichel | |
273 | 24 | jknichel | colonet_server.run_server(); |
274 | |||
275 | return 0; |
||
276 | } |
||
277 | |||
278 | 23 | jknichel | //this is old code that was commented out that I didn't want to delete yet
|
279 | /*
|
||
280 | int parseCommandLine(int argc, char * argv[], commandParams_t * params)
|
||
281 | {
|
||
282 | printf("Parsing Command Line...\n");
|
||
283 | if (!params)
|
||
284 | return -1;
|
||
285 | |||
286 | //if no command line parameters were specified, set to defaults
|
||
287 | if (argc == 1) {
|
||
288 | printf("No port was specified for listening for client connections."
|
||
289 | " Defaulting to %d\n", DEFAULTPORT);
|
||
290 | params->listenPort = DEFAULTPORT;
|
||
291 | }
|
||
292 | |||
293 | if (!argv)
|
||
294 | return -1;
|
||
295 | |||
296 | int i;
|
||
297 | for (i = 1; i < argc; i++) {
|
||
298 | char * temp = argv[i];
|
||
299 | if (temp[0] != '-')
|
||
300 | {
|
||
301 | printUsage(argv[0]);
|
||
302 | return -1;
|
||
303 | }
|
||
304 | |||
305 | switch(temp[1])
|
||
306 | {
|
||
307 | case 'h': printUsage(argv[0]);
|
||
308 | break;
|
||
309 | case 'p':
|
||
310 | {
|
||
311 | if (i >= argc-1)
|
||
312 | {
|
||
313 | printUsage(argv[0]);
|
||
314 | return -1;
|
||
315 | }
|
||
316 | i++;
|
||
317 | char * portString = argv[i];
|
||
318 | char * endptr = NULL;
|
||
319 | int port = strtol(portString, &endptr, 10);
|
||
320 | if (*endptr != '\0')
|
||
321 | {
|
||
322 | printf("Invalid port specified...%s, %d\n", endptr, port);
|
||
323 | return -1;
|
||
324 | }
|
||
325 | if (port < SMALLEST_POSS_LISTEN_PORT)
|
||
326 | {
|
||
327 | printf("You cannot listen on a port less than %d.\n",
|
||
328 | SMALLEST_POSS_LISTEN_PORT);
|
||
329 | return -1;
|
||
330 | }
|
||
331 | params->listenPort = port;
|
||
332 | printf("Setting port to listen on to %d.\n", params->listenPort);
|
||
333 | }
|
||
334 | break;
|
||
335 | default: printUsage(argv[0]);
|
||
336 | return -1;
|
||
337 | break;
|
||
338 | }
|
||
339 | }
|
||
340 | |||
341 | return 0;
|
||
342 | }
|
||
343 | */
|