Revision 23
moved more code around and got rid of the initialization file
trunk/code/projects/colonet/ColonetServer/initialization.c | ||
---|---|---|
1 |
/** |
|
2 |
* @file initialization.c |
|
3 |
* |
|
4 |
* @author Jason Knichel |
|
5 |
* @author Eugene Marinelli |
|
6 |
*/ |
|
7 |
|
|
8 |
#include <sys/socket.h> |
|
9 |
#include <sys/types.h> |
|
10 |
#include <netinet/in.h> |
|
11 |
#include <string.h> |
|
12 |
#include <fcntl.h> |
|
13 |
#include <unistd.h> |
|
14 |
#include <stdio.h> |
|
15 |
#include <stdlib.h> |
|
16 |
|
|
17 |
#include "includes/initialization.h" |
|
18 |
#include "includes/client.h" |
|
19 |
#include "includes/options.h" |
|
20 |
#include "includes/Logging.h" |
|
21 |
|
|
22 |
extern int listenSocket; |
|
23 |
|
|
24 |
/* |
|
25 |
int parseCommandLine(int argc, char * argv[], commandParams_t * params) |
|
26 |
{ |
|
27 |
printf("Parsing Command Line...\n"); |
|
28 |
if (!params) |
|
29 |
return -1; |
|
30 |
|
|
31 |
//if no command line parameters were specified, set to defaults |
|
32 |
if (argc == 1) { |
|
33 |
printf("No port was specified for listening for client connections." |
|
34 |
" Defaulting to %d\n", DEFAULTPORT); |
|
35 |
params->listenPort = DEFAULTPORT; |
|
36 |
} |
|
37 |
|
|
38 |
if (!argv) |
|
39 |
return -1; |
|
40 |
|
|
41 |
int i; |
|
42 |
for (i = 1; i < argc; i++) { |
|
43 |
char * temp = argv[i]; |
|
44 |
if (temp[0] != '-') |
|
45 |
{ |
|
46 |
printUsage(argv[0]); |
|
47 |
return -1; |
|
48 |
} |
|
49 |
|
|
50 |
switch(temp[1]) |
|
51 |
{ |
|
52 |
case 'h': printUsage(argv[0]); |
|
53 |
break; |
|
54 |
case 'p': |
|
55 |
{ |
|
56 |
if (i >= argc-1) |
|
57 |
{ |
|
58 |
printUsage(argv[0]); |
|
59 |
return -1; |
|
60 |
} |
|
61 |
i++; |
|
62 |
char * portString = argv[i]; |
|
63 |
char * endptr = NULL; |
|
64 |
int port = strtol(portString, &endptr, 10); |
|
65 |
if (*endptr != '\0') |
|
66 |
{ |
|
67 |
printf("Invalid port specified...%s, %d\n", endptr, port); |
|
68 |
return -1; |
|
69 |
} |
|
70 |
if (port < SMALLEST_POSS_LISTEN_PORT) |
|
71 |
{ |
|
72 |
printf("You cannot listen on a port less than %d.\n", |
|
73 |
SMALLEST_POSS_LISTEN_PORT); |
|
74 |
return -1; |
|
75 |
} |
|
76 |
params->listenPort = port; |
|
77 |
printf("Setting port to listen on to %d.\n", params->listenPort); |
|
78 |
} |
|
79 |
break; |
|
80 |
default: printUsage(argv[0]); |
|
81 |
return -1; |
|
82 |
break; |
|
83 |
} |
|
84 |
} |
|
85 |
|
|
86 |
return 0; |
|
87 |
} |
|
88 |
*/ |
|
89 |
|
|
90 |
int initConnection(int port) |
|
91 |
{ |
|
92 |
printf("Initializing connection that will be used to listen for " |
|
93 |
"clients...\n"); |
|
94 |
int opts = 1; |
|
95 |
struct sockaddr_in my_addr; |
|
96 |
|
|
97 |
//get a socket fd |
|
98 |
if ((listenSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { |
|
99 |
printf("\tThere was an error creating a socket\n"); |
|
100 |
return -1; |
|
101 |
} |
|
102 |
|
|
103 |
//set up the address struct |
|
104 |
memset(&my_addr,'\0',sizeof(my_addr)); |
|
105 |
my_addr.sin_family = AF_INET; |
|
106 |
my_addr.sin_addr.s_addr = htonl(INADDR_ANY); |
|
107 |
my_addr.sin_port = htons(port); |
|
108 |
|
|
109 |
setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &opts, sizeof(opts)); |
|
110 |
|
|
111 |
//get the current socket options |
|
112 |
if ((opts = fcntl(listenSocket, F_GETFL)) < 0) { |
|
113 |
printf("\tThere was an error getting the socket options.\n"); |
|
114 |
return -1; |
|
115 |
} |
|
116 |
|
|
117 |
//set the socket to non blocking |
|
118 |
opts = (opts | O_NONBLOCK); |
|
119 |
if (fcntl(listenSocket, F_SETFL, opts) < 0) { |
|
120 |
printf("\tThere was an error setting the socket to be non blocking.\n"); |
|
121 |
return -1; |
|
122 |
} |
|
123 |
|
|
124 |
//bind the socket to listen on the specified port |
|
125 |
if (bind(listenSocket, (struct sockaddr *) &my_addr, sizeof(my_addr)) < 0) { |
|
126 |
printf("\tThere was an error binding the socket\n"); |
|
127 |
return -1; |
|
128 |
} |
|
129 |
|
|
130 |
return 0; |
|
131 |
} |
trunk/code/projects/colonet/ColonetServer/includes/initialization.h | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
* @author Jason Knichel |
|
4 |
* |
|
5 |
*/ |
|
6 |
|
|
7 |
#ifndef INITIALIZATION_H |
|
8 |
#define INITIALIZATION_H |
|
9 |
|
|
10 |
int initializeServer(int argc, char * argv[]); |
|
11 |
int initConnection(int port); |
|
12 |
|
|
13 |
#endif |
trunk/code/projects/colonet/ColonetServer/includes/ColonetServer.h | ||
---|---|---|
27 | 27 |
Log logger; |
28 | 28 |
|
29 | 29 |
int initialize_wireless(); |
30 |
int initConnection(int port); |
|
30 | 31 |
|
31 |
|
|
32 | 32 |
}; |
33 | 33 |
|
34 | 34 |
#endif |
trunk/code/projects/colonet/ColonetServer/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 |
|
trunk/code/projects/colonet/ColonetServer/Makefile | ||
---|---|---|
5 | 5 |
|
6 | 6 |
COLONETCPPFILES = ColonetServer.cpp client.cpp ConnectionPool.cpp |
7 | 7 |
COLONETCPPOBJECTS = $(COLONETCPPFILES:.cpp=.o) |
8 |
COLONETFILES = initialization.c options.c
|
|
8 |
COLONETFILES = options.c |
|
9 | 9 |
COLONETOBJECTS = $(COLONETFILES:.c=.o) |
10 | 10 |
LOGGINGFILES = Logging.cpp |
11 | 11 |
LOGGINGOBJECTS = $(LOGGINGFILES:.cpp=.o) |
Also available in: Unified diff