Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (10 KB)

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

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

    
17
#include <ConnectionPool.h>
18
#include <Command.h>
19
#include <colonet_defs.h>
20

    
21
#include <colonet_wireless.h>
22

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

    
31
  FD_ZERO(&ready_set);
32
  FD_ZERO(&read_set);
33
  FD_ZERO(&write_set);
34

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

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

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

    
60
  if (next_available_slot == MAX_CONNECTIONS) {
61
    return ERROR_TOO_MANY_CLIENTS;
62
  }
63

    
64
  if (client_file_descriptor > max_file_descriptor) {
65
    max_file_descriptor = client_file_descriptor;
66
  }
67

    
68
  FD_SET(client_file_descriptor, &ready_set);
69

    
70
  int next_slot = next_available_slot;
71

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

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

    
85
  write_buffer_size[next_slot] = 0;
86

    
87
  next_available_slot++;
88

    
89
  return 0;
90
}
91

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

    
104
  int client_file_descriptor = client_file_descriptor_array[pool_index];
105

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

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

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

    
134
  printf("Removing client.\n");
135

    
136
  return 0;
137
}
138

    
139
/**
140
 * @brief Checks the status of the clients
141
 *
142
 * Sees is any clients are ready to read from their file descriptor or are
143
 *  ready to write to their file descriptor.
144
 *
145
 * @return 0 on success, negative error code on error
146
 */
147
//TODO: test that it drops commands properly if it gets sent too much data
148
//      do we want it to drop the data or drop the connection?
149
int ConnectionPool::check_clients() {
150
  int i;
151

    
152
  for (i = 0; i < next_available_slot; i++) {
153
    int client_file_descriptor = client_file_descriptor_array[i];
154

    
155
    if (FD_ISSET(client_file_descriptor, &read_set)) {
156
      if (read_data(i, client_file_descriptor) == DECREMENT_INDEX_COUNTER) {
157
        i--;
158
        continue;
159
      }
160
    }
161

    
162
    if (FD_ISSET(client_file_descriptor, &write_set)) {
163
      write_data(i, client_file_descriptor);
164
    }
165
  }
166

    
167
  return 0;
168
}
169

    
170
/**
171
 * @brief Puts text into a write buffer that will be written to a client's file
172
 *  descriptor sometime when the client is ready to write.
173
 *
174
 * @param pool_index Index in the pool of the client to write to
175
 * @param message The message to be written
176
 * @param length The length of the message
177
 *
178
 * @return 0 on success, negative error code on failure
179
 */
180
int ConnectionPool::write_to_client(int pool_index, char * message, int length) {
181
  if (pool_index < 0 || pool_index >= next_available_slot) {
182
    return ERROR_INVALID_CLIENT_ID;
183
  }
184

    
185
  if (!message) {
186
    return ERROR_INVALID_MESSAGE;
187
  }
188

    
189
  if (length < 0) {
190
    return ERROR_INVALID_MESSAGE_LENGTH;
191
  }
192

    
193
  if (length > (WRITE_BUFFER_SIZE-write_buffer_size[pool_index])) {
194
    //TODO: make this a logging statement instead of a print statement
195
    printf("There is not enough room in the write buffer to send the data to the client.\n");
196
    return ERROR_NOT_ENOUGH_ROOM;
197
  }
198

    
199
  printf("Connection pool: attempting to write [%s], length %i to index %i.\n", message, length, pool_index);
200

    
201
  memcpy(write_buffer[pool_index], message, length);
202
  write_buffer_size[pool_index] += length;
203

    
204
  return 0;
205
}
206

    
207
/**
208
 * @brief Sets the socket to listen on
209
 *
210
 * @param listen_socket The socket to listen on
211
 *
212
 * @return void
213
 */
214
void ConnectionPool::add_new_socket_to_pool(int new_socket) {
215
  if (new_socket < 0)
216
    return;
217

    
218
  FD_SET(new_socket, &ready_set);
219

    
220
  if (new_socket > max_file_descriptor) {
221
    max_file_descriptor = new_socket;
222
  }
223
}
224

    
225
/**
226
 * @brief Find out what file descriptors are ready to write to and read from
227
 *
228
 * @param listen_socket The socket to listen on
229
 * @param select_timeout The timeout for the select statement
230
 *
231
 * @return 0
232
 */
233
int ConnectionPool::perform_select(int listen_socket) {
234
  read_set = ready_set;
235
  write_set = ready_set;
236

    
237
  struct timeval select_timeout;
238
  memset(&select_timeout, 0, sizeof(select_timeout));
239

    
240
  //TODO(Jason): think about why I put this there
241
  if (max_file_descriptor < listen_socket)
242
    max_file_descriptor = listen_socket;
243

    
244
  number_clients_ready = select(max_file_descriptor+1, &(read_set), &(write_set), NULL, &select_timeout);
245

    
246
  if (number_clients_ready < 0) {
247
    perror(__FUNCTION__);
248
  }
249

    
250
  return 0;
251
}
252

    
253
int ConnectionPool::is_socket_ready_to_read(int socket) {
254
  return FD_ISSET(socket, &read_set);
255
}
256

    
257
int ConnectionPool::get_number_clients_ready() {
258
  return number_clients_ready;
259
}
260

    
261

    
262
int ConnectionPool::read_data(int pool_index, int client_file_descriptor) {
263
  char temporary_buffer[READ_BUFFER_SIZE];
264
  char temporary_command_buffer[READ_BUFFER_SIZE+1];
265
  int num_bytes_read;
266
  int length;
267
  int command_length;
268

    
269
  num_bytes_read = read(client_file_descriptor, temporary_buffer, READ_BUFFER_SIZE);
270

    
271
  if (num_bytes_read == 0 || (num_bytes_read == -1 && errno == ECONNRESET)) {
272
    remove_client(pool_index);
273
    return DECREMENT_INDEX_COUNTER;
274
  }
275

    
276
  while (num_bytes_read > 0) {
277
    length = num_bytes_read;
278

    
279
    if (length + read_buffer_size[pool_index] > READ_BUFFER_SIZE) {
280
      length = READ_BUFFER_SIZE - read_buffer_size[pool_index];
281
    }
282

    
283
    memcpy(read_buffer[pool_index]+read_buffer_size[pool_index], temporary_buffer, length);
284
    read_buffer_size[pool_index] += length;
285
    num_bytes_read -= length;
286

    
287
    if (num_bytes_read > 0) {
288
      memmove(temporary_buffer, temporary_buffer+length, READ_BUFFER_SIZE - length);
289
    }
290

    
291
    printf("Read buffer is %s\n", read_buffer[pool_index]);
292

    
293
    char* newline_position;
294

    
295
    while ((newline_position = strstr(read_buffer[pool_index], "\n"))) {
296

    
297
      //if no newline if found in the entire readbuffer (when its full),
298
      //toss out the command
299
      // because either the command being given is too long or someone is trying
300
      // to do something bad to the server
301
      //TODO: this is from before all this code was put in the loop.  reconsider
302
      //      how to check this error condition and do it elsewhere
303
      if (!newline_position && (read_buffer_size[pool_index] == READ_BUFFER_SIZE)) {
304
        read_buffer_size[pool_index] = 0;
305
        break;
306
      }
307

    
308
      //if no newline is found then there is not a command in the buffer
309
      if (!newline_position) {
310
        break;
311
      }
312

    
313
      command_length = (newline_position - read_buffer[pool_index])+1;
314

    
315
      //the newline was found in garbage in the currently not used portion
316
      // of the read buffer
317
      if (command_length > read_buffer_size[pool_index]) {
318
        break;
319
      }
320

    
321
      memcpy(temporary_command_buffer, read_buffer[pool_index], command_length);
322
      //do command_length-1 to get rid of the newline terminating the command
323
      temporary_command_buffer[command_length-1] = '\0';
324
      //did this because telnet was putting a \r\n on the end instead of just \n
325
      if (isspace(temporary_command_buffer[command_length-2])) {
326
        temporary_command_buffer[command_length-2] = '\0';
327
      }
328

    
329
      memmove(read_buffer[pool_index], read_buffer[pool_index]+command_length, read_buffer_size[pool_index] - command_length);
330
      read_buffer_size[pool_index] -= command_length;
331

    
332
      if (command_length > MAX_COMMAND_LEN) {
333
        printf("The command was too long.  Tossing command out.\n");
334
        break;
335
      }
336

    
337
      Command command(this);
338
      if (command.parse_command(temporary_command_buffer, pool_index) != 0) {
339
        printf("There was an error parsing command\n");
340
        break;
341
      }
342
    }
343
  }
344

    
345
  return 0;
346
}
347

    
348

    
349
int ConnectionPool::write_data(int pool_index, int client_file_descriptor) {
350
  if (write_buffer_size[pool_index] == 0) {
351
    return 0;
352
  }
353

    
354
  int sent = write(client_file_descriptor, write_buffer[pool_index], write_buffer_size[pool_index]);
355
  memmove(write_buffer[pool_index], write_buffer[pool_index]+sent, WRITE_BUFFER_SIZE - sent);
356
  write_buffer_size[pool_index] -= sent;
357

    
358
  return 0;
359
}