Project

General

Profile

Revision 143

Added by Jason knichel over 16 years ago

improved the data encapsulation of connection pool

View differences:

ColonetServer.cpp
86 86
 * @brief Starts the server running (starts an infinite loop)
87 87
 */
88 88
int ColonetServer::run_server() {
89
  connection_pool.set_listen_socket_in_ready_set(listen_socket);
89
  connection_pool.add_new_socket_to_pool(listen_socket);
90 90

  
91
  //TODO: why is all of this in that if statement and not just conn_pool.maxfd = listen_socket ?
92
  if (listen_socket > connection_pool.get_max_file_descriptor()) {
93
    connection_pool.set_max_file_descriptor(listen_socket);
91
  int accept_socket = 0;
92
  struct sockaddr_in client_addr;
93
  socklen_t client_addr_size = sizeof(client_addr);
94 94

  
95
    int accept_socket = 0;
96
    struct sockaddr_in client_addr;
97
    socklen_t client_addr_size = sizeof(client_addr);
95
  logger.log_message("Server initialized.  About to start listening for connections");
98 96

  
99
    logger.log_message("Server initialized.  About to start listening for connections");
97
  while(1) {
98
    connection_pool.perform_select(listen_socket);
100 99

  
101
    while(1) {
102
      connection_pool.perform_select(listen_socket);
100
    //either no descriptors are ready or there was an error
101
    if (connection_pool.get_number_clients_ready() <= 0) {
102
      continue;
103
    }
103 104

  
104
      //either no descriptors are ready or there was an error
105
      if (connection_pool.get_number_clients_ready() <= 0) {
106
        continue;
105
    if (connection_pool.is_socket_ready_to_read(listen_socket)) {
106
      printf("Something is trying to connect...\n");
107
      if ((accept_socket = accept(listen_socket, (struct sockaddr*) &client_addr, &client_addr_size)) < 0) {
108
	if (errno == EMFILE) {
109
	  printf("\tWhen attempting to accept a connection, "
110
		 "reached the per process limit of file descriptors."
111
		 "  Dropping the new connection.\n");
112
	  continue;
113
	} else {
114
	  printf("\tThere was an error when attempting to accept a connection");
115
	}
116
	continue;
107 117
      }
108 118

  
109
      if (connection_pool.is_socket_ready_to_read(listen_socket)) {
110
        printf("Something is trying to connect...\n");
111
        if ((accept_socket = accept(listen_socket, (struct sockaddr*) &client_addr, &client_addr_size)) < 0) {
112
          if (errno == EMFILE) {
113
            printf("\tWhen attempting to accept a connection, "
114
                   "reached the per process limit of file descriptors."
115
                   "  Dropping the new connection.\n");
116
            continue;
117
          } else {
118
            printf("\tThere was an error when attempting to accept a connection");
119
          }
120
          continue;
121
        }
119
      char log_buffer[LOG_BUFFER_LENGTH];
120
      snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s attempting to connect.", 
121
	       inet_ntoa(client_addr.sin_addr));
122
      logger.log_string(LOG_TYPE_CONNECT, log_buffer);
122 123

  
123
        char log_buffer[LOG_BUFFER_LENGTH];
124
        snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s attempting to connect.", 
125
                 inet_ntoa(client_addr.sin_addr));
126
        logger.log_string(LOG_TYPE_CONNECT, log_buffer);
127

  
128
        if (connection_pool.add_client(accept_socket) < 0) {
129
          printf("\tThere was an error when trying to add a client to the connection pool.");
130
          continue;
131
        }
132

  
133
        snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s successfully added to connection pool.", 
134
                 inet_ntoa(client_addr.sin_addr));
135
        logger.log_string(LOG_TYPE_CONNECT, log_buffer);
124
      if (connection_pool.add_client(accept_socket) < 0) {
125
	printf("\tThere was an error when trying to add a client to the connection pool.");
126
	continue;
136 127
      }
137 128

  
138
      if (connection_pool.check_clients() < 0) {
139
        printf("\tThere was an error trying to update the clients.");
140
        continue;
141
      }
129
      snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s successfully added to connection pool.", 
130
	       inet_ntoa(client_addr.sin_addr));
131
      logger.log_string(LOG_TYPE_CONNECT, log_buffer);
142 132
    }
133

  
134
    if (connection_pool.check_clients() < 0) {
135
      printf("\tThere was an error trying to update the clients.");
136
      continue;
137
    }
143 138
  }
144 139

  
145 140
  return 0;

Also available in: Unified diff