Wentao Shang | 7297a8d | 2012-11-19 11:57:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * @author: Wentao Shang |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | * Implement WebSocket proxy between ccnd and javascript stack. |
| 5 | */ |
| 6 | |
| 7 | var WebSocketServer = require('ws').Server; |
| 8 | var net = require('net'); |
| 9 | |
| 10 | var opt = require('node-getopt').create([ |
| 11 | ['c' , 'ccnd=ARG', 'host name or ip of ccnd router'], |
Wentao Shang | 7eb8c40 | 2012-11-19 13:30:44 -0800 | [diff] [blame] | 12 | ['p' , 'port=ARG', 'port number on which the proxy will listen'], |
Wentao Shang | 7297a8d | 2012-11-19 11:57:45 -0800 | [diff] [blame] | 13 | ['m' , 'maxclient=ARG', 'maximum number of concurrent client'], |
| 14 | ['L' , 'LOG=ARG', 'level of log message display'], |
| 15 | ['h' , 'help', 'display this help'] |
| 16 | ]) // create Getopt instance |
| 17 | .bindHelp() // bind option 'help' to default action |
| 18 | .parseSystem(); // parse command line |
| 19 | |
| 20 | //console.log(opt); |
| 21 | |
| 22 | var ccndhost = opt.options.ccnd || 'localhost'; |
| 23 | //console.log(ccndhost); |
| 24 | |
Wentao Shang | 7eb8c40 | 2012-11-19 13:30:44 -0800 | [diff] [blame] | 25 | var wsport = opt.options.port || 9696; |
| 26 | //console.log(wsport); |
| 27 | |
| 28 | var wss = new WebSocketServer({port:wsport, host:'0.0.0.0'}); // Set host to '0.0.0.0' so that we can accept connections from anywhere |
| 29 | // This host has nothing to do with ccndhost. |
Wentao Shang | 7297a8d | 2012-11-19 11:57:45 -0800 | [diff] [blame] | 30 | |
| 31 | var MaxNumOfClients = opt.options.maxclient || 40; |
| 32 | //console.log(MaxNumOfClients); |
| 33 | |
| 34 | var LOG = opt.options.LOG || 1; |
| 35 | //console.log(LOG); |
| 36 | |
| 37 | if (LOG > 0) console.log('WebSocketServer started...'); |
| 38 | |
| 39 | wss.on('connection', function(ws) { |
| 40 | if (LOG > 0) console.log('WebSocket client connection received.'); |
| 41 | if (LOG > 0) console.log('Number of clients now is ' + wss.clients.length); |
| 42 | |
| 43 | if (wss.clients.length > MaxNumOfClients) { |
| 44 | if (LOG > 0) console.log('Max num of clients exceeded. Close WS connection now.'); |
| 45 | ws.terminate(); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | var sock_ready = false; |
Wentao Shang | 86a3eae | 2013-03-15 17:36:21 -0700 | [diff] [blame] | 50 | var ws_ready = true; |
Wentao Shang | 7297a8d | 2012-11-19 11:57:45 -0800 | [diff] [blame] | 51 | var send_queue = []; |
| 52 | var sock = net.connect({port: 9695, host: ccndhost}); |
| 53 | |
| 54 | ws.on('message', function(message) { |
| 55 | if (typeof message == 'string') { |
| 56 | if (LOG > 1) console.log("Message from clinet: " + message); |
| 57 | } |
| 58 | else if (typeof message == 'object') { |
Wentao Shang | aa0b412 | 2013-04-16 13:37:45 -0700 | [diff] [blame] | 59 | var bytesView = new Buffer(message); |
Wentao Shang | 7297a8d | 2012-11-19 11:57:45 -0800 | [diff] [blame] | 60 | |
| 61 | if (LOG > 1) { |
| 62 | var logMsg = 'Byte array from client: '; |
| 63 | for (var i = 0; i < bytesView.length; i++) |
| 64 | logMsg += String.fromCharCode(bytesView[i]); |
| 65 | console.log(logMsg); |
| 66 | } |
| 67 | |
| 68 | if (sock_ready) { |
Wentao Shang | aa0b412 | 2013-04-16 13:37:45 -0700 | [diff] [blame] | 69 | sock.write(bytesView); |
Wentao Shang | 7297a8d | 2012-11-19 11:57:45 -0800 | [diff] [blame] | 70 | } else { |
| 71 | send_queue.push(message); |
| 72 | } |
| 73 | } |
| 74 | }); |
| 75 | |
| 76 | ws.on('close', function() { |
| 77 | if (LOG > 0) console.log('WebSocket connection closed.'); |
Wentao Shang | 86a3eae | 2013-03-15 17:36:21 -0700 | [diff] [blame] | 78 | ws_ready = false; |
Wentao Shang | 7297a8d | 2012-11-19 11:57:45 -0800 | [diff] [blame] | 79 | sock.end(); |
| 80 | }); |
| 81 | |
| 82 | sock.on('connect', function() { |
| 83 | while (send_queue.length > 0) { |
| 84 | var message = send_queue.shift(); |
| 85 | sock.write(message); |
| 86 | } |
| 87 | sock_ready = true; |
| 88 | if (LOG > 0) console.log('ccnd socket connection ready.'); |
| 89 | }); |
| 90 | |
| 91 | sock.on('data', function(data) { |
| 92 | if (typeof data == 'object') { |
Wentao Shang | aa0b412 | 2013-04-16 13:37:45 -0700 | [diff] [blame] | 93 | var bytesView = new Buffer(data); |
Wentao Shang | 7297a8d | 2012-11-19 11:57:45 -0800 | [diff] [blame] | 94 | |
| 95 | if (LOG > 1) { |
| 96 | console.log('Byte array from server: '); |
| 97 | var logMsg = ""; |
| 98 | for (var i = 0; i < bytesView.length; i++) |
| 99 | logMsg += String.fromCharCode(bytesView[i]); |
| 100 | console.log(logMsg); |
| 101 | } |
| 102 | |
Wentao Shang | 86a3eae | 2013-03-15 17:36:21 -0700 | [diff] [blame] | 103 | if (ws_ready == true) { |
Wentao Shang | aa0b412 | 2013-04-16 13:37:45 -0700 | [diff] [blame] | 104 | ws.send(bytesView, {binary: true, mask: false}); |
Wentao Shang | 86a3eae | 2013-03-15 17:36:21 -0700 | [diff] [blame] | 105 | } |
Wentao Shang | 7297a8d | 2012-11-19 11:57:45 -0800 | [diff] [blame] | 106 | } |
| 107 | }); |
| 108 | |
| 109 | sock.on('end', function() { |
| 110 | if (LOG > 0) console.log('TCP connection terminated by ccnd. Shut down WS connection to client.'); |
| 111 | ws.terminate(); |
| 112 | }); |
| 113 | |
| 114 | sock.on('error', function() { |
| 115 | if (LOG > 0) console.log('Error on TCP connection to ccnd. Shut down WS connection to client.'); |
| 116 | ws.terminate(); |
| 117 | }); |
| 118 | }); |