Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / ColonetServer / ConnectionPool.cpp @ 143

History | View | Annotate | Download (9.73 KB)

1
/**
2
 * @file ConnectionPool.cpp
3
 *
4
 * @author Jason Knichel
5
 * @date 7/22/07
6
 */
7

    
8
#include <sys/select.h>
9
#include <ctype.h>
10
#include <errno.h>
11
#include <unistd.h>
12
#include <string.h>
13
#include <stdlib.h>
14
#include <stdio.h>
15

    
16
#include "includes/ConnectionPool.h"
17
#include "includes/Command.h"
18
#include "../lib/colonet_defs.h"
19
#include <colonet_wireless.h>
20

    
21
/**
22
 * @brief The default constructor for ConnectionPool
23
 */
24
ConnectionPool::ConnectionPool() {
25
  max_file_descriptor = 0;
26
  next_available_slot = 0;
27
  number_clients_ready = 0;
28

    
29
  FD_ZERO(&ready_set);
30
  FD_ZERO(&read_set);
31
  FD_ZERO(&write_set);
32

    
33
  memset(&client_file_descriptor_array, 0, sizeof(int)*MAX_CONNECTIONS);
34
  memset(&read_buffer, 0, sizeof(char *)*MAX_CONNECTIONS);
35
  memset(&read_buffer_size, 0, sizeof(int)*MAX_CONNECTIONS);
36
  memset(&write_buffer, 0, sizeof(char *)*MAX_CONNECTIONS);
37
  memset(&write_buffer_size, 0, sizeof(int)*MAX_CONNECTIONS);
38
}
39

    
40
/**
41
 * @brief The destructor for ConnectionPool
42
 */
43
ConnectionPool::~ConnectionPool() {
44
}
45

    
46
/**
47
 * @brief Adds a client to the connection pool
48
 *
49
 * @param client_file_descriptor The file descriptor to add to the connection pool
50
 *
51
 * @return 0 on success, negative error code on failure
52
 */
53
int ConnectionPool::add_client(int client_file_descriptor) {
54
  if (client_file_descriptor < 0) {
55
    return ERROR_INVALID_CLIENT_DESCRIPTOR;
56
  }
57

    
58
  if (next_available_slot == MAX_CONNECTIONS) {
59
    return ERROR_TOO_MANY_CLIENTS;
60
  }
61

    
62
  if (client_file_descriptor > max_file_descriptor) {
63
    max_file_descriptor = client_file_descriptor;
64
  }
65

    
66
  FD_SET(client_file_descriptor, &ready_set);
67

    
68
  int next_slot = next_available_slot;
69

    
70
  client_file_descriptor_array[next_slot] = client_file_descriptor;
71
  read_buffer[next_slot] = (char*) malloc(sizeof(char) * READ_BUFFER_SIZE);
72
  if (!(read_buffer[next_slot])) {
73
    return ERROR_ALLOCATING_MEMORY;
74
  }
75
  read_buffer_size[next_slot] = 0;
76
  write_buffer[next_slot] = (char *)malloc(sizeof(char) * WRITE_BUFFER_SIZE);
77

    
78
  if (!(write_buffer[next_slot])) {
79
    free(read_buffer[next_slot]);
80
    return ERROR_ALLOCATING_MEMORY;
81
  }
82

    
83
  write_buffer_size[next_slot] = 0;
84

    
85
  next_available_slot++;
86

    
87
  return 0;
88
}
89

    
90
/**
91
 * @brief Removes a client from the connection pool
92
 *
93
 * @param The index in the pool of the client to remove
94
 *
95
 * @return 0 on success, negative error code on failure
96
 */
97
int ConnectionPool::remove_client(int pool_index) {
98
  if (pool_index < 0 || pool_index >= next_available_slot) {
99
    return ERROR_INVALID_CLIENT_DESCRIPTOR;
100
  }
101

    
102
  int client_file_descriptor = client_file_descriptor_array[pool_index];
103

    
104
  if (FD_ISSET(client_file_descriptor, &ready_set)) {
105
    FD_CLR(client_file_descriptor, &ready_set);
106
  }
107
  if (FD_ISSET(client_file_descriptor, &read_set)) {
108
    FD_CLR(client_file_descriptor, &read_set);
109
  }
110
  if (FD_ISSET(client_file_descriptor, &write_set)) {
111
    FD_CLR(client_file_descriptor, &write_set);
112
  }
113

    
114
  free(read_buffer[pool_index]);
115
  free(write_buffer[pool_index]);
116
  for (int j = pool_index; j < next_available_slot - 1; j++) {
117
    client_file_descriptor_array[pool_index] = client_file_descriptor_array[pool_index+1];
118
    read_buffer[pool_index] = read_buffer[pool_index+1];
119
    read_buffer_size[pool_index] = read_buffer_size[pool_index+1];
120
    write_buffer[pool_index] = write_buffer[pool_index+1];
121
    write_buffer_size[pool_index] = write_buffer_size[pool_index+1];
122
  }
123
  next_available_slot--;
124
  int temp_max_file_descriptor = 0;
125

    
126
  for (int j = 0; j < next_available_slot; j++) {
127
    if (client_file_descriptor_array[j] > temp_max_file_descriptor)
128
      temp_max_file_descriptor = client_file_descriptor_array[j];
129
  }
130
  max_file_descriptor = temp_max_file_descriptor;
131

    
132
  printf("Removing client.\n");
133

    
134
  return 0;
135
}
136

    
137
/**
138
 * @brief Checks the status of the clients
139
 *
140
 * Sees is any clients are ready to read from their file descriptor or are
141
 *  ready to write to their file descriptor.
142
 *
143
 * @return 0 on success, negative error code on error
144
 */
145
//TODO: test that it drops commands properly if it gets sent too much data
146
//      do we want it to drop the data or drop the connection?
147
int ConnectionPool::check_clients() {
148
  char temporary_buffer[READ_BUFFER_SIZE];
149
  char temporary_command_buffer[READ_BUFFER_SIZE+1];
150
  int i;
151
  int client_file_descriptor;
152
  int num_bytes_read;
153
  int length;
154
  int sent;
155
  int command_length;
156

    
157
  for (i = 0; i < next_available_slot; i++) {
158
    client_file_descriptor = client_file_descriptor_array[i];
159

    
160
    if (FD_ISSET(client_file_descriptor, &read_set)) {
161
      num_bytes_read = read(client_file_descriptor, temporary_buffer, READ_BUFFER_SIZE);
162

    
163
      if (num_bytes_read == 0 || (num_bytes_read == -1 && errno == ECONNRESET)) {
164
        remove_client(i);
165
        i--;
166
        continue;
167
      }
168

    
169
      while (num_bytes_read > 0) {
170
        length = num_bytes_read;
171

    
172
        if (length + read_buffer_size[i] > READ_BUFFER_SIZE) {
173
          length = READ_BUFFER_SIZE - read_buffer_size[i];
174
        }
175

    
176
        memcpy(read_buffer[i]+read_buffer_size[i], temporary_buffer, length);
177
        read_buffer_size[i] += length;
178
        num_bytes_read -= length;
179

    
180
        if (num_bytes_read > 0) {
181
          memmove(temporary_buffer, temporary_buffer+length, READ_BUFFER_SIZE - length);
182
        }
183

    
184
        printf("Read buffer is %s\n", read_buffer[i]);
185

    
186
        char* newline_position;
187

    
188
        while ((newline_position = strstr(read_buffer[i], "\n"))) {
189

    
190
          //if no newline if found in the entire readbuffer (when its full), 
191
          //toss out the command
192
          // because either the command being given is too long or someone is trying
193
          // to do something bad to the server
194
          //TODO: this is from before all this code was put in the loop.  reconsider
195
          //      how to check this error condition and do it elsewhere
196
          if (!newline_position && (read_buffer_size[i] == READ_BUFFER_SIZE)) {
197
            read_buffer_size[i] = 0;
198
            break;
199
          }
200

    
201
          //if no newline is found then there is not a command in the buffer
202
          if (!newline_position) {
203
            break;
204
          }
205

    
206
          command_length = (newline_position - read_buffer[i])+1;
207

    
208
          //the newline was found in garbage in the currently not used portion
209
          // of the read buffer
210
          if (command_length > read_buffer_size[i]) {
211
            break;
212
          }
213

    
214
          memcpy(temporary_command_buffer, read_buffer[i], command_length);
215
          //do command_length-1 to get rid of the newline terminating the command
216
          temporary_command_buffer[command_length-1] = '\0';
217
          //did this because telnet was putting a \r\n on the end instead of just \n
218
          if (isspace(temporary_command_buffer[command_length-2])) {
219
            temporary_command_buffer[command_length-2] = '\0';
220
          }
221

    
222
          memmove(read_buffer[i], read_buffer[i]+command_length, read_buffer_size[i] - command_length);
223
          read_buffer_size[i] -= command_length;
224

    
225
          if (command_length > MAX_COMMAND_LEN) {
226
            printf("The command was too long.  Tossing command out.\n");
227
            break;
228
          }
229

    
230
          Command command(this);
231
          if (command.parse_command(temporary_command_buffer, i) < 0) {
232
            printf("There was an error parsing command\n");
233
            break;
234
          }
235
        }
236
      }
237
    }
238

    
239
    if (FD_ISSET(client_file_descriptor, &write_set)) {
240
      if (write_buffer_size[i] == 0) {
241
        continue;
242
      }
243

    
244
      sent = write(client_file_descriptor, write_buffer[i], write_buffer_size[i]);
245
      memmove(write_buffer[i], write_buffer[i]+sent, WRITE_BUFFER_SIZE - sent);
246
      write_buffer_size[i] -= sent;
247
    }
248
  }
249

    
250
  return 0;
251
}
252

    
253
/**
254
 * @brief Puts text into a write buffer that will be written to a client's file
255
 *  descriptor sometime when the client is ready to write.
256
 *
257
 * @param pool_index Index in the pool of the client to write to
258
 * @param message The message to be written
259
 * @param length The length of the message
260
 *
261
 * @return 0 on success, negative error code on failure
262
 */
263
int ConnectionPool::write_to_client(int pool_index, char * message, int length) {
264
  if (pool_index < 0 || pool_index >= next_available_slot) {
265
    return ERROR_INVALID_CLIENT_ID;
266
  }
267

    
268
  if (!message) {
269
    return ERROR_INVALID_MESSAGE;
270
  }
271

    
272
  if (length < 0) {
273
    return ERROR_INVALID_MESSAGE_LENGTH;
274
  }
275

    
276
  if (length > (WRITE_BUFFER_SIZE-write_buffer_size[pool_index])) {
277
    //TODO: make this a logging statement instead of a print statement
278
    printf("There is not enough room in the write buffer to send the data to the client.\n");
279
    return ERROR_NOT_ENOUGH_ROOM;
280
  }
281

    
282
  memcpy(write_buffer[pool_index], message, length);
283
  write_buffer_size[pool_index] += length;
284

    
285
  return 0;
286
}
287

    
288
/**
289
 * @brief Sets the socket to listen on
290
 *
291
 * @param listen_socket The socket to listen on
292
 *
293
 * @return void
294
 */
295
void ConnectionPool::add_new_socket_to_pool(int new_socket) {
296
  if (new_socket < 0)
297
    return;
298

    
299
  FD_SET(new_socket, &ready_set);
300

    
301
  if (new_socket > max_file_descriptor) {
302
    max_file_descriptor = new_socket;
303
  }
304
}
305

    
306
/**
307
 * @brief Find out what file descriptors are ready to write to and read from
308
 *
309
 * @param listen_socket The socket to listen on
310
 * @param select_timeout The timeout for the select statement
311
 *
312
 * @return 0
313
 */
314
int ConnectionPool::perform_select(int listen_socket) {
315
  read_set = ready_set;
316
  write_set = ready_set;
317

    
318
  struct timeval select_timeout;
319
  memset(&select_timeout, 0, sizeof(select_timeout));
320

    
321
  //TODO(Jason): think about why I put this there
322
  if (max_file_descriptor < listen_socket)
323
    max_file_descriptor = listen_socket;
324

    
325
  number_clients_ready = select(max_file_descriptor+1, &(read_set), &(write_set), NULL, &select_timeout);
326

    
327
  if (number_clients_ready < 0) {
328
    perror(__FUNCTION__);
329
  }
330

    
331
  return 0;
332
}
333

    
334
int ConnectionPool::is_socket_ready_to_read(int socket) {
335
  return FD_ISSET(socket, &read_set);
336
}
337

    
338
int ConnectionPool::get_number_clients_ready() {
339
  return number_clients_ready;
340
}
341

    
342