blob: 8ff84a28402d5e1c4c5f7f6f3282c5a9a29350e5 [file] [log] [blame]
Wentao Shang7297a8d2012-11-19 11:57:45 -08001/*
2 * @author: Wentao Shang
3 * See COPYING for copyright and distribution information.
4 * Implement WebSocket proxy between ccnd and javascript stack.
5 */
6
7var WebSocketServer = require('ws').Server;
8var net = require('net');
9
10var 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 Shang7297a8d2012-11-19 11:57:45 -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
19
20//console.log(opt);
21
22var ccndhost = opt.options.ccnd || 'localhost';
23//console.log(ccndhost);
24
Wentao Shang7eb8c402012-11-19 13:30:44 -080025var wsport = opt.options.port || 9696;
26//console.log(wsport);
27
28var 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
29 // This host has nothing to do with ccndhost.
Wentao Shang7297a8d2012-11-19 11:57:45 -080030
31var MaxNumOfClients = opt.options.maxclient || 40;
32//console.log(MaxNumOfClients);
33
34var LOG = opt.options.LOG || 1;
35//console.log(LOG);
36
37if (LOG > 0) console.log('WebSocketServer started...');
38
39wss.on('connection', function(ws) {
40 if (LOG > 0) console.log('WebSocket client connection received.');
41 if (LOG > 0) console.log('Number of clients now is ' + wss.clients.length);
42
43 if (wss.clients.length > MaxNumOfClients) {
44 if (LOG > 0) console.log('Max num of clients exceeded. Close WS connection now.');
45 ws.terminate();
46 return;
47 }
48
49 var sock_ready = false;
Wentao Shang86a3eae2013-03-15 17:36:21 -070050 var ws_ready = true;
Wentao Shang7297a8d2012-11-19 11:57:45 -080051 var send_queue = [];
52 var sock = net.connect({port: 9695, host: ccndhost});
53
54 ws.on('message', function(message) {
55 if (typeof message == 'string') {
56 if (LOG > 1) console.log("Message from clinet: " + message);
57 }
58 else if (typeof message == 'object') {
Wentao Shangaa0b4122013-04-16 13:37:45 -070059 var bytesView = new Buffer(message);
Wentao Shang7297a8d2012-11-19 11:57:45 -080060
61 if (LOG > 1) {
62 var logMsg = 'Byte array from client: ';
63 for (var i = 0; i < bytesView.length; i++)
64 logMsg += String.fromCharCode(bytesView[i]);
65 console.log(logMsg);
66 }
67
68 if (sock_ready) {
Wentao Shangaa0b4122013-04-16 13:37:45 -070069 sock.write(bytesView);
Wentao Shang7297a8d2012-11-19 11:57:45 -080070 } else {
71 send_queue.push(message);
72 }
73 }
74 });
75
76 ws.on('close', function() {
77 if (LOG > 0) console.log('WebSocket connection closed.');
Wentao Shang86a3eae2013-03-15 17:36:21 -070078 ws_ready = false;
Wentao Shang7297a8d2012-11-19 11:57:45 -080079 sock.end();
80 });
81
82 sock.on('connect', function() {
83 while (send_queue.length > 0) {
84 var message = send_queue.shift();
85 sock.write(message);
86 }
87 sock_ready = true;
88 if (LOG > 0) console.log('ccnd socket connection ready.');
89 });
90
91 sock.on('data', function(data) {
92 if (typeof data == 'object') {
Wentao Shangaa0b4122013-04-16 13:37:45 -070093 var bytesView = new Buffer(data);
Wentao Shang7297a8d2012-11-19 11:57:45 -080094
95 if (LOG > 1) {
96 console.log('Byte array from server: ');
97 var logMsg = "";
98 for (var i = 0; i < bytesView.length; i++)
99 logMsg += String.fromCharCode(bytesView[i]);
100 console.log(logMsg);
101 }
102
Wentao Shang86a3eae2013-03-15 17:36:21 -0700103 if (ws_ready == true) {
Wentao Shangaa0b4122013-04-16 13:37:45 -0700104 ws.send(bytesView, {binary: true, mask: false});
Wentao Shang86a3eae2013-03-15 17:36:21 -0700105 }
Wentao Shang7297a8d2012-11-19 11:57:45 -0800106 }
107 });
108
109 sock.on('end', function() {
110 if (LOG > 0) console.log('TCP connection terminated by ccnd. Shut down WS connection to client.');
111 ws.terminate();
112 });
113
114 sock.on('error', function() {
115 if (LOG > 0) console.log('Error on TCP connection to ccnd. Shut down WS connection to client.');
116 ws.terminate();
117 });
118});