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