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