forked from Spacebrew/spacebrew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_server.js
executable file
·198 lines (182 loc) · 5.73 KB
/
node_server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env node
/**
* Spacebrew Server
* ----------------
*
* This script runs the Spacebrew server, and optionally the spacebrew live
* persistent router. In order to find out about the comand line flags just
* run the script with '-h' or '--help' flag.
*
* @author: Quin Kennedy, Julio Terra, and other contributors
* @filename: node_server.js
* @date: May 31, 2013
* @updated with version: 0.3.0
*
*/
var spacebrew = require('./spacebrew')
, persister = require('./spacebrew_live_persist')
, logger = require('./logger')
;
var defaultPort = 9000
, forceClose = false
, doPing = true
, persist = true
, help = false
;
/**
* Prints startup message to console/terminal
*/
var printStartupMsg = function() {
console.log("");
console.log("Running Spacebrew, start with argument '--help' to see available configuration arguments.");
console.log("More info at http://www.spacebrew.cc");
console.log("");
}
/**
* Processes the command line arguments when app is launched
*/
var processArguments = function(){
var argv = process.argv;
for(var i = 0; i < argv.length; i++){
switch(argv[i]){
case "-l":
case "--log":
setLogLevel("info");
break;
case "--loglevel":
setLogLevel( argv[(i += 1)] );
break;
case "-h":
case "--help":
printHelp();
help = true;
break;
case "-p":
case "--port":
setDefaultPort(argv[++i]);
break;
case "-c":
case "--close":
forceClose = true;
break;
case "-t":
case "--timeout":
forceClose = true;
setCloseTimeout(argv[++i]);
break;
case "--ping":
doPing = true;
break;
case "--noping":
doPing = false;
break;
case "--pinginterval":
doPing = true;
setPingIntervalTime(argv[++i]);
break;
case "--persist":
persist = true;
break;
case "--nopersist":
persist = false;
break;
}
}
};
/**
* Set the port to open for ws connections. defaults to 9000.
*
* @type {Number}
*/
var setDefaultPort = function(newPort){
var tempPort = parseInt(newPort, 10);
//check that tempPort != NaN
//and that the port is in the valid port range
if (tempPort == tempPort &&
tempPort >= 1 && tempPort <= 65535){
defaultPort = tempPort;
}
logger.log("info", "[setDefaultPort] port set to " + defaultPort);
};
var closeTimeout = 10000;//default to 10 seconds
var setCloseTimeout = function(newTimeout){
var tempTimeout = parseInt(newTimeout);
if (tempTimeout == tempTimeout && tempTimeout > 0){
closeTimeout = tempTimeout;
}
};
var pingIntervalTime = 1000;//every second
var setPingIntervalTime = function( newInterval ){
var tempInterval = parseInt(newInterval);
if (tempInterval == tempInterval && tempInterval > 0){
pingIntervalTime = tempInterval;
}
};
/**
* method that is used to set the log level when user sets log level via command line
*
* @param {String} newLevel New log level - "error", "warn", "debug", or "info"
*/
var setLogLevel = function( newLevel ) {
logger.debugLevel = newLevel;
logger.log("info", "[setLogLevel] log level set to " + logger.debugLevel);
}
/**
* Prints the node server help message to screen
*/
var printHelp = function(){
console.log("");
console.log("command line parameters:");
console.log("\t--port (-p): set the port of the spacebrew server (default 9000)");
console.log("\t--help (-h): print this help text");
console.log("\t--close (-c): force close clients that don't respond to pings");
console.log("\t--timeout (-t): minimum number of ms to wait for response pong before force closing (implies --close, default 10000 [10 seconds])");
console.log("\t--ping: enable pinging of clients to track who is potentially disconnected (default)");
console.log("\t--noping: opposite of --ping");
console.log("\t--persist: enables the live route persister, which saves route configurations");
console.log("\t--nopersist: opposite of --persist");
console.log("\t--pinginterval: the number of ms between pings (implies --ping, default 1000 [1 second])");
console.log("\t--log (-l): sets logging to debug level");
console.log("\t--loglevel: set logging to info, debug, warn, error or critical - not fully supported yet");
console.log("examples:");
console.log("\tnode node_server.js -p 9011 -t 1000 --pinginterval 1000");
console.log("\tnode node_server.js --noping");
console.log("");
};
/**
* Method that handles key app functions including printing app startup message, processing
* command line arguments, and starting up the spacebrew and persistent servers
*/
var main = function() {
var server_configs = {}
, persist_configs = {}
;
printStartupMsg();
processArguments();
// if app command included help flag then don't run app
if (help) {
process.exit();
}
// if app command did not include help flag then start-up persist server
else {
server_configs = {
"port": defaultPort,
"forceClose": forceClose,
"ping": doPing,
"pingInterval": pingIntervalTime,
"closeTimeout": closeTimeout,
"logLevel": logger.debugLevel
}
persist_configs = {
"host": "localhost",
"port": defaultPort,
"logLevel": logger.debugLevel
}
spacebrew.createServer( server_configs );
if (persist) persister.persistRoutes( persist_configs);
}
}
/**
* Run the app
*/
main();