root / trunk / code / projects / colonet / ColonetServer / ColonetServer.cpp @ 348
History | View | Annotate | Download (7.14 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 | 11 | emarinel | #include <sys/select.h> |
17 | #include <sys/socket.h> |
||
18 | |||
19 | 20 | jknichel | #include <colonet_wireless.h> |
20 | |||
21 | #include "includes/ColonetServer.h" |
||
22 | 11 | emarinel | #include "includes/ConnectionPool.h" |
23 | 142 | jknichel | #include "includes/wirelessMessageHandler.h" |
24 | 11 | emarinel | #include "includes/options.h" |
25 | 57 | jknichel | #include "includes/Log.h" |
26 | 11 | emarinel | |
27 | #define LISTEN_BACKLOG 5 |
||
28 | #define LOG_BUFFER_LENGTH 128 |
||
29 | |||
30 | 347 | emarinel | #define u_int32_t unsigned |
31 | |||
32 | 20 | jknichel | ConnectionPool * connection_pool; |
33 | |||
34 | 29 | jknichel | /**
|
35 | * @brief Default constructor for ColonetServer
|
||
36 | */
|
||
37 | 20 | jknichel | ColonetServer::ColonetServer(): logger("logFile.txt") {
|
38 | 24 | jknichel | listen_socket = 0;
|
39 | 20 | jknichel | } |
40 | |||
41 | 29 | jknichel | /**
|
42 | * @brief Destructor for ColonetServer
|
||
43 | */
|
||
44 | 20 | jknichel | ColonetServer::~ColonetServer() { |
45 | } |
||
46 | |||
47 | 29 | jknichel | /**
|
48 | * @brief Initializes the various elements needed for the server to run
|
||
49 | *
|
||
50 | * @param argc The number of command line arguments passed to the program
|
||
51 | * @param argv The command line arguments passed to the program
|
||
52 | *
|
||
53 | * @return 0 on success, negative error code on failure
|
||
54 | */
|
||
55 | 20 | jknichel | int ColonetServer::initialize_server(int argc, char * argv[]) { |
56 | printf("Initializing Server...\n");
|
||
57 | |||
58 | parseCmdLine(argc, argv); |
||
59 | |||
60 | 27 | jknichel | if (initialize_connection(optionsG.listen_port) < 0) { |
61 | 20 | jknichel | return -1; |
62 | 27 | jknichel | } |
63 | 20 | jknichel | |
64 | 22 | jknichel | if (initialize_wireless() < 0) { |
65 | 20 | jknichel | fprintf(stderr, "%s: initWireless failed\n", __FUNCTION__);
|
66 | return -1; |
||
67 | } |
||
68 | |||
69 | return 0; |
||
70 | } |
||
71 | |||
72 | 29 | jknichel | /**
|
73 | * @brief Starts the server listening on the socket that was opened for listening
|
||
74 | *
|
||
75 | * @return 0 on success, negative error code on failure
|
||
76 | */
|
||
77 | 25 | jknichel | int ColonetServer::start_listening() {
|
78 | if (listen(listen_socket, LISTEN_BACKLOG) < 0) { |
||
79 | 132 | jknichel | logger.log_error("\t\nThere was an error telling the socket to "
|
80 | 140 | jknichel | "listen for connections from clients. Terminating Server...\n");
|
81 | 25 | jknichel | return -1; |
82 | } |
||
83 | return 0; |
||
84 | } |
||
85 | 22 | jknichel | |
86 | 20 | jknichel | |
87 | 29 | jknichel | /**
|
88 | * @brief Starts the server running (starts an infinite loop)
|
||
89 | */
|
||
90 | 24 | jknichel | int ColonetServer::run_server() {
|
91 | 143 | jknichel | connection_pool.add_new_socket_to_pool(listen_socket); |
92 | 20 | jknichel | |
93 | 143 | jknichel | int accept_socket = 0; |
94 | struct sockaddr_in client_addr;
|
||
95 | socklen_t client_addr_size = sizeof(client_addr);
|
||
96 | 20 | jknichel | |
97 | 143 | jknichel | logger.log_message("Server initialized. About to start listening for connections");
|
98 | 11 | emarinel | |
99 | 143 | jknichel | while(1) { |
100 | connection_pool.perform_select(listen_socket); |
||
101 | 11 | emarinel | |
102 | 143 | jknichel | //either no descriptors are ready or there was an error
|
103 | if (connection_pool.get_number_clients_ready() <= 0) { |
||
104 | continue;
|
||
105 | } |
||
106 | 11 | emarinel | |
107 | 143 | jknichel | if (connection_pool.is_socket_ready_to_read(listen_socket)) {
|
108 | printf("Something is trying to connect...\n");
|
||
109 | if ((accept_socket = accept(listen_socket, (struct sockaddr*) &client_addr, &client_addr_size)) < 0) { |
||
110 | if (errno == EMFILE) {
|
||
111 | printf("\tWhen attempting to accept a connection, "
|
||
112 | "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 | 11 | emarinel | } |
120 | |||
121 | 143 | jknichel | char log_buffer[LOG_BUFFER_LENGTH];
|
122 | 161 | emarinel | snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s attempting to connect.",
|
123 | 143 | jknichel | inet_ntoa(client_addr.sin_addr)); |
124 | logger.log_string(LOG_TYPE_CONNECT, log_buffer); |
||
125 | 11 | emarinel | |
126 | 143 | jknichel | 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 | 11 | emarinel | } |
130 | |||
131 | 161 | emarinel | snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s successfully added to connection pool.",
|
132 | 143 | jknichel | inet_ntoa(client_addr.sin_addr)); |
133 | logger.log_string(LOG_TYPE_CONNECT, log_buffer); |
||
134 | 11 | emarinel | } |
135 | 143 | jknichel | |
136 | if (connection_pool.check_clients() < 0) { |
||
137 | printf("\tThere was an error trying to update the clients.");
|
||
138 | continue;
|
||
139 | } |
||
140 | 11 | emarinel | } |
141 | |||
142 | 20 | jknichel | return 0; |
143 | 11 | emarinel | } |
144 | 20 | jknichel | |
145 | 140 | jknichel | int ColonetServer::process_received_wireless_message(unsigned char type, short source, int dest, |
146 | 297 | emarinel | unsigned char * data, int len) { |
147 | 140 | jknichel | if (type == COLONET_RESPONSE) {
|
148 | printf("response\n");
|
||
149 | |||
150 | 334 | gtress | /*
|
151 | //Eugene's code
|
||
152 | 140 | jknichel | char buffer[WRITE_BUFFER_SIZE];
|
153 | 161 | emarinel | |
154 | 140 | jknichel | int value = (int)(data[0]);
|
155 | snprintf(buffer, WRITE_BUFFER_SIZE, "%d\n", value);
|
||
156 | 161 | emarinel | |
157 | 140 | jknichel | int buflen = strlen(buffer);
|
158 | 334 | gtress | */
|
159 | |||
160 | //Greg's code
|
||
161 | char * buffer = (char *) &(data[5]); |
||
162 | int buflen = strlen(buffer);
|
||
163 | buffer[buflen] = '\n';
|
||
164 | buflen++; |
||
165 | |||
166 | 297 | emarinel | if (connection_pool.write_to_client(dest, buffer, buflen) == ERROR_INVALID_CLIENT_ID) {
|
167 | printf("The robot wanted to pass the data to a client not in the pool.\n");
|
||
168 | 140 | jknichel | return -1; |
169 | } |
||
170 | |||
171 | printf("Put data in write buffer for client.\n");
|
||
172 | } else {
|
||
173 | printf("not a response\n");
|
||
174 | return -1; |
||
175 | } |
||
176 | |||
177 | return 0; |
||
178 | 20 | jknichel | } |
179 | 22 | jknichel | |
180 | 140 | jknichel | |
181 | 29 | jknichel | /**
|
182 | * @brief Initializes the wireless
|
||
183 | *
|
||
184 | * @return 0 on success, negative error code on error
|
||
185 | */
|
||
186 | 27 | jknichel | int ColonetServer::initialize_wireless() {
|
187 | 23 | jknichel | char* log_filename = NULL; |
188 | 22 | jknichel | |
189 | 23 | jknichel | if (optionsG.logging_enabled) {
|
190 | 348 | emarinel | printf("Logging enabled. Log filename: %s\n", optionsG.log_filename);
|
191 | 23 | jknichel | log_filename = optionsG.log_filename; |
192 | 347 | emarinel | } else {
|
193 | printf("Logging disabled.\n");
|
||
194 | 23 | jknichel | } |
195 | |||
196 | 347 | emarinel | colonet_wl_init(optionsG.wireless_port, wirelessMessageHandler, log_filename); |
197 | |||
198 | 164 | emarinel | if (colonet_wl_run_listener_thread()) {
|
199 | 348 | emarinel | fprintf(stderr, "%s: colonet_wl_run_listener_thread failed.\n", __FUNCTION__);
|
200 | 34 | emarinel | return -1; |
201 | 161 | emarinel | } |
202 | 23 | jknichel | |
203 | 34 | emarinel | return 0; |
204 | 23 | jknichel | } |
205 | |||
206 | 29 | jknichel | /**
|
207 | * @brief Initialize a connection to listen on
|
||
208 | *
|
||
209 | * @port The port to try to open to listen on
|
||
210 | *
|
||
211 | * @return 0 on success, negative error code on error
|
||
212 | */
|
||
213 | 27 | jknichel | int ColonetServer::initialize_connection(int port) { |
214 | 348 | emarinel | printf("Initializing connection that will be used to listen for clients...\n");
|
215 | 27 | jknichel | int options = 1; |
216 | struct sockaddr_in my_address;
|
||
217 | 23 | jknichel | |
218 | //get a socket fd
|
||
219 | 24 | jknichel | if ((listen_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { |
220 | 23 | jknichel | printf("\tThere was an error creating a socket\n");
|
221 | return -1; |
||
222 | } |
||
223 | |||
224 | //set up the address struct
|
||
225 | 27 | jknichel | memset(&my_address,'\0',sizeof(my_address)); |
226 | my_address.sin_family = AF_INET; |
||
227 | my_address.sin_addr.s_addr = htonl(INADDR_ANY); |
||
228 | my_address.sin_port = htons(port); |
||
229 | 161 | emarinel | |
230 | 27 | jknichel | setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &options, sizeof(options));
|
231 | 23 | jknichel | |
232 | //get the current socket options
|
||
233 | 27 | jknichel | if ((options = fcntl(listen_socket, F_GETFL)) < 0) { |
234 | 23 | jknichel | printf("\tThere was an error getting the socket options.\n");
|
235 | return -1; |
||
236 | } |
||
237 | |||
238 | //set the socket to non blocking
|
||
239 | 27 | jknichel | options = (options | O_NONBLOCK); |
240 | if (fcntl(listen_socket, F_SETFL, options) < 0) { |
||
241 | 23 | jknichel | printf("\tThere was an error setting the socket to be non blocking.\n");
|
242 | return -1; |
||
243 | } |
||
244 | 161 | emarinel | |
245 | 23 | jknichel | //bind the socket to listen on the specified port
|
246 | 27 | jknichel | if (bind(listen_socket, (struct sockaddr *) &my_address, sizeof(my_address)) < 0) { |
247 | 23 | jknichel | printf("\tThere was an error binding the socket\n");
|
248 | return -1; |
||
249 | } |
||
250 | 161 | emarinel | |
251 | 23 | jknichel | return 0; |
252 | } |
||
253 | |||
254 | 29 | jknichel | /**
|
255 | * @brief The main function of the server
|
||
256 | *
|
||
257 | * @param argc The number of command line arguments passed to the program
|
||
258 | * @param argv The command line arguments passed to the program
|
||
259 | *
|
||
260 | * @return 0 on success, negative error code on error
|
||
261 | */
|
||
262 | 140 | jknichel | ColonetServer colonet_server; |
263 | 24 | jknichel | int main(int argc, char** argv) { |
264 | |||
265 | if (colonet_server.initialize_server(argc, argv) < 0) { |
||
266 | 132 | jknichel | printf("\t\nThere was an error initializing the server. "
|
267 | 140 | jknichel | "Terminating server...\n");
|
268 | 24 | jknichel | return -1; |
269 | } |
||
270 | |||
271 | 25 | jknichel | if (colonet_server.start_listening() < 0) { |
272 | 24 | jknichel | return -1; |
273 | } |
||
274 | 161 | emarinel | |
275 | 24 | jknichel | colonet_server.run_server(); |
276 | |||
277 | return 0; |
||
278 | } |