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: Meki Cherkaoui, Jeff Thompson, Wentao Shang |
Jeff Thompson | 745026e | 2012-10-13 12:49:20 -0700 | [diff] [blame] | 3 | * See COPYING for copyright and distribution information. |
| 4 | * This class represents the top-level object for communicating with an NDN host. |
| 5 | */ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 6 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 7 | /** |
| 8 | * Set this to a higher number to dump more debugging log messages. |
| 9 | * @type Number |
| 10 | */ |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 11 | var LOG = 0; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 12 | |
Jeff Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 13 | /** |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 14 | * Create a new NDN with the given settings. |
| 15 | * This throws an exception if NDN.supported is false. |
| 16 | * @constructor |
| 17 | * @param {Object} settings if not null, an associative array with the following defaults: |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 18 | * { |
Jeff Thompson | ac0ed60 | 2013-02-18 21:52:42 -0800 | [diff] [blame] | 19 | * getTransport: function() { return new WebSocketTransport(); }, |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 20 | * getHostAndPort: transport.defaultGetHostAndPort, // a function, on each call it returns a new { host: host, port: port } or null if there are no more hosts. |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 21 | * host: null, // If null, use getHostAndPort when connecting. |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 22 | * port: 9696, |
Jeff Thompson | ac0ed60 | 2013-02-18 21:52:42 -0800 | [diff] [blame] | 23 | * onopen: function() { if (LOG > 3) console.log("NDN connection established."); }, |
| 24 | * onclose: function() { if (LOG > 3) console.log("NDN connection closed."); }, |
| 25 | * verify: true // If false, don't verify and call upcall with Closure.UPCALL_CONTENT_UNVERIFIED. |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 26 | * } |
Jeff Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 27 | */ |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 28 | var NDN = function NDN(settings) { |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 29 | if (!NDN.supported) |
| 30 | throw new Error("The necessary JavaScript support is not available on this platform."); |
Jeff Thompson | e2f7b6d | 2013-02-24 20:37:27 -0800 | [diff] [blame] | 31 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 32 | settings = (settings || {}); |
| 33 | var getTransport = (settings.getTransport || function() { return new WebSocketTransport(); }); |
| 34 | this.transport = getTransport(); |
| 35 | this.getHostAndPort = (settings.getHostAndPort || this.transport.defaultGetHostAndPort); |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 36 | this.host = (settings.host !== undefined ? settings.host : null); |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 37 | this.port = (settings.port || 9696); |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 38 | this.readyStatus = NDN.UNOPEN; |
| 39 | this.verify = (settings.verify !== undefined ? settings.verify : true); |
| 40 | // Event handler |
| 41 | this.onopen = (settings.onopen || function() { if (LOG > 3) console.log("NDN connection established."); }); |
| 42 | this.onclose = (settings.onclose || function() { if (LOG > 3) console.log("NDN connection closed."); }); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 43 | this.ccndid = null; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 46 | NDN.UNOPEN = 0; // created but not opened yet |
| 47 | NDN.OPENED = 1; // connection to ccnd opened |
| 48 | NDN.CLOSED = 2; // connection to ccnd closed |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 49 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 50 | /** |
Jeff Thompson | e2f7b6d | 2013-02-24 20:37:27 -0800 | [diff] [blame] | 51 | * Return true if necessary JavaScript support is available, else log an error and return false. |
| 52 | */ |
| 53 | NDN.getSupported = function() { |
| 54 | try { |
| 55 | var dummy = new Uint8Array(1).subarray(0, 1); |
| 56 | } catch (ex) { |
| 57 | console.log("NDN not available: Uint8Array not supported. " + ex); |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | return true; |
| 62 | }; |
| 63 | |
| 64 | NDN.supported = NDN.getSupported(); |
| 65 | |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 66 | NDN.ccndIdFetcher = new Name('/%C1.M.S.localhost/%C1.M.SRV/ccnd/KEY'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 67 | |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 68 | NDN.prototype.createRoute = function(host, port) { |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 69 | this.host=host; |
| 70 | this.port=port; |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 71 | }; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 72 | |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 73 | |
| 74 | NDN.KeyStore = new Array(); |
| 75 | |
Wentao Shang | b5d0c3e | 2012-12-30 11:12:03 -0800 | [diff] [blame] | 76 | var KeyStoreEntry = function KeyStoreEntry(name, rsa, time) { |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 77 | this.keyName = name; // KeyName |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 78 | this.rsaKey = rsa; // RSA key |
Wentao Shang | b5d0c3e | 2012-12-30 11:12:03 -0800 | [diff] [blame] | 79 | this.timeStamp = time; // Time Stamp |
| 80 | }; |
| 81 | |
| 82 | NDN.addKeyEntry = function(/* KeyStoreEntry */ keyEntry) { |
| 83 | var result = NDN.getKeyByName(keyEntry.keyName); |
| 84 | if (result == null) |
| 85 | NDN.KeyStore.push(keyEntry); |
| 86 | else |
| 87 | result = keyEntry; |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | NDN.getKeyByName = function(/* KeyName */ name) { |
| 91 | var result = null; |
| 92 | |
| 93 | for (var i = 0; i < NDN.KeyStore.length; i++) { |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 94 | if (NDN.KeyStore[i].keyName.contentName.match(name.contentName)) { |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 95 | if (result == null || |
| 96 | NDN.KeyStore[i].keyName.contentName.components.length > result.keyName.contentName.components.length) |
| 97 | result = NDN.KeyStore[i]; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return result; |
| 102 | }; |
| 103 | |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 104 | // For fetching data |
| 105 | NDN.PITTable = new Array(); |
| 106 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 107 | /** |
| 108 | * @constructor |
| 109 | */ |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 110 | var PITEntry = function PITEntry(interest, closure) { |
| 111 | this.interest = interest; // Interest |
| 112 | this.closure = closure; // Closure |
Wentao Shang | fcb1626 | 2013-01-20 14:42:46 -0800 | [diff] [blame] | 113 | this.timerID = -1; // Timer ID |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 114 | }; |
| 115 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 116 | /** |
Jeff Thompson | 202728a | 2013-02-10 22:20:08 -0800 | [diff] [blame] | 117 | * Return the entry from NDN.PITTable where the name conforms to the interest selectors, and |
| 118 | * the interest name is the longest that matches name. |
| 119 | */ |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 120 | NDN.getEntryForExpressedInterest = function(/*Name*/ name) { |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 121 | var result = null; |
| 122 | |
| 123 | for (var i = 0; i < NDN.PITTable.length; i++) { |
| 124 | if (NDN.PITTable[i].interest.matches_name(name)) { |
| 125 | if (result == null || |
| 126 | NDN.PITTable[i].interest.name.components.length > result.interest.name.components.length) |
| 127 | result = NDN.PITTable[i]; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return result; |
| 132 | }; |
| 133 | |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 134 | // For publishing data |
| 135 | NDN.CSTable = new Array(); |
| 136 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 137 | /** |
| 138 | * @constructor |
| 139 | */ |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 140 | var CSEntry = function CSEntry(name, closure) { |
| 141 | this.name = name; // String |
| 142 | this.closure = closure; // Closure |
| 143 | }; |
| 144 | |
| 145 | function getEntryForRegisteredPrefix(name) { |
| 146 | for (var i = 0; i < NDN.CSTable.length; i++) { |
Wentao Shang | 2a74ed8 | 2013-07-26 20:00:49 -0700 | [diff] [blame] | 147 | if (NDN.CSTable[i].name.match(name)) |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 148 | return NDN.CSTable[i]; |
| 149 | } |
| 150 | return null; |
| 151 | } |
| 152 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 153 | /** |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 154 | * Return a function that selects a host at random from hostList and returns { host: host, port: port }. |
| 155 | * If no more hosts remain, return null. |
| 156 | */ |
| 157 | NDN.makeShuffledGetHostAndPort = function(hostList, port) { |
| 158 | // Make a copy. |
| 159 | hostList = hostList.slice(0, hostList.length); |
| 160 | DataUtils.shuffle(hostList); |
| 161 | |
| 162 | return function() { |
| 163 | if (hostList.length == 0) |
| 164 | return null; |
| 165 | |
| 166 | return { host: hostList.splice(0, 1)[0], port: port }; |
| 167 | }; |
| 168 | }; |
| 169 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 170 | /** |
| 171 | * Encode name as an Interest and send the it to host:port, read the entire response and call |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 172 | * closure.upcall(Closure.UPCALL_CONTENT (or Closure.UPCALL_CONTENT_UNVERIFIED), |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 173 | * new UpcallInfo(this, interest, 0, contentObject)). |
| 174 | * @param {Name} name |
| 175 | * @param {Closure} closure |
| 176 | * @param {Interest} template if not null, use its attributes |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 177 | */ |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 178 | NDN.prototype.expressInterest = function (name, closure, template) { |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 179 | var interest = new Interest(name); |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 180 | if (template != null) { |
Jeff Thompson | 4404ab5 | 2012-10-21 10:29:48 -0700 | [diff] [blame] | 181 | interest.minSuffixComponents = template.minSuffixComponents; |
| 182 | interest.maxSuffixComponents = template.maxSuffixComponents; |
| 183 | interest.publisherPublicKeyDigest = template.publisherPublicKeyDigest; |
| 184 | interest.exclude = template.exclude; |
| 185 | interest.childSelector = template.childSelector; |
| 186 | interest.answerOriginKind = template.answerOriginKind; |
| 187 | interest.scope = template.scope; |
| 188 | interest.interestLifetime = template.interestLifetime; |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 189 | } |
| 190 | else |
| 191 | interest.interestLifetime = 4000; // default interest timeout value in milliseconds. |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 192 | |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 193 | if (this.host == null || this.port == null) { |
| 194 | if (this.getHostAndPort == null) |
| 195 | console.log('ERROR: host OR port NOT SET'); |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 196 | else { |
| 197 | var thisNDN = this; |
| 198 | this.connectAndExecute |
Jeff Thompson | f668acb | 2013-01-26 20:29:46 -0800 | [diff] [blame] | 199 | (function() { thisNDN.reconnectAndExpressInterest(interest, closure); }); |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 200 | } |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 201 | } |
| 202 | else |
Jeff Thompson | f668acb | 2013-01-26 20:29:46 -0800 | [diff] [blame] | 203 | this.reconnectAndExpressInterest(interest, closure); |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 204 | }; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 205 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 206 | /** |
Jeff Thompson | f668acb | 2013-01-26 20:29:46 -0800 | [diff] [blame] | 207 | * If the host and port are different than the ones in this.transport, then call |
| 208 | * this.transport.connect to change the connection (or connect for the first time). |
| 209 | * Then call expressInterestHelper. |
| 210 | */ |
| 211 | NDN.prototype.reconnectAndExpressInterest = function(interest, closure) { |
| 212 | if (this.transport.connectedHost != this.host || this.transport.connectedPort != this.port) { |
| 213 | var thisNDN = this; |
| 214 | this.transport.connect(thisNDN, function() { thisNDN.expressInterestHelper(interest, closure); }); |
| 215 | } |
| 216 | else |
| 217 | this.expressInterestHelper(interest, closure); |
| 218 | }; |
| 219 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 220 | /** |
Jeff Thompson | f668acb | 2013-01-26 20:29:46 -0800 | [diff] [blame] | 221 | * Do the work of reconnectAndExpressInterest once we know we are connected. Set the PITTable and call |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 222 | * this.transport.send to send the interest. |
| 223 | */ |
| 224 | NDN.prototype.expressInterestHelper = function(interest, closure) { |
Jeff Thompson | e8de582 | 2013-02-17 21:18:49 -0800 | [diff] [blame] | 225 | var binaryInterest = encodeToBinaryInterest(interest); |
| 226 | var thisNDN = this; |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 227 | //TODO: check local content store first |
| 228 | if (closure != null) { |
| 229 | var pitEntry = new PITEntry(interest, closure); |
| 230 | // TODO: This needs to be a single thread-safe transaction on a global object. |
| 231 | NDN.PITTable.push(pitEntry); |
| 232 | closure.pitEntry = pitEntry; |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 233 | |
Jeff Thompson | e8de582 | 2013-02-17 21:18:49 -0800 | [diff] [blame] | 234 | // Set interest timer. |
Jeff Thompson | a53e65a | 2013-02-10 10:52:52 -0800 | [diff] [blame] | 235 | var timeoutMilliseconds = (interest.interestLifetime || 4000); |
Jeff Thompson | e8de582 | 2013-02-17 21:18:49 -0800 | [diff] [blame] | 236 | var timeoutCallback = function() { |
Jeff Thompson | 3d9ad14 | 2013-03-10 16:55:28 -0700 | [diff] [blame] | 237 | if (LOG > 1) console.log("Interest time out: " + interest.name.to_uri()); |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 238 | |
Jeff Thompson | e8de582 | 2013-02-17 21:18:49 -0800 | [diff] [blame] | 239 | // Remove PIT entry from NDN.PITTable, even if we add it again later to re-express |
| 240 | // the interest because we don't want to match it in the mean time. |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 241 | // TODO: Make this a thread-safe operation on the global PITTable. |
| 242 | var index = NDN.PITTable.indexOf(pitEntry); |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 243 | if (index >= 0) |
| 244 | NDN.PITTable.splice(index, 1); |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 245 | |
| 246 | // Raise closure callback |
Jeff Thompson | e8de582 | 2013-02-17 21:18:49 -0800 | [diff] [blame] | 247 | if (closure.upcall(Closure.UPCALL_INTEREST_TIMED_OUT, |
| 248 | new UpcallInfo(thisNDN, interest, 0, null)) == Closure.RESULT_REEXPRESS) { |
Jeff Thompson | 3d9ad14 | 2013-03-10 16:55:28 -0700 | [diff] [blame] | 249 | if (LOG > 1) console.log("Re-express interest: " + interest.name.to_uri()); |
Jeff Thompson | e8de582 | 2013-02-17 21:18:49 -0800 | [diff] [blame] | 250 | pitEntry.timerID = setTimeout(timeoutCallback, timeoutMilliseconds); |
| 251 | NDN.PITTable.push(pitEntry); |
| 252 | thisNDN.transport.send(binaryInterest); |
| 253 | } |
| 254 | }; |
| 255 | pitEntry.timerID = setTimeout(timeoutCallback, timeoutMilliseconds); |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Jeff Thompson | e8de582 | 2013-02-17 21:18:49 -0800 | [diff] [blame] | 258 | this.transport.send(binaryInterest); |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 259 | }; |
| 260 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 261 | /** |
| 262 | * Register name with the connected NDN hub and receive interests with closure.upcall. |
| 263 | * @param {Name} name |
| 264 | * @param {Closure} closure |
| 265 | * @param {number} flag |
| 266 | */ |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 267 | NDN.prototype.registerPrefix = function(name, closure, flag) { |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 268 | var thisNDN = this; |
| 269 | var onConnected = function() { |
| 270 | if (thisNDN.ccndid == null) { |
| 271 | // Fetch ccndid first, then register. |
| 272 | var interest = new Interest(NDN.ccndIdFetcher); |
| 273 | interest.interestLifetime = 4000; // milliseconds |
| 274 | if (LOG>3) console.log('Expressing interest for ccndid from ccnd.'); |
Jeff Thompson | f668acb | 2013-01-26 20:29:46 -0800 | [diff] [blame] | 275 | thisNDN.reconnectAndExpressInterest |
| 276 | (interest, new NDN.FetchCcndidClosure(thisNDN, name, closure, flag)); |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 277 | } |
| 278 | else |
| 279 | thisNDN.registerPrefixHelper(name, closure, flag); |
| 280 | }; |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 281 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 282 | if (this.host == null || this.port == null) { |
| 283 | if (this.getHostAndPort == null) |
| 284 | console.log('ERROR: host OR port NOT SET'); |
| 285 | else |
| 286 | this.connectAndExecute(onConnected); |
| 287 | } |
| 288 | else |
| 289 | onConnected(); |
| 290 | }; |
| 291 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 292 | /** |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 293 | * This is a closure to receive the ContentObject for NDN.ccndIdFetcher and call |
| 294 | * registerPrefixHelper(name, callerClosure, flag). |
| 295 | */ |
| 296 | NDN.FetchCcndidClosure = function FetchCcndidClosure(ndn, name, callerClosure, flag) { |
| 297 | // Inherit from Closure. |
| 298 | Closure.call(this); |
| 299 | |
| 300 | this.ndn = ndn; |
| 301 | this.name = name; |
| 302 | this.callerClosure = callerClosure; |
| 303 | this.flag = flag; |
| 304 | }; |
| 305 | |
| 306 | NDN.FetchCcndidClosure.prototype.upcall = function(kind, upcallInfo) { |
| 307 | if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) { |
| 308 | console.log("Timeout while requesting the ccndid. Cannot registerPrefix for " + |
| 309 | this.name.to_uri() + " ."); |
| 310 | return Closure.RESULT_OK; |
| 311 | } |
| 312 | if (!(kind == Closure.UPCALL_CONTENT || |
| 313 | kind == Closure.UPCALL_CONTENT_UNVERIFIED)) |
| 314 | // The upcall is not for us. |
| 315 | return Closure.RESULT_ERR; |
| 316 | |
| 317 | var co = upcallInfo.contentObject; |
| 318 | if (!co.signedInfo || !co.signedInfo.publisher |
| 319 | || !co.signedInfo.publisher.publisherPublicKeyDigest) |
| 320 | console.log |
| 321 | ("ContentObject doesn't have a publisherPublicKeyDigest. Cannot set ccndid and registerPrefix for " |
| 322 | + this.name.to_uri() + " ."); |
| 323 | else { |
| 324 | if (LOG>3) console.log('Got ccndid from ccnd.'); |
| 325 | this.ndn.ccndid = co.signedInfo.publisher.publisherPublicKeyDigest; |
| 326 | if (LOG>3) console.log(this.ndn.ccndid); |
| 327 | |
| 328 | this.ndn.registerPrefixHelper(this.name, this.callerClosure, this.flag); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 329 | } |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 330 | |
| 331 | return Closure.RESULT_OK; |
| 332 | }; |
| 333 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 334 | /** |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 335 | * Do the work of registerPrefix once we know we are connected with a ccndid. |
| 336 | */ |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 337 | NDN.prototype.registerPrefixHelper = function(name, closure, flag) { |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 338 | var fe = new ForwardingEntry('selfreg', name, null, null, 3, 2147483647); |
| 339 | var bytes = encodeForwardingEntry(fe); |
| 340 | |
| 341 | var si = new SignedInfo(); |
| 342 | si.setFields(); |
| 343 | |
Jeff Thompson | 2c58044 | 2013-07-31 11:51:18 -0700 | [diff] [blame^] | 344 | var co = new ContentObject(new Name(), si, bytes); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 345 | co.sign(); |
| 346 | var coBinary = encodeToBinaryContentObject(co); |
| 347 | |
| 348 | //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'); |
| 349 | var nodename = this.ccndid; |
| 350 | var interestName = new Name(['ccnx', nodename, 'selfreg', coBinary]); |
| 351 | |
| 352 | var interest = new Interest(interestName); |
| 353 | interest.scope = 1; |
| 354 | if (LOG > 3) console.log('Send Interest registration packet.'); |
| 355 | |
| 356 | var csEntry = new CSEntry(name.getName(), closure); |
| 357 | NDN.CSTable.push(csEntry); |
| 358 | |
| 359 | this.transport.send(encodeToBinaryInterest(interest)); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 360 | }; |
| 361 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 362 | /** |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 363 | * This is called when an entire binary XML element is received, such as a ContentObject or Interest. |
| 364 | * Look up in the PITTable and call the closure callback. |
| 365 | */ |
| 366 | NDN.prototype.onReceivedElement = function(element) { |
Jeff Thompson | a46083c | 2013-01-20 23:55:21 -0800 | [diff] [blame] | 367 | if (LOG>3) console.log('Complete element received. Length ' + element.length + '. Start decoding.'); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 368 | var decoder = new BinaryXMLDecoder(element); |
| 369 | // Dispatch according to packet type |
| 370 | if (decoder.peekStartElement(CCNProtocolDTags.Interest)) { // Interest packet |
| 371 | if (LOG > 3) console.log('Interest packet received.'); |
| 372 | |
| 373 | var interest = new Interest(); |
| 374 | interest.from_ccnb(decoder); |
| 375 | if (LOG > 3) console.log(interest); |
| 376 | var nameStr = escape(interest.name.getName()); |
| 377 | if (LOG > 3) console.log(nameStr); |
| 378 | |
| 379 | var entry = getEntryForRegisteredPrefix(nameStr); |
| 380 | if (entry != null) { |
| 381 | //console.log(entry); |
| 382 | var info = new UpcallInfo(this, interest, 0, null); |
| 383 | var ret = entry.closure.upcall(Closure.UPCALL_INTEREST, info); |
| 384 | if (ret == Closure.RESULT_INTEREST_CONSUMED && info.contentObject != null) |
| 385 | this.transport.send(encodeToBinaryContentObject(info.contentObject)); |
| 386 | } |
| 387 | } else if (decoder.peekStartElement(CCNProtocolDTags.ContentObject)) { // Content packet |
| 388 | if (LOG > 3) console.log('ContentObject packet received.'); |
| 389 | |
| 390 | var co = new ContentObject(); |
| 391 | co.from_ccnb(decoder); |
| 392 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 393 | var pitEntry = NDN.getEntryForExpressedInterest(co.name); |
| 394 | if (pitEntry != null) { |
Jeff Thompson | 0ee721e | 2013-02-18 17:46:55 -0800 | [diff] [blame] | 395 | // Cancel interest timer |
| 396 | clearTimeout(pitEntry.timerID); |
| 397 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 398 | // Remove PIT entry from NDN.PITTable |
| 399 | var index = NDN.PITTable.indexOf(pitEntry); |
| 400 | if (index >= 0) |
| 401 | NDN.PITTable.splice(index, 1); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 402 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 403 | var currentClosure = pitEntry.closure; |
Jeff Thompson | 0ee721e | 2013-02-18 17:46:55 -0800 | [diff] [blame] | 404 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 405 | if (this.verify == false) { |
| 406 | // Pass content up without verifying the signature |
Alexander Afanasyev | aa7e9a0 | 2013-03-02 13:48:21 -0800 | [diff] [blame] | 407 | currentClosure.upcall(Closure.UPCALL_CONTENT_UNVERIFIED, new UpcallInfo(this, pitEntry.interest, 0, co)); |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 408 | return; |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 409 | } |
Wentao Shang | d460739 | 2013-01-24 23:08:49 -0800 | [diff] [blame] | 410 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 411 | // Key verification |
| 412 | |
| 413 | // Recursive key fetching & verification closure |
| 414 | var KeyFetchClosure = function KeyFetchClosure(content, closure, key, sig, wit) { |
| 415 | this.contentObject = content; // unverified content object |
| 416 | this.closure = closure; // closure corresponding to the contentObject |
| 417 | this.keyName = key; // name of current key to be fetched |
| 418 | this.sigHex = sig; // hex signature string to be verified |
| 419 | this.witness = wit; |
| 420 | |
| 421 | Closure.call(this); |
| 422 | }; |
| 423 | |
| 424 | var thisNDN = this; |
| 425 | KeyFetchClosure.prototype.upcall = function(kind, upcallInfo) { |
| 426 | if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) { |
| 427 | console.log("In KeyFetchClosure.upcall: interest time out."); |
| 428 | console.log(this.keyName.contentName.getName()); |
| 429 | } else if (kind == Closure.UPCALL_CONTENT) { |
| 430 | //console.log("In KeyFetchClosure.upcall: signature verification passed"); |
| 431 | |
| 432 | var rsakey = decodeSubjectPublicKeyInfo(upcallInfo.contentObject.content); |
| 433 | var verified = rsakey.verifyByteArray(this.contentObject.rawSignatureData, this.witness, this.sigHex); |
| 434 | |
| 435 | var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD; |
| 436 | //console.log("raise encapsulated closure"); |
| 437 | this.closure.upcall(flag, new UpcallInfo(thisNDN, null, 0, this.contentObject)); |
| 438 | |
| 439 | // Store key in cache |
| 440 | var keyEntry = new KeyStoreEntry(keylocator.keyName, rsakey, new Date().getTime()); |
| 441 | NDN.addKeyEntry(keyEntry); |
| 442 | //console.log(NDN.KeyStore); |
| 443 | } else if (kind == Closure.UPCALL_CONTENT_BAD) { |
| 444 | console.log("In KeyFetchClosure.upcall: signature verification failed"); |
Wentao Shang | d460739 | 2013-01-24 23:08:49 -0800 | [diff] [blame] | 445 | } |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 446 | }; |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 447 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 448 | if (co.signedInfo && co.signedInfo.locator && co.signature) { |
| 449 | if (LOG > 3) console.log("Key verification..."); |
| 450 | var sigHex = DataUtils.toHex(co.signature.signature).toLowerCase(); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 451 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 452 | var wit = null; |
| 453 | if (co.signature.Witness != null) { |
| 454 | wit = new Witness(); |
| 455 | wit.decode(co.signature.Witness); |
| 456 | } |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 457 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 458 | var keylocator = co.signedInfo.locator; |
| 459 | if (keylocator.type == KeyLocatorType.KEYNAME) { |
| 460 | if (LOG > 3) console.log("KeyLocator contains KEYNAME"); |
| 461 | //var keyname = keylocator.keyName.contentName.getName(); |
| 462 | //console.log(nameStr); |
| 463 | //console.log(keyname); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 464 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 465 | if (keylocator.keyName.contentName.match(co.name)) { |
| 466 | if (LOG > 3) console.log("Content is key itself"); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 467 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 468 | var rsakey = decodeSubjectPublicKeyInfo(co.content); |
| 469 | var verified = rsakey.verifyByteArray(co.rawSignatureData, wit, sigHex); |
| 470 | var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD; |
Alexander Afanasyev | aa7e9a0 | 2013-03-02 13:48:21 -0800 | [diff] [blame] | 471 | |
| 472 | currentClosure.upcall(flag, new UpcallInfo(this, pitEntry.interest, 0, co)); |
| 473 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 474 | // SWT: We don't need to store key here since the same key will be |
| 475 | // stored again in the closure. |
| 476 | //var keyEntry = new KeyStoreEntry(keylocator.keyName, rsakey, new Date().getTime()); |
| 477 | //NDN.addKeyEntry(keyEntry); |
| 478 | //console.log(NDN.KeyStore); |
| 479 | } else { |
| 480 | // Check local key store |
| 481 | var keyEntry = NDN.getKeyByName(keylocator.keyName); |
| 482 | if (keyEntry) { |
| 483 | // Key found, verify now |
| 484 | if (LOG > 3) console.log("Local key cache hit"); |
| 485 | var rsakey = keyEntry.rsaKey; |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 486 | var verified = rsakey.verifyByteArray(co.rawSignatureData, wit, sigHex); |
| 487 | var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD; |
Alexander Afanasyev | aa7e9a0 | 2013-03-02 13:48:21 -0800 | [diff] [blame] | 488 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 489 | // Raise callback |
Alexander Afanasyev | aa7e9a0 | 2013-03-02 13:48:21 -0800 | [diff] [blame] | 490 | currentClosure.upcall(flag, new UpcallInfo(this, pitEntry.interest, 0, co)); |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 491 | } else { |
| 492 | // Not found, fetch now |
| 493 | if (LOG > 3) console.log("Fetch key according to keylocator"); |
| 494 | var nextClosure = new KeyFetchClosure(co, currentClosure, keylocator.keyName, sigHex, wit); |
| 495 | this.expressInterest(keylocator.keyName.contentName.getPrefix(4), nextClosure); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 496 | } |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 497 | } |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 498 | } else if (keylocator.type == KeyLocatorType.KEY) { |
| 499 | if (LOG > 3) console.log("Keylocator contains KEY"); |
| 500 | |
| 501 | var rsakey = decodeSubjectPublicKeyInfo(co.signedInfo.locator.publicKey); |
| 502 | var verified = rsakey.verifyByteArray(co.rawSignatureData, wit, sigHex); |
| 503 | |
| 504 | var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD; |
| 505 | // Raise callback |
Alexander Afanasyev | aa7e9a0 | 2013-03-02 13:48:21 -0800 | [diff] [blame] | 506 | currentClosure.upcall(Closure.UPCALL_CONTENT, new UpcallInfo(this, pitEntry.interest, 0, co)); |
| 507 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 508 | // Since KeyLocator does not contain key name for this key, |
| 509 | // we have no way to store it as a key entry in KeyStore. |
| 510 | } else { |
| 511 | var cert = keylocator.certificate; |
| 512 | console.log("KeyLocator contains CERT"); |
| 513 | console.log(cert); |
| 514 | |
| 515 | // TODO: verify certificate |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | } |
| 519 | } else |
| 520 | console.log('Incoming packet is not Interest or ContentObject. Discard now.'); |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 521 | }; |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 522 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 523 | /** |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 524 | * Assume this.getHostAndPort is not null. This is called when this.host is null or its host |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 525 | * is not alive. Get a host and port, connect, then execute onConnected(). |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 526 | */ |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 527 | NDN.prototype.connectAndExecute = function(onConnected) { |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 528 | var hostAndPort = this.getHostAndPort(); |
| 529 | if (hostAndPort == null) { |
| 530 | console.log('ERROR: No more hosts from getHostAndPort'); |
| 531 | this.host = null; |
| 532 | return; |
| 533 | } |
| 534 | |
| 535 | if (hostAndPort.host == this.host && hostAndPort.port == this.port) { |
| 536 | console.log('ERROR: The host returned by getHostAndPort is not alive: ' + |
| 537 | this.host + ":" + this.port); |
| 538 | return; |
| 539 | } |
| 540 | |
| 541 | this.host = hostAndPort.host; |
| 542 | this.port = hostAndPort.port; |
Jeff Thompson | 3d9ad14 | 2013-03-10 16:55:28 -0700 | [diff] [blame] | 543 | if (LOG>0) console.log("connectAndExecute: trying host from getHostAndPort: " + this.host); |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 544 | |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 545 | // Fetch any content. |
| 546 | var interest = new Interest(new Name("/")); |
Jeff Thompson | 42806a1 | 2012-12-29 18:19:39 -0800 | [diff] [blame] | 547 | interest.interestLifetime = 4000; // milliseconds |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 548 | |
| 549 | var thisNDN = this; |
| 550 | var timerID = setTimeout(function() { |
Jeff Thompson | 3d9ad14 | 2013-03-10 16:55:28 -0700 | [diff] [blame] | 551 | if (LOG>0) console.log("connectAndExecute: timeout waiting for host " + thisNDN.host); |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 552 | // Try again. |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 553 | thisNDN.connectAndExecute(onConnected); |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 554 | }, 3000); |
| 555 | |
Jeff Thompson | f668acb | 2013-01-26 20:29:46 -0800 | [diff] [blame] | 556 | this.reconnectAndExpressInterest |
| 557 | (interest, new NDN.ConnectClosure(this, onConnected, timerID)); |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 558 | }; |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 559 | |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 560 | NDN.ConnectClosure = function ConnectClosure(ndn, onConnected, timerID) { |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 561 | // Inherit from Closure. |
| 562 | Closure.call(this); |
| 563 | |
| 564 | this.ndn = ndn; |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 565 | this.onConnected = onConnected; |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 566 | this.timerID = timerID; |
| 567 | }; |
| 568 | |
| 569 | NDN.ConnectClosure.prototype.upcall = function(kind, upcallInfo) { |
| 570 | if (!(kind == Closure.UPCALL_CONTENT || |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 571 | kind == Closure.UPCALL_CONTENT_UNVERIFIED)) |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 572 | // The upcall is not for us. |
| 573 | return Closure.RESULT_ERR; |
| 574 | |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 575 | // The host is alive, so cancel the timeout and continue with onConnected(). |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 576 | clearTimeout(this.timerID); |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 577 | |
| 578 | // Call NDN.onopen after success |
| 579 | this.ndn.readyStatus = NDN.OPENED; |
| 580 | this.ndn.onopen(); |
| 581 | |
Jeff Thompson | 3d9ad14 | 2013-03-10 16:55:28 -0700 | [diff] [blame] | 582 | if (LOG>0) console.log("connectAndExecute: connected to host " + this.ndn.host); |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 583 | this.onConnected(); |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 584 | |
| 585 | return Closure.RESULT_OK; |
| 586 | }; |
| 587 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 588 | /** |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 589 | * A BinaryXmlElementReader lets you call onReceivedData multiple times which uses a |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 590 | * BinaryXMLStructureDecoder to detect the end of a binary XML element and calls |
| 591 | * elementListener.onReceivedElement(element) with the element. |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 592 | * This handles the case where a single call to onReceivedData may contain multiple elements. |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 593 | * @constructor |
| 594 | * @param {{onReceivedElement:function}} elementListener |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 595 | */ |
| 596 | var BinaryXmlElementReader = function BinaryXmlElementReader(elementListener) { |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 597 | this.elementListener = elementListener; |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 598 | this.dataParts = []; |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 599 | this.structureDecoder = new BinaryXMLStructureDecoder(); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 600 | }; |
| 601 | |
Jeff Thompson | 27fbfdc | 2013-07-15 14:36:54 -0700 | [diff] [blame] | 602 | BinaryXmlElementReader.prototype.onReceivedData = function(/* Uint8Array */ data) { |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 603 | // Process multiple objects in the data. |
| 604 | while(true) { |
| 605 | // Scan the input to check if a whole ccnb object has been read. |
| 606 | this.structureDecoder.seek(0); |
Jeff Thompson | 27fbfdc | 2013-07-15 14:36:54 -0700 | [diff] [blame] | 607 | if (this.structureDecoder.findElementEnd(data)) { |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 608 | // Got the remainder of an object. Report to the caller. |
Jeff Thompson | 27fbfdc | 2013-07-15 14:36:54 -0700 | [diff] [blame] | 609 | this.dataParts.push(data.subarray(0, this.structureDecoder.offset)); |
Jeff Thompson | 0790af8 | 2013-01-26 19:54:27 -0800 | [diff] [blame] | 610 | var element = DataUtils.concatArrays(this.dataParts); |
| 611 | this.dataParts = []; |
| 612 | try { |
| 613 | this.elementListener.onReceivedElement(element); |
| 614 | } catch (ex) { |
| 615 | console.log("BinaryXmlElementReader: ignoring exception from onReceivedElement: " + ex); |
| 616 | } |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 617 | |
| 618 | // Need to read a new object. |
Jeff Thompson | 27fbfdc | 2013-07-15 14:36:54 -0700 | [diff] [blame] | 619 | data = data.subarray(this.structureDecoder.offset, data.length); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 620 | this.structureDecoder = new BinaryXMLStructureDecoder(); |
Jeff Thompson | 27fbfdc | 2013-07-15 14:36:54 -0700 | [diff] [blame] | 621 | if (data.length == 0) |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 622 | // No more data in the packet. |
| 623 | return; |
| 624 | |
| 625 | // else loop back to decode. |
| 626 | } |
| 627 | else { |
| 628 | // Save for a later call to concatArrays so that we only copy data once. |
Jeff Thompson | 27fbfdc | 2013-07-15 14:36:54 -0700 | [diff] [blame] | 629 | this.dataParts.push(data); |
| 630 | if (LOG>3) console.log('Incomplete packet received. Length ' + data.length + '. Wait for more input.'); |
| 631 | return; |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 632 | } |
| 633 | } |
Jeff Thompson | 27fbfdc | 2013-07-15 14:36:54 -0700 | [diff] [blame] | 634 | }; |