root / trunk / code / projects / colonet / ColonetServer / ColonetServer.cpp @ 424
History | View | Annotate | Download (6.29 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(): logger("logFile.txt") {
|
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 |
parseCmdLine(argc, argv); |
58 |
|
59 |
if (initialize_connection(optionsG.listen_port) < 0) { |
60 |
return -1; |
61 |
} |
62 |
|
63 |
if (initialize_wireless() < 0) { |
64 |
fprintf(stderr, "%s: initWireless failed\n", __FUNCTION__);
|
65 |
return -1; |
66 |
} |
67 |
|
68 |
return 0; |
69 |
} |
70 |
|
71 |
/**
|
72 |
* @brief Starts the server listening on the socket that was opened for listening
|
73 |
*
|
74 |
* @return 0 on success, negative error code on failure
|
75 |
*/
|
76 |
int ColonetServer::start_listening() {
|
77 |
if (listen(listen_socket, LISTEN_BACKLOG) < 0) { |
78 |
logger.log_error("\t\nThere was an error telling the socket to "
|
79 |
"listen for connections from clients. Terminating Server...\n");
|
80 |
return -1; |
81 |
} |
82 |
return 0; |
83 |
} |
84 |
|
85 |
|
86 |
/**
|
87 |
* @brief Starts the server running (starts an infinite loop)
|
88 |
*/
|
89 |
int ColonetServer::run_server() {
|
90 |
connection_pool.add_new_socket_to_pool(listen_socket); |
91 |
|
92 |
int accept_socket = 0; |
93 |
struct sockaddr_in client_addr;
|
94 |
socklen_t client_addr_size = sizeof(client_addr);
|
95 |
|
96 |
logger.log_message("Server initialized. About to start listening for connections");
|
97 |
|
98 |
while(1) { |
99 |
usleep(10000);
|
100 |
|
101 |
connection_pool.perform_select(listen_socket); |
102 |
|
103 |
//either no descriptors are ready or there was an error
|
104 |
if (connection_pool.get_number_clients_ready() <= 0) { |
105 |
continue;
|
106 |
} |
107 |
|
108 |
if (connection_pool.is_socket_ready_to_read(listen_socket)) {
|
109 |
printf("Something is trying to connect...\n");
|
110 |
if ((accept_socket = accept(listen_socket, (struct sockaddr*) &client_addr, &client_addr_size)) < 0) { |
111 |
if (errno == EMFILE) {
|
112 |
printf("\tWhen attempting to accept a connection, reached the per process limit of file descriptors."
|
113 |
" Dropping the new connection.\n");
|
114 |
continue;
|
115 |
} else {
|
116 |
printf("\tThere was an error when attempting to accept a connection");
|
117 |
} |
118 |
continue;
|
119 |
} |
120 |
|
121 |
char log_buffer[LOG_BUFFER_LENGTH];
|
122 |
snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s attempting to connect.",
|
123 |
inet_ntoa(client_addr.sin_addr)); |
124 |
logger.log_string(LOG_TYPE_CONNECT, log_buffer); |
125 |
|
126 |
if (connection_pool.add_client(accept_socket) < 0) { |
127 |
printf("\tThere was an error when trying to add a client to the connection pool.");
|
128 |
continue;
|
129 |
} |
130 |
|
131 |
snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s successfully added to connection pool.",
|
132 |
inet_ntoa(client_addr.sin_addr)); |
133 |
logger.log_string(LOG_TYPE_CONNECT, log_buffer); |
134 |
} |
135 |
|
136 |
if (connection_pool.check_clients() < 0) { |
137 |
printf("\tThere was an error trying to update the clients.");
|
138 |
continue;
|
139 |
} |
140 |
} |
141 |
|
142 |
return 0; |
143 |
} |
144 |
|
145 |
/**
|
146 |
* @param source - ID of robot that message is from.
|
147 |
* @param dest - ID of internet client to send message to.
|
148 |
* @param data - Data to send to internet client.
|
149 |
* @param len - Length of the data param.
|
150 |
*/
|
151 |
int ColonetServer::process_received_wireless_message(int dest, char* data, int len) { |
152 |
if (connection_pool.write_to_client(dest, data, len) == ERROR_INVALID_CLIENT_ID) {
|
153 |
printf("The robot wanted to pass the data to a client not in the pool.\n");
|
154 |
return -1; |
155 |
} |
156 |
|
157 |
//("Put data in write buffer for client.\n");
|
158 |
|
159 |
return 0; |
160 |
} |
161 |
|
162 |
/**
|
163 |
* @brief Initializes the wireless
|
164 |
*
|
165 |
* @return 0 on success, negative error code on error
|
166 |
*/
|
167 |
int ColonetServer::initialize_wireless() {
|
168 |
char* log_filename = NULL; |
169 |
|
170 |
if (optionsG.logging_enabled) {
|
171 |
printf("Logging enabled. Log filename: %s\n", optionsG.log_filename);
|
172 |
log_filename = optionsG.log_filename; |
173 |
} else {
|
174 |
printf("Logging disabled.\n");
|
175 |
} |
176 |
|
177 |
if (colonet_wl_init(optionsG.wireless_port, log_filename) != 0) { |
178 |
fprintf(stderr, "ERROR - colonet_wl_init failed.\n");
|
179 |
return -1; |
180 |
} |
181 |
|
182 |
if (colonet_wl_run_listener_thread()) {
|
183 |
fprintf(stderr, "%s: colonet_wl_run_listener_thread failed.\n", __FUNCTION__);
|
184 |
return -1; |
185 |
} |
186 |
|
187 |
return 0; |
188 |
} |
189 |
|
190 |
/**
|
191 |
* @brief Initialize a connection to listen on
|
192 |
*
|
193 |
* @port The port to try to open to listen on
|
194 |
*
|
195 |
* @return 0 on success, negative error code on error
|
196 |
*/
|
197 |
int ColonetServer::initialize_connection(int port) { |
198 |
printf("Initializing connection that will be used to listen for clients...\n");
|
199 |
int options = 1; |
200 |
struct sockaddr_in my_address;
|
201 |
|
202 |
//get a socket fd
|
203 |
if ((listen_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { |
204 |
printf("\tThere was an error creating socket\n");
|
205 |
return -1; |
206 |
} |
207 |
|
208 |
//set up the address struct
|
209 |
memset(&my_address,'\0',sizeof(my_address)); |
210 |
my_address.sin_family = AF_INET; |
211 |
my_address.sin_addr.s_addr = htonl(INADDR_ANY); |
212 |
my_address.sin_port = htons(port); |
213 |
|
214 |
setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &options, sizeof(options));
|
215 |
|
216 |
//get the current socket options
|
217 |
if ((options = fcntl(listen_socket, F_GETFL)) < 0) { |
218 |
printf("\tThere was an error getting the socket options.\n");
|
219 |
return -1; |
220 |
} |
221 |
|
222 |
//set the socket to non blocking
|
223 |
options = (options | O_NONBLOCK); |
224 |
if (fcntl(listen_socket, F_SETFL, options) < 0) { |
225 |
printf("\tThere was an error setting the socket to be non blocking.\n");
|
226 |
return -1; |
227 |
} |
228 |
|
229 |
//bind the socket to listen on the specified port
|
230 |
if (bind(listen_socket, (struct sockaddr *) &my_address, sizeof(my_address)) < 0) { |
231 |
printf("\tThere was an error binding the socket\n");
|
232 |
return -1; |
233 |
} |
234 |
|
235 |
return 0; |
236 |
} |