Wentao Shang | bd63e46 | 2012-12-03 16:19:33 -0800 | [diff] [blame] | 1 | /** |
Jeff Thompson | 146d7de | 2012-11-17 16:15:28 -0800 | [diff] [blame] | 2 | * @author: Meki Cheraoui |
Jeff Thompson | 745026e | 2012-10-13 12:49:20 -0700 | [diff] [blame] | 3 | * See COPYING for copyright and distribution information. |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 4 | * This class represents Interest Objects |
| 5 | */ |
| 6 | |
Jeff Thompson | 42806a1 | 2012-12-29 18:19:39 -0800 | [diff] [blame] | 7 | // _interestLifetime is in milliseconds. |
Jeff Thompson | ea3a690 | 2013-02-17 21:56:34 -0800 | [diff] [blame] | 8 | var Interest = function Interest |
| 9 | (_name, _faceInstance, _minSuffixComponents, _maxSuffixComponents, _publisherPublicKeyDigest, _exclude, |
| 10 | _childSelector, _answerOriginKind, _scope, _interestLifetime, _nonce) { |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 11 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 12 | this.name = _name; |
| 13 | this.faceInstance = _faceInstance; |
| 14 | this.maxSuffixComponents = _maxSuffixComponents; |
| 15 | this.minSuffixComponents = _minSuffixComponents; |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 16 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 17 | this.publisherPublicKeyDigest = _publisherPublicKeyDigest; |
| 18 | this.exclude = _exclude; |
| 19 | this.childSelector = _childSelector; |
| 20 | this.answerOriginKind = _answerOriginKind; |
| 21 | this.scope = _scope; |
Jeff Thompson | 42806a1 | 2012-12-29 18:19:39 -0800 | [diff] [blame] | 22 | this.interestLifetime = _interestLifetime; // milli seconds |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 23 | this.nonce = _nonce; |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 24 | }; |
| 25 | |
Jeff Thompson | 821183d | 2012-11-24 18:46:21 -0800 | [diff] [blame] | 26 | Interest.RECURSIVE_POSTFIX = "*"; |
| 27 | |
| 28 | Interest.CHILD_SELECTOR_LEFT = 0; |
| 29 | Interest.CHILD_SELECTOR_RIGHT = 1; |
| 30 | Interest.ANSWER_CONTENT_STORE = 1; |
| 31 | Interest.ANSWER_GENERATED = 2; |
| 32 | Interest.ANSWER_STALE = 4; // Stale answer OK |
| 33 | Interest.MARK_STALE = 16; // Must have scope 0. Michael calls this a "hack" |
| 34 | |
| 35 | Interest.DEFAULT_ANSWER_ORIGIN_KIND = Interest.ANSWER_CONTENT_STORE | Interest.ANSWER_GENERATED; |
| 36 | |
| 37 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 38 | Interest.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) { |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 39 | |
| 40 | decoder.readStartElement(CCNProtocolDTags.Interest); |
| 41 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 42 | this.name = new Name(); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 43 | this.name.from_ccnb(decoder); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 44 | |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 45 | if (decoder.peekStartElement(CCNProtocolDTags.MinSuffixComponents)) |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 46 | this.minSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MinSuffixComponents); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 47 | |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 48 | if (decoder.peekStartElement(CCNProtocolDTags.MaxSuffixComponents)) |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 49 | this.maxSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MaxSuffixComponents); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 50 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 51 | if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) { |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 52 | this.publisherPublicKeyDigest = new PublisherPublicKeyDigest(); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 53 | this.publisherPublicKeyDigest.from_ccnb(decoder); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 54 | } |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 55 | |
| 56 | if (decoder.peekStartElement(CCNProtocolDTags.Exclude)) { |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 57 | this.exclude = new Exclude(); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 58 | this.exclude.from_ccnb(decoder); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 61 | if (decoder.peekStartElement(CCNProtocolDTags.ChildSelector)) |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 62 | this.childSelector = decoder.readIntegerElement(CCNProtocolDTags.ChildSelector); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 63 | |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 64 | if (decoder.peekStartElement(CCNProtocolDTags.AnswerOriginKind)) |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 65 | this.answerOriginKind = decoder.readIntegerElement(CCNProtocolDTags.AnswerOriginKind); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 66 | |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 67 | if (decoder.peekStartElement(CCNProtocolDTags.Scope)) |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 68 | this.scope = decoder.readIntegerElement(CCNProtocolDTags.Scope); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 69 | |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 70 | if (decoder.peekStartElement(CCNProtocolDTags.InterestLifetime)) |
Jeff Thompson | 42806a1 | 2012-12-29 18:19:39 -0800 | [diff] [blame] | 71 | this.interestLifetime = 1000.0 * DataUtils.bigEndianToUnsignedInt |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 72 | (decoder.readBinaryElement(CCNProtocolDTags.InterestLifetime)) / 4096; |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 73 | |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 74 | if (decoder.peekStartElement(CCNProtocolDTags.Nonce)) |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 75 | this.nonce = decoder.readBinaryElement(CCNProtocolDTags.Nonce); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 76 | |
| 77 | decoder.readEndElement(); |
| 78 | }; |
| 79 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 80 | Interest.prototype.to_ccnb = function(/*XMLEncoder*/ encoder){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 81 | //Could check if name is present |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 82 | |
| 83 | encoder.writeStartElement(CCNProtocolDTags.Interest); |
| 84 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 85 | this.name.to_ccnb(encoder); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 86 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 87 | if (null != this.minSuffixComponents) |
| 88 | encoder.writeElement(CCNProtocolDTags.MinSuffixComponents, this.minSuffixComponents); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 89 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 90 | if (null != this.maxSuffixComponents) |
| 91 | encoder.writeElement(CCNProtocolDTags.MaxSuffixComponents, this.maxSuffixComponents); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 92 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 93 | if (null != this.publisherPublicKeyDigest) |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 94 | this.publisherPublicKeyDigest.to_ccnb(encoder); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 95 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 96 | if (null != this.exclude) |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 97 | this.exclude.to_ccnb(encoder); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 98 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 99 | if (null != this.childSelector) |
| 100 | encoder.writeElement(CCNProtocolDTags.ChildSelector, this.childSelector); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 101 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 102 | if (this.DEFAULT_ANSWER_ORIGIN_KIND != this.answerOriginKind && this.answerOriginKind!=null) |
| 103 | encoder.writeElement(CCNProtocolDTags.AnswerOriginKind, this.answerOriginKind); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 104 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 105 | if (null != this.scope) |
| 106 | encoder.writeElement(CCNProtocolDTags.Scope, this.scope); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 107 | |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 108 | if (null != this.interestLifetime) |
| 109 | encoder.writeElement(CCNProtocolDTags.InterestLifetime, |
Jeff Thompson | 42806a1 | 2012-12-29 18:19:39 -0800 | [diff] [blame] | 110 | DataUtils.nonNegativeIntToBigEndian((this.interestLifetime / 1000.0) * 4096)); |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 111 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 112 | if (null != this.nonce) |
| 113 | encoder.writeElement(CCNProtocolDTags.Nonce, this.nonce); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 114 | |
| 115 | encoder.writeEndElement(); |
| 116 | |
| 117 | }; |
| 118 | |
Jeff Thompson | 202728a | 2013-02-10 22:20:08 -0800 | [diff] [blame] | 119 | /* |
| 120 | * Return true if this.name.match(name) and the name conforms to the interest selectors. |
| 121 | */ |
Jeff Thompson | 3f4be55 | 2013-01-05 22:36:40 -0800 | [diff] [blame] | 122 | Interest.prototype.matches_name = function(/*Name*/ name) { |
Jeff Thompson | 202728a | 2013-02-10 22:20:08 -0800 | [diff] [blame] | 123 | if (!this.name.match(name)) |
| 124 | return false; |
| 125 | |
| 126 | if (this.minSuffixComponents != null && |
| 127 | // Add 1 for the implicit digest. |
| 128 | !(name.components.length + 1 - this.name.components.length >= this.minSuffixComponents)) |
| 129 | return false; |
| 130 | if (this.maxSuffixComponents != null && |
| 131 | // Add 1 for the implicit digest. |
| 132 | !(name.components.length + 1 - this.name.components.length <= this.maxSuffixComponents)) |
| 133 | return false; |
| 134 | if (this.exclude != null && name.components.length > this.name.components.length && |
| 135 | this.exclude.matches(name.components[this.name.components.length])) |
| 136 | return false; |
| 137 | |
| 138 | return true; |
Jeff Thompson | ea3a690 | 2013-02-17 21:56:34 -0800 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | /* |
| 142 | * Return a new Interest with the same fields as this Interest. |
| 143 | * Note: This does NOT make a deep clone of the name, exclue or other objects. |
| 144 | */ |
| 145 | Interest.prototype.clone = function() { |
| 146 | return new Interest |
| 147 | (this.name, this.faceInstance, this.minSuffixComponents, this.maxSuffixComponents, |
| 148 | this.publisherPublicKeyDigest, this.exclude, this.childSelector, this.answerOriginKind, |
| 149 | this.scope, this.interestLifetime, this.nonce); |
| 150 | }; |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 151 | |
Jeff Thompson | 08d41b7 | 2013-02-03 23:09:29 -0800 | [diff] [blame] | 152 | /* |
| 153 | * Handle the interest Exclude element. |
| 154 | * _values is an array where each element is either Uint8Array component or Exclude.ANY. |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 155 | */ |
Jeff Thompson | 08d41b7 | 2013-02-03 23:09:29 -0800 | [diff] [blame] | 156 | var Exclude = function Exclude(_values) { |
| 157 | this.values = (_values || []); |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Jeff Thompson | 08d41b7 | 2013-02-03 23:09:29 -0800 | [diff] [blame] | 160 | Exclude.ANY = "*"; |
| 161 | |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 162 | Exclude.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) { |
Jeff Thompson | 08d41b7 | 2013-02-03 23:09:29 -0800 | [diff] [blame] | 163 | decoder.readStartElement(CCNProtocolDTags.Exclude); |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 164 | |
Jeff Thompson | 08d41b7 | 2013-02-03 23:09:29 -0800 | [diff] [blame] | 165 | while (true) { |
| 166 | if (decoder.peekStartElement(CCNProtocolDTags.Component)) |
| 167 | this.values.push(decoder.readBinaryElement(CCNProtocolDTags.Component)); |
| 168 | else if (decoder.peekStartElement(CCNProtocolDTags.Any)) { |
| 169 | decoder.readStartElement(CCNProtocolDTags.Any); |
| 170 | decoder.readEndElement(); |
| 171 | this.values.push(Exclude.ANY); |
| 172 | } |
| 173 | else if (decoder.peekStartElement(CCNProtocolDTags.Bloom)) { |
| 174 | // Skip the Bloom and treat it as Any. |
| 175 | decoder.readBinaryElement(CCNProtocolDTags.Bloom); |
| 176 | this.values.push(Exclude.ANY); |
| 177 | } |
| 178 | else |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | decoder.readEndElement(); |
| 183 | }; |
| 184 | |
| 185 | Exclude.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) { |
| 186 | if (this.values == null || this.values.length == 0) |
| 187 | return; |
| 188 | |
| 189 | encoder.writeStartElement(CCNProtocolDTags.Exclude); |
| 190 | |
| 191 | // TODO: Do we want to order the components (except for ANY)? |
| 192 | for (var i = 0; i < this.values.length; ++i) { |
| 193 | if (this.values[i] == Exclude.ANY) { |
| 194 | encoder.writeStartElement(CCNProtocolDTags.Any); |
| 195 | encoder.writeEndElement(); |
| 196 | } |
| 197 | else |
| 198 | encoder.writeElement(CCNProtocolDTags.Component, this.values[i]); |
| 199 | } |
| 200 | |
| 201 | encoder.writeEndElement(); |
| 202 | }; |
| 203 | |
Jeff Thompson | 202728a | 2013-02-10 22:20:08 -0800 | [diff] [blame] | 204 | /* |
| 205 | * Return a string with elements separated by "," and Exclude.ANY shown as "*". |
| 206 | */ |
Jeff Thompson | 08d41b7 | 2013-02-03 23:09:29 -0800 | [diff] [blame] | 207 | Exclude.prototype.to_uri = function() { |
| 208 | if (this.values == null || this.values.length == 0) |
| 209 | return ""; |
| 210 | |
| 211 | var result = ""; |
| 212 | for (var i = 0; i < this.values.length; ++i) { |
| 213 | if (i > 0) |
| 214 | result += ","; |
Jeff Thompson | fced4c2 | 2013-01-27 16:34:13 -0800 | [diff] [blame] | 215 | |
Jeff Thompson | 08d41b7 | 2013-02-03 23:09:29 -0800 | [diff] [blame] | 216 | if (this.values[i] == Exclude.ANY) |
| 217 | result += "*"; |
| 218 | else |
| 219 | result += Name.toEscapedString(this.values[i]); |
| 220 | } |
| 221 | return result; |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 222 | }; |
Jeff Thompson | 202728a | 2013-02-10 22:20:08 -0800 | [diff] [blame] | 223 | |
| 224 | /* |
| 225 | * Return true if the component matches any of the exclude criteria. |
| 226 | */ |
| 227 | Exclude.prototype.matches = function(/*Uint8Array*/ component) { |
| 228 | for (var i = 0; i < this.values.length; ++i) { |
| 229 | if (this.values[i] == Exclude.ANY) { |
| 230 | var lowerBound = null; |
| 231 | if (i > 0) |
| 232 | lowerBound = this.values[i - 1]; |
| 233 | |
| 234 | // Find the upper bound, possibly skipping over multiple ANY in a row. |
| 235 | var iUpperBound; |
| 236 | var upperBound = null; |
| 237 | for (iUpperBound = i + 1; iUpperBound < this.values.length; ++iUpperBound) { |
| 238 | if (this.values[iUpperBound] != Exclude.ANY) { |
| 239 | upperBound = this.values[iUpperBound]; |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // If lowerBound != null, we already checked component equals lowerBound on the last pass. |
| 245 | // If upperBound != null, we will check component equals upperBound on the next pass. |
| 246 | if (upperBound != null) { |
| 247 | if (lowerBound != null) { |
| 248 | if (Exclude.compareComponents(component, lowerBound) > 0 && |
| 249 | Exclude.compareComponents(component, upperBound) < 0) |
| 250 | return true; |
| 251 | } |
| 252 | else { |
| 253 | if (Exclude.compareComponents(component, upperBound) < 0) |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | // Make i equal iUpperBound on the next pass. |
| 258 | i = iUpperBound - 1; |
| 259 | } |
| 260 | else { |
| 261 | if (lowerBound != null) { |
| 262 | if (Exclude.compareComponents(component, lowerBound) > 0) |
| 263 | return true; |
| 264 | } |
| 265 | else |
| 266 | // this.values has only ANY. |
| 267 | return true; |
| 268 | } |
| 269 | } |
| 270 | else { |
| 271 | if (DataUtils.arraysEqual(component, this.values[i])) |
| 272 | return true; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return false; |
| 277 | }; |
| 278 | |
| 279 | /* |
| 280 | * Return -1 if component1 is less than component2, 1 if greater or 0 if equal. |
| 281 | * A component is less if it is shorter, otherwise if equal length do a byte comparison. |
| 282 | */ |
| 283 | Exclude.compareComponents = function(/*Uint8Array*/ component1, /*Uint8Array*/ component2) { |
| 284 | if (component1.length < component2.length) |
| 285 | return -1; |
| 286 | if (component1.length > component2.length) |
| 287 | return 1; |
| 288 | |
| 289 | for (var i = 0; i < component1.length; ++i) { |
| 290 | if (component1[i] < component2[i]) |
| 291 | return -1; |
| 292 | if (component1[i] > component2[i]) |
| 293 | return 1; |
| 294 | } |
| 295 | |
| 296 | return 0; |
| 297 | }; |