blob: 7c6406daf659edc0371950766b5f82dc51db6e53 [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();
13};
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
24 var self = this;
25 this.ws.onmessage = function(ev) {
26 var result = ev.data;
27 //console.log('RecvHandle called.');
28
29 if(result == null || result == undefined || result == "" ) {
30 console.log('INVALID ANSWER');
31 } else if (result instanceof ArrayBuffer) {
32 var bytearray = new Uint8Array(result);
33
Jeff Thompson13c2e7b2012-12-01 17:33:30 -080034 if (LOG>3) console.log('BINARY RESPONSE IS ' + DataUtils.toHex(bytearray));
Jeff Thompson5b265a72012-11-12 01:13:08 -080035
36 try {
Wentao Shang2b740e62012-12-07 00:02:53 -080037 if (bytearray.length + self.bufferOffset >= self.buffer.byteLength) {
38 if (LOG>3) {
39 console.log("NDN.ws.onmessage: buffer overflow. Accumulate received length: " + self.bufferOffset
40 + ". Current packet length: " + bytearray.length + ".");
41 }
42
Jeff Thompson5b265a72012-11-12 01:13:08 -080043 // Purge and quit.
44 delete self.structureDecoder;
45 delete self.buffer;
46 self.structureDecoder = new BinaryXMLStructureDecoder();
47 self.buffer = new Uint8Array(self.maxBufferSize);
Wentao Shang2b740e62012-12-07 00:02:53 -080048 self.bufferOffset = 0;
Jeff Thompson5b265a72012-11-12 01:13:08 -080049 return;
50 }
51
52 /*for (var i = 0; i < bytearray.length; i++) {
53 self.buffer.push(bytearray[i]);
54 }*/
Wentao Shang2b740e62012-12-07 00:02:53 -080055 self.buffer.set(bytearray, self.bufferOffset);
56 self.bufferOffset += bytearray.length;
Jeff Thompson5b265a72012-11-12 01:13:08 -080057
Wentao Shang2b740e62012-12-07 00:02:53 -080058 if (!self.structureDecoder.findElementEnd(self.buffer.subarray(0, self.bufferOffset))) {
Jeff Thompson5b265a72012-11-12 01:13:08 -080059 // Need more data to decode
Wentao Shang2b740e62012-12-07 00:02:53 -080060 if (LOG>3) console.log('Incomplete packet received. Length ' + bytearray.length + '. Wait for more input.');
Jeff Thompson5b265a72012-11-12 01:13:08 -080061 return;
62 }
Wentao Shang2b740e62012-12-07 00:02:53 -080063 if (LOG>3) console.log('Complete packet received. Length ' + bytearray.length + '. Start decoding.');
Jeff Thompson5b265a72012-11-12 01:13:08 -080064 } catch (ex) {
65 console.log("NDN.ws.onmessage exception: " + ex);
66 return;
67 }
68
69 var decoder = new BinaryXMLDecoder(self.buffer);
70 // Dispatch according to packet type
71 if (decoder.peekStartElement(CCNProtocolDTags.Interest)) { // Interest packet
Jeff Thompson3c263812012-12-01 17:20:28 -080072 if (LOG > 3) console.log('Interest packet received.');
Jeff Thompson5b265a72012-11-12 01:13:08 -080073
74 var interest = new Interest();
75 interest.from_ccnb(decoder);
76 if (LOG>3) console.log(interest);
77 var nameStr = escape(interest.name.getName());
Jeff Thompson3c263812012-12-01 17:20:28 -080078 if (LOG > 3) console.log(nameStr);
Jeff Thompson5b265a72012-11-12 01:13:08 -080079
80 var entry = getEntryForRegisteredPrefix(nameStr);
81 if (entry != null) {
82 //console.log(entry);
83 entry.closure.upcall(Closure.UPCALL_INTEREST, new UpcallInfo(ndn, interest, 0, null));
84 }
85
86 } else if (decoder.peekStartElement(CCNProtocolDTags.ContentObject)) { // Content packet
Jeff Thompson3c263812012-12-01 17:20:28 -080087 if (LOG > 3) console.log('ContentObject packet received.');
Jeff Thompson5b265a72012-11-12 01:13:08 -080088
89 var co = new ContentObject();
90 co.from_ccnb(decoder);
Jeff Thompson3c263812012-12-01 17:20:28 -080091 if (LOG > 3) console.log(co);
Jeff Thompson5b265a72012-11-12 01:13:08 -080092 nameStr = co.name.getName();
Jeff Thompson3c263812012-12-01 17:20:28 -080093 if (LOG > 3) console.log(nameStr);
Jeff Thompson5b265a72012-11-12 01:13:08 -080094
Jeff Thompsonbd25df22012-12-13 21:50:13 -080095 if (self.ccndid == null && nameStr.match(NDN.ccndIdFetcher) != null) {
Jeff Thompson5b265a72012-11-12 01:13:08 -080096 // We are in starting phase, record publisherPublicKeyDigest in self.ccndid
97 if(!co.signedInfo || !co.signedInfo.publisher
98 || !co.signedInfo.publisher.publisherPublicKeyDigest) {
Wentao Shangaf25c6b2012-12-03 00:09:30 -080099 console.log("Cannot contact router, close NDN now.");
Wentao Shang0e291c82012-12-02 23:36:29 -0800100
101 // Close NDN if we fail to connect to a ccn router
102 ndn.readyStatus = NDN.CLOSED;
103 ndn.onclose();
Wentao Shangaf25c6b2012-12-03 00:09:30 -0800104 //console.log("NDN.onclose event fired.");
Jeff Thompson5b265a72012-11-12 01:13:08 -0800105 } else {
Wentao Shangaf25c6b2012-12-03 00:09:30 -0800106 //console.log('Connected to ccnd.');
Jeff Thompson5b265a72012-11-12 01:13:08 -0800107 self.ccndid = co.signedInfo.publisher.publisherPublicKeyDigest;
108 if (LOG>3) console.log(self.ccndid);
Wentao Shang0e291c82012-12-02 23:36:29 -0800109
110 // Call NDN.onopen after success
111 ndn.readyStatus = NDN.OPENED;
112 ndn.onopen();
Wentao Shangaf25c6b2012-12-03 00:09:30 -0800113 //console.log("NDN.onopen event fired.");
Jeff Thompson5b265a72012-11-12 01:13:08 -0800114 }
115 } else {
116 var pitEntry = getEntryForExpressedInterest(nameStr);
117 if (pitEntry != null) {
118 //console.log(pitEntry);
Wentao Shangc0311e52012-12-03 10:38:23 -0800119
120 // Cancel interest timer
121 clearTimeout(pitEntry.closure.timerID);
122 //console.log("Clear interest timer");
123 //console.log(pitEntry.closure.timerID);
Wentao Shangbd63e462012-12-03 16:19:33 -0800124
125 // Remove PIT entry from PITTable
Jeff Thompson53733fb2012-12-09 16:34:56 -0800126 var index = PITTable.indexOf(pitEntry);
Wentao Shangbd63e462012-12-03 16:19:33 -0800127 PITTable.splice(index, 1);
128
Wentao Shangc0311e52012-12-03 10:38:23 -0800129 // Raise callback
Jeff Thompson5b265a72012-11-12 01:13:08 -0800130 pitEntry.closure.upcall(Closure.UPCALL_CONTENT, new UpcallInfo(ndn, null, 0, co));
131 }
132 }
133 } else {
134 console.log('Incoming packet is not Interest or ContentObject. Discard now.');
135 }
136
137 delete decoder;
138
139 // Renew StrcutureDecoder and buffer after we process a full packet
140 delete self.structureDecoder;
141 delete self.buffer;
142 self.structureDecoder = new BinaryXMLStructureDecoder();
143 self.buffer = new Uint8Array(self.maxBufferSize);
Wentao Shang2b740e62012-12-07 00:02:53 -0800144 self.bufferOffset = 0;
Jeff Thompson5b265a72012-11-12 01:13:08 -0800145 }
146 }
147
148 this.ws.onopen = function(ev) {
Jeff Thompson3c263812012-12-01 17:20:28 -0800149 if (LOG > 3) console.log(ev);
150 if (LOG > 3) console.log('ws.onopen: WebSocket connection opened.');
151 if (LOG > 3) console.log('ws.onopen: ReadyState: ' + this.readyState);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800152
153 // Fetch ccndid now
Jeff Thompsonbd25df22012-12-13 21:50:13 -0800154 var interest = new Interest(new Name(NDN.ccndIdFetcher));
Jeff Thompson84db2632012-12-09 22:31:39 -0800155 interest.interestLifetime = 4.0; // seconds
Jeff Thompson64db7c02012-12-09 21:56:57 -0800156 var subarray = encodeToBinaryInterest(interest);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800157
Jeff Thompson64db7c02012-12-09 21:56:57 -0800158 var bytes = new Uint8Array(subarray.length);
159 bytes.set(subarray);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800160
161 self.ws.send(bytes.buffer);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800162 }
163
164 this.ws.onerror = function(ev) {
165 console.log('ws.onerror: ReadyState: ' + this.readyState);
166 console.log(ev);
167 console.log('ws.onerror: WebSocket error: ' + ev.data);
168 }
169
170 this.ws.onclose = function(ev) {
171 console.log('ws.onclose: WebSocket connection closed.');
172 self.ws = null;
Wentao Shang0e291c82012-12-02 23:36:29 -0800173
174 // Close NDN when WebSocket is closed
175 ndn.readyStatus = NDN.CLOSED;
176 ndn.onclose();
Wentao Shangaf25c6b2012-12-03 00:09:30 -0800177 //console.log("NDN.onclose event fired.");
Jeff Thompson5b265a72012-11-12 01:13:08 -0800178 }
179}
180
181
182// For fetching data
183var PITTable = new Array();
184
185var PITEntry = function PITEntry(interest, closure) {
186 this.interest = interest; // String
187 this.closure = closure; // Closure
188}
189
190function getEntryForExpressedInterest(name) {
191 for (var i = 0; i < PITTable.length; i++) {
192 if (name.match(PITTable[i].interest) != null)
193 return PITTable[i];
194 // TODO: handle multiple matching prefixes
195 }
196 return null;
197}
198
Wentao Shangc0311e52012-12-03 10:38:23 -0800199WebSocketTransport.prototype.expressInterest = function(ndn, interest, closure) {
200 if (this.ws != null) {
201 //TODO: check local content store first
202
203 var binaryInterest = encodeToBinaryInterest(interest);
204 var bytearray = new Uint8Array(binaryInterest.length);
205 bytearray.set(binaryInterest);
206
207 var pitEntry = new PITEntry(interest.name.getName(), closure);
208 PITTable.push(pitEntry);
209
210 this.ws.send(bytearray.buffer);
211 if (LOG > 3) console.log('ws.send() returned.');
212
213 // Set interest timer
214 closure.timerID = setTimeout(function() {
Wentao Shangbd63e462012-12-03 16:19:33 -0800215 if (LOG > 3) console.log("Interest time out.");
Wentao Shangc0311e52012-12-03 10:38:23 -0800216
217 // Remove PIT entry from PITTable
Jeff Thompson53733fb2012-12-09 16:34:56 -0800218 var index = PITTable.indexOf(pitEntry);
Wentao Shangc0311e52012-12-03 10:38:23 -0800219 //console.log(PITTable);
220 PITTable.splice(index, 1);
221 //console.log(PITTable);
222 // Raise closure callback
223 closure.upcall(Closure.UPCALL_INTEREST_TIMED_OUT, new UpcallInfo(ndn, interest, 0, null));
Jeff Thompson84db2632012-12-09 22:31:39 -0800224 }, interest.interestLifetime * 1000); // convert interestLifetime from seconds to ms.
Wentao Shangc0311e52012-12-03 10:38:23 -0800225 //console.log(closure.timerID);
226 }
227 else
228 console.log('WebSocket connection is not established.');
229};
230
Jeff Thompson5b265a72012-11-12 01:13:08 -0800231
232// For publishing data
233var CSTable = new Array();
234
235var CSEntry = function CSEntry(name, closure) {
236 this.name = name; // String
237 this.closure = closure; // Closure
238}
239
240function getEntryForRegisteredPrefix(name) {
241 for (var i = 0; i < CSTable.length; i++) {
242 if (CSTable[i].name.match(name) != null)
243 return CSTable[i];
244 }
245 return null;
246}
247
248WebSocketTransport.prototype.registerPrefix = function(ndn, name, closure, flag) {
249 if (this.ws != null) {
250 if (this.ccndid == null) {
251 console.log('ccnd node ID unkonwn. Cannot register prefix.');
Jeff Thompson3c263812012-12-01 17:20:28 -0800252 return -1;
Jeff Thompson5b265a72012-11-12 01:13:08 -0800253 }
254
Jeff Thompson48ba4ff2012-11-12 01:23:13 -0800255 var fe = new ForwardingEntry('selfreg', name, null, null, 3, 2147483647);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800256 var bytes = encodeForwardingEntry(fe);
257
258 var si = new SignedInfo();
259 si.setFields();
260
261 var co = new ContentObject(new Name(), si, bytes, new Signature());
262 co.sign();
263 var coBinary = encodeToBinaryContentObject(co);
264
Jeff Thompsonbd829262012-11-30 22:28:37 -0800265 //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');
266 var nodename = this.ccndid;
267 var interestName = new Name(['ccnx', nodename, 'selfreg', coBinary]);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800268
269 var interest = new Interest(interestName);
270 interest.scope = 1;
Jeff Thompson5b265a72012-11-12 01:13:08 -0800271 var binaryInterest = encodeToBinaryInterest(interest);
Wentao Shangc05dc532012-11-19 12:00:33 -0800272 // If we directly use binaryInterest.buffer to feed ws.send(),
273 // WebSocket will end up sending a packet with 10000 bytes of data.
274 // That is, WebSocket will flush the entire buffer in BinaryXMLEncoder
275 // regardless of the offset of the Uint8Array. So we have to create
276 // a new Uint8Array buffer with just the right size and copy the
277 // content from binaryInterest to the new buffer.
278 // ---Wentao
Jeff Thompson5b265a72012-11-12 01:13:08 -0800279 var bytearray = new Uint8Array(binaryInterest.length);
280 bytearray.set(binaryInterest);
Jeff Thompson3c263812012-12-01 17:20:28 -0800281 if (LOG > 3) console.log('Send Interest registration packet.');
Jeff Thompson5b265a72012-11-12 01:13:08 -0800282
Jeff Thompson48ba4ff2012-11-12 01:23:13 -0800283 var csEntry = new CSEntry(name.getName(), closure);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800284 CSTable.push(csEntry);
285
286 this.ws.send(bytearray.buffer);
Jeff Thompson5b265a72012-11-12 01:13:08 -0800287
288 return 0;
289 } else {
290 console.log('WebSocket connection is not established.');
291 return -1;
292 }
293}
294