Project

General

Profile

Revision 20

Added by Jason knichel over 16 years ago

started moving ColonetServer into a class. the code is pretty messy at the moment

View differences:

ColonetServer.cpp
14 14
#include <errno.h>
15 15
#include <arpa/inet.h>
16 16

  
17
#include <colonet_wireless.h>
18

  
19
#include "includes/ColonetServer.h"
17 20
#include "includes/initialization.h"
18 21
#include "includes/ConnectionPool.h"
19 22
#include "includes/client.h"
......
23 26
#define LISTEN_BACKLOG 5
24 27
#define LOG_BUFFER_LENGTH 128
25 28

  
29
ConnectionPool * connection_pool;
30

  
31
ColonetServer::ColonetServer(): logger("logFile.txt") {
32
}
33

  
34
ColonetServer::~ColonetServer() {
35
}
36

  
37
int ColonetServer::initialize_server(int argc, char * argv[]) {
38
  printf("Initializing Server...\n");
39

  
40
  parseCmdLine(argc, argv);
41

  
42
  if (initConnection(optionsG.listen_port) < 0)
43
    return -1;
44

  
45
  if (initWireless() < 0) {
46
    fprintf(stderr, "%s: initWireless failed\n", __FUNCTION__);
47
    return -1;
48
  }
49

  
50
  return 0;
51
}
52

  
53
int ColonetServer::log_error(char * error_message) {
54
  return logger.logMessage(LOG_TYPE_ERROR, error_message);
55
}
56

  
57
int ColonetServer::log_message(char * message) {
58
  return logger.logMessage(LOG_TYPE_MESSAGE, message);
59
}
60

  
26 61
int listenSocket = 0;
27
ConnectionPool connection_pool;
28
Log * logger;
62
//ConnectionPool connection_pool;
29 63

  
64
//TODO: make it so the log file name is passed in on command line and default it to something if it isn't
65
//Log logger("logFile.txt");
66

  
30 67
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");
68
  ColonetServer colonet_server;
69

  
70
  connection_pool = colonet_server.get_connection_pool_pointer();
71

  
72
  if (colonet_server.initialize_server(argc, argv) < 0) {
73
    colonet_server.log_error("\t\nThere was an error initializing the server. "
74
                             "Terminating server...\n");
34 75
    return -1;
35 76
  }
36 77

  
37 78
  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");
79
    colonet_server.log_error("\t\nThere was an error telling the socket to "
80
                             "listen for connections from clients.  Terminating Server...\n");
40 81
    return -1;
41 82
  }
42 83

  
43
  connection_pool.set_listen_socket_in_ready_set(listenSocket);
84
  colonet_server.run_server(listenSocket);
44 85

  
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);
86
  return 0;
87
}
48 88

  
89
int ColonetServer::run_server(int listen_socket) {
90
  connection_pool.set_listen_socket_in_ready_set(listen_socket);
91

  
92
  //TODO: why is all of this in that if statement and not just conn_pool.maxfd = listen_socket ?
93
  if (listen_socket > connection_pool.get_max_file_descriptor()) {
94
    connection_pool.set_max_file_descriptor(listen_socket);
95

  
49 96
    int acceptSocket = 0;
50 97
    struct sockaddr_in clientAddr;
51 98
    socklen_t clen = sizeof(clientAddr);
......
53 100
    
54 101
    memset(&selectTimeout,0,sizeof(selectTimeout));
55 102

  
56
    logger->logMessage(LOG_TYPE_MESSAGE, "Server initialized.  About to start listening for connections");
103
    logger.logMessage(LOG_TYPE_MESSAGE, "Server initialized.  About to start listening for connections");
57 104

  
58 105
    while(1) {
59
      connection_pool.perform_select(listenSocket, &selectTimeout);
106
      connection_pool.perform_select(listen_socket, &selectTimeout);
60 107

  
61 108
      //either no descriptors are ready or there was an error
62 109
      //TODO: check for specific errors
......
64 111
        continue;
65 112
      }
66 113

  
67
      if (connection_pool.is_socket_ready_to_read(listenSocket)) {
114
      if (connection_pool.is_socket_ready_to_read(listen_socket)) {
68 115
        printf("Something is trying to connect...\n");
69
        if ((acceptSocket = accept(listenSocket, (struct sockaddr*) &clientAddr, &clen)) < 0) {
116
        if ((acceptSocket = accept(listen_socket, (struct sockaddr*) &clientAddr, &clen)) < 0) {
70 117
          if (errno == EMFILE) {
71 118
            printf("\tWhen attempting to accept a connection, "
72 119
                   "reached the per process limit of file descriptors."
......
81 128

  
82 129
        char logBuffer[LOG_BUFFER_LENGTH];
83 130
        snprintf(logBuffer, LOG_BUFFER_LENGTH, "Client at address %s attempting to connect.", inet_ntoa(clientAddr.sin_addr));
84
        logger->logMessage(LOG_TYPE_CONNECT, logBuffer);
131
        logger.logMessage(LOG_TYPE_CONNECT, logBuffer);
85 132

  
86 133
        //TODO: remove this
87 134
        //printf("Attempting to add a client.\n");
......
94 141
        }
95 142

  
96 143
        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);
144
        logger.logMessage(LOG_TYPE_CONNECT, logBuffer);
98 145
        
99 146
      }
100 147

  
......
105 152
    }
106 153
  }
107 154

  
108
   return 0;
155
  return 0;
109 156
}
157

  
158
ConnectionPool * ColonetServer::get_connection_pool_pointer() {
159
  return &connection_pool;
160
}

Also available in: Unified diff