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'], |
| 12 | ['m' , 'maxclient=ARG', 'maximum number of concurrent client'], |
| 13 | ['L' , 'LOG=ARG', 'level of log message display'], |
| 14 | ['h' , 'help', 'display this help'] |
| 15 | ]) // create Getopt instance |
| 16 | .bindHelp() // bind option 'help' to default action |
| 17 | .parseSystem(); // parse command line |
| 18 | |
| 19 | //console.log(opt); |
| 20 | |
| 21 | var ccndhost = opt.options.ccnd || 'localhost'; |
| 22 | //console.log(ccndhost); |
| 23 | |
| 24 | var wss = new WebSocketServer({port:9696}); //TODO: Do we need to make WS server port number configurable? |
| 25 | |
| 26 | var MaxNumOfClients = opt.options.maxclient || 40; |
| 27 | //console.log(MaxNumOfClients); |
| 28 | |
| 29 | var LOG = opt.options.LOG || 1; |
| 30 | //console.log(LOG); |
| 31 | |
| 32 | if (LOG > 0) console.log('WebSocketServer started...'); |
| 33 | |
| 34 | wss.on('connection', function(ws) { |
| 35 | if (LOG > 0) console.log('WebSocket client connection received.'); |
| 36 | if (LOG > 0) console.log('Number of clients now is ' + wss.clients.length); |
| 37 | |
| 38 | if (wss.clients.length > MaxNumOfClients) { |
| 39 | if (LOG > 0) console.log('Max num of clients exceeded. Close WS connection now.'); |
| 40 | ws.terminate(); |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | var sock_ready = false; |
| 45 | var send_queue = []; |
| 46 | var sock = net.connect({port: 9695, host: ccndhost}); |
| 47 | |
| 48 | ws.on('message', function(message) { |
| 49 | if (typeof message == 'string') { |
| 50 | if (LOG > 1) console.log("Message from clinet: " + message); |
| 51 | } |
| 52 | else if (typeof message == 'object') { |
| 53 | var bytesView = new Uint8Array(message); |
| 54 | |
| 55 | if (LOG > 1) { |
| 56 | var logMsg = 'Byte array from client: '; |
| 57 | for (var i = 0; i < bytesView.length; i++) |
| 58 | logMsg += String.fromCharCode(bytesView[i]); |
| 59 | console.log(logMsg); |
| 60 | } |
| 61 | |
| 62 | if (sock_ready) { |
| 63 | sock.write(bytesView.buffer); |
| 64 | } else { |
| 65 | send_queue.push(message); |
| 66 | } |
| 67 | } |
| 68 | }); |
| 69 | |
| 70 | ws.on('close', function() { |
| 71 | if (LOG > 0) console.log('WebSocket connection closed.'); |
| 72 | sock.end(); |
| 73 | }); |
| 74 | |
| 75 | sock.on('connect', function() { |
| 76 | while (send_queue.length > 0) { |
| 77 | var message = send_queue.shift(); |
| 78 | sock.write(message); |
| 79 | } |
| 80 | sock_ready = true; |
| 81 | if (LOG > 0) console.log('ccnd socket connection ready.'); |
| 82 | }); |
| 83 | |
| 84 | sock.on('data', function(data) { |
| 85 | if (typeof data == 'object') { |
| 86 | var bytesView = new Uint8Array(data); |
| 87 | |
| 88 | if (LOG > 1) { |
| 89 | console.log('Byte array from server: '); |
| 90 | var logMsg = ""; |
| 91 | for (var i = 0; i < bytesView.length; i++) |
| 92 | logMsg += String.fromCharCode(bytesView[i]); |
| 93 | console.log(logMsg); |
| 94 | } |
| 95 | |
| 96 | ws.send(bytesView.buffer, {binary: true, mask: false}); |
| 97 | } |
| 98 | }); |
| 99 | |
| 100 | sock.on('end', function() { |
| 101 | if (LOG > 0) console.log('TCP connection terminated by ccnd. Shut down WS connection to client.'); |
| 102 | ws.terminate(); |
| 103 | }); |
| 104 | |
| 105 | sock.on('error', function() { |
| 106 | if (LOG > 0) console.log('Error on TCP connection to ccnd. Shut down WS connection to client.'); |
| 107 | ws.terminate(); |
| 108 | }); |
| 109 | }); |