root / trunk / code / projects / colonet / ColonetServer / ColonetServer.cpp @ 426
History | View | Annotate | Download (6.59 KB)
1 |
/**
|
---|---|
2 |
* @file ColonetServer.cpp
|
3 |
*
|
4 |
* @brief colonet_server - primary server application for Colonet
|
5 |
*
|
6 |
* @author Jason Knichel
|
7 |
* @author Eugene Marinelli
|
8 |
* @date 10/31/06
|
9 |
*/
|
10 |
|
11 |
#include <arpa/inet.h> |
12 |
#include <fcntl.h> |
13 |
#include <errno.h> |
14 |
#include <netinet/in.h> |
15 |
#include <string.h> |
16 |
#include <unistd.h> |
17 |
#include <sys/select.h> |
18 |
#include <sys/socket.h> |
19 |
|
20 |
#include <colonet_wireless.h> |
21 |
|
22 |
#include <ColonetServer.h> |
23 |
#include <ConnectionPool.h> |
24 |
#include <options.h> |
25 |
#include <Log.h> |
26 |
|
27 |
#define LISTEN_BACKLOG 5 |
28 |
#define LOG_BUFFER_LENGTH 128 |
29 |
#define MAX_MSG_BUFFER_LENGTH 128 |
30 |
|
31 |
#define u_int32_t unsigned |
32 |
|
33 |
static ConnectionPool* connection_pool;
|
34 |
|
35 |
/**
|
36 |
* @brief Default constructor for ColonetServer
|
37 |
*/
|
38 |
ColonetServer::ColonetServer() { |
39 |
listen_socket = 0;
|
40 |
} |
41 |
|
42 |
/**
|
43 |
* @brief Destructor for ColonetServer
|
44 |
*/
|
45 |
ColonetServer::~ColonetServer() { |
46 |
} |
47 |
|
48 |
/**
|
49 |
* @brief Initializes the various elements needed for the server to run
|
50 |
*
|
51 |
* @param argc The number of command line arguments passed to the program
|
52 |
* @param argv The command line arguments passed to the program
|
53 |
*
|
54 |
* @return 0 on success, negative error code on failure
|
55 |
*/
|
56 |
int ColonetServer::initialize_server(int argc, char * argv[]) { |
57 |
options_parseCmdLine(argc, argv); |
58 |
|
59 |
if (optionsG.logging_enabled) {
|
60 |
logger = new Log("colonet_server_log.txt"); |
61 |
} |
62 |
|
63 |
if (initialize_connection(optionsG.listen_port) < 0) { |
64 |
return -1; |
65 |
} |
66 |
|
67 |
if (initialize_wireless() < 0) { |
68 |
fprintf(stderr, "%s: initWireless failed\n", __FUNCTION__);
|
69 |
return -1; |
70 |
} |
71 |
|
72 |
return 0; |
73 |
} |
74 |
|
75 |
/**
|
76 |
* @brief Starts the server listening on the socket that was opened for listening
|
77 |
*
|
78 |
* @return 0 on success, negative error code on failure
|
79 |
*/
|
80 |
int ColonetServer::start_listening() {
|
81 |
if (listen(listen_socket, LISTEN_BACKLOG) < 0) { |
82 |
if (optionsG.logging_enabled) {
|
83 |
logger->log_error("\t\nThere was an error telling the socket to listen for connections from clients. "
|
84 |
"Terminating Server...\n");
|
85 |
} |
86 |
return -1; |
87 |
} |
88 |
return 0; |
89 |
} |
90 |
|
91 |
/**
|
92 |
* @brief Starts the server running (starts an infinite loop)
|
93 |
*/
|
94 |
int ColonetServer::run_server() {
|
95 |
connection_pool.add_new_socket_to_pool(listen_socket); |
96 |
|
97 |
int accept_socket = 0; |
98 |
struct sockaddr_in client_addr;
|
99 |
socklen_t client_addr_size = sizeof(client_addr);
|
100 |
|
101 |
if (optionsG.logging_enabled) {
|
102 |
logger->log_message("Server initialized. About to start listening for connections");
|
103 |
} |
104 |
|
105 |
while(1) { |
106 |
usleep(10000);
|
107 |
|
108 |
connection_pool.perform_select(listen_socket); |
109 |
|
110 |
//either no descriptors are ready or there was an error
|
111 |
if (connection_pool.get_number_clients_ready() <= 0) { |
112 |
continue;
|
113 |
} |
114 |
|
115 |
if (connection_pool.is_socket_ready_to_read(listen_socket)) {
|
116 |
printf("Something is trying to connect...\n");
|
117 |
if ((accept_socket = accept(listen_socket, (struct sockaddr*) &client_addr, &client_addr_size)) < 0) { |
118 |
if (errno == EMFILE) {
|
119 |
printf("\tWhen attempting to accept a connection, reached the per process limit of file descriptors."
|
120 |
" Dropping the new connection.\n");
|
121 |
continue;
|
122 |
} else {
|
123 |
printf("\tThere was an error when attempting to accept a connection");
|
124 |
} |
125 |
continue;
|
126 |
} |
127 |
|
128 |
if (optionsG.logging_enabled) {
|
129 |
char log_buffer[LOG_BUFFER_LENGTH];
|
130 |
snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s attempting to connect.",
|
131 |
inet_ntoa(client_addr.sin_addr)); |
132 |
logger->log_string(LOG_TYPE_CONNECT, log_buffer); |
133 |
} |
134 |
|
135 |
if (connection_pool.add_client(accept_socket) < 0) { |
136 |
printf("\tThere was an error when trying to add a client to the connection pool.");
|
137 |
continue;
|
138 |
} |
139 |
|
140 |
if (optionsG.logging_enabled) {
|
141 |
char log_buffer[LOG_BUFFER_LENGTH];
|
142 |
snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s successfully added to connection pool.",
|
143 |
inet_ntoa(client_addr.sin_addr)); |
144 |
logger->log_string(LOG_TYPE_CONNECT, log_buffer); |
145 |
} |
146 |
} |
147 |
|
148 |
if (connection_pool.check_clients() < 0) { |
149 |
printf("\tThere was an error trying to update the clients.");
|
150 |
continue;
|
151 |
} |
152 |
} |
153 |
|
154 |
return 0; |
155 |
} |
156 |
|
157 |
/**
|
158 |
* @param source - ID of robot that message is from.
|
159 |
* @param dest - ID of internet client to send message to.
|
160 |
* @param data - Data to send to internet client.
|
161 |
* @param len - Length of the data param.
|
162 |
*/
|
163 |
int ColonetServer::process_received_wireless_message(int dest, char* data, int len) { |
164 |
if (connection_pool.write_to_client(dest, data, len) == ERROR_INVALID_CLIENT_ID) {
|
165 |
printf("The robot wanted to pass the data to a client not in the pool.\n");
|
166 |
return -1; |
167 |
} |
168 |
|
169 |
//("Put data in write buffer for client.\n");
|
170 |
|
171 |
return 0; |
172 |
} |
173 |
|
174 |
/**
|
175 |
* @brief Initializes the wireless
|
176 |
*
|
177 |
* @return 0 on success, negative error code on error
|
178 |
*/
|
179 |
int ColonetServer::initialize_wireless() {
|
180 |
char* log_filename = NULL; |
181 |
|
182 |
if (optionsG.logging_enabled) {
|
183 |
printf("Logging enabled. Log filename: %s\n", optionsG.log_filename);
|
184 |
log_filename = optionsG.log_filename; |
185 |
} else {
|
186 |
printf("Logging disabled.\n");
|
187 |
} |
188 |
|
189 |
if (colonet_wl_init(optionsG.wireless_port, log_filename) != 0) { |
190 |
fprintf(stderr, "ERROR - colonet_wl_init failed.\n");
|
191 |
return -1; |
192 |
} |
193 |
|
194 |
if (colonet_wl_run_listener_thread()) {
|
195 |
fprintf(stderr, "%s: colonet_wl_run_listener_thread failed.\n", __FUNCTION__);
|
196 |
return -1; |
197 |
} |
198 |
|
199 |
return 0; |
200 |
} |
201 |
|
202 |
/**
|
203 |
* @brief Initialize a connection to listen on
|
204 |
*
|
205 |
* @port The port to try to open to listen on
|
206 |
*
|
207 |
* @return 0 on success, negative error code on error
|
208 |
*/
|
209 |
int ColonetServer::initialize_connection(int port) { |
210 |
printf("Initializing connection that will be used to listen for clients...\n");
|
211 |
int options = 1; |
212 |
struct sockaddr_in my_address;
|
213 |
|
214 |
//get a socket fd
|
215 |
if ((listen_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { |
216 |
printf("\tThere was an error creating socket\n");
|
217 |
return -1; |
218 |
} |
219 |
|
220 |
//set up the address struct
|
221 |
memset(&my_address,'\0',sizeof(my_address)); |
222 |
my_address.sin_family = AF_INET; |
223 |
my_address.sin_addr.s_addr = htonl(INADDR_ANY); |
224 |
my_address.sin_port = htons(port); |
225 |
|
226 |
setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &options, sizeof(options));
|
227 |
|
228 |
//get the current socket options
|
229 |
if ((options = fcntl(listen_socket, F_GETFL)) < 0) { |
230 |
printf("\tThere was an error getting the socket options.\n");
|
231 |
return -1; |
232 |
} |
233 |
|
234 |
//set the socket to non blocking
|
235 |
options = (options | O_NONBLOCK); |
236 |
if (fcntl(listen_socket, F_SETFL, options) < 0) { |
237 |
printf("\tThere was an error setting the socket to be non blocking.\n");
|
238 |
return -1; |
239 |
} |
240 |
|
241 |
//bind the socket to listen on the specified port
|
242 |
if (bind(listen_socket, (struct sockaddr *) &my_address, sizeof(my_address)) < 0) { |
243 |
printf("\tThere was an error binding the socket\n");
|
244 |
return -1; |
245 |
} |
246 |
|
247 |
return 0; |
248 |
} |