blob: eb8a938274a4ca54ae483aa6dc1bdf73baffb1dc [file] [log] [blame]
Wentao Shangc0311e52012-12-03 10:38:23 -08001/**
Jeff Thompson5b265a72012-11-12 01:13:08 -08002 * @author: Wentao Shang
3 * See COPYING for copyright and distribution information.
Jeff Thompson5b265a72012-11-12 01:13:08 -08004 */
5
6var WebSocketTransport = function WebSocketTransport() {
7 this.ws = null;
Jeff Thompsonf668acb2013-01-26 20:29:46 -08008 this.connectedHost = null; // Read by NDN.
9 this.connectedPort = null; // Read by NDN.
Jeff Thompsonbb9802d2013-01-20 23:54:18 -080010 this.elementReader = null;
Jeff Thompsonc881b552012-12-14 01:29:12 -080011 this.defaultGetHostAndPort = NDN.makeShuffledGetHostAndPort
12 (["A.ws.ndn.ucla.edu", "B.ws.ndn.ucla.edu", "C.ws.ndn.ucla.edu", "D.ws.ndn.ucla.edu",
13 "E.ws.ndn.ucla.edu"],
14 9696);
Jeff Thompson5b265a72012-11-12 01:13:08 -080015};
16
Jeff Thompsonf668acb2013-01-26 20:29:46 -080017/*
18 * Connect to the host and port in ndn. This replaces a previous connection and sets connectedHost
19 * and connectedPort. Once connected, call onopenCallback().
20 * Listen on the port to read an entire binary XML encoded element and call
21 * ndn.onReceivedElement(element).
22 */
Jeff Thompsond771b122013-01-26 19:04:41 -080023WebSocketTransport.prototype.connect = function(ndn, onopenCallback) {
Jeff Thompson5b265a72012-11-12 01:13:08 -080024 if (this.ws != null)
25 delete this.ws;
26
27 this.ws = new WebSocket('ws://' + ndn.host + ':' + ndn.port);
Jeff Thompson3c263812012-12-01 17:20:28 -080028 if (LOG > 0) console.log('ws connection created.');
Jeff Thompsond771b122013-01-26 19:04:41 -080029 this.connectedHost = ndn.host;
30 this.connectedPort = ndn.port;
Jeff Thompson5b265a72012-11-12 01:13:08 -080031
32 this.ws.binaryType = "arraybuffer";
33
Jeff Thompsonbb9802d2013-01-20 23:54:18 -080034 this.elementReader = new BinaryXmlElementReader(ndn);
Jeff Thompson5b265a72012-11-12 01:13:08 -080035 var self = this;
36 this.ws.onmessage = function(ev) {
37 var result = ev.data;
38 //console.log('RecvHandle called.');
39
40 if(result == null || result == undefined || result == "" ) {
41 console.log('INVALID ANSWER');
42 } else if (result instanceof ArrayBuffer) {
43 var bytearray = new Uint8Array(result);
44
Jeff Thompson13c2e7b2012-12-01 17:33:30 -080045 if (LOG>3) console.log('BINARY RESPONSE IS ' + DataUtils.toHex(bytearray));
Jeff Thompson5b265a72012-11-12 01:13:08 -080046
47 try {
Jeff Thompsonbb9802d2013-01-20 23:54:18 -080048 // Find the end of the binary XML element and call ndn.onReceivedElement.
49 self.elementReader.onReceivedData(bytearray);
Jeff Thompson5b265a72012-11-12 01:13:08 -080050 } catch (ex) {
51 console.log("NDN.ws.onmessage exception: " + ex);
52 return;
53 }
Jeff Thompson5b265a72012-11-12 01:13:08 -080054 }
55 }
56
57 this.ws.onopen = function(ev) {
Jeff Thompson3c263812012-12-01 17:20:28 -080058 if (LOG > 3) console.log(ev);
59 if (LOG > 3) console.log('ws.onopen: WebSocket connection opened.');
60 if (LOG > 3) console.log('ws.onopen: ReadyState: ' + this.readyState);
Jeff Thompsond771b122013-01-26 19:04:41 -080061 // NDN.registerPrefix will fetch the ccndid when needed.
62
63 onopenCallback();
Jeff Thompson5b265a72012-11-12 01:13:08 -080064 }
65
66 this.ws.onerror = function(ev) {
67 console.log('ws.onerror: ReadyState: ' + this.readyState);
68 console.log(ev);
69 console.log('ws.onerror: WebSocket error: ' + ev.data);
70 }
71
72 this.ws.onclose = function(ev) {
73 console.log('ws.onclose: WebSocket connection closed.');
74 self.ws = null;
Wentao Shang0e291c82012-12-02 23:36:29 -080075
76 // Close NDN when WebSocket is closed
77 ndn.readyStatus = NDN.CLOSED;
78 ndn.onclose();
Wentao Shangaf25c6b2012-12-03 00:09:30 -080079 //console.log("NDN.onclose event fired.");
Jeff Thompson5b265a72012-11-12 01:13:08 -080080 }
Jeff Thompsonbe85be62012-12-13 22:32:01 -080081};
Jeff Thompson5b265a72012-11-12 01:13:08 -080082
Jeff Thompson75771cb2013-01-20 23:27:38 -080083/*
84 * Send the Uint8Array data.
85 */
86WebSocketTransport.prototype.send = function(data) {
Wentao Shangc0311e52012-12-03 10:38:23 -080087 if (this.ws != null) {
Jeff Thompson75771cb2013-01-20 23:27:38 -080088 // If we directly use data.buffer to feed ws.send(),
89 // WebSocket may end up sending a packet with 10000 bytes of data.
90 // That is, WebSocket will flush the entire buffer
91 // regardless of the offset of the Uint8Array. So we have to create
92 // a new Uint8Array buffer with just the right size and copy the
93 // content from binaryInterest to the new buffer.
94 // ---Wentao
95 var bytearray = new Uint8Array(data.length);
96 bytearray.set(data);
97 this.ws.send(bytearray.buffer);
Wentao Shangc0311e52012-12-03 10:38:23 -080098 if (LOG > 3) console.log('ws.send() returned.');
Wentao Shangc0311e52012-12-03 10:38:23 -080099 }
100 else
101 console.log('WebSocket connection is not established.');
Jeff Thompson5b265a72012-11-12 01:13:08 -0800102}