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 | WebSocketTransport.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 Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 20 | if (LOG > 0) console.log('ws connection created.'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 21 | |
| 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 Thompson | 13c2e7b | 2012-12-01 17:33:30 -0800 | [diff] [blame] | 34 | if (LOG>3) console.log('BINARY RESPONSE IS ' + DataUtils.toHex(bytearray)); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 35 | |
| 36 | try { |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 37 | 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 Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 43 | // Purge and quit. |
| 44 | delete self.structureDecoder; |
| 45 | delete self.buffer; |
| 46 | self.structureDecoder = new BinaryXMLStructureDecoder(); |
| 47 | self.buffer = new Uint8Array(self.maxBufferSize); |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 48 | self.bufferOffset = 0; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 49 | return; |
| 50 | } |
| 51 | |
| 52 | /*for (var i = 0; i < bytearray.length; i++) { |
| 53 | self.buffer.push(bytearray[i]); |
| 54 | }*/ |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 55 | self.buffer.set(bytearray, self.bufferOffset); |
| 56 | self.bufferOffset += bytearray.length; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 57 | |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 58 | if (!self.structureDecoder.findElementEnd(self.buffer.subarray(0, self.bufferOffset))) { |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 59 | // Need more data to decode |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 60 | 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] | 61 | return; |
| 62 | } |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 63 | 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] | 64 | } 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 Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 72 | if (LOG > 3) console.log('Interest packet received.'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 73 | |
| 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 Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 78 | if (LOG > 3) console.log(nameStr); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 79 | |
| 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 Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 87 | if (LOG > 3) console.log('ContentObject packet received.'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 88 | |
| 89 | var co = new ContentObject(); |
| 90 | co.from_ccnb(decoder); |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 91 | if (LOG > 3) console.log(co); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 92 | nameStr = co.name.getName(); |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 93 | if (LOG > 3) console.log(nameStr); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 94 | |
Jeff Thompson | bd25df2 | 2012-12-13 21:50:13 -0800 | [diff] [blame^] | 95 | if (self.ccndid == null && nameStr.match(NDN.ccndIdFetcher) != null) { |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 96 | // We are in starting phase, record publisherPublicKeyDigest in self.ccndid |
| 97 | if(!co.signedInfo || !co.signedInfo.publisher |
| 98 | || !co.signedInfo.publisher.publisherPublicKeyDigest) { |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 99 | console.log("Cannot contact router, close NDN now."); |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 100 | |
| 101 | // Close NDN if we fail to connect to a ccn router |
| 102 | ndn.readyStatus = NDN.CLOSED; |
| 103 | ndn.onclose(); |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 104 | //console.log("NDN.onclose event fired."); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 105 | } else { |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 106 | //console.log('Connected to ccnd.'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 107 | self.ccndid = co.signedInfo.publisher.publisherPublicKeyDigest; |
| 108 | if (LOG>3) console.log(self.ccndid); |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 109 | |
| 110 | // Call NDN.onopen after success |
| 111 | ndn.readyStatus = NDN.OPENED; |
| 112 | ndn.onopen(); |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 113 | //console.log("NDN.onopen event fired."); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 114 | } |
| 115 | } else { |
| 116 | var pitEntry = getEntryForExpressedInterest(nameStr); |
| 117 | if (pitEntry != null) { |
| 118 | //console.log(pitEntry); |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 119 | |
| 120 | // Cancel interest timer |
| 121 | clearTimeout(pitEntry.closure.timerID); |
| 122 | //console.log("Clear interest timer"); |
| 123 | //console.log(pitEntry.closure.timerID); |
Wentao Shang | bd63e46 | 2012-12-03 16:19:33 -0800 | [diff] [blame] | 124 | |
| 125 | // Remove PIT entry from PITTable |
Jeff Thompson | 53733fb | 2012-12-09 16:34:56 -0800 | [diff] [blame] | 126 | var index = PITTable.indexOf(pitEntry); |
Wentao Shang | bd63e46 | 2012-12-03 16:19:33 -0800 | [diff] [blame] | 127 | PITTable.splice(index, 1); |
| 128 | |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 129 | // Raise callback |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 130 | 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 Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 144 | self.bufferOffset = 0; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | this.ws.onopen = function(ev) { |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 149 | 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 Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 152 | |
| 153 | // Fetch ccndid now |
Jeff Thompson | bd25df2 | 2012-12-13 21:50:13 -0800 | [diff] [blame^] | 154 | var interest = new Interest(new Name(NDN.ccndIdFetcher)); |
Jeff Thompson | 84db263 | 2012-12-09 22:31:39 -0800 | [diff] [blame] | 155 | interest.interestLifetime = 4.0; // seconds |
Jeff Thompson | 64db7c0 | 2012-12-09 21:56:57 -0800 | [diff] [blame] | 156 | var subarray = encodeToBinaryInterest(interest); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 157 | |
Jeff Thompson | 64db7c0 | 2012-12-09 21:56:57 -0800 | [diff] [blame] | 158 | var bytes = new Uint8Array(subarray.length); |
| 159 | bytes.set(subarray); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 160 | |
| 161 | self.ws.send(bytes.buffer); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 162 | } |
| 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 Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 173 | |
| 174 | // Close NDN when WebSocket is closed |
| 175 | ndn.readyStatus = NDN.CLOSED; |
| 176 | ndn.onclose(); |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 177 | //console.log("NDN.onclose event fired."); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | |
| 182 | // For fetching data |
| 183 | var PITTable = new Array(); |
| 184 | |
| 185 | var PITEntry = function PITEntry(interest, closure) { |
| 186 | this.interest = interest; // String |
| 187 | this.closure = closure; // Closure |
| 188 | } |
| 189 | |
| 190 | function 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 Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 199 | WebSocketTransport.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 Shang | bd63e46 | 2012-12-03 16:19:33 -0800 | [diff] [blame] | 215 | if (LOG > 3) console.log("Interest time out."); |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 216 | |
| 217 | // Remove PIT entry from PITTable |
Jeff Thompson | 53733fb | 2012-12-09 16:34:56 -0800 | [diff] [blame] | 218 | var index = PITTable.indexOf(pitEntry); |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 219 | //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 Thompson | 84db263 | 2012-12-09 22:31:39 -0800 | [diff] [blame] | 224 | }, interest.interestLifetime * 1000); // convert interestLifetime from seconds to ms. |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 225 | //console.log(closure.timerID); |
| 226 | } |
| 227 | else |
| 228 | console.log('WebSocket connection is not established.'); |
| 229 | }; |
| 230 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 231 | |
| 232 | // For publishing data |
| 233 | var CSTable = new Array(); |
| 234 | |
| 235 | var CSEntry = function CSEntry(name, closure) { |
| 236 | this.name = name; // String |
| 237 | this.closure = closure; // Closure |
| 238 | } |
| 239 | |
| 240 | function 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 | |
| 248 | WebSocketTransport.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 Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 252 | return -1; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Jeff Thompson | 48ba4ff | 2012-11-12 01:23:13 -0800 | [diff] [blame] | 255 | var fe = new ForwardingEntry('selfreg', name, null, null, 3, 2147483647); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 256 | 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 Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 265 | //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 Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 268 | |
| 269 | var interest = new Interest(interestName); |
| 270 | interest.scope = 1; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 271 | var binaryInterest = encodeToBinaryInterest(interest); |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 272 | // 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 Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 279 | var bytearray = new Uint8Array(binaryInterest.length); |
| 280 | bytearray.set(binaryInterest); |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 281 | if (LOG > 3) console.log('Send Interest registration packet.'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 282 | |
Jeff Thompson | 48ba4ff | 2012-11-12 01:23:13 -0800 | [diff] [blame] | 283 | var csEntry = new CSEntry(name.getName(), closure); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 284 | CSTable.push(csEntry); |
| 285 | |
| 286 | this.ws.send(bytearray.buffer); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 287 | |
| 288 | return 0; |
| 289 | } else { |
| 290 | console.log('WebSocket connection is not established.'); |
| 291 | return -1; |
| 292 | } |
| 293 | } |
| 294 | |