Some files is forgotten to be commited...
diff --git a/wsproxy/wsproxy-udp.js b/wsproxy/wsproxy-udp.js
index bec9421..9b79ac2 100644
--- a/wsproxy/wsproxy-udp.js
+++ b/wsproxy/wsproxy-udp.js
@@ -1,18 +1,37 @@
+/*
+ * @author: Wentao Shang
+ * See COPYING for copyright and distribution information.
+ * Implement WebSocket proxy between ccnd and javascript stack.
+ */
+
var WebSocketServer = require('ws').Server;
var dgram = require('dgram');
-var ccndhost = 'localhost';
+var opt = require('node-getopt').create([
+ ['c' , 'ccnd=ARG', 'host name or ip of ccnd router'],
+ ['m' , 'maxclient=ARG', 'maximum number of concurrent client'],
+ ['L' , 'LOG=ARG', 'level of log message display'],
+ ['h' , 'help', 'display this help']
+]) // create Getopt instance
+.bindHelp() // bind option 'help' to default action
+.parseSystem(); // parse command line
-var wss = new WebSocketServer({port:9696, host:ccndhost});
+var ccndhost = opt.options.ccnd || 'localhost';
-var MaxNumOfClients = 2;
+var wss = new WebSocketServer({port:9696});
+
+var MaxNumOfClients = opt.options.maxclient || 40;
+
+var LOG = opt.options.LOG || 1;
+
+if (LOG > 0) console.log('WebSocketServer started...');
wss.on('connection', function(ws) {
- console.log('wss.onconnection: WebSocket client connection received.');
- console.log('wss.onconnection: Number of clients now is ' + wss.clients.length);
+ if (LOG > 0) console.log('wss.onconnection: WebSocket client connection received.');
+ if (LOG > 0) console.log('wss.onconnection: Number of clients now is ' + wss.clients.length);
if (wss.clients.length > MaxNumOfClients) {
- console.log('wss.onconnection: Max num of clients exceeded. Close WS connection now.');
+ if (LOG > 0) console.log('wss.onconnection: Max num of clients exceeded. Close WS connection now.');
ws.terminate();
return;
}
@@ -43,30 +62,32 @@
var hb = new Buffer(1);
hb[0] = 0x21;
udp.send(hb, 0, 1, 9695, ccndhost, null);
- //console.log('UDP heartbeat fired at ccnd.');
+ if (LOG > 1) console.log('UDP heartbeat fired at ccnd.');
},
8000 // 8000 ms delay
);
ws.on('message', function(message) {
- if (typeof message == 'string')
- console.log("ws.onmessage: Message from clinet: " + message);
+ if (typeof message == 'string') {
+ if (LOG > 2) console.log("ws.onmessage: Message from clinet: " + message);
+ }
else if (typeof message == 'object') {
// From JS array to Buffer
var buffer = new Buffer(message);
- var logMsg = 'ws.onmessage: Byte array from client: ';
- for (var i = 0; i < buffer.length; i++)
- logMsg += String.fromCharCode(buffer[i]);
- console.log(logMsg);
+ if (LOG > 2) {
+ var logMsg = 'ws.onmessage: Byte array from client: ';
+ for (var i = 0; i < buffer.length; i++)
+ logMsg += String.fromCharCode(buffer[i]);
+ console.log(logMsg);
+ }
udp.send(buffer, 0, buffer.length, 9695, ccndhost, null);
- console.log('ws.onmessage: udp.send() returned.');
}
});
ws.on('close', function() {
- console.log('ws.onclose: WebSocket connection closed. Close UDP connection to ccnd and stop "heartbeat" timer.');
+ if (LOG > 0) console.log('ws.onclose: WebSocket connection closed. Close UDP connection to ccnd and stop "heartbeat" timer.');
clearInterval(timerID);
udp.close();
udp = null;
@@ -77,15 +98,16 @@
// From Buffer to ArrayBuffer
var bytesView = new Uint8Array(msg);
- console.log('udp.onmessage: Byte array from server: ');
- console.log('udp.onmessage: bytesView.length ' + bytesView.length);
- var logMsg = "";
- for (var i = 0; i < bytesView.length; i++)
- logMsg += String.fromCharCode(bytesView[i]);
- console.log(logMsg);
+ if (LOG > 2) {
+ console.log('udp.onmessage: Byte array from server: ');
+ console.log('udp.onmessage: bytesView.length ' + bytesView.length);
+ var logMsg = "";
+ for (var i = 0; i < bytesView.length; i++)
+ logMsg += String.fromCharCode(bytesView[i]);
+ console.log(logMsg);
+ }
ws.send(bytesView.buffer, {binary: true, mask: false});
- console.log('udp.onmessage: ws.send() returned.');
}
});
@@ -97,13 +119,13 @@
// will check the 'readyState' before closing, therefore avoids 'close' event loop.
// --Wentao
udp.on('close', function() {
- console.log('udp.onclose: UDP connection to ccnd terminated. Shut down WS connection to client and stop "heartbeat" timer.');
+ if (LOG > 0) console.log('udp.onclose: UDP connection to ccnd terminated. Shut down WS connection to client and stop "heartbeat" timer.');
clearInterval(timerID);
ws.terminate();
});
udp.on('error', function() {
- console.log('udp.onerror: Error on UDP connection to ccnd. Shut down WS connection to client and stop "heartbeat" timer.');
+ if (LOG > 0) console.log('udp.onerror: Error on UDP connection to ccnd. Shut down WS connection to client and stop "heartbeat" timer.');
clearInterval(timerID);
ws.terminate();
});