Revision 23
moved more code around and got rid of the initialization file
ColonetServer.cpp | ||
---|---|---|
14 | 14 |
#include <errno.h> |
15 | 15 |
#include <arpa/inet.h> |
16 | 16 |
|
17 |
#include <fcntl.h> |
|
18 |
|
|
17 | 19 |
#include <colonet_wireless.h> |
18 | 20 |
|
19 | 21 |
#include "includes/ColonetServer.h" |
20 |
#include "includes/initialization.h" |
|
21 | 22 |
#include "includes/ConnectionPool.h" |
22 | 23 |
#include "includes/client.h" |
23 | 24 |
#include "includes/options.h" |
... | ... | |
28 | 29 |
|
29 | 30 |
ConnectionPool * connection_pool; |
30 | 31 |
ColonetWireless* wireless; |
32 |
int listenSocket = 0; |
|
31 | 33 |
|
34 |
|
|
32 | 35 |
ColonetServer::ColonetServer(): logger("logFile.txt") { |
33 | 36 |
} |
34 | 37 |
|
... | ... | |
51 | 54 |
return 0; |
52 | 55 |
} |
53 | 56 |
|
54 |
int ColonetServer::initialize_wireless() |
|
55 |
{ |
|
56 |
char* log_filename = NULL; |
|
57 | 57 |
|
58 |
if (optionsG.logging_enabled) { |
|
59 |
log_filename = optionsG.log_filename; |
|
60 |
} |
|
61 |
|
|
62 |
wireless = new ColonetWireless(optionsG.wireless_port, |
|
63 |
wirelessMessageHandler, log_filename, |
|
64 |
/*!optionsG.listener_mode*/false, true); |
|
65 |
//Note: last arg set to true ignores token ring; in general, this should |
|
66 |
//probably be false (changed for demo purposes) |
|
67 |
|
|
68 |
if (!wireless->run_listener_thread()) { |
|
69 |
return -1; |
|
70 |
} |
|
71 |
|
|
72 |
return 0; |
|
73 |
} |
|
74 |
|
|
75 | 58 |
int ColonetServer::log_error(char * error_message) { |
76 | 59 |
return logger.logMessage(LOG_TYPE_ERROR, error_message); |
77 | 60 |
} |
... | ... | |
80 | 63 |
return logger.logMessage(LOG_TYPE_MESSAGE, message); |
81 | 64 |
} |
82 | 65 |
|
83 |
int listenSocket = 0; |
|
84 |
//ConnectionPool connection_pool; |
|
85 |
|
|
86 | 66 |
//TODO: make it so the log file name is passed in on command line and default it to something if it isn't |
87 | 67 |
//Log logger("logFile.txt"); |
88 | 68 |
|
... | ... | |
181 | 161 |
return &connection_pool; |
182 | 162 |
} |
183 | 163 |
|
164 |
int ColonetServer::initialize_wireless() |
|
165 |
{ |
|
166 |
char* log_filename = NULL; |
|
184 | 167 |
|
168 |
if (optionsG.logging_enabled) { |
|
169 |
log_filename = optionsG.log_filename; |
|
170 |
} |
|
171 |
|
|
172 |
wireless = new ColonetWireless(optionsG.wireless_port, |
|
173 |
wirelessMessageHandler, log_filename, |
|
174 |
/*!optionsG.listener_mode*/false, true); |
|
175 |
//Note: last arg set to true ignores token ring; in general, this should |
|
176 |
//probably be false (changed for demo purposes) |
|
177 |
|
|
178 |
if (!wireless->run_listener_thread()) { |
|
179 |
return -1; |
|
180 |
} |
|
181 |
|
|
182 |
return 0; |
|
183 |
} |
|
184 |
|
|
185 |
int ColonetServer::initConnection(int port) |
|
186 |
{ |
|
187 |
printf("Initializing connection that will be used to listen for " |
|
188 |
"clients...\n"); |
|
189 |
int opts = 1; |
|
190 |
struct sockaddr_in my_addr; |
|
191 |
|
|
192 |
//get a socket fd |
|
193 |
if ((listenSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { |
|
194 |
printf("\tThere was an error creating a socket\n"); |
|
195 |
return -1; |
|
196 |
} |
|
197 |
|
|
198 |
//set up the address struct |
|
199 |
memset(&my_addr,'\0',sizeof(my_addr)); |
|
200 |
my_addr.sin_family = AF_INET; |
|
201 |
my_addr.sin_addr.s_addr = htonl(INADDR_ANY); |
|
202 |
my_addr.sin_port = htons(port); |
|
203 |
|
|
204 |
setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &opts, sizeof(opts)); |
|
205 |
|
|
206 |
//get the current socket options |
|
207 |
if ((opts = fcntl(listenSocket, F_GETFL)) < 0) { |
|
208 |
printf("\tThere was an error getting the socket options.\n"); |
|
209 |
return -1; |
|
210 |
} |
|
211 |
|
|
212 |
//set the socket to non blocking |
|
213 |
opts = (opts | O_NONBLOCK); |
|
214 |
if (fcntl(listenSocket, F_SETFL, opts) < 0) { |
|
215 |
printf("\tThere was an error setting the socket to be non blocking.\n"); |
|
216 |
return -1; |
|
217 |
} |
|
218 |
|
|
219 |
//bind the socket to listen on the specified port |
|
220 |
if (bind(listenSocket, (struct sockaddr *) &my_addr, sizeof(my_addr)) < 0) { |
|
221 |
printf("\tThere was an error binding the socket\n"); |
|
222 |
return -1; |
|
223 |
} |
|
224 |
|
|
225 |
return 0; |
|
226 |
} |
|
227 |
|
|
228 |
//this is old code that was commented out that I didn't want to delete yet |
|
229 |
/* |
|
230 |
int parseCommandLine(int argc, char * argv[], commandParams_t * params) |
|
231 |
{ |
|
232 |
printf("Parsing Command Line...\n"); |
|
233 |
if (!params) |
|
234 |
return -1; |
|
235 |
|
|
236 |
//if no command line parameters were specified, set to defaults |
|
237 |
if (argc == 1) { |
|
238 |
printf("No port was specified for listening for client connections." |
|
239 |
" Defaulting to %d\n", DEFAULTPORT); |
|
240 |
params->listenPort = DEFAULTPORT; |
|
241 |
} |
|
242 |
|
|
243 |
if (!argv) |
|
244 |
return -1; |
|
245 |
|
|
246 |
int i; |
|
247 |
for (i = 1; i < argc; i++) { |
|
248 |
char * temp = argv[i]; |
|
249 |
if (temp[0] != '-') |
|
250 |
{ |
|
251 |
printUsage(argv[0]); |
|
252 |
return -1; |
|
253 |
} |
|
254 |
|
|
255 |
switch(temp[1]) |
|
256 |
{ |
|
257 |
case 'h': printUsage(argv[0]); |
|
258 |
break; |
|
259 |
case 'p': |
|
260 |
{ |
|
261 |
if (i >= argc-1) |
|
262 |
{ |
|
263 |
printUsage(argv[0]); |
|
264 |
return -1; |
|
265 |
} |
|
266 |
i++; |
|
267 |
char * portString = argv[i]; |
|
268 |
char * endptr = NULL; |
|
269 |
int port = strtol(portString, &endptr, 10); |
|
270 |
if (*endptr != '\0') |
|
271 |
{ |
|
272 |
printf("Invalid port specified...%s, %d\n", endptr, port); |
|
273 |
return -1; |
|
274 |
} |
|
275 |
if (port < SMALLEST_POSS_LISTEN_PORT) |
|
276 |
{ |
|
277 |
printf("You cannot listen on a port less than %d.\n", |
|
278 |
SMALLEST_POSS_LISTEN_PORT); |
|
279 |
return -1; |
|
280 |
} |
|
281 |
params->listenPort = port; |
|
282 |
printf("Setting port to listen on to %d.\n", params->listenPort); |
|
283 |
} |
|
284 |
break; |
|
285 |
default: printUsage(argv[0]); |
|
286 |
return -1; |
|
287 |
break; |
|
288 |
} |
|
289 |
} |
|
290 |
|
|
291 |
return 0; |
|
292 |
} |
|
293 |
*/ |
|
294 |
|
Also available in: Unified diff