blob: b81e2bd93cc88a048769dc0bb4f297e9ad77951e [file] [log] [blame]
Wentao Shangc05dc532012-11-19 12:00:33 -08001/*
2 * @author: Wentao Shang
3 * See COPYING for copyright and distribution information.
4 * Implement WebSocket proxy between ccnd and javascript stack.
5 */
6
Jeff Thompson287a3182012-11-11 18:12:20 -08007var WebSocketServer = require('ws').Server;
8var dgram = require('dgram');
9
Wentao Shangc05dc532012-11-19 12:00:33 -080010var opt = require('node-getopt').create([
11 ['c' , 'ccnd=ARG', 'host name or ip of ccnd router'],
Wentao Shang7eb8c402012-11-19 13:30:44 -080012 ['p' , 'port=ARG', 'port number on which the proxy will listen'],
Wentao Shangc05dc532012-11-19 12:00:33 -080013 ['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 Thompson287a3182012-11-11 18:12:20 -080019
Wentao Shangc05dc532012-11-19 12:00:33 -080020var ccndhost = opt.options.ccnd || 'localhost';
Wentao Shang7eb8c402012-11-19 13:30:44 -080021var wsport = opt.options.port || 9696;
Jeff Thompson287a3182012-11-11 18:12:20 -080022
Wentao Shang7eb8c402012-11-19 13:30:44 -080023var 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 Shangc05dc532012-11-19 12:00:33 -080025
26var MaxNumOfClients = opt.options.maxclient || 40;
27
28var LOG = opt.options.LOG || 1;
29
30if (LOG > 0) console.log('WebSocketServer started...');
Jeff Thompson287a3182012-11-11 18:12:20 -080031
32wss.on('connection', function(ws) {
Wentao Shangc05dc532012-11-19 12:00:33 -080033 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 Thompson287a3182012-11-11 18:12:20 -080035
36 if (wss.clients.length > MaxNumOfClients) {
Wentao Shangc05dc532012-11-19 12:00:33 -080037 if (LOG > 0) console.log('wss.onconnection: Max num of clients exceeded. Close WS connection now.');
Jeff Thompson287a3182012-11-11 18:12:20 -080038 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 Shangc05dc532012-11-19 12:00:33 -080068 if (LOG > 1) console.log('UDP heartbeat fired at ccnd.');
Jeff Thompson287a3182012-11-11 18:12:20 -080069 },
70 8000 // 8000 ms delay
71 );
72
73 ws.on('message', function(message) {
Wentao Shangc05dc532012-11-19 12:00:33 -080074 if (typeof message == 'string') {
75 if (LOG > 2) console.log("ws.onmessage: Message from clinet: " + message);
76 }
Jeff Thompson287a3182012-11-11 18:12:20 -080077 else if (typeof message == 'object') {
78 // From JS array to Buffer
79 var buffer = new Buffer(message);
80
Wentao Shangc05dc532012-11-19 12:00:33 -080081 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 Thompson287a3182012-11-11 18:12:20 -080087
88 udp.send(buffer, 0, buffer.length, 9695, ccndhost, null);
Jeff Thompson287a3182012-11-11 18:12:20 -080089 }
90 });
91
92 ws.on('close', function() {
Wentao Shangc05dc532012-11-19 12:00:33 -080093 if (LOG > 0) console.log('ws.onclose: WebSocket connection closed. Close UDP connection to ccnd and stop "heartbeat" timer.');
Jeff Thompson287a3182012-11-11 18:12:20 -080094 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 Shangc05dc532012-11-19 12:00:33 -0800104 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 Thompson287a3182012-11-11 18:12:20 -0800112
113 ws.send(bytesView.buffer, {binary: true, mask: false});
Jeff Thompson287a3182012-11-11 18:12:20 -0800114 }
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 Shangc05dc532012-11-19 12:00:33 -0800125 if (LOG > 0) console.log('udp.onclose: UDP connection to ccnd terminated. Shut down WS connection to client and stop "heartbeat" timer.');
Jeff Thompson287a3182012-11-11 18:12:20 -0800126 clearInterval(timerID);
127 ws.terminate();
128 });
129
130 udp.on('error', function() {
Wentao Shangc05dc532012-11-19 12:00:33 -0800131 if (LOG > 0) console.log('udp.onerror: Error on UDP connection to ccnd. Shut down WS connection to client and stop "heartbeat" timer.');
Jeff Thompson287a3182012-11-11 18:12:20 -0800132 clearInterval(timerID);
133 ws.terminate();
134 });
135});