blob: 0e700c603900cb5a61a3a9b743a9171b9e65ca2b [file] [log] [blame]
Alexander Afanasyev181a8b92013-02-28 13:28:53 -08001#!/usr/bin/env node
2
Wentao Shang7297a8d2012-11-19 11:57:45 -08003/*
4 * @author: Wentao Shang
5 * See COPYING for copyright and distribution information.
6 * Implement WebSocket proxy between ccnd and javascript stack.
7 */
8
9var WebSocketServer = require('ws').Server;
10var net = require('net');
11
12var opt = require('node-getopt').create([
13 ['c' , 'ccnd=ARG', 'host name or ip of ccnd router'],
Wentao Shang7eb8c402012-11-19 13:30:44 -080014 ['p' , 'port=ARG', 'port number on which the proxy will listen'],
Wentao Shang7297a8d2012-11-19 11:57:45 -080015 ['m' , 'maxclient=ARG', 'maximum number of concurrent client'],
16 ['L' , 'LOG=ARG', 'level of log message display'],
17 ['h' , 'help', 'display this help']
18]) // create Getopt instance
19.bindHelp() // bind option 'help' to default action
20.parseSystem(); // parse command line
21
22//console.log(opt);
23
24var ccndhost = opt.options.ccnd || 'localhost';
25//console.log(ccndhost);
26
Wentao Shang7eb8c402012-11-19 13:30:44 -080027var wsport = opt.options.port || 9696;
28//console.log(wsport);
29
30var 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
31 // This host has nothing to do with ccndhost.
Wentao Shang7297a8d2012-11-19 11:57:45 -080032
33var MaxNumOfClients = opt.options.maxclient || 40;
34//console.log(MaxNumOfClients);
35
36var LOG = opt.options.LOG || 1;
37//console.log(LOG);
38
39if (LOG > 0) console.log('WebSocketServer started...');
40
41wss.on('connection', function(ws) {
42 if (LOG > 0) console.log('WebSocket client connection received.');
43 if (LOG > 0) console.log('Number of clients now is ' + wss.clients.length);
44
45 if (wss.clients.length > MaxNumOfClients) {
46 if (LOG > 0) console.log('Max num of clients exceeded. Close WS connection now.');
47 ws.terminate();
48 return;
49 }
50
51 var sock_ready = false;
52 var send_queue = [];
53 var sock = net.connect({port: 9695, host: ccndhost});
54
55 ws.on('message', function(message) {
56 if (typeof message == 'string') {
57 if (LOG > 1) console.log("Message from clinet: " + message);
58 }
59 else if (typeof message == 'object') {
60 var bytesView = new Uint8Array(message);
61
62 if (LOG > 1) {
63 var logMsg = 'Byte array from client: ';
64 for (var i = 0; i < bytesView.length; i++)
65 logMsg += String.fromCharCode(bytesView[i]);
66 console.log(logMsg);
67 }
68
69 if (sock_ready) {
70 sock.write(bytesView.buffer);
71 } else {
72 send_queue.push(message);
73 }
74 }
75 });
76
77 ws.on('close', function() {
78 if (LOG > 0) console.log('WebSocket connection closed.');
79 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') {
93 var bytesView = new Uint8Array(data);
94
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
103 ws.send(bytesView.buffer, {binary: true, mask: false});
104 }
105 });
106
107 sock.on('end', function() {
108 if (LOG > 0) console.log('TCP connection terminated by ccnd. Shut down WS connection to client.');
109 ws.terminate();
110 });
111
112 sock.on('error', function() {
113 if (LOG > 0) console.log('Error on TCP connection to ccnd. Shut down WS connection to client.');
114 ws.terminate();
115 });
116});