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; |
| 50 | var send_queue = []; |
| 51 | var sock = net.connect({port: 9695, host: ccndhost}); |
| 52 | |
| 53 | ws.on('message', function(message) { |
| 54 | if (typeof message == 'string') { |
| 55 | if (LOG > 1) console.log("Message from clinet: " + message); |
| 56 | } |
| 57 | else if (typeof message == 'object') { |
| 58 | var bytesView = new Uint8Array(message); |
| 59 | |
| 60 | if (LOG > 1) { |
| 61 | var logMsg = 'Byte array from client: '; |
| 62 | for (var i = 0; i < bytesView.length; i++) |
| 63 | logMsg += String.fromCharCode(bytesView[i]); |
| 64 | console.log(logMsg); |
| 65 | } |
| 66 | |
| 67 | if (sock_ready) { |
| 68 | sock.write(bytesView.buffer); |
| 69 | } else { |
| 70 | send_queue.push(message); |
| 71 | } |
| 72 | } |
| 73 | }); |
| 74 | |
| 75 | ws.on('close', function() { |
| 76 | if (LOG > 0) console.log('WebSocket connection closed.'); |
| 77 | sock.end(); |
| 78 | }); |
| 79 | |
| 80 | sock.on('connect', function() { |
| 81 | while (send_queue.length > 0) { |
| 82 | var message = send_queue.shift(); |
| 83 | sock.write(message); |
| 84 | } |
| 85 | sock_ready = true; |
| 86 | if (LOG > 0) console.log('ccnd socket connection ready.'); |
| 87 | }); |
| 88 | |
| 89 | sock.on('data', function(data) { |
| 90 | if (typeof data == 'object') { |
| 91 | var bytesView = new Uint8Array(data); |
| 92 | |
| 93 | if (LOG > 1) { |
| 94 | console.log('Byte array from server: '); |
| 95 | var logMsg = ""; |
| 96 | for (var i = 0; i < bytesView.length; i++) |
| 97 | logMsg += String.fromCharCode(bytesView[i]); |
| 98 | console.log(logMsg); |
| 99 | } |
| 100 | |
| 101 | ws.send(bytesView.buffer, {binary: true, mask: false}); |
| 102 | } |
| 103 | }); |
| 104 | |
| 105 | sock.on('end', function() { |
| 106 | if (LOG > 0) console.log('TCP connection terminated by ccnd. Shut down WS connection to client.'); |
| 107 | ws.terminate(); |
| 108 | }); |
| 109 | |
| 110 | sock.on('error', function() { |
| 111 | if (LOG > 0) console.log('Error on TCP connection to ccnd. Shut down WS connection to client.'); |
| 112 | ws.terminate(); |
| 113 | }); |
| 114 | }); |