blob: 9b79ac2c5b9aee9fcc905d19a85d30f86b3e3456 [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'],
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 Thompson287a3182012-11-11 18:12:20 -080018
Wentao Shangc05dc532012-11-19 12:00:33 -080019var ccndhost = opt.options.ccnd || 'localhost';
Jeff Thompson287a3182012-11-11 18:12:20 -080020
Wentao Shangc05dc532012-11-19 12:00:33 -080021var wss = new WebSocketServer({port:9696});
22
23var MaxNumOfClients = opt.options.maxclient || 40;
24
25var LOG = opt.options.LOG || 1;
26
27if (LOG > 0) console.log('WebSocketServer started...');
Jeff Thompson287a3182012-11-11 18:12:20 -080028
29wss.on('connection', function(ws) {
Wentao Shangc05dc532012-11-19 12:00:33 -080030 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 Thompson287a3182012-11-11 18:12:20 -080032
33 if (wss.clients.length > MaxNumOfClients) {
Wentao Shangc05dc532012-11-19 12:00:33 -080034 if (LOG > 0) console.log('wss.onconnection: Max num of clients exceeded. Close WS connection now.');
Jeff Thompson287a3182012-11-11 18:12:20 -080035 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 Shangc05dc532012-11-19 12:00:33 -080065 if (LOG > 1) console.log('UDP heartbeat fired at ccnd.');
Jeff Thompson287a3182012-11-11 18:12:20 -080066 },
67 8000 // 8000 ms delay
68 );
69
70 ws.on('message', function(message) {
Wentao Shangc05dc532012-11-19 12:00:33 -080071 if (typeof message == 'string') {
72 if (LOG > 2) console.log("ws.onmessage: Message from clinet: " + message);
73 }
Jeff Thompson287a3182012-11-11 18:12:20 -080074 else if (typeof message == 'object') {
75 // From JS array to Buffer
76 var buffer = new Buffer(message);
77
Wentao Shangc05dc532012-11-19 12:00:33 -080078 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 Thompson287a3182012-11-11 18:12:20 -080084
85 udp.send(buffer, 0, buffer.length, 9695, ccndhost, null);
Jeff Thompson287a3182012-11-11 18:12:20 -080086 }
87 });
88
89 ws.on('close', function() {
Wentao Shangc05dc532012-11-19 12:00:33 -080090 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 -080091 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 Shangc05dc532012-11-19 12:00:33 -0800101 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 Thompson287a3182012-11-11 18:12:20 -0800109
110 ws.send(bytesView.buffer, {binary: true, mask: false});
Jeff Thompson287a3182012-11-11 18:12:20 -0800111 }
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 Shangc05dc532012-11-19 12:00:33 -0800122 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 -0800123 clearInterval(timerID);
124 ws.terminate();
125 });
126
127 udp.on('error', function() {
Wentao Shangc05dc532012-11-19 12:00:33 -0800128 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 -0800129 clearInterval(timerID);
130 ws.terminate();
131 });
132});