root / trunk / code / projects / colonet / server / ColonetServer.cpp @ 648
History | View | Annotate | Download (8.18 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 |
//TODO: is this needed anymore? it still compiles when i comment it out
|
34 |
//static ConnectionPool* connection_pool;
|
35 |
|
36 |
/**
|
37 |
* @brief Default constructor for ColonetServer
|
38 |
*/
|
39 |
ColonetServer::ColonetServer() { |
40 |
listen_socket = 0;
|
41 |
connection_pool = new ConnectionPool(this); |
42 |
} |
43 |
|
44 |
/**
|
45 |
* @brief Destructor for ColonetServer
|
46 |
*/
|
47 |
ColonetServer::~ColonetServer() { |
48 |
} |
49 |
|
50 |
/**
|
51 |
* @brief Initializes the various elements needed for the server to run
|
52 |
*
|
53 |
* @param argc The number of command line arguments passed to the program
|
54 |
* @param argv The command line arguments passed to the program
|
55 |
*
|
56 |
* @return 0 on success, negative error code on failure
|
57 |
*/
|
58 |
int ColonetServer::initialize_server(int argc, char * argv[]) { |
59 |
options_parseCmdLine(argc, argv); |
60 |
|
61 |
if (optionsG.logging_enabled) {
|
62 |
logger = new Log("colonet_server_log.txt"); |
63 |
} |
64 |
|
65 |
if (initialize_connection(optionsG.listen_port) < 0) { |
66 |
return -1; |
67 |
} |
68 |
|
69 |
if (initialize_wireless() < 0) { |
70 |
fprintf(stderr, "%s: initWireless failed\n", __FUNCTION__);
|
71 |
return -1; |
72 |
} |
73 |
|
74 |
return 0; |
75 |
} |
76 |
|
77 |
/**
|
78 |
* @brief Starts the server listening on the socket that was opened for listening
|
79 |
*
|
80 |
* @return 0 on success, negative error code on failure
|
81 |
*/
|
82 |
int ColonetServer::start_listening() {
|
83 |
if (listen(listen_socket, LISTEN_BACKLOG) < 0) { |
84 |
if (optionsG.logging_enabled) {
|
85 |
logger->log_error("\t\nThere was an error telling the socket to listen for connections from clients. "
|
86 |
"Terminating Server...\n");
|
87 |
} |
88 |
return -1; |
89 |
} |
90 |
|
91 |
return 0; |
92 |
} |
93 |
|
94 |
/**
|
95 |
* @brief The function that is run in the new pthread in order to do the position monitoring
|
96 |
*
|
97 |
* @param arg the argument to give to the new thread
|
98 |
*/
|
99 |
void* run_thread(void* arg) { |
100 |
PositionMonitor* p = (PositionMonitor*) arg; |
101 |
p->run(); |
102 |
return NULL; |
103 |
} |
104 |
|
105 |
/**
|
106 |
* @brief This function spawns a new thread to do the position monitoring based off data
|
107 |
* retrieved from the library that parses the image from the camera
|
108 |
*/
|
109 |
int ColonetServer::run_position_monitor() {
|
110 |
pthread_t posmon_thread; |
111 |
pthread_create(&posmon_thread, NULL, run_thread, &position_monitor);
|
112 |
|
113 |
return 0; |
114 |
} |
115 |
|
116 |
/**
|
117 |
* @brief Starts the server running (starts an infinite loop)
|
118 |
*/
|
119 |
int ColonetServer::run_server() {
|
120 |
//add the socket that you will listen to connections on to the connection pool
|
121 |
connection_pool->add_new_socket_to_pool(listen_socket); |
122 |
|
123 |
int accept_socket = 0; |
124 |
struct sockaddr_in client_addr;
|
125 |
socklen_t client_addr_size = sizeof(client_addr);
|
126 |
|
127 |
if (optionsG.logging_enabled) {
|
128 |
logger->log_message("Server initialized. About to start listening for connections");
|
129 |
} |
130 |
|
131 |
while(1) { |
132 |
//sleep for a little bit so this program doesn't hog cpu
|
133 |
usleep(10000);
|
134 |
|
135 |
//tell the connection pool to look at the file descriptors and see which ones are ready
|
136 |
connection_pool->perform_select(listen_socket); |
137 |
|
138 |
//either no descriptors are ready or there was an error
|
139 |
if (connection_pool->get_number_clients_ready() <= 0) { |
140 |
continue;
|
141 |
} |
142 |
|
143 |
//check to see if a new client is trying to connect
|
144 |
if (connection_pool->is_socket_ready_to_read(listen_socket)) {
|
145 |
printf("Something is trying to connect...\n");
|
146 |
//accept the connection
|
147 |
if ((accept_socket = accept(listen_socket, (struct sockaddr*) &client_addr, &client_addr_size)) < 0) { |
148 |
if (errno == EMFILE) {
|
149 |
printf("\tWhen attempting to accept a connection, reached the per process limit of file descriptors."
|
150 |
" Dropping the new connection.\n");
|
151 |
continue;
|
152 |
} else {
|
153 |
printf("\tThere was an error when attempting to accept a connection");
|
154 |
} |
155 |
continue;
|
156 |
} |
157 |
|
158 |
if (optionsG.logging_enabled) {
|
159 |
char log_buffer[LOG_BUFFER_LENGTH];
|
160 |
snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s attempting to connect.",
|
161 |
inet_ntoa(client_addr.sin_addr)); |
162 |
logger->log_string(LOG_TYPE_CONNECT, log_buffer); |
163 |
} |
164 |
|
165 |
//add the new client to the connection pool
|
166 |
if (connection_pool->add_client(accept_socket) < 0) { |
167 |
printf("\tThere was an error when trying to add a client to the connection pool.");
|
168 |
continue;
|
169 |
} |
170 |
|
171 |
if (optionsG.logging_enabled) {
|
172 |
char log_buffer[LOG_BUFFER_LENGTH];
|
173 |
snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s successfully added to connection pool.",
|
174 |
inet_ntoa(client_addr.sin_addr)); |
175 |
logger->log_string(LOG_TYPE_CONNECT, log_buffer); |
176 |
} |
177 |
} |
178 |
|
179 |
//check all the clients in the connection pool
|
180 |
if (connection_pool->check_clients() < 0) { |
181 |
printf("\tThere was an error trying to update the clients.");
|
182 |
continue;
|
183 |
} |
184 |
} |
185 |
|
186 |
return 0; |
187 |
} |
188 |
|
189 |
/**
|
190 |
* @brief when the server receives a new wireless message, it calls this function to process it
|
191 |
*
|
192 |
* @param source - ID of robot that message is from.
|
193 |
* @param dest - ID of internet client to send message to.
|
194 |
* @param data - Data to send to internet client.
|
195 |
* @param len - Length of the data param.
|
196 |
*/
|
197 |
int ColonetServer::process_received_wireless_message(int dest, char* data, int len) { |
198 |
//try to pass the robot's response onto the client it wanted to respond to
|
199 |
if (connection_pool->write_to_client(dest, data, len) == ERROR_INVALID_CLIENT_ID) {
|
200 |
printf("The robot wanted to pass the data to a client not in the pool.\n");
|
201 |
return -1; |
202 |
} |
203 |
printf("Processing data from robot: %s\n", data);
|
204 |
return 0; |
205 |
} |
206 |
|
207 |
/**
|
208 |
* @brief Initializes the wireless
|
209 |
*
|
210 |
* @return 0 on success, negative error code on error
|
211 |
*/
|
212 |
int ColonetServer::initialize_wireless() {
|
213 |
char* log_filename = NULL; |
214 |
|
215 |
if (optionsG.logging_enabled) {
|
216 |
printf("Logging enabled. Log filename: %s\n", optionsG.log_filename);
|
217 |
log_filename = optionsG.log_filename; |
218 |
} else {
|
219 |
printf("Logging disabled.\n");
|
220 |
} |
221 |
|
222 |
//initialize the wireless library giving it the port and the log file name
|
223 |
if (colonet_wl_init(optionsG.wireless_port, log_filename) != 0) { |
224 |
fprintf(stderr, "ERROR - colonet_wl_init failed.\n");
|
225 |
return -1; |
226 |
} |
227 |
|
228 |
//call the function that will start the thread that will listen for wireless messages
|
229 |
if (colonet_wl_run_listener_thread()) {
|
230 |
fprintf(stderr, "%s: colonet_wl_run_listener_thread failed.\n", __FUNCTION__);
|
231 |
return -1; |
232 |
} |
233 |
|
234 |
return 0; |
235 |
} |
236 |
|
237 |
/**
|
238 |
* @brief Initialize a connection to listen on
|
239 |
*
|
240 |
* @port The port to try to open to listen on
|
241 |
*
|
242 |
* @return 0 on success, negative error code on error
|
243 |
*/
|
244 |
int ColonetServer::initialize_connection(int port) { |
245 |
printf("Initializing connection that will be used to listen for clients...\n");
|
246 |
int options = 1; |
247 |
struct sockaddr_in my_address;
|
248 |
|
249 |
//get a socket fd
|
250 |
if ((listen_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { |
251 |
printf("\tThere was an error creating socket\n");
|
252 |
return -1; |
253 |
} |
254 |
|
255 |
//set up the address struct
|
256 |
memset(&my_address,'\0',sizeof(my_address)); |
257 |
my_address.sin_family = AF_INET; |
258 |
my_address.sin_addr.s_addr = htonl(INADDR_ANY); |
259 |
my_address.sin_port = htons(port); |
260 |
|
261 |
setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &options, sizeof(options));
|
262 |
|
263 |
//get the current socket options
|
264 |
if ((options = fcntl(listen_socket, F_GETFL)) < 0) { |
265 |
printf("\tThere was an error getting the socket options.\n");
|
266 |
return -1; |
267 |
} |
268 |
|
269 |
//set the socket to non blocking
|
270 |
options = (options | O_NONBLOCK); |
271 |
if (fcntl(listen_socket, F_SETFL, options) < 0) { |
272 |
printf("\tThere was an error setting the socket to be non blocking.\n");
|
273 |
return -1; |
274 |
} |
275 |
|
276 |
//bind the socket to listen on the specified port
|
277 |
if (bind(listen_socket, (struct sockaddr *) &my_address, sizeof(my_address)) < 0) { |
278 |
printf("\tThere was an error binding the socket\n");
|
279 |
return -1; |
280 |
} |
281 |
|
282 |
return 0; |
283 |
} |
284 |
|
285 |
/**
|
286 |
* @brief returns the position monitor
|
287 |
*/
|
288 |
PositionMonitor* ColonetServer::getPositionMonitor() { |
289 |
return &position_monitor;
|
290 |
} |