Revision 24
moved more code around
trunk/code/projects/colonet/ColonetServer/includes/ColonetServer.h | ||
---|---|---|
18 | 18 |
int log_error(char * error_message); |
19 | 19 |
int log_message(char * message); |
20 | 20 |
|
21 |
int run_server(int listenSocket);
|
|
21 |
int run_server(); |
|
22 | 22 |
|
23 | 23 |
ConnectionPool * get_connection_pool_pointer(); |
24 | 24 |
|
25 |
int get_listen_socket(); |
|
26 |
|
|
25 | 27 |
private: |
26 | 28 |
ConnectionPool connection_pool; |
27 | 29 |
Log logger; |
30 |
int listen_socket; |
|
28 | 31 |
|
29 | 32 |
int initialize_wireless(); |
30 | 33 |
int initConnection(int port); |
trunk/code/projects/colonet/ColonetServer/ColonetServer.cpp | ||
---|---|---|
29 | 29 |
|
30 | 30 |
ConnectionPool * connection_pool; |
31 | 31 |
ColonetWireless* wireless; |
32 |
int listenSocket = 0; |
|
33 | 32 |
|
34 |
|
|
35 | 33 |
ColonetServer::ColonetServer(): logger("logFile.txt") { |
34 |
listen_socket = 0; |
|
36 | 35 |
} |
37 | 36 |
|
38 | 37 |
ColonetServer::~ColonetServer() { |
... | ... | |
63 | 62 |
return logger.logMessage(LOG_TYPE_MESSAGE, message); |
64 | 63 |
} |
65 | 64 |
|
66 |
//TODO: make it so the log file name is passed in on command line and default it to something if it isn't |
|
67 |
//Log logger("logFile.txt"); |
|
68 |
|
|
69 |
int main(int argc, char** argv) { |
|
70 |
ColonetServer colonet_server; |
|
71 |
|
|
72 |
connection_pool = colonet_server.get_connection_pool_pointer(); |
|
73 |
|
|
74 |
if (colonet_server.initialize_server(argc, argv) < 0) { |
|
75 |
colonet_server.log_error("\t\nThere was an error initializing the server. " |
|
76 |
"Terminating server...\n"); |
|
77 |
return -1; |
|
78 |
} |
|
79 |
|
|
80 |
if (listen(listenSocket, LISTEN_BACKLOG) < 0) { |
|
81 |
colonet_server.log_error("\t\nThere was an error telling the socket to " |
|
82 |
"listen for connections from clients. Terminating Server...\n"); |
|
83 |
return -1; |
|
84 |
} |
|
85 |
|
|
86 |
colonet_server.run_server(listenSocket); |
|
87 |
|
|
88 |
return 0; |
|
89 |
} |
|
90 |
|
|
91 |
int ColonetServer::run_server(int listen_socket) { |
|
65 |
int ColonetServer::run_server() { |
|
92 | 66 |
connection_pool.set_listen_socket_in_ready_set(listen_socket); |
93 | 67 |
|
94 | 68 |
//TODO: why is all of this in that if statement and not just conn_pool.maxfd = listen_socket ? |
... | ... | |
161 | 135 |
return &connection_pool; |
162 | 136 |
} |
163 | 137 |
|
138 |
int ColonetServer::get_listen_socket() { |
|
139 |
return listen_socket; |
|
140 |
} |
|
141 |
|
|
164 | 142 |
int ColonetServer::initialize_wireless() |
165 | 143 |
{ |
166 | 144 |
char* log_filename = NULL; |
... | ... | |
190 | 168 |
struct sockaddr_in my_addr; |
191 | 169 |
|
192 | 170 |
//get a socket fd |
193 |
if ((listenSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
|
171 |
if ((listen_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
|
194 | 172 |
printf("\tThere was an error creating a socket\n"); |
195 | 173 |
return -1; |
196 | 174 |
} |
... | ... | |
201 | 179 |
my_addr.sin_addr.s_addr = htonl(INADDR_ANY); |
202 | 180 |
my_addr.sin_port = htons(port); |
203 | 181 |
|
204 |
setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &opts, sizeof(opts));
|
|
182 |
setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &opts, sizeof(opts));
|
|
205 | 183 |
|
206 | 184 |
//get the current socket options |
207 |
if ((opts = fcntl(listenSocket, F_GETFL)) < 0) {
|
|
185 |
if ((opts = fcntl(listen_socket, F_GETFL)) < 0) {
|
|
208 | 186 |
printf("\tThere was an error getting the socket options.\n"); |
209 | 187 |
return -1; |
210 | 188 |
} |
211 | 189 |
|
212 | 190 |
//set the socket to non blocking |
213 | 191 |
opts = (opts | O_NONBLOCK); |
214 |
if (fcntl(listenSocket, F_SETFL, opts) < 0) {
|
|
192 |
if (fcntl(listen_socket, F_SETFL, opts) < 0) {
|
|
215 | 193 |
printf("\tThere was an error setting the socket to be non blocking.\n"); |
216 | 194 |
return -1; |
217 | 195 |
} |
218 | 196 |
|
219 | 197 |
//bind the socket to listen on the specified port |
220 |
if (bind(listenSocket, (struct sockaddr *) &my_addr, sizeof(my_addr)) < 0) {
|
|
198 |
if (bind(listen_socket, (struct sockaddr *) &my_addr, sizeof(my_addr)) < 0) {
|
|
221 | 199 |
printf("\tThere was an error binding the socket\n"); |
222 | 200 |
return -1; |
223 | 201 |
} |
... | ... | |
225 | 203 |
return 0; |
226 | 204 |
} |
227 | 205 |
|
206 |
int main(int argc, char** argv) { |
|
207 |
ColonetServer colonet_server; |
|
208 |
|
|
209 |
connection_pool = colonet_server.get_connection_pool_pointer(); |
|
210 |
|
|
211 |
if (colonet_server.initialize_server(argc, argv) < 0) { |
|
212 |
colonet_server.log_error("\t\nThere was an error initializing the server. " |
|
213 |
"Terminating server...\n"); |
|
214 |
return -1; |
|
215 |
} |
|
216 |
|
|
217 |
if (listen(colonet_server.get_listen_socket(), LISTEN_BACKLOG) < 0) { |
|
218 |
colonet_server.log_error("\t\nThere was an error telling the socket to " |
|
219 |
"listen for connections from clients. Terminating Server...\n"); |
|
220 |
return -1; |
|
221 |
} |
|
222 |
|
|
223 |
colonet_server.run_server(); |
|
224 |
|
|
225 |
return 0; |
|
226 |
} |
|
227 |
|
|
228 | 228 |
//this is old code that was commented out that I didn't want to delete yet |
229 | 229 |
/* |
230 | 230 |
int parseCommandLine(int argc, char * argv[], commandParams_t * params) |
Also available in: Unified diff