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:

trunk/code/projects/colonet/ColonetServer/client.cpp
20 20

  
21 21
/* Globals */
22 22
static ColonetWireless* wireless;
23
extern ConnectionPool connection_pool;
23
extern ConnectionPool * connection_pool;
24 24

  
25 25

  
26 26
/* Public functions */
......
38 38
    printf("response\n");
39 39

  
40 40
    int dest = (int)(pkt->msg_dest);
41
    if (dest < connection_pool.get_next_available_slot())
41
    if (dest < connection_pool->get_next_available_slot())
42 42
    {
43 43
      char buffer[WRITE_BUFFER_SIZE];
44 44
          
......
46 46
      snprintf(buffer, WRITE_BUFFER_SIZE, "%d\n", value);
47 47
          
48 48
      int len = strlen(buffer);
49
      connection_pool.write_to_client(dest, buffer, len);
49
      connection_pool->write_to_client(dest, buffer, len);
50 50
      printf("Put data in write buffer for client.\n");
51 51
    }
52 52
    else
trunk/code/projects/colonet/ColonetServer/includes/ColonetServer.h
1
/**
2
 * @author Jason Knichel
3
 * @date 9/10/07
4
 */
5

  
6
#ifndef COLONETSERVER_H
7
#define COLONETSERVER_H
8

  
9
#include "ConnectionPool.h"
10
#include "Logging.h"
11

  
12
class ColonetServer {
13
public:
14
  ColonetServer();
15
  ~ColonetServer();
16
  int initialize_server(int argc, char * argv[]);
17
  
18
  int log_error(char * error_message);
19
  int log_message(char * message);
20

  
21
  int run_server(int listenSocket);
22

  
23
  ConnectionPool * get_connection_pool_pointer();
24

  
25
private:
26
  ConnectionPool connection_pool;
27
  Log logger;
28
};
29

  
30
#endif
trunk/code/projects/colonet/ColonetServer/includes/Logging.h
1
//TODO: rename either the file or the class so they match
1 2
/**
2 3
 *
3 4
 *  @author Jason Knichel
trunk/code/projects/colonet/ColonetServer/initialization.c
20 20
#include "includes/Logging.h"
21 21

  
22 22
extern int listenSocket;
23
extern Log * logger;
24 23

  
25
int initializeServer(int argc, char** argv)
26
{
27
  printf("Initializing Server...\n");
28

  
29
  parseCmdLine(argc, argv);
30

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

  
34
  if (initConnection(optionsG.listen_port) < 0)
35
    return -1;
36

  
37
  if (initWireless() < 0) {
38
    fprintf(stderr, "%s: initWireless failed\n", __FUNCTION__);
39
    return -1;
40
  }
41

  
42
  return 0;
43
}
44

  
45 24
/*
46 25
int parseCommandLine(int argc, char * argv[], commandParams_t * params)
47 26
{
trunk/code/projects/colonet/ColonetServer/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