Alexander Afanasyev | 181a8b9 | 2013-02-28 13:28:53 -0800 | [diff] [blame] | 1 | #!/usr/bin/env node |
| 2 | |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 3 | /* |
| 4 | * @author: Wentao Shang |
| 5 | * See COPYING for copyright and distribution information. |
Alexander Afanasyev | 03d3f74 | 2013-08-14 17:47:28 -0700 | [diff] [blame] | 6 | * Implement WebSocket proxy between ndnd and javascript stack. |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 7 | */ |
| 8 | |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 9 | var WebSocketServer = require('ws').Server; |
| 10 | var dgram = require('dgram'); |
| 11 | |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 12 | var opt = require('node-getopt').create([ |
Alexander Afanasyev | 03d3f74 | 2013-08-14 17:47:28 -0700 | [diff] [blame] | 13 | ['c' , 'ndnd=ARG', 'host name or ip of ndnd router'], |
Wentao Shang | 7eb8c40 | 2012-11-19 13:30:44 -0800 | [diff] [blame] | 14 | ['p' , 'port=ARG', 'port number on which the proxy will listen'], |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 15 | ['m' , 'maxclient=ARG', 'maximum number of concurrent client'], |
| 16 | ['L' , 'LOG=ARG', 'level of log message display'], |
| 17 | ['h' , 'help', 'display this help'] |
| 18 | ]) // create Getopt instance |
| 19 | .bindHelp() // bind option 'help' to default action |
| 20 | .parseSystem(); // parse command line |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 21 | |
Alexander Afanasyev | 03d3f74 | 2013-08-14 17:47:28 -0700 | [diff] [blame] | 22 | var ndndhost = opt.options.ndnd || 'localhost'; |
Wentao Shang | 7eb8c40 | 2012-11-19 13:30:44 -0800 | [diff] [blame] | 23 | var wsport = opt.options.port || 9696; |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 24 | |
Wentao Shang | 7eb8c40 | 2012-11-19 13:30:44 -0800 | [diff] [blame] | 25 | 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 |
Alexander Afanasyev | 03d3f74 | 2013-08-14 17:47:28 -0700 | [diff] [blame] | 26 | // This host has nothing to do with ndndhost. |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 27 | |
| 28 | var MaxNumOfClients = opt.options.maxclient || 40; |
| 29 | |
| 30 | var LOG = opt.options.LOG || 1; |
| 31 | |
| 32 | if (LOG > 0) console.log('WebSocketServer started...'); |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 33 | |
| 34 | wss.on('connection', function(ws) { |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 35 | if (LOG > 0) console.log('wss.onconnection: WebSocket client connection received.'); |
| 36 | if (LOG > 0) console.log('wss.onconnection: Number of clients now is ' + wss.clients.length); |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 37 | |
| 38 | if (wss.clients.length > MaxNumOfClients) { |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 39 | if (LOG > 0) console.log('wss.onconnection: Max num of clients exceeded. Close WS connection now.'); |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 40 | ws.terminate(); |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | var udp = dgram.createSocket("udp4"); |
| 45 | |
| 46 | /* |
| 47 | * According to the email discussion with Michael, when we use |
Alexander Afanasyev | 03d3f74 | 2013-08-14 17:47:28 -0700 | [diff] [blame] | 48 | * UDP to connect to ndnd, we MUST first send a 'heartbeat' |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 49 | * UDP packet with 1-byte payload (content of this byte can |
Alexander Afanasyev | 03d3f74 | 2013-08-14 17:47:28 -0700 | [diff] [blame] | 50 | * be random). The purpose of this packet is to let ndnd |
| 51 | * mark the incoming FACE as 'friendly' (with NDN_FACE_GG |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 52 | * flag set). We also need to periodically send this 'heartbeat' |
Alexander Afanasyev | 03d3f74 | 2013-08-14 17:47:28 -0700 | [diff] [blame] | 53 | * packet every few seconds to keep ndnd from recycling the UDP |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 54 | * face. Michael recomended 8 seconds interval. |
| 55 | * --Wentao |
| 56 | */ |
| 57 | // Send 'heartbeat' packet now |
| 58 | var heartbeat = new Buffer(1); |
| 59 | heartbeat[0] = 0x21; |
Alexander Afanasyev | 03d3f74 | 2013-08-14 17:47:28 -0700 | [diff] [blame] | 60 | udp.send(heartbeat, 0, 1, 9695, ndndhost, null); |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 61 | |
| 62 | // Schedule a timer to send 'heartbeat' periodically |
| 63 | var timerID = setInterval(function() { |
| 64 | if (udp == null || udp == undefined) |
| 65 | return; |
| 66 | |
| 67 | var hb = new Buffer(1); |
| 68 | hb[0] = 0x21; |
Alexander Afanasyev | 03d3f74 | 2013-08-14 17:47:28 -0700 | [diff] [blame] | 69 | udp.send(hb, 0, 1, 9695, ndndhost, null); |
| 70 | if (LOG > 1) console.log('UDP heartbeat fired at ndnd.'); |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 71 | }, |
| 72 | 8000 // 8000 ms delay |
| 73 | ); |
| 74 | |
| 75 | ws.on('message', function(message) { |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 76 | if (typeof message == 'string') { |
| 77 | if (LOG > 2) console.log("ws.onmessage: Message from clinet: " + message); |
| 78 | } |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 79 | else if (typeof message == 'object') { |
| 80 | // From JS array to Buffer |
| 81 | var buffer = new Buffer(message); |
| 82 | |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 83 | if (LOG > 2) { |
| 84 | var logMsg = 'ws.onmessage: Byte array from client: '; |
| 85 | for (var i = 0; i < buffer.length; i++) |
| 86 | logMsg += String.fromCharCode(buffer[i]); |
| 87 | console.log(logMsg); |
| 88 | } |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 89 | |
Alexander Afanasyev | 03d3f74 | 2013-08-14 17:47:28 -0700 | [diff] [blame] | 90 | udp.send(buffer, 0, buffer.length, 9695, ndndhost, null); |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 91 | } |
| 92 | }); |
| 93 | |
| 94 | ws.on('close', function() { |
Alexander Afanasyev | 03d3f74 | 2013-08-14 17:47:28 -0700 | [diff] [blame] | 95 | if (LOG > 0) console.log('ws.onclose: WebSocket connection closed. Close UDP connection to ndnd and stop "heartbeat" timer.'); |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 96 | clearInterval(timerID); |
| 97 | udp.close(); |
| 98 | udp = null; |
| 99 | }); |
| 100 | |
| 101 | udp.on('message', function(msg, rinfo) { |
Wentao Shang | aa0b412 | 2013-04-16 13:37:45 -0700 | [diff] [blame] | 102 | if (msg instanceof Buffer) { |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 103 | if (LOG > 2) { |
| 104 | console.log('udp.onmessage: Byte array from server: '); |
Wentao Shang | aa0b412 | 2013-04-16 13:37:45 -0700 | [diff] [blame] | 105 | console.log('udp.onmessage: msg.length ' + msg.length); |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 106 | var logMsg = ""; |
Wentao Shang | aa0b412 | 2013-04-16 13:37:45 -0700 | [diff] [blame] | 107 | for (var i = 0; i < msg.length; i++) |
| 108 | logMsg += String.fromCharCode(msg[i]); |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 109 | console.log(logMsg); |
| 110 | } |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 111 | |
Wentao Shang | aa0b412 | 2013-04-16 13:37:45 -0700 | [diff] [blame] | 112 | ws.send(msg, {binary: true, mask: false}); |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 113 | } |
| 114 | }); |
| 115 | |
Alexander Afanasyev | 03d3f74 | 2013-08-14 17:47:28 -0700 | [diff] [blame] | 116 | // Actually the socket close by ndnd will not cause the 'close' event to raise. |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 117 | // So this event handle is only called when the client browser shuts down the WS |
| 118 | // connection, causing ws 'close' event to raise. In that event handle, we explicitly |
| 119 | // call udp.close(). So in this function we can do nothing. Anyway, here we clear the |
| 120 | // timer and terminate ws for a second time since that will not throw exception. 'ws' |
| 121 | // will check the 'readyState' before closing, therefore avoids 'close' event loop. |
| 122 | // --Wentao |
| 123 | udp.on('close', function() { |
Alexander Afanasyev | 03d3f74 | 2013-08-14 17:47:28 -0700 | [diff] [blame] | 124 | if (LOG > 0) console.log('udp.onclose: UDP connection to ndnd terminated. Shut down WS connection to client and stop "heartbeat" timer.'); |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 125 | clearInterval(timerID); |
| 126 | ws.terminate(); |
| 127 | }); |
| 128 | |
| 129 | udp.on('error', function() { |
Alexander Afanasyev | 03d3f74 | 2013-08-14 17:47:28 -0700 | [diff] [blame] | 130 | if (LOG > 0) console.log('udp.onerror: Error on UDP connection to ndnd. Shut down WS connection to client and stop "heartbeat" timer.'); |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 131 | clearInterval(timerID); |
| 132 | ws.terminate(); |
| 133 | }); |
| 134 | }); |