Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (3.33 KB)

1 11 emarinel
/** @file ColonetServer.cpp
2
 *
3
 * @brief colonet_server - primary server application for Colonet
4
 *
5
 * @author Jason Knichel
6
 * @author Eugene Marinelli
7
 * @date 10/31/06
8
 */
9
10
#include <colonet_wireless.h>
11
#include <sys/select.h>
12
#include <sys/socket.h>
13
#include <netinet/in.h>
14
#include <errno.h>
15
#include <arpa/inet.h>
16
17
#include "includes/initialization.h"
18
#include "includes/ConnectionPool.h"
19
#include "includes/client.h"
20
#include "includes/options.h"
21
#include "includes/Logging.h"
22
23
#define LISTEN_BACKLOG 5
24
#define LOG_BUFFER_LENGTH 128
25
26
int listenSocket = 0;
27
ConnectionPool connection_pool;
28
Log * logger;
29
30
int main(int argc, char** argv) {
31
  if (initializeServer(argc, argv) < 0) {
32
    printf("\t\nThere was an error initializing the server. "
33
           "Terminating server...\n");
34
    return -1;
35
  }
36
37
  if (listen(listenSocket, LISTEN_BACKLOG) < 0) {
38
    printf("\t\nThere was an error telling the socket to "
39
           "listen for connections from clients.  Terminating Server...\n");
40
    return -1;
41
  }
42
43
  connection_pool.set_listen_socket_in_ready_set(listenSocket);
44
45
  //TODO: why is all of this in that if statement and not just conn_pool.maxfd = listenSocket ?
46
  if (listenSocket > connection_pool.get_max_file_descriptor()) {
47
    connection_pool.set_max_file_descriptor(listenSocket);
48
49
    int acceptSocket = 0;
50
    struct sockaddr_in clientAddr;
51
    socklen_t clen = sizeof(clientAddr);
52
    struct timeval selectTimeout;
53
54
    memset(&selectTimeout,0,sizeof(selectTimeout));
55
56
    logger->logMessage(LOG_TYPE_MESSAGE, "Server initialized.  About to start listening for connections");
57
58
    while(1) {
59
      connection_pool.perform_select(listenSocket, &selectTimeout);
60
61
      //either no descriptors are ready or there was an error
62
      //TODO: check for specific errors
63
      if (connection_pool.get_number_clients_ready() <= 0) {
64
        continue;
65
      }
66
67
      if (connection_pool.is_socket_ready_to_read(listenSocket)) {
68
        printf("Something is trying to connect...\n");
69
        if ((acceptSocket = accept(listenSocket, (struct sockaddr*) &clientAddr, &clen)) < 0) {
70
          if (errno == EMFILE) {
71
            printf("\tWhen attempting to accept a connection, "
72
                   "reached the per process limit of file descriptors."
73
                   "  Dropping the new connection.\n");
74
            continue;
75
          } else {
76
            printf("\tThere was an error when attempting to accept a "
77
                   "connection");
78
          }
79
          continue;
80
        }
81
82
        char logBuffer[LOG_BUFFER_LENGTH];
83
        snprintf(logBuffer, LOG_BUFFER_LENGTH, "Client at address %s attempting to connect.", inet_ntoa(clientAddr.sin_addr));
84
        logger->logMessage(LOG_TYPE_CONNECT, logBuffer);
85
86
        //TODO: remove this
87
        //printf("Attempting to add a client.\n");
88
89
90
        if (connection_pool.add_client(acceptSocket) < 0) {
91
          printf("\tThere was an error when trying to add a client to the "
92
                 "connection pool.");
93
          continue;
94
        }
95
96
        snprintf(logBuffer, LOG_BUFFER_LENGTH, "Client at address %s successfully added to connection pool.", inet_ntoa(clientAddr.sin_addr));
97
        logger->logMessage(LOG_TYPE_CONNECT, logBuffer);
98
99
      }
100
101
      if (connection_pool.check_clients() < 0) {
102
        printf("\tThere was an error trying to update the clients.");
103
        continue;
104
      }
105
    }
106
  }
107
108
   return 0;
109
}