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); |
| 80 | if (LOG>3) console.log(interest); |
| 81 | var nameStr = escape(interest.name.getName()); |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 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 |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -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); |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 110 | if (LOG > 3) console.log(co); |
Wentao Shang | f8b4a7d | 2012-12-25 12:52:07 -0800 | [diff] [blame^] | 111 | var nameStr = co.name.getName(); |
| 112 | console.log(nameStr); |
| 113 | |
| 114 | // Key verification |
| 115 | if (co.signedInfo && co.signature) { |
| 116 | if (LOG > 3) console.log("Key verification..."); |
| 117 | var signature = DataUtils.toHex(co.signature.signature).toLowerCase(); |
| 118 | |
| 119 | var keylocator = co.signedInfo.locator; |
| 120 | if (keylocator.type == KeyLocatorType.KEYNAME) { |
| 121 | console.log("KeyLocator contains KEYNAME"); |
| 122 | var keyname = keylocator.keyName.contentName.getName(); |
| 123 | console.log(keyname); |
| 124 | } else if (keylocator.type == KeyLocatorType.KEY) { |
| 125 | console.log("Keylocator contains KEY"); |
| 126 | var publickeyHex = DataUtils.toHex(co.signedInfo.locator.publicKey).toLowerCase(); |
| 127 | |
| 128 | var kp = publickeyHex.slice(56, 314); |
| 129 | var exp = publickeyHex.slice(318, 324); |
| 130 | |
| 131 | var rsakey = new RSAKey(); |
| 132 | rsakey.setPublic(kp, exp); |
| 133 | var result = rsakey.verifyByteArray(co.rawSignatureData, signature); |
| 134 | if (result) |
| 135 | console.log('SIGNATURE VALID'); |
| 136 | else |
| 137 | console.log('SIGNATURE INVALID'); |
| 138 | } else { |
| 139 | var cert = keylocator.certificate; |
| 140 | console.log("KeyLocator contains CERT"); |
| 141 | console.log(cert); |
| 142 | } |
| 143 | } |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 144 | |
Jeff Thompson | bd25df2 | 2012-12-13 21:50:13 -0800 | [diff] [blame] | 145 | if (self.ccndid == null && nameStr.match(NDN.ccndIdFetcher) != null) { |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 146 | // We are in starting phase, record publisherPublicKeyDigest in self.ccndid |
| 147 | if(!co.signedInfo || !co.signedInfo.publisher |
| 148 | || !co.signedInfo.publisher.publisherPublicKeyDigest) { |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 149 | console.log("Cannot contact router, close NDN now."); |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 150 | |
| 151 | // Close NDN if we fail to connect to a ccn router |
| 152 | ndn.readyStatus = NDN.CLOSED; |
| 153 | ndn.onclose(); |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 154 | //console.log("NDN.onclose event fired."); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 155 | } else { |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 156 | //console.log('Connected to ccnd.'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 157 | self.ccndid = co.signedInfo.publisher.publisherPublicKeyDigest; |
| 158 | if (LOG>3) console.log(self.ccndid); |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 159 | |
| 160 | // Call NDN.onopen after success |
| 161 | ndn.readyStatus = NDN.OPENED; |
| 162 | ndn.onopen(); |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 163 | //console.log("NDN.onopen event fired."); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 164 | } |
| 165 | } else { |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 166 | var pitEntry = NDN.getEntryForExpressedInterest(co.name); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 167 | if (pitEntry != null) { |
| 168 | //console.log(pitEntry); |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 169 | |
| 170 | // Cancel interest timer |
| 171 | clearTimeout(pitEntry.closure.timerID); |
| 172 | //console.log("Clear interest timer"); |
| 173 | //console.log(pitEntry.closure.timerID); |
Wentao Shang | bd63e46 | 2012-12-03 16:19:33 -0800 | [diff] [blame] | 174 | |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 175 | // Remove PIT entry from NDN.PITTable |
| 176 | var index = NDN.PITTable.indexOf(pitEntry); |
Jeff Thompson | c881b55 | 2012-12-14 01:29:12 -0800 | [diff] [blame] | 177 | if (index >= 0) |
| 178 | NDN.PITTable.splice(index, 1); |
Wentao Shang | bd63e46 | 2012-12-03 16:19:33 -0800 | [diff] [blame] | 179 | |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 180 | // Raise callback |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 181 | pitEntry.closure.upcall(Closure.UPCALL_CONTENT, new UpcallInfo(ndn, null, 0, co)); |
| 182 | } |
| 183 | } |
| 184 | } else { |
| 185 | console.log('Incoming packet is not Interest or ContentObject. Discard now.'); |
| 186 | } |
| 187 | |
| 188 | delete decoder; |
| 189 | |
| 190 | // Renew StrcutureDecoder and buffer after we process a full packet |
| 191 | delete self.structureDecoder; |
| 192 | delete self.buffer; |
| 193 | self.structureDecoder = new BinaryXMLStructureDecoder(); |
| 194 | self.buffer = new Uint8Array(self.maxBufferSize); |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 195 | self.bufferOffset = 0; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | this.ws.onopen = function(ev) { |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 200 | if (LOG > 3) console.log(ev); |
| 201 | if (LOG > 3) console.log('ws.onopen: WebSocket connection opened.'); |
| 202 | if (LOG > 3) console.log('ws.onopen: ReadyState: ' + this.readyState); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 203 | |
| 204 | // Fetch ccndid now |
Jeff Thompson | bd25df2 | 2012-12-13 21:50:13 -0800 | [diff] [blame] | 205 | var interest = new Interest(new Name(NDN.ccndIdFetcher)); |
Jeff Thompson | 84db263 | 2012-12-09 22:31:39 -0800 | [diff] [blame] | 206 | interest.interestLifetime = 4.0; // seconds |
Jeff Thompson | 64db7c0 | 2012-12-09 21:56:57 -0800 | [diff] [blame] | 207 | var subarray = encodeToBinaryInterest(interest); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 208 | |
Jeff Thompson | 64db7c0 | 2012-12-09 21:56:57 -0800 | [diff] [blame] | 209 | var bytes = new Uint8Array(subarray.length); |
| 210 | bytes.set(subarray); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 211 | |
| 212 | self.ws.send(bytes.buffer); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | this.ws.onerror = function(ev) { |
| 216 | console.log('ws.onerror: ReadyState: ' + this.readyState); |
| 217 | console.log(ev); |
| 218 | console.log('ws.onerror: WebSocket error: ' + ev.data); |
| 219 | } |
| 220 | |
| 221 | this.ws.onclose = function(ev) { |
| 222 | console.log('ws.onclose: WebSocket connection closed.'); |
| 223 | self.ws = null; |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 224 | |
| 225 | // Close NDN when WebSocket is closed |
| 226 | ndn.readyStatus = NDN.CLOSED; |
| 227 | ndn.onclose(); |
Wentao Shang | af25c6b | 2012-12-03 00:09:30 -0800 | [diff] [blame] | 228 | //console.log("NDN.onclose event fired."); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 229 | } |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 230 | }; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 231 | |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 232 | WebSocketTransport.prototype.expressInterest = function(ndn, interest, closure) { |
| 233 | if (this.ws != null) { |
| 234 | //TODO: check local content store first |
| 235 | |
| 236 | var binaryInterest = encodeToBinaryInterest(interest); |
| 237 | var bytearray = new Uint8Array(binaryInterest.length); |
| 238 | bytearray.set(binaryInterest); |
| 239 | |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 240 | var pitEntry = new PITEntry(interest, closure); |
| 241 | NDN.PITTable.push(pitEntry); |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 242 | |
| 243 | this.ws.send(bytearray.buffer); |
| 244 | if (LOG > 3) console.log('ws.send() returned.'); |
| 245 | |
| 246 | // Set interest timer |
| 247 | closure.timerID = setTimeout(function() { |
Wentao Shang | bd63e46 | 2012-12-03 16:19:33 -0800 | [diff] [blame] | 248 | if (LOG > 3) console.log("Interest time out."); |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 249 | |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 250 | // Remove PIT entry from NDN.PITTable |
| 251 | var index = NDN.PITTable.indexOf(pitEntry); |
| 252 | //console.log(NDN.PITTable); |
Jeff Thompson | c881b55 | 2012-12-14 01:29:12 -0800 | [diff] [blame] | 253 | if (index >= 0) |
| 254 | NDN.PITTable.splice(index, 1); |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 255 | //console.log(NDN.PITTable); |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 256 | // Raise closure callback |
| 257 | 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] | 258 | }, interest.interestLifetime * 1000); // convert interestLifetime from seconds to ms. |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 259 | //console.log(closure.timerID); |
| 260 | } |
| 261 | else |
| 262 | console.log('WebSocket connection is not established.'); |
| 263 | }; |
| 264 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 265 | |
| 266 | // For publishing data |
| 267 | var CSTable = new Array(); |
| 268 | |
| 269 | var CSEntry = function CSEntry(name, closure) { |
| 270 | this.name = name; // String |
| 271 | this.closure = closure; // Closure |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 272 | }; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 273 | |
| 274 | function getEntryForRegisteredPrefix(name) { |
| 275 | for (var i = 0; i < CSTable.length; i++) { |
| 276 | if (CSTable[i].name.match(name) != null) |
| 277 | return CSTable[i]; |
| 278 | } |
| 279 | return null; |
| 280 | } |
| 281 | |
| 282 | WebSocketTransport.prototype.registerPrefix = function(ndn, name, closure, flag) { |
| 283 | if (this.ws != null) { |
| 284 | if (this.ccndid == null) { |
| 285 | console.log('ccnd node ID unkonwn. Cannot register prefix.'); |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 286 | return -1; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Jeff Thompson | 48ba4ff | 2012-11-12 01:23:13 -0800 | [diff] [blame] | 289 | var fe = new ForwardingEntry('selfreg', name, null, null, 3, 2147483647); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 290 | var bytes = encodeForwardingEntry(fe); |
| 291 | |
| 292 | var si = new SignedInfo(); |
| 293 | si.setFields(); |
| 294 | |
| 295 | var co = new ContentObject(new Name(), si, bytes, new Signature()); |
| 296 | co.sign(); |
| 297 | var coBinary = encodeToBinaryContentObject(co); |
| 298 | |
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 299 | //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'); |
| 300 | var nodename = this.ccndid; |
| 301 | var interestName = new Name(['ccnx', nodename, 'selfreg', coBinary]); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 302 | |
| 303 | var interest = new Interest(interestName); |
| 304 | interest.scope = 1; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 305 | var binaryInterest = encodeToBinaryInterest(interest); |
Wentao Shang | c05dc53 | 2012-11-19 12:00:33 -0800 | [diff] [blame] | 306 | // If we directly use binaryInterest.buffer to feed ws.send(), |
| 307 | // WebSocket will end up sending a packet with 10000 bytes of data. |
| 308 | // That is, WebSocket will flush the entire buffer in BinaryXMLEncoder |
| 309 | // regardless of the offset of the Uint8Array. So we have to create |
| 310 | // a new Uint8Array buffer with just the right size and copy the |
| 311 | // content from binaryInterest to the new buffer. |
| 312 | // ---Wentao |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 313 | var bytearray = new Uint8Array(binaryInterest.length); |
| 314 | bytearray.set(binaryInterest); |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 315 | if (LOG > 3) console.log('Send Interest registration packet.'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 316 | |
Jeff Thompson | 48ba4ff | 2012-11-12 01:23:13 -0800 | [diff] [blame] | 317 | var csEntry = new CSEntry(name.getName(), closure); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 318 | CSTable.push(csEntry); |
| 319 | |
| 320 | this.ws.send(bytearray.buffer); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 321 | |
| 322 | return 0; |
| 323 | } else { |
| 324 | console.log('WebSocket connection is not established.'); |
| 325 | return -1; |
| 326 | } |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 327 | }; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 328 | |