Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -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 | |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 7 | var WebSocketServer = require('ws').Server; |
| 8 | var dgram = require('dgram'); |
| 9 | |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 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 | c05dc53 | 2012-11-19 12:00:33 -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 |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 19 | |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 20 | var ccndhost = opt.options.ccnd || 'localhost'; |
Wentao Shang | 7eb8c40 | 2012-11-19 13:30:44 -0800 | [diff] [blame^] | 21 | var wsport = opt.options.port || 9696; |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 22 | |
Wentao Shang | 7eb8c40 | 2012-11-19 13:30:44 -0800 | [diff] [blame^] | 23 | 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 |
| 24 | // This host has nothing to do with ccndhost. |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 25 | |
| 26 | var MaxNumOfClients = opt.options.maxclient || 40; |
| 27 | |
| 28 | var LOG = opt.options.LOG || 1; |
| 29 | |
| 30 | if (LOG > 0) console.log('WebSocketServer started...'); |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 31 | |
| 32 | wss.on('connection', function(ws) { |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 33 | if (LOG > 0) console.log('wss.onconnection: WebSocket client connection received.'); |
| 34 | 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] | 35 | |
| 36 | if (wss.clients.length > MaxNumOfClients) { |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 37 | 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] | 38 | ws.terminate(); |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | var udp = dgram.createSocket("udp4"); |
| 43 | |
| 44 | /* |
| 45 | * According to the email discussion with Michael, when we use |
| 46 | * UDP to connect to ccnd, we MUST first send a 'heartbeat' |
| 47 | * UDP packet with 1-byte payload (content of this byte can |
| 48 | * be random). The purpose of this packet is to let ccnd |
| 49 | * mark the incoming FACE as 'friendly' (with CCN_FACE_GG |
| 50 | * flag set). We also need to periodically send this 'heartbeat' |
| 51 | * packet every few seconds to keep ccnd from recycling the UDP |
| 52 | * face. Michael recomended 8 seconds interval. |
| 53 | * --Wentao |
| 54 | */ |
| 55 | // Send 'heartbeat' packet now |
| 56 | var heartbeat = new Buffer(1); |
| 57 | heartbeat[0] = 0x21; |
| 58 | udp.send(heartbeat, 0, 1, 9695, ccndhost, null); |
| 59 | |
| 60 | // Schedule a timer to send 'heartbeat' periodically |
| 61 | var timerID = setInterval(function() { |
| 62 | if (udp == null || udp == undefined) |
| 63 | return; |
| 64 | |
| 65 | var hb = new Buffer(1); |
| 66 | hb[0] = 0x21; |
| 67 | udp.send(hb, 0, 1, 9695, ccndhost, null); |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 68 | if (LOG > 1) console.log('UDP heartbeat fired at ccnd.'); |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 69 | }, |
| 70 | 8000 // 8000 ms delay |
| 71 | ); |
| 72 | |
| 73 | ws.on('message', function(message) { |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 74 | if (typeof message == 'string') { |
| 75 | if (LOG > 2) console.log("ws.onmessage: Message from clinet: " + message); |
| 76 | } |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 77 | else if (typeof message == 'object') { |
| 78 | // From JS array to Buffer |
| 79 | var buffer = new Buffer(message); |
| 80 | |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 81 | if (LOG > 2) { |
| 82 | var logMsg = 'ws.onmessage: Byte array from client: '; |
| 83 | for (var i = 0; i < buffer.length; i++) |
| 84 | logMsg += String.fromCharCode(buffer[i]); |
| 85 | console.log(logMsg); |
| 86 | } |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 87 | |
| 88 | udp.send(buffer, 0, buffer.length, 9695, ccndhost, null); |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 89 | } |
| 90 | }); |
| 91 | |
| 92 | ws.on('close', function() { |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 93 | if (LOG > 0) console.log('ws.onclose: WebSocket connection closed. Close UDP connection to ccnd and stop "heartbeat" timer.'); |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 94 | clearInterval(timerID); |
| 95 | udp.close(); |
| 96 | udp = null; |
| 97 | }); |
| 98 | |
| 99 | udp.on('message', function(msg, rinfo) { |
| 100 | if (typeof msg == 'object') { |
| 101 | // From Buffer to ArrayBuffer |
| 102 | var bytesView = new Uint8Array(msg); |
| 103 | |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 104 | if (LOG > 2) { |
| 105 | console.log('udp.onmessage: Byte array from server: '); |
| 106 | console.log('udp.onmessage: bytesView.length ' + bytesView.length); |
| 107 | var logMsg = ""; |
| 108 | for (var i = 0; i < bytesView.length; i++) |
| 109 | logMsg += String.fromCharCode(bytesView[i]); |
| 110 | console.log(logMsg); |
| 111 | } |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 112 | |
| 113 | ws.send(bytesView.buffer, {binary: true, mask: false}); |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 114 | } |
| 115 | }); |
| 116 | |
| 117 | // Actually the socket close by ccnd will not cause the 'close' event to raise. |
| 118 | // So this event handle is only called when the client browser shuts down the WS |
| 119 | // connection, causing ws 'close' event to raise. In that event handle, we explicitly |
| 120 | // call udp.close(). So in this function we can do nothing. Anyway, here we clear the |
| 121 | // timer and terminate ws for a second time since that will not throw exception. 'ws' |
| 122 | // will check the 'readyState' before closing, therefore avoids 'close' event loop. |
| 123 | // --Wentao |
| 124 | udp.on('close', function() { |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 125 | if (LOG > 0) console.log('udp.onclose: UDP connection to ccnd terminated. Shut down WS connection to client and stop "heartbeat" timer.'); |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 126 | clearInterval(timerID); |
| 127 | ws.terminate(); |
| 128 | }); |
| 129 | |
| 130 | udp.on('error', function() { |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 131 | if (LOG > 0) console.log('udp.onerror: Error on UDP connection to ccnd. Shut down WS connection to client and stop "heartbeat" timer.'); |
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 132 | clearInterval(timerID); |
| 133 | ws.terminate(); |
| 134 | }); |
| 135 | }); |