Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 1 | /** |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 2 | * @author: Wentao Shang |
| 3 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | var 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 Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 11 | this.bufferOffset = 0; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 12 | this.structureDecoder = new BinaryXMLStructureDecoder(); |
| 13 | }; |
| 14 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 15 | var ccndIdFetcher = '/%C1.M.S.localhost/%C1.M.SRV/ccnd/KEY'; |
| 16 | |
| 17 | WebSocketTransport.prototype.connectWebSocket = function(ndn) { |
| 18 | if (this.ws != null) |
| 19 | delete this.ws; |
| 20 | |
| 21 | this.ws = new WebSocket('ws://' + ndn.host + ':' + ndn.port); |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 22 | if (LOG > 0) console.log('ws connection created.'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 23 | |
| 24 | this.ws.binaryType = "arraybuffer"; |
| 25 | |
| 26 | var self = this; |
| 27 | this.ws.onmessage = function(ev) { |
| 28 | var result = ev.data; |
| 29 | //console.log('RecvHandle called.'); |
| 30 | |
| 31 | if(result == null || result == undefined || result == "" ) { |
| 32 | console.log('INVALID ANSWER'); |
| 33 | } else if (result instanceof ArrayBuffer) { |
| 34 | var bytearray = new Uint8Array(result); |
| 35 | |
Jeff Thompson | 13c2e7b | 2012-12-01 17:33:30 -0800 | [diff] [blame] | 36 | if (LOG>3) console.log('BINARY RESPONSE IS ' + DataUtils.toHex(bytearray)); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 37 | |
| 38 | try { |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 39 | if (bytearray.length + self.bufferOffset >= self.buffer.byteLength) { |
| 40 | if (LOG>3) { |
| 41 | console.log("NDN.ws.onmessage: buffer overflow. Accumulate received length: " + self.bufferOffset |
| 42 | + ". Current packet length: " + bytearray.length + "."); |
| 43 | } |
| 44 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 45 | // Purge and quit. |
| 46 | delete self.structureDecoder; |
| 47 | delete self.buffer; |
| 48 | self.structureDecoder = new BinaryXMLStructureDecoder(); |
| 49 | self.buffer = new Uint8Array(self.maxBufferSize); |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 50 | self.bufferOffset = 0; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 51 | return; |
| 52 | } |
| 53 | |
| 54 | /*for (var i = 0; i < bytearray.length; i++) { |
| 55 | self.buffer.push(bytearray[i]); |
| 56 | }*/ |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 57 | self.buffer.set(bytearray, self.bufferOffset); |
| 58 | self.bufferOffset += bytearray.length; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 59 | |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 60 | if (!self.structureDecoder.findElementEnd(self.buffer.subarray(0, self.bufferOffset))) { |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 61 | // Need more data to decode |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 62 | if (LOG>3) console.log('Incomplete packet received. Length ' + bytearray.length + '. Wait for more input.'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 63 | return; |
| 64 | } |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 65 | if (LOG>3) console.log('Complete packet received. Length ' + bytearray.length + '. Start decoding.'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 66 | } catch (ex) { |
| 67 | console.log("NDN.ws.onmessage exception: " + ex); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | var decoder = new BinaryXMLDecoder(self.buffer); |
| 72 | // Dispatch according to packet type |
| 73 | if (decoder.peekStartElement(CCNProtocolDTags.Interest)) { // Interest packet |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 74 | if (LOG > 3) console.log('Interest packet received.'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 75 | |
| 76 | var interest = new Interest(); |
| 77 | interest.from_ccnb(decoder); |
| 78 | if (LOG>3) console.log(interest); |
| 79 | var nameStr = escape(interest.name.getName()); |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 80 | if (LOG > 3) console.log(nameStr); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 81 | |
| 82 | var entry = getEntryForRegisteredPrefix(nameStr); |
| 83 | if (entry != null) { |
| 84 | //console.log(entry); |
| 85 | entry.closure.upcall(Closure.UPCALL_INTEREST, new UpcallInfo(ndn, interest, 0, null)); |
| 86 | } |
| 87 | |
| 88 | } else if (decoder.peekStartElement(CCNProtocolDTags.ContentObject)) { // Content packet |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 89 | if (LOG > 3) console.log('ContentObject packet received.'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 90 | |
| 91 | var co = new ContentObject(); |
| 92 | co.from_ccnb(decoder); |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 93 | if (LOG > 3) console.log(co); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 94 | nameStr = co.name.getName(); |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 95 | if (LOG > 3) console.log(nameStr); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 96 | |
| 97 | if (self.ccndid == null && nameStr.match(ccndIdFetcher) != null) { |
| 98 | // We are in starting phase, record publisherPublicKeyDigest in self.ccndid |
| 99 | if(!co.signedInfo || !co.signedInfo.publisher |
| 100 | || !co.signedInfo.publisher.publisherPublicKeyDigest) { |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 101 | console.log("Cannot contact router, close NDN now."); |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 102 | |
| 103 | // Close NDN if we fail to connect to a ccn router |
| 104 | ndn.readyStatus = NDN.CLOSED; |
| 105 | ndn.onclose(); |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 106 | //console.log("NDN.onclose event fired."); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 107 | } else { |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 108 | //console.log('Connected to ccnd.'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 109 | self.ccndid = co.signedInfo.publisher.publisherPublicKeyDigest; |
| 110 | if (LOG>3) console.log(self.ccndid); |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 111 | |
| 112 | // Call NDN.onopen after success |
| 113 | ndn.readyStatus = NDN.OPENED; |
| 114 | ndn.onopen(); |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 115 | //console.log("NDN.onopen event fired."); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 116 | } |
| 117 | } else { |
| 118 | var pitEntry = getEntryForExpressedInterest(nameStr); |
| 119 | if (pitEntry != null) { |
| 120 | //console.log(pitEntry); |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 121 | |
| 122 | // Cancel interest timer |
| 123 | clearTimeout(pitEntry.closure.timerID); |
| 124 | //console.log("Clear interest timer"); |
| 125 | //console.log(pitEntry.closure.timerID); |
Wentao Shang | bd63e46 | 2012-12-03 16:19:33 -0800 | [diff] [blame] | 126 | |
| 127 | // Remove PIT entry from PITTable |
Jeff Thompson | 53733fb | 2012-12-09 16:34:56 -0800 | [diff] [blame] | 128 | var index = PITTable.indexOf(pitEntry); |
Wentao Shang | bd63e46 | 2012-12-03 16:19:33 -0800 | [diff] [blame] | 129 | PITTable.splice(index, 1); |
| 130 | |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 131 | // Raise callback |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 132 | pitEntry.closure.upcall(Closure.UPCALL_CONTENT, new UpcallInfo(ndn, null, 0, co)); |
| 133 | } |
| 134 | } |
| 135 | } else { |
| 136 | console.log('Incoming packet is not Interest or ContentObject. Discard now.'); |
| 137 | } |
| 138 | |
| 139 | delete decoder; |
| 140 | |
| 141 | // Renew StrcutureDecoder and buffer after we process a full packet |
| 142 | delete self.structureDecoder; |
| 143 | delete self.buffer; |
| 144 | self.structureDecoder = new BinaryXMLStructureDecoder(); |
| 145 | self.buffer = new Uint8Array(self.maxBufferSize); |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 146 | self.bufferOffset = 0; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | this.ws.onopen = function(ev) { |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 151 | if (LOG > 3) console.log(ev); |
| 152 | if (LOG > 3) console.log('ws.onopen: WebSocket connection opened.'); |
| 153 | if (LOG > 3) console.log('ws.onopen: ReadyState: ' + this.readyState); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 154 | |
| 155 | // Fetch ccndid now |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 156 | var interest = new Interest(new Name(ccndIdFetcher)); |
Jeff Thompson | 84db263 | 2012-12-09 22:31:39 -0800 | [diff] [blame^] | 157 | interest.interestLifetime = 4.0; // seconds |
Jeff Thompson | 64db7c0 | 2012-12-09 21:56:57 -0800 | [diff] [blame] | 158 | var subarray = encodeToBinaryInterest(interest); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 159 | |
Jeff Thompson | 64db7c0 | 2012-12-09 21:56:57 -0800 | [diff] [blame] | 160 | var bytes = new Uint8Array(subarray.length); |
| 161 | bytes.set(subarray); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 162 | |
| 163 | self.ws.send(bytes.buffer); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | this.ws.onerror = function(ev) { |
| 167 | console.log('ws.onerror: ReadyState: ' + this.readyState); |
| 168 | console.log(ev); |
| 169 | console.log('ws.onerror: WebSocket error: ' + ev.data); |
| 170 | } |
| 171 | |
| 172 | this.ws.onclose = function(ev) { |
| 173 | console.log('ws.onclose: WebSocket connection closed.'); |
| 174 | self.ws = null; |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 175 | |
| 176 | // Close NDN when WebSocket is closed |
| 177 | ndn.readyStatus = NDN.CLOSED; |
| 178 | ndn.onclose(); |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 179 | //console.log("NDN.onclose event fired."); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | |
| 184 | // For fetching data |
| 185 | var PITTable = new Array(); |
| 186 | |
| 187 | var PITEntry = function PITEntry(interest, closure) { |
| 188 | this.interest = interest; // String |
| 189 | this.closure = closure; // Closure |
| 190 | } |
| 191 | |
| 192 | function getEntryForExpressedInterest(name) { |
| 193 | for (var i = 0; i < PITTable.length; i++) { |
| 194 | if (name.match(PITTable[i].interest) != null) |
| 195 | return PITTable[i]; |
| 196 | // TODO: handle multiple matching prefixes |
| 197 | } |
| 198 | return null; |
| 199 | } |
| 200 | |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 201 | WebSocketTransport.prototype.expressInterest = function(ndn, interest, closure) { |
| 202 | if (this.ws != null) { |
| 203 | //TODO: check local content store first |
| 204 | |
| 205 | var binaryInterest = encodeToBinaryInterest(interest); |
| 206 | var bytearray = new Uint8Array(binaryInterest.length); |
| 207 | bytearray.set(binaryInterest); |
| 208 | |
| 209 | var pitEntry = new PITEntry(interest.name.getName(), closure); |
| 210 | PITTable.push(pitEntry); |
| 211 | |
| 212 | this.ws.send(bytearray.buffer); |
| 213 | if (LOG > 3) console.log('ws.send() returned.'); |
| 214 | |
| 215 | // Set interest timer |
| 216 | closure.timerID = setTimeout(function() { |
Wentao Shang | bd63e46 | 2012-12-03 16:19:33 -0800 | [diff] [blame] | 217 | if (LOG > 3) console.log("Interest time out."); |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 218 | |
| 219 | // Remove PIT entry from PITTable |
Jeff Thompson | 53733fb | 2012-12-09 16:34:56 -0800 | [diff] [blame] | 220 | var index = PITTable.indexOf(pitEntry); |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 221 | //console.log(PITTable); |
| 222 | PITTable.splice(index, 1); |
| 223 | //console.log(PITTable); |
| 224 | // Raise closure callback |
| 225 | closure.upcall(Closure.UPCALL_INTEREST_TIMED_OUT, new UpcallInfo(ndn, interest, 0, null)); |
Jeff Thompson | 84db263 | 2012-12-09 22:31:39 -0800 | [diff] [blame^] | 226 | }, interest.interestLifetime * 1000); // convert interestLifetime from seconds to ms. |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 227 | //console.log(closure.timerID); |
| 228 | } |
| 229 | else |
| 230 | console.log('WebSocket connection is not established.'); |
| 231 | }; |
| 232 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 233 | |
| 234 | // For publishing data |
| 235 | var CSTable = new Array(); |
| 236 | |
| 237 | var CSEntry = function CSEntry(name, closure) { |
| 238 | this.name = name; // String |
| 239 | this.closure = closure; // Closure |
| 240 | } |
| 241 | |
| 242 | function getEntryForRegisteredPrefix(name) { |
| 243 | for (var i = 0; i < CSTable.length; i++) { |
| 244 | if (CSTable[i].name.match(name) != null) |
| 245 | return CSTable[i]; |
| 246 | } |
| 247 | return null; |
| 248 | } |
| 249 | |
| 250 | WebSocketTransport.prototype.registerPrefix = function(ndn, name, closure, flag) { |
| 251 | if (this.ws != null) { |
| 252 | if (this.ccndid == null) { |
| 253 | console.log('ccnd node ID unkonwn. Cannot register prefix.'); |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 254 | return -1; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Jeff Thompson | 48ba4ff | 2012-11-12 01:23:13 -0800 | [diff] [blame] | 257 | var fe = new ForwardingEntry('selfreg', name, null, null, 3, 2147483647); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 258 | var bytes = encodeForwardingEntry(fe); |
| 259 | |
| 260 | var si = new SignedInfo(); |
| 261 | si.setFields(); |
| 262 | |
| 263 | var co = new ContentObject(new Name(), si, bytes, new Signature()); |
| 264 | co.sign(); |
| 265 | var coBinary = encodeToBinaryContentObject(co); |
| 266 | |
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 267 | //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'); |
| 268 | var nodename = this.ccndid; |
| 269 | var interestName = new Name(['ccnx', nodename, 'selfreg', coBinary]); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 270 | |
| 271 | var interest = new Interest(interestName); |
| 272 | interest.scope = 1; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 273 | var binaryInterest = encodeToBinaryInterest(interest); |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 274 | // If we directly use binaryInterest.buffer to feed ws.send(), |
| 275 | // WebSocket will end up sending a packet with 10000 bytes of data. |
| 276 | // That is, WebSocket will flush the entire buffer in BinaryXMLEncoder |
| 277 | // regardless of the offset of the Uint8Array. So we have to create |
| 278 | // a new Uint8Array buffer with just the right size and copy the |
| 279 | // content from binaryInterest to the new buffer. |
| 280 | // ---Wentao |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 281 | var bytearray = new Uint8Array(binaryInterest.length); |
| 282 | bytearray.set(binaryInterest); |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 283 | if (LOG > 3) console.log('Send Interest registration packet.'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 284 | |
Jeff Thompson | 48ba4ff | 2012-11-12 01:23:13 -0800 | [diff] [blame] | 285 | var csEntry = new CSEntry(name.getName(), closure); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 286 | CSTable.push(csEntry); |
| 287 | |
| 288 | this.ws.send(bytearray.buffer); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 289 | |
| 290 | return 0; |
| 291 | } else { |
| 292 | console.log('WebSocket connection is not established.'); |
| 293 | return -1; |
| 294 | } |
| 295 | } |
| 296 | |