Added new websocket files.
diff --git a/wsproxy/README.md b/wsproxy/README.md
new file mode 100644
index 0000000..eb0cdc0
--- /dev/null
+++ b/wsproxy/README.md
@@ -0,0 +1,10 @@
+ws-ndn-js
+=========
+
+WebSocket proxy server between NDN javascript stack and ccnd
+
+This proxy runs on top of 'node.js'. 'ws' package is required. It listens for WebSocket connection request on port number 9696. Once it receives a incoming connection, it issues a TCP connection to the specified 'ccnd' router (port number 9695). It then translates packet frames from WebSocket to pure TCP byte streams and vice versa.
+
+To run the proxy, simply use the command 'node ws-ndn.js'.
+
+Acknowledgement: this code is extended from Junxiao's WebSocket proxy implementation (https://gist.github.com/3835425).
\ No newline at end of file
diff --git a/wsproxy/ws-ndn.js b/wsproxy/ws-ndn.js
new file mode 100644
index 0000000..2b67e0d
--- /dev/null
+++ b/wsproxy/ws-ndn.js
@@ -0,0 +1,80 @@
+var WebSocketServer = require('ws').Server;
+var net = require('net');
+
+var wss = new WebSocketServer({port:9696, host:'0.0.0.0'});
+
+var MaxNumOfClients = 2;
+
+wss.on('connection', function(ws) {
+ console.log('WebSocket client connection received.');
+ console.log('Number of clients now is ' + wss.clients.length);
+
+ if (wss.clients.length > MaxNumOfClients) {
+ console.log('Max num of clients exceeded. Close WS connection now.');
+ ws.terminate();
+ return;
+ }
+
+ var sock_ready = false;
+ var send_queue = [];
+ var sock = net.createConnection(9695);
+
+ ws.on('message', function(message) {
+ if (typeof message == 'string')
+ console.log("Message from clinet: " + message);
+ else if (typeof message == 'object') {
+ var bytesView = new Uint8Array(message);
+
+ var logMsg = 'Byte array from client: ';
+ for (var i = 0; i < bytesView.length; i++)
+ logMsg += String.fromCharCode(bytesView[i]);
+ console.log(logMsg);
+
+ if (sock_ready) {
+ sock.write(bytesView.buffer);
+ console.log('sock.write() returned.');
+ } else {
+ send_queue.push(message);
+ }
+ }
+ });
+
+ ws.on('close', function() {
+ console.log('WebSocket connection closed.');
+ sock.end();
+ });
+
+ sock.on('connect', function() {
+ while (send_queue.length > 0) {
+ var message = send_queue.shift();
+ sock.write(message);
+ }
+ sock_ready = true;
+ console.log('ccnd socket connection ready.');
+ });
+
+ sock.on('data', function(data) {
+ if (typeof data == 'object') {
+ var bytesView = new Uint8Array(data);
+
+ console.log('Byte array from server: ');
+ var logMsg = "";
+ for (var i = 0; i < bytesView.length; i++)
+ logMsg += String.fromCharCode(bytesView[i]);
+ console.log(logMsg);
+
+ ws.send(bytesView.buffer, {binary: true, mask: false});
+ console.log('ws.send() returned.');
+ }
+ });
+
+ sock.on('end', function() {
+ console.log('TCP connection terminated by ccnd. Shut down WS connection to client.');
+ ws.terminate();
+ });
+
+ sock.on('error', function() {
+ console.log('Error on TCP connection to ccnd. Shut down WS connection to client.');
+ ws.terminate();
+ });
+});
diff --git a/wsproxy/wsproxy-udp.js b/wsproxy/wsproxy-udp.js
new file mode 100644
index 0000000..bec9421
--- /dev/null
+++ b/wsproxy/wsproxy-udp.js
@@ -0,0 +1,110 @@
+var WebSocketServer = require('ws').Server;
+var dgram = require('dgram');
+
+var ccndhost = 'localhost';
+
+var wss = new WebSocketServer({port:9696, host:ccndhost});
+
+var MaxNumOfClients = 2;
+
+wss.on('connection', function(ws) {
+ console.log('wss.onconnection: WebSocket client connection received.');
+ console.log('wss.onconnection: Number of clients now is ' + wss.clients.length);
+
+ if (wss.clients.length > MaxNumOfClients) {
+ console.log('wss.onconnection: Max num of clients exceeded. Close WS connection now.');
+ ws.terminate();
+ return;
+ }
+
+ var udp = dgram.createSocket("udp4");
+
+ /*
+ * According to the email discussion with Michael, when we use
+ * UDP to connect to ccnd, we MUST first send a 'heartbeat'
+ * UDP packet with 1-byte payload (content of this byte can
+ * be random). The purpose of this packet is to let ccnd
+ * mark the incoming FACE as 'friendly' (with CCN_FACE_GG
+ * flag set). We also need to periodically send this 'heartbeat'
+ * packet every few seconds to keep ccnd from recycling the UDP
+ * face. Michael recomended 8 seconds interval.
+ * --Wentao
+ */
+ // Send 'heartbeat' packet now
+ var heartbeat = new Buffer(1);
+ heartbeat[0] = 0x21;
+ udp.send(heartbeat, 0, 1, 9695, ccndhost, null);
+
+ // Schedule a timer to send 'heartbeat' periodically
+ var timerID = setInterval(function() {
+ if (udp == null || udp == undefined)
+ return;
+
+ var hb = new Buffer(1);
+ hb[0] = 0x21;
+ udp.send(hb, 0, 1, 9695, ccndhost, null);
+ //console.log('UDP heartbeat fired at ccnd.');
+ },
+ 8000 // 8000 ms delay
+ );
+
+ ws.on('message', function(message) {
+ if (typeof message == 'string')
+ console.log("ws.onmessage: Message from clinet: " + message);
+ else if (typeof message == 'object') {
+ // From JS array to Buffer
+ var buffer = new Buffer(message);
+
+ var logMsg = 'ws.onmessage: Byte array from client: ';
+ for (var i = 0; i < buffer.length; i++)
+ logMsg += String.fromCharCode(buffer[i]);
+ console.log(logMsg);
+
+ udp.send(buffer, 0, buffer.length, 9695, ccndhost, null);
+ console.log('ws.onmessage: udp.send() returned.');
+ }
+ });
+
+ ws.on('close', function() {
+ console.log('ws.onclose: WebSocket connection closed. Close UDP connection to ccnd and stop "heartbeat" timer.');
+ clearInterval(timerID);
+ udp.close();
+ udp = null;
+ });
+
+ udp.on('message', function(msg, rinfo) {
+ if (typeof msg == 'object') {
+ // From Buffer to ArrayBuffer
+ var bytesView = new Uint8Array(msg);
+
+ console.log('udp.onmessage: Byte array from server: ');
+ console.log('udp.onmessage: bytesView.length ' + bytesView.length);
+ var logMsg = "";
+ for (var i = 0; i < bytesView.length; i++)
+ logMsg += String.fromCharCode(bytesView[i]);
+ console.log(logMsg);
+
+ ws.send(bytesView.buffer, {binary: true, mask: false});
+ console.log('udp.onmessage: ws.send() returned.');
+ }
+ });
+
+ // Actually the socket close by ccnd will not cause the 'close' event to raise.
+ // So this event handle is only called when the client browser shuts down the WS
+ // connection, causing ws 'close' event to raise. In that event handle, we explicitly
+ // call udp.close(). So in this function we can do nothing. Anyway, here we clear the
+ // timer and terminate ws for a second time since that will not throw exception. 'ws'
+ // will check the 'readyState' before closing, therefore avoids 'close' event loop.
+ // --Wentao
+ udp.on('close', function() {
+ console.log('udp.onclose: UDP connection to ccnd terminated. Shut down WS connection to client and stop "heartbeat" timer.');
+ clearInterval(timerID);
+ ws.terminate();
+ });
+
+ udp.on('error', function() {
+ console.log('udp.onerror: Error on UDP connection to ccnd. Shut down WS connection to client and stop "heartbeat" timer.');
+ clearInterval(timerID);
+ ws.terminate();
+ });
+});