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(); |
Jeff Thompson | c881b55 | 2012-12-14 01:29:12 -0800 | [diff] [blame] | 13 | 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 Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 17 | }; |
| 18 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 19 | WebSocketTransport.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 Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 24 | if (LOG > 0) console.log('ws connection created.'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 25 | |
| 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 Thompson | 13c2e7b | 2012-12-01 17:33:30 -0800 | [diff] [blame] | 38 | if (LOG>3) console.log('BINARY RESPONSE IS ' + DataUtils.toHex(bytearray)); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 39 | |
| 40 | try { |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 41 | 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 Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 47 | // Purge and quit. |
| 48 | delete self.structureDecoder; |
| 49 | delete self.buffer; |
| 50 | self.structureDecoder = new BinaryXMLStructureDecoder(); |
| 51 | self.buffer = new Uint8Array(self.maxBufferSize); |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 52 | self.bufferOffset = 0; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 53 | return; |
| 54 | } |
| 55 | |
| 56 | /*for (var i = 0; i < bytearray.length; i++) { |
| 57 | self.buffer.push(bytearray[i]); |
| 58 | }*/ |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 59 | self.buffer.set(bytearray, self.bufferOffset); |
| 60 | self.bufferOffset += bytearray.length; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 61 | |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 62 | if (!self.structureDecoder.findElementEnd(self.buffer.subarray(0, self.bufferOffset))) { |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 63 | // Need more data to decode |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 64 | 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] | 65 | return; |
| 66 | } |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 67 | 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] | 68 | } 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 Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 76 | if (LOG > 3) console.log('Interest packet received.'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 77 | |
| 78 | var interest = new Interest(); |
| 79 | interest.from_ccnb(decoder); |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 80 | if (LOG > 3) console.log(interest); |
Wentao Shang | fddf90d | 2013-01-05 17:18:49 -0800 | [diff] [blame] | 81 | var nameStr = escape(interest.name.getName()); |
| 82 | if (LOG > 3) console.log(nameStr); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 83 | |
| 84 | var entry = getEntryForRegisteredPrefix(nameStr); |
| 85 | if (entry != null) { |
| 86 | //console.log(entry); |
Wentao Shang | ab9018d | 2012-12-18 11:35:45 -0800 | [diff] [blame] | 87 | var info = new UpcallInfo(ndn, interest, 0, null); |
| 88 | var ret = entry.closure.upcall(Closure.UPCALL_INTEREST, info); |
| 89 | if (ret == Closure.RESULT_INTEREST_CONSUMED && info.contentObject != null) { |
| 90 | var coBinary = encodeToBinaryContentObject(info.contentObject); |
| 91 | // If we directly use coBinary.buffer to feed ws.send(), WebSocket |
| 92 | // will end up sending a packet with 10000 bytes of data. That |
| 93 | // is, WebSocket will flush the entire buffer in BinaryXMLEncoder |
| 94 | // regardless of the offset of the Uint8Array. So we have to |
| 95 | // create a new Uint8Array buffer with just the right size and |
| 96 | // copy the content from coBinary to the new buffer. |
| 97 | // ---Wentao |
| 98 | var bytearray = new Uint8Array(coBinary.length); |
| 99 | bytearray.set(coBinary); |
| 100 | |
| 101 | self.ws.send(bytearray.buffer); |
| 102 | } |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | } else if (decoder.peekStartElement(CCNProtocolDTags.ContentObject)) { // Content packet |
Wentao Shang | 98b595c | 2012-12-30 10:14:26 -0800 | [diff] [blame] | 106 | if (LOG > 3) console.log('ContentObject packet received.'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 107 | |
| 108 | var co = new ContentObject(); |
| 109 | co.from_ccnb(decoder); |
Wentao Shang | fddf90d | 2013-01-05 17:18:49 -0800 | [diff] [blame] | 110 | //console.log(co); |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 111 | //var nameStr = co.name.getName(); |
Wentao Shang | 882e34e | 2013-01-05 02:49:51 -0800 | [diff] [blame] | 112 | //console.log(nameStr); |
Wentao Shang | f8b4a7d | 2012-12-25 12:52:07 -0800 | [diff] [blame] | 113 | |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 114 | if (self.ccndid == null && NDN.ccndIdFetcher.match(co.name)) { |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 115 | // We are in starting phase, record publisherPublicKeyDigest in self.ccndid |
| 116 | if(!co.signedInfo || !co.signedInfo.publisher |
| 117 | || !co.signedInfo.publisher.publisherPublicKeyDigest) { |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 118 | console.log("Cannot contact router, close NDN now."); |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 119 | |
| 120 | // Close NDN if we fail to connect to a ccn router |
| 121 | ndn.readyStatus = NDN.CLOSED; |
| 122 | ndn.onclose(); |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 123 | //console.log("NDN.onclose event fired."); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 124 | } else { |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 125 | //console.log('Connected to ccnd.'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 126 | self.ccndid = co.signedInfo.publisher.publisherPublicKeyDigest; |
| 127 | if (LOG>3) console.log(self.ccndid); |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 128 | |
| 129 | // Call NDN.onopen after success |
| 130 | ndn.readyStatus = NDN.OPENED; |
| 131 | ndn.onopen(); |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 132 | //console.log("NDN.onopen event fired."); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 133 | } |
| 134 | } else { |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 135 | var pitEntry = NDN.getEntryForExpressedInterest(co.name); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 136 | if (pitEntry != null) { |
| 137 | //console.log(pitEntry); |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 138 | // Remove PIT entry from NDN.PITTable |
| 139 | var index = NDN.PITTable.indexOf(pitEntry); |
Jeff Thompson | c881b55 | 2012-12-14 01:29:12 -0800 | [diff] [blame] | 140 | if (index >= 0) |
Wentao Shang | fa24596 | 2012-12-25 20:26:26 -0800 | [diff] [blame] | 141 | NDN.PITTable.splice(index, 1); |
Wentao Shang | bd63e46 | 2012-12-03 16:19:33 -0800 | [diff] [blame] | 142 | |
Wentao Shang | fa24596 | 2012-12-25 20:26:26 -0800 | [diff] [blame] | 143 | var currentClosure = pitEntry.closure; |
| 144 | |
| 145 | // Cancel interest timer |
| 146 | clearTimeout(currentClosure.timerID); |
| 147 | //console.log("Clear interest timer"); |
| 148 | //console.log(currentClosure.timerID); |
| 149 | |
| 150 | // Key verification |
Wentao Shang | fa24596 | 2012-12-25 20:26:26 -0800 | [diff] [blame] | 151 | |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 152 | // Recursive key fetching & verification closure |
Wentao Shang | 882e34e | 2013-01-05 02:49:51 -0800 | [diff] [blame] | 153 | var KeyFetchClosure = function KeyFetchClosure(content, closure, key, sig, wit) { |
Wentao Shang | fa24596 | 2012-12-25 20:26:26 -0800 | [diff] [blame] | 154 | this.contentObject = content; // unverified content object |
| 155 | this.closure = closure; // closure corresponding to the contentObject |
| 156 | this.keyName = key; // name of current key to be fetched |
Wentao Shang | 882e34e | 2013-01-05 02:49:51 -0800 | [diff] [blame] | 157 | this.sigHex = sig; // hex signature string to be verified |
| 158 | this.witness = wit; |
Wentao Shang | fa24596 | 2012-12-25 20:26:26 -0800 | [diff] [blame] | 159 | |
| 160 | Closure.call(this); |
| 161 | }; |
| 162 | |
| 163 | KeyFetchClosure.prototype.upcall = function(kind, upcallInfo) { |
| 164 | if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) { |
| 165 | console.log("In KeyFetchClosure.upcall: interest time out."); |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 166 | console.log(this.keyName.contentName.getName()); |
Wentao Shang | fa24596 | 2012-12-25 20:26:26 -0800 | [diff] [blame] | 167 | } else if (kind == Closure.UPCALL_CONTENT) { |
Wentao Shang | fddf90d | 2013-01-05 17:18:49 -0800 | [diff] [blame] | 168 | //console.log("In KeyFetchClosure.upcall: signature verification passed"); |
Wentao Shang | fa24596 | 2012-12-25 20:26:26 -0800 | [diff] [blame] | 169 | |
Wentao Shang | b5d0c3e | 2012-12-30 11:12:03 -0800 | [diff] [blame] | 170 | var rsakey = decodeSubjectPublicKeyInfo(upcallInfo.contentObject.content); |
Wentao Shang | 882e34e | 2013-01-05 02:49:51 -0800 | [diff] [blame] | 171 | var verified = rsakey.verifyByteArray(this.contentObject.rawSignatureData, this.witness, this.sigHex); |
Wentao Shang | fa24596 | 2012-12-25 20:26:26 -0800 | [diff] [blame] | 172 | |
Wentao Shang | 882e34e | 2013-01-05 02:49:51 -0800 | [diff] [blame] | 173 | var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD; |
Wentao Shang | 98b595c | 2012-12-30 10:14:26 -0800 | [diff] [blame] | 174 | //console.log("raise encapsulated closure"); |
Wentao Shang | fa24596 | 2012-12-25 20:26:26 -0800 | [diff] [blame] | 175 | this.closure.upcall(flag, new UpcallInfo(ndn, null, 0, this.contentObject)); |
Wentao Shang | b5d0c3e | 2012-12-30 11:12:03 -0800 | [diff] [blame] | 176 | |
| 177 | // Store key in cache |
| 178 | var keyEntry = new KeyStoreEntry(keylocator.keyName, rsakey, new Date().getTime()); |
| 179 | NDN.addKeyEntry(keyEntry); |
| 180 | //console.log(NDN.KeyStore); |
Wentao Shang | fddf90d | 2013-01-05 17:18:49 -0800 | [diff] [blame] | 181 | } else if (kind == Closure.UPCALL_CONTENT_BAD) { |
| 182 | console.log("In KeyFetchClosure.upcall: signature verification failed"); |
Wentao Shang | fa24596 | 2012-12-25 20:26:26 -0800 | [diff] [blame] | 183 | } |
| 184 | }; |
| 185 | |
| 186 | if (co.signedInfo && co.signedInfo.locator && co.signature) { |
| 187 | if (LOG > 3) console.log("Key verification..."); |
| 188 | var sigHex = DataUtils.toHex(co.signature.signature).toLowerCase(); |
| 189 | |
Wentao Shang | fddf90d | 2013-01-05 17:18:49 -0800 | [diff] [blame] | 190 | var wit = null; |
| 191 | if (co.signature.Witness != null) { |
| 192 | wit = new Witness(); |
| 193 | wit.decode(co.signature.Witness); |
| 194 | } |
| 195 | |
Wentao Shang | fa24596 | 2012-12-25 20:26:26 -0800 | [diff] [blame] | 196 | var keylocator = co.signedInfo.locator; |
| 197 | if (keylocator.type == KeyLocatorType.KEYNAME) { |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 198 | if (LOG > 3) console.log("KeyLocator contains KEYNAME"); |
| 199 | //var keyname = keylocator.keyName.contentName.getName(); |
| 200 | //console.log(nameStr); |
| 201 | //console.log(keyname); |
Wentao Shang | fa24596 | 2012-12-25 20:26:26 -0800 | [diff] [blame] | 202 | |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 203 | if (keylocator.keyName.contentName.match(co.name)) { |
| 204 | if (LOG > 3) console.log("Content is key itself"); |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 205 | |
Wentao Shang | b5d0c3e | 2012-12-30 11:12:03 -0800 | [diff] [blame] | 206 | var rsakey = decodeSubjectPublicKeyInfo(co.content); |
Wentao Shang | 882e34e | 2013-01-05 02:49:51 -0800 | [diff] [blame] | 207 | var verified = rsakey.verifyByteArray(co.rawSignatureData, wit, sigHex); |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 208 | var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD; |
| 209 | |
| 210 | currentClosure.upcall(flag, new UpcallInfo(ndn, null, 0, co)); |
| 211 | |
Wentao Shang | b5d0c3e | 2012-12-30 11:12:03 -0800 | [diff] [blame] | 212 | // SWT: We don't need to store key here since the same key will be |
| 213 | // stored again in the closure. |
| 214 | //var keyEntry = new KeyStoreEntry(keylocator.keyName, rsakey, new Date().getTime()); |
| 215 | //NDN.addKeyEntry(keyEntry); |
| 216 | //console.log(NDN.KeyStore); |
Wentao Shang | fa24596 | 2012-12-25 20:26:26 -0800 | [diff] [blame] | 217 | } else { |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 218 | // Check local key store |
| 219 | var keyEntry = NDN.getKeyByName(keylocator.keyName); |
| 220 | if (keyEntry) { |
| 221 | // Key found, verify now |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 222 | if (LOG > 3) console.log("Local key cache hit"); |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 223 | var rsakey = keyEntry.rsaKey; |
Wentao Shang | 882e34e | 2013-01-05 02:49:51 -0800 | [diff] [blame] | 224 | var verified = rsakey.verifyByteArray(co.rawSignatureData, wit, sigHex); |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 225 | var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD; |
| 226 | |
| 227 | // Raise callback |
Wentao Shang | b5d0c3e | 2012-12-30 11:12:03 -0800 | [diff] [blame] | 228 | currentClosure.upcall(flag, new UpcallInfo(ndn, null, 0, co)); |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 229 | } else { |
| 230 | // Not found, fetch now |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 231 | if (LOG > 3) console.log("Fetch key according to keylocator"); |
Wentao Shang | 882e34e | 2013-01-05 02:49:51 -0800 | [diff] [blame] | 232 | var nextClosure = new KeyFetchClosure(co, currentClosure, keylocator.keyName, sigHex, wit); |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 233 | var interest = new Interest(keylocator.keyName.contentName.getPrefix(4)); |
Jeff Thompson | b322ff8 | 2013-01-13 16:37:21 -0800 | [diff] [blame] | 234 | interest.interestLifetime = 4000; // milliseconds |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 235 | self.expressInterest(ndn, interest, nextClosure); |
| 236 | } |
Wentao Shang | fa24596 | 2012-12-25 20:26:26 -0800 | [diff] [blame] | 237 | } |
| 238 | } else if (keylocator.type == KeyLocatorType.KEY) { |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 239 | if (LOG > 3) console.log("Keylocator contains KEY"); |
Wentao Shang | fa24596 | 2012-12-25 20:26:26 -0800 | [diff] [blame] | 240 | |
Wentao Shang | fddf90d | 2013-01-05 17:18:49 -0800 | [diff] [blame] | 241 | var rsakey = decodeSubjectPublicKeyInfo(co.signedInfo.locator.publicKey); |
| 242 | var verified = rsakey.verifyByteArray(co.rawSignatureData, wit, sigHex); |
Wentao Shang | 882e34e | 2013-01-05 02:49:51 -0800 | [diff] [blame] | 243 | |
| 244 | var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD; |
Wentao Shang | fa24596 | 2012-12-25 20:26:26 -0800 | [diff] [blame] | 245 | // Raise callback |
| 246 | currentClosure.upcall(Closure.UPCALL_CONTENT, new UpcallInfo(ndn, null, 0, co)); |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 247 | |
Wentao Shang | b5d0c3e | 2012-12-30 11:12:03 -0800 | [diff] [blame] | 248 | // Since KeyLocator does not contain key name for this key, |
| 249 | // we have no way to store it as a key entry in KeyStore. |
Wentao Shang | fa24596 | 2012-12-25 20:26:26 -0800 | [diff] [blame] | 250 | } else { |
| 251 | var cert = keylocator.certificate; |
| 252 | console.log("KeyLocator contains CERT"); |
| 253 | console.log(cert); |
| 254 | |
| 255 | // TODO: verify certificate |
| 256 | } |
| 257 | } |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | } else { |
| 261 | console.log('Incoming packet is not Interest or ContentObject. Discard now.'); |
| 262 | } |
| 263 | |
| 264 | delete decoder; |
| 265 | |
| 266 | // Renew StrcutureDecoder and buffer after we process a full packet |
| 267 | delete self.structureDecoder; |
| 268 | delete self.buffer; |
| 269 | self.structureDecoder = new BinaryXMLStructureDecoder(); |
| 270 | self.buffer = new Uint8Array(self.maxBufferSize); |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 271 | self.bufferOffset = 0; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
| 275 | this.ws.onopen = function(ev) { |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 276 | if (LOG > 3) console.log(ev); |
| 277 | if (LOG > 3) console.log('ws.onopen: WebSocket connection opened.'); |
| 278 | if (LOG > 3) console.log('ws.onopen: ReadyState: ' + this.readyState); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 279 | |
| 280 | // Fetch ccndid now |
Wentao Shang | e0d7f05 | 2013-01-05 16:37:02 -0800 | [diff] [blame] | 281 | var interest = new Interest(NDN.ccndIdFetcher); |
Jeff Thompson | 42806a1 | 2012-12-29 18:19:39 -0800 | [diff] [blame] | 282 | interest.interestLifetime = 4000; // milliseconds |
Jeff Thompson | 64db7c0 | 2012-12-09 21:56:57 -0800 | [diff] [blame] | 283 | var subarray = encodeToBinaryInterest(interest); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 284 | |
Jeff Thompson | 64db7c0 | 2012-12-09 21:56:57 -0800 | [diff] [blame] | 285 | var bytes = new Uint8Array(subarray.length); |
| 286 | bytes.set(subarray); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 287 | |
| 288 | self.ws.send(bytes.buffer); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | this.ws.onerror = function(ev) { |
| 292 | console.log('ws.onerror: ReadyState: ' + this.readyState); |
| 293 | console.log(ev); |
| 294 | console.log('ws.onerror: WebSocket error: ' + ev.data); |
| 295 | } |
| 296 | |
| 297 | this.ws.onclose = function(ev) { |
| 298 | console.log('ws.onclose: WebSocket connection closed.'); |
| 299 | self.ws = null; |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 300 | |
| 301 | // Close NDN when WebSocket is closed |
| 302 | ndn.readyStatus = NDN.CLOSED; |
| 303 | ndn.onclose(); |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 304 | //console.log("NDN.onclose event fired."); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 305 | } |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 306 | }; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 307 | |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 308 | WebSocketTransport.prototype.expressInterest = function(ndn, interest, closure) { |
| 309 | if (this.ws != null) { |
| 310 | //TODO: check local content store first |
| 311 | |
| 312 | var binaryInterest = encodeToBinaryInterest(interest); |
| 313 | var bytearray = new Uint8Array(binaryInterest.length); |
| 314 | bytearray.set(binaryInterest); |
| 315 | |
Wentao Shang | dc5c29f | 2013-01-17 12:43:22 -0800 | [diff] [blame^] | 316 | if (closure != null) { |
| 317 | var pitEntry = new PITEntry(interest, closure); |
| 318 | NDN.PITTable.push(pitEntry); |
| 319 | } |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 320 | |
| 321 | this.ws.send(bytearray.buffer); |
| 322 | if (LOG > 3) console.log('ws.send() returned.'); |
| 323 | |
| 324 | // Set interest timer |
Wentao Shang | dc5c29f | 2013-01-17 12:43:22 -0800 | [diff] [blame^] | 325 | if (closure != null) { |
| 326 | closure.timerID = setTimeout(function() { |
| 327 | if (LOG > 3) console.log("Interest time out."); |
| 328 | |
| 329 | // Remove PIT entry from NDN.PITTable |
| 330 | var index = NDN.PITTable.indexOf(pitEntry); |
| 331 | //console.log(NDN.PITTable); |
| 332 | if (index >= 0) |
| 333 | NDN.PITTable.splice(index, 1); |
| 334 | //console.log(NDN.PITTable); |
| 335 | // Raise closure callback |
| 336 | closure.upcall(Closure.UPCALL_INTEREST_TIMED_OUT, new UpcallInfo(ndn, interest, 0, null)); |
| 337 | }, interest.interestLifetime); // interestLifetime is in milliseconds. |
| 338 | //console.log(closure.timerID); |
| 339 | } |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 340 | } |
| 341 | else |
| 342 | console.log('WebSocket connection is not established.'); |
| 343 | }; |
| 344 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 345 | |
| 346 | // For publishing data |
| 347 | var CSTable = new Array(); |
| 348 | |
| 349 | var CSEntry = function CSEntry(name, closure) { |
| 350 | this.name = name; // String |
| 351 | this.closure = closure; // Closure |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 352 | }; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 353 | |
| 354 | function getEntryForRegisteredPrefix(name) { |
| 355 | for (var i = 0; i < CSTable.length; i++) { |
| 356 | if (CSTable[i].name.match(name) != null) |
| 357 | return CSTable[i]; |
| 358 | } |
| 359 | return null; |
| 360 | } |
| 361 | |
| 362 | WebSocketTransport.prototype.registerPrefix = function(ndn, name, closure, flag) { |
| 363 | if (this.ws != null) { |
| 364 | if (this.ccndid == null) { |
| 365 | console.log('ccnd node ID unkonwn. Cannot register prefix.'); |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 366 | return -1; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Jeff Thompson | 48ba4ff | 2012-11-12 01:23:13 -0800 | [diff] [blame] | 369 | var fe = new ForwardingEntry('selfreg', name, null, null, 3, 2147483647); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 370 | var bytes = encodeForwardingEntry(fe); |
| 371 | |
| 372 | var si = new SignedInfo(); |
| 373 | si.setFields(); |
| 374 | |
| 375 | var co = new ContentObject(new Name(), si, bytes, new Signature()); |
| 376 | co.sign(); |
| 377 | var coBinary = encodeToBinaryContentObject(co); |
| 378 | |
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 379 | //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'); |
| 380 | var nodename = this.ccndid; |
| 381 | var interestName = new Name(['ccnx', nodename, 'selfreg', coBinary]); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 382 | |
| 383 | var interest = new Interest(interestName); |
| 384 | interest.scope = 1; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 385 | var binaryInterest = encodeToBinaryInterest(interest); |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 386 | // If we directly use binaryInterest.buffer to feed ws.send(), |
| 387 | // WebSocket will end up sending a packet with 10000 bytes of data. |
| 388 | // That is, WebSocket will flush the entire buffer in BinaryXMLEncoder |
| 389 | // regardless of the offset of the Uint8Array. So we have to create |
| 390 | // a new Uint8Array buffer with just the right size and copy the |
| 391 | // content from binaryInterest to the new buffer. |
| 392 | // ---Wentao |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 393 | var bytearray = new Uint8Array(binaryInterest.length); |
| 394 | bytearray.set(binaryInterest); |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 395 | if (LOG > 3) console.log('Send Interest registration packet.'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 396 | |
Jeff Thompson | 48ba4ff | 2012-11-12 01:23:13 -0800 | [diff] [blame] | 397 | var csEntry = new CSEntry(name.getName(), closure); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 398 | CSTable.push(csEntry); |
| 399 | |
| 400 | this.ws.send(bytearray.buffer); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 401 | |
| 402 | return 0; |
| 403 | } else { |
| 404 | console.log('WebSocket connection is not established.'); |
| 405 | return -1; |
| 406 | } |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 407 | }; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 408 | |