blob: 5f028033be55ede3bd3af3fbdbfae55bee0f746e [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 Thompsonbb9802d2013-01-20 23:54:18 -08008 this.elementReader = null;
Jeff Thompsonc881b552012-12-14 01:29:12 -08009 this.defaultGetHostAndPort = NDN.makeShuffledGetHostAndPort
10 (["A.ws.ndn.ucla.edu", "B.ws.ndn.ucla.edu", "C.ws.ndn.ucla.edu", "D.ws.ndn.ucla.edu",
11 "E.ws.ndn.ucla.edu"],
12 9696);
Jeff Thompson5b265a72012-11-12 01:13:08 -080013};
14
Jeff Thompson5b265a72012-11-12 01:13:08 -080015WebSocketTransport.prototype.connectWebSocket = function(ndn) {
16 if (this.ws != null)
17 delete this.ws;
18
19 this.ws = new WebSocket('ws://' + ndn.host + ':' + ndn.port);
Jeff Thompson3c263812012-12-01 17:20:28 -080020 if (LOG > 0) console.log('ws connection created.');
Jeff Thompson5b265a72012-11-12 01:13:08 -080021
22 this.ws.binaryType = "arraybuffer";
23
Jeff Thompsonbb9802d2013-01-20 23:54:18 -080024 this.elementReader = new BinaryXmlElementReader(ndn);
Jeff Thompson5b265a72012-11-12 01:13:08 -080025 var self = this;
26 this.ws.onmessage = function(ev) {
27 var result = ev.data;
28 //console.log('RecvHandle called.');
29
30 if(result == null || result == undefined || result == "" ) {
31 console.log('INVALID ANSWER');
32 } else if (result instanceof ArrayBuffer) {
33 var bytearray = new Uint8Array(result);
34
Jeff Thompson13c2e7b2012-12-01 17:33:30 -080035 if (LOG>3) console.log('BINARY RESPONSE IS ' + DataUtils.toHex(bytearray));
Jeff Thompson5b265a72012-11-12 01:13:08 -080036
37 try {
Jeff Thompsonbb9802d2013-01-20 23:54:18 -080038 // Find the end of the binary XML element and call ndn.onReceivedElement.
39 self.elementReader.onReceivedData(bytearray);
Jeff Thompson5b265a72012-11-12 01:13:08 -080040 } catch (ex) {
41 console.log("NDN.ws.onmessage exception: " + ex);
42 return;
43 }
Jeff Thompson5b265a72012-11-12 01:13:08 -080044 }
45 }
46
47 this.ws.onopen = function(ev) {
Jeff Thompson3c263812012-12-01 17:20:28 -080048 if (LOG > 3) console.log(ev);
49 if (LOG > 3) console.log('ws.onopen: WebSocket connection opened.');
50 if (LOG > 3) console.log('ws.onopen: ReadyState: ' + this.readyState);
Jeff Thompson5b265a72012-11-12 01:13:08 -080051
52 // Fetch ccndid now
Wentao Shange0d7f052013-01-05 16:37:02 -080053 var interest = new Interest(NDN.ccndIdFetcher);
Jeff Thompson42806a12012-12-29 18:19:39 -080054 interest.interestLifetime = 4000; // milliseconds
Jeff Thompson75771cb2013-01-20 23:27:38 -080055 this.send(encodeToBinaryInterest(interest));
Jeff Thompson5b265a72012-11-12 01:13:08 -080056 }
57
58 this.ws.onerror = function(ev) {
59 console.log('ws.onerror: ReadyState: ' + this.readyState);
60 console.log(ev);
61 console.log('ws.onerror: WebSocket error: ' + ev.data);
62 }
63
64 this.ws.onclose = function(ev) {
65 console.log('ws.onclose: WebSocket connection closed.');
66 self.ws = null;
Wentao Shang0e291c82012-12-02 23:36:29 -080067
68 // Close NDN when WebSocket is closed
69 ndn.readyStatus = NDN.CLOSED;
70 ndn.onclose();
Wentao Shangaf25c6b2012-12-03 00:09:30 -080071 //console.log("NDN.onclose event fired.");
Jeff Thompson5b265a72012-11-12 01:13:08 -080072 }
Jeff Thompsonbe85be62012-12-13 22:32:01 -080073};
Jeff Thompson5b265a72012-11-12 01:13:08 -080074
Jeff Thompson75771cb2013-01-20 23:27:38 -080075/*
76 * Send the Uint8Array data.
77 */
78WebSocketTransport.prototype.send = function(data) {
Wentao Shangc0311e52012-12-03 10:38:23 -080079 if (this.ws != null) {
Jeff Thompson75771cb2013-01-20 23:27:38 -080080 // If we directly use data.buffer to feed ws.send(),
81 // WebSocket may end up sending a packet with 10000 bytes of data.
82 // That is, WebSocket will flush the entire buffer
83 // regardless of the offset of the Uint8Array. So we have to create
84 // a new Uint8Array buffer with just the right size and copy the
85 // content from binaryInterest to the new buffer.
86 // ---Wentao
87 var bytearray = new Uint8Array(data.length);
88 bytearray.set(data);
89 this.ws.send(bytearray.buffer);
Wentao Shangc0311e52012-12-03 10:38:23 -080090 if (LOG > 3) console.log('ws.send() returned.');
Wentao Shangc0311e52012-12-03 10:38:23 -080091 }
92 else
93 console.log('WebSocket connection is not established.');
Jeff Thompson5b265a72012-11-12 01:13:08 -080094}
95
Jeff Thompson75771cb2013-01-20 23:27:38 -080096WebSocketTransport.prototype.expressInterest = function(ndn, interest, closure) {
97 if (ndn.readyStatus != NDN.OPENED) {
98 console.log('Connection is not established.');
99 return;
100 }
101
102 //TODO: check local content store first
103 if (closure != null) {
104 var pitEntry = new PITEntry(interest, closure);
105 // TODO: This needs to be a single thread-safe transaction on a global object.
106 NDN.PITTable.push(pitEntry);
107 closure.pitEntry = pitEntry;
Jeff Thompson5b265a72012-11-12 01:13:08 -0800108 }
Jeff Thompson75771cb2013-01-20 23:27:38 -0800109
110 // Set interest timer
111 if (closure != null) {
112 pitEntry.timerID = setTimeout(function() {
113 if (LOG > 3) console.log("Interest time out.");
114
115 // Remove PIT entry from NDN.PITTable.
116 // TODO: Make this a thread-safe operation on the global PITTable.
117 var index = NDN.PITTable.indexOf(pitEntry);
118 //console.log(NDN.PITTable);
119 if (index >= 0)
120 NDN.PITTable.splice(index, 1);
121 //console.log(NDN.PITTable);
122 //console.log(pitEntry.interest.name.getName());
123
124 // Raise closure callback
125 closure.upcall(Closure.UPCALL_INTEREST_TIMED_OUT, new UpcallInfo(ndn, interest, 0, null));
126 }, interest.interestLifetime); // interestLifetime is in milliseconds.
127 //console.log(closure.timerID);
128 }
129
130 this.send(encodeToBinaryInterest(interest));
Jeff Thompsonbe85be62012-12-13 22:32:01 -0800131};
Jeff Thompson5b265a72012-11-12 01:13:08 -0800132