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