blob: 99cb224ef0554f1cfd8ccb8c3a80a6740fdb0c8d [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;
8 this.ccndid = null;
9 this.maxBufferSize = 10000; // Currently support 10000 bytes data input, consistent with BinaryXMLEncoder
10 this.buffer = new Uint8Array(this.maxBufferSize);
Wentao Shang2b740e62012-12-07 00:02:53 -080011 this.bufferOffset = 0;
Jeff Thompson5b265a72012-11-12 01:13:08 -080012 this.structureDecoder = new BinaryXMLStructureDecoder();
Jeff Thompsonc881b552012-12-14 01:29:12 -080013 this.defaultGetHostAndPort = NDN.makeShuffledGetHostAndPort
14 (["A.ws.ndn.ucla.edu", "B.ws.ndn.ucla.edu", "C.ws.ndn.ucla.edu", "D.ws.ndn.ucla.edu",
15 "E.ws.ndn.ucla.edu"],
16 9696);
Jeff Thompson5b265a72012-11-12 01:13:08 -080017};
18
Jeff Thompson5b265a72012-11-12 01:13:08 -080019WebSocketTransport.prototype.connectWebSocket = function(ndn) {
20 if (this.ws != null)
21 delete this.ws;
22
23 this.ws = new WebSocket('ws://' + ndn.host + ':' + ndn.port);
Jeff Thompson3c263812012-12-01 17:20:28 -080024 if (LOG > 0) console.log('ws connection created.');
Jeff Thompson5b265a72012-11-12 01:13:08 -080025
26 this.ws.binaryType = "arraybuffer";
27
28 var self = this;
29 this.ws.onmessage = function(ev) {
30 var result = ev.data;
31 //console.log('RecvHandle called.');
32
33 if(result == null || result == undefined || result == "" ) {
34 console.log('INVALID ANSWER');
35 } else if (result instanceof ArrayBuffer) {
36 var bytearray = new Uint8Array(result);
37
Jeff Thompson13c2e7b2012-12-01 17:33:30 -080038 if (LOG>3) console.log('BINARY RESPONSE IS ' + DataUtils.toHex(bytearray));
Jeff Thompson5b265a72012-11-12 01:13:08 -080039
40 try {
Wentao Shang2b740e62012-12-07 00:02:53 -080041 if (bytearray.length + self.bufferOffset >= self.buffer.byteLength) {
42 if (LOG>3) {
43 console.log("NDN.ws.onmessage: buffer overflow. Accumulate received length: " + self.bufferOffset
44 + ". Current packet length: " + bytearray.length + ".");
45 }
46
Jeff Thompson5b265a72012-11-12 01:13:08 -080047 // Purge and quit.
48 delete self.structureDecoder;
49 delete self.buffer;
50 self.structureDecoder = new BinaryXMLStructureDecoder();
51 self.buffer = new Uint8Array(self.maxBufferSize);
Wentao Shang2b740e62012-12-07 00:02:53 -080052 self.bufferOffset = 0;
Jeff Thompson5b265a72012-11-12 01:13:08 -080053 return;
54 }
55
56 /*for (var i = 0; i < bytearray.length; i++) {
57 self.buffer.push(bytearray[i]);
58 }*/
Wentao Shang2b740e62012-12-07 00:02:53 -080059 self.buffer.set(bytearray, self.bufferOffset);
60 self.bufferOffset += bytearray.length;
Jeff Thompson5b265a72012-11-12 01:13:08 -080061
Wentao Shang2b740e62012-12-07 00:02:53 -080062 if (!self.structureDecoder.findElementEnd(self.buffer.subarray(0, self.bufferOffset))) {
Jeff Thompson5b265a72012-11-12 01:13:08 -080063 // Need more data to decode
Wentao Shang2b740e62012-12-07 00:02:53 -080064 if (LOG>3) console.log('Incomplete packet received. Length ' + bytearray.length + '. Wait for more input.');
Jeff Thompson5b265a72012-11-12 01:13:08 -080065 return;
66 }
Wentao Shang2b740e62012-12-07 00:02:53 -080067 if (LOG>3) console.log('Complete packet received. Length ' + bytearray.length + '. Start decoding.');
Jeff Thompson5b265a72012-11-12 01:13:08 -080068 } catch (ex) {
69 console.log("NDN.ws.onmessage exception: " + ex);
70 return;
71 }
72
73 var decoder = new BinaryXMLDecoder(self.buffer);
74 // Dispatch according to packet type
75 if (decoder.peekStartElement(CCNProtocolDTags.Interest)) { // Interest packet
Jeff Thompson3c263812012-12-01 17:20:28 -080076 if (LOG > 3) console.log('Interest packet received.');
Jeff Thompson5b265a72012-11-12 01:13:08 -080077
78 var interest = new Interest();
79 interest.from_ccnb(decoder);
80 if (LOG>3) console.log(interest);
81 var nameStr = escape(interest.name.getName());
Jeff Thompson3c263812012-12-01 17:20:28 -080082 if (LOG > 3) console.log(nameStr);
Jeff Thompson5b265a72012-11-12 01:13:08 -080083
84 var entry = getEntryForRegisteredPrefix(nameStr);
85 if (entry != null) {
86 //console.log(entry);
87 entry.closure.upcall(Closure.UPCALL_INTEREST, new UpcallInfo(ndn, interest, 0, null));
88 }
89
90 } else if (decoder.peekStartElement(CCNProtocolDTags.ContentObject)) { // Content packet
Jeff Thompson3c263812012-12-01 17:20:28 -080091 if (LOG > 3) console.log('ContentObject packet received.');
Jeff Thompson5b265a72012-11-12 01:13:08 -080092
93 var co = new ContentObject();
94 co.from_ccnb(decoder);
Jeff Thompson3c263812012-12-01 17:20:28 -080095 if (LOG > 3) console.log(co);
Jeff Thompson5b265a72012-11-12 01:13:08 -080096 nameStr = co.name.getName();
Jeff Thompson3c263812012-12-01 17:20:28 -080097 if (LOG > 3) console.log(nameStr);
Jeff Thompson5b265a72012-11-12 01:13:08 -080098
Jeff Thompsonbd25df22012-12-13 21:50:13 -080099 if (self.ccndid == null && nameStr.match(NDN.ccndIdFetcher) != null) {
Jeff Thompson5b265a72012-11-12 01:13:08 -0800100 // We are in starting phase, record publisherPublicKeyDigest in self.ccndid
101 if(!co.signedInfo || !co.signedInfo.publisher
102 || !co.signedInfo.publisher.publisherPublicKeyDigest) {
Wentao Shangaf25c6b2012-12-03 00:09:30 -0800103 console.log("Cannot contact router, close NDN now.");
Wentao Shang0e291c82012-12-02 23:36:29 -0800104
105 // Close NDN if we fail to connect to a ccn router
106 ndn.readyStatus = NDN.CLOSED;
107 ndn.onclose();
Wentao Shangaf25c6b2012-12-03 00:09:30 -0800108 //console.log("NDN.onclose event fired.");
Jeff Thompson5b265a72012-11-12 01:13:08 -0800109 } else {
Wentao Shangaf25c6b2012-12-03 00:09:30 -0800110 //console.log('Connected to ccnd.');
Jeff Thompson5b265a72012-11-12 01:13:08 -0800111 self.ccndid = co.signedInfo.publisher.publisherPublicKeyDigest;
112 if (LOG>3) console.log(self.ccndid);
Wentao Shang0e291c82012-12-02 23:36:29 -0800113
114 // Call NDN.onopen after success
115 ndn.readyStatus = NDN.OPENED;
116 ndn.onopen();
Wentao Shangaf25c6b2012-12-03 00:09:30 -0800117 //console.log("NDN.onopen event fired.");
Jeff Thompson5b265a72012-11-12 01:13:08 -0800118 }
119 } else {
Jeff Thompsonbe85be62012-12-13 22:32:01 -0800120 var pitEntry = NDN.getEntryForExpressedInterest(co.name);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800121 if (pitEntry != null) {
122 //console.log(pitEntry);
Wentao Shangc0311e52012-12-03 10:38:23 -0800123
124 // Cancel interest timer
125 clearTimeout(pitEntry.closure.timerID);
126 //console.log("Clear interest timer");
127 //console.log(pitEntry.closure.timerID);
Wentao Shangbd63e462012-12-03 16:19:33 -0800128
Jeff Thompsonbe85be62012-12-13 22:32:01 -0800129 // Remove PIT entry from NDN.PITTable
130 var index = NDN.PITTable.indexOf(pitEntry);
Jeff Thompsonc881b552012-12-14 01:29:12 -0800131 if (index >= 0)
132 NDN.PITTable.splice(index, 1);
Wentao Shangbd63e462012-12-03 16:19:33 -0800133
Wentao Shangc0311e52012-12-03 10:38:23 -0800134 // Raise callback
Jeff Thompson5b265a72012-11-12 01:13:08 -0800135 pitEntry.closure.upcall(Closure.UPCALL_CONTENT, new UpcallInfo(ndn, null, 0, co));
136 }
137 }
138 } else {
139 console.log('Incoming packet is not Interest or ContentObject. Discard now.');
140 }
141
142 delete decoder;
143
144 // Renew StrcutureDecoder and buffer after we process a full packet
145 delete self.structureDecoder;
146 delete self.buffer;
147 self.structureDecoder = new BinaryXMLStructureDecoder();
148 self.buffer = new Uint8Array(self.maxBufferSize);
Wentao Shang2b740e62012-12-07 00:02:53 -0800149 self.bufferOffset = 0;
Jeff Thompson5b265a72012-11-12 01:13:08 -0800150 }
151 }
152
153 this.ws.onopen = function(ev) {
Jeff Thompson3c263812012-12-01 17:20:28 -0800154 if (LOG > 3) console.log(ev);
155 if (LOG > 3) console.log('ws.onopen: WebSocket connection opened.');
156 if (LOG > 3) console.log('ws.onopen: ReadyState: ' + this.readyState);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800157
158 // Fetch ccndid now
Jeff Thompsonbd25df22012-12-13 21:50:13 -0800159 var interest = new Interest(new Name(NDN.ccndIdFetcher));
Jeff Thompson84db2632012-12-09 22:31:39 -0800160 interest.interestLifetime = 4.0; // seconds
Jeff Thompson64db7c02012-12-09 21:56:57 -0800161 var subarray = encodeToBinaryInterest(interest);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800162
Jeff Thompson64db7c02012-12-09 21:56:57 -0800163 var bytes = new Uint8Array(subarray.length);
164 bytes.set(subarray);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800165
166 self.ws.send(bytes.buffer);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800167 }
168
169 this.ws.onerror = function(ev) {
170 console.log('ws.onerror: ReadyState: ' + this.readyState);
171 console.log(ev);
172 console.log('ws.onerror: WebSocket error: ' + ev.data);
173 }
174
175 this.ws.onclose = function(ev) {
176 console.log('ws.onclose: WebSocket connection closed.');
177 self.ws = null;
Wentao Shang0e291c82012-12-02 23:36:29 -0800178
179 // Close NDN when WebSocket is closed
180 ndn.readyStatus = NDN.CLOSED;
181 ndn.onclose();
Wentao Shangaf25c6b2012-12-03 00:09:30 -0800182 //console.log("NDN.onclose event fired.");
Jeff Thompson5b265a72012-11-12 01:13:08 -0800183 }
Jeff Thompsonbe85be62012-12-13 22:32:01 -0800184};
Jeff Thompson5b265a72012-11-12 01:13:08 -0800185
Wentao Shangc0311e52012-12-03 10:38:23 -0800186WebSocketTransport.prototype.expressInterest = function(ndn, interest, closure) {
187 if (this.ws != null) {
188 //TODO: check local content store first
189
190 var binaryInterest = encodeToBinaryInterest(interest);
191 var bytearray = new Uint8Array(binaryInterest.length);
192 bytearray.set(binaryInterest);
193
Jeff Thompsonbe85be62012-12-13 22:32:01 -0800194 var pitEntry = new PITEntry(interest, closure);
195 NDN.PITTable.push(pitEntry);
Wentao Shangc0311e52012-12-03 10:38:23 -0800196
197 this.ws.send(bytearray.buffer);
198 if (LOG > 3) console.log('ws.send() returned.');
199
200 // Set interest timer
201 closure.timerID = setTimeout(function() {
Wentao Shangbd63e462012-12-03 16:19:33 -0800202 if (LOG > 3) console.log("Interest time out.");
Wentao Shangc0311e52012-12-03 10:38:23 -0800203
Jeff Thompsonbe85be62012-12-13 22:32:01 -0800204 // Remove PIT entry from NDN.PITTable
205 var index = NDN.PITTable.indexOf(pitEntry);
206 //console.log(NDN.PITTable);
Jeff Thompsonc881b552012-12-14 01:29:12 -0800207 if (index >= 0)
208 NDN.PITTable.splice(index, 1);
Jeff Thompsonbe85be62012-12-13 22:32:01 -0800209 //console.log(NDN.PITTable);
Wentao Shangc0311e52012-12-03 10:38:23 -0800210 // Raise closure callback
211 closure.upcall(Closure.UPCALL_INTEREST_TIMED_OUT, new UpcallInfo(ndn, interest, 0, null));
Jeff Thompson84db2632012-12-09 22:31:39 -0800212 }, interest.interestLifetime * 1000); // convert interestLifetime from seconds to ms.
Wentao Shangc0311e52012-12-03 10:38:23 -0800213 //console.log(closure.timerID);
214 }
215 else
216 console.log('WebSocket connection is not established.');
217};
218
Jeff Thompson5b265a72012-11-12 01:13:08 -0800219
220// For publishing data
221var CSTable = new Array();
222
223var CSEntry = function CSEntry(name, closure) {
224 this.name = name; // String
225 this.closure = closure; // Closure
Jeff Thompsonbe85be62012-12-13 22:32:01 -0800226};
Jeff Thompson5b265a72012-11-12 01:13:08 -0800227
228function getEntryForRegisteredPrefix(name) {
229 for (var i = 0; i < CSTable.length; i++) {
230 if (CSTable[i].name.match(name) != null)
231 return CSTable[i];
232 }
233 return null;
234}
235
236WebSocketTransport.prototype.registerPrefix = function(ndn, name, closure, flag) {
237 if (this.ws != null) {
238 if (this.ccndid == null) {
239 console.log('ccnd node ID unkonwn. Cannot register prefix.');
Jeff Thompson3c263812012-12-01 17:20:28 -0800240 return -1;
Jeff Thompson5b265a72012-11-12 01:13:08 -0800241 }
242
Jeff Thompson48ba4ff2012-11-12 01:23:13 -0800243 var fe = new ForwardingEntry('selfreg', name, null, null, 3, 2147483647);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800244 var bytes = encodeForwardingEntry(fe);
245
246 var si = new SignedInfo();
247 si.setFields();
248
249 var co = new ContentObject(new Name(), si, bytes, new Signature());
250 co.sign();
251 var coBinary = encodeToBinaryContentObject(co);
252
Jeff Thompsonbd829262012-11-30 22:28:37 -0800253 //var nodename = unescape('%00%88%E2%F4%9C%91%16%16%D6%21%8E%A0c%95%A5%A6r%11%E0%A0%82%89%A6%A9%85%AB%D6%E2%065%DB%AF');
254 var nodename = this.ccndid;
255 var interestName = new Name(['ccnx', nodename, 'selfreg', coBinary]);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800256
257 var interest = new Interest(interestName);
258 interest.scope = 1;
Jeff Thompson5b265a72012-11-12 01:13:08 -0800259 var binaryInterest = encodeToBinaryInterest(interest);
Wentao Shangc05dc532012-11-19 12:00:33 -0800260 // If we directly use binaryInterest.buffer to feed ws.send(),
261 // WebSocket will end up sending a packet with 10000 bytes of data.
262 // That is, WebSocket will flush the entire buffer in BinaryXMLEncoder
263 // regardless of the offset of the Uint8Array. So we have to create
264 // a new Uint8Array buffer with just the right size and copy the
265 // content from binaryInterest to the new buffer.
266 // ---Wentao
Jeff Thompson5b265a72012-11-12 01:13:08 -0800267 var bytearray = new Uint8Array(binaryInterest.length);
268 bytearray.set(binaryInterest);
Jeff Thompson3c263812012-12-01 17:20:28 -0800269 if (LOG > 3) console.log('Send Interest registration packet.');
Jeff Thompson5b265a72012-11-12 01:13:08 -0800270
Jeff Thompson48ba4ff2012-11-12 01:23:13 -0800271 var csEntry = new CSEntry(name.getName(), closure);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800272 CSTable.push(csEntry);
273
274 this.ws.send(bytearray.buffer);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800275
276 return 0;
277 } else {
278 console.log('WebSocket connection is not established.');
279 return -1;
280 }
Jeff Thompsonbe85be62012-12-13 22:32:01 -0800281};
Jeff Thompson5b265a72012-11-12 01:13:08 -0800282