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 | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 8 | var Interest = function Interest(_name,_faceInstance,_minSuffixComponents,_maxSuffixComponents,_publisherPublicKeyDigest, _exclude, _childSelector,_answerOriginKind,_scope,_interestLifetime,_nonce){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 9 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 10 | this.name = _name; |
| 11 | this.faceInstance = _faceInstance; |
| 12 | this.maxSuffixComponents = _maxSuffixComponents; |
| 13 | this.minSuffixComponents = _minSuffixComponents; |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 14 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 15 | this.publisherPublicKeyDigest = _publisherPublicKeyDigest; |
| 16 | this.exclude = _exclude; |
| 17 | this.childSelector = _childSelector; |
| 18 | this.answerOriginKind = _answerOriginKind; |
| 19 | this.scope = _scope; |
Jeff Thompson | 42806a1 | 2012-12-29 18:19:39 -0800 | [diff] [blame] | 20 | this.interestLifetime = _interestLifetime; // milli seconds |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 21 | this.nonce = _nonce; |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 22 | }; |
| 23 | |
Jeff Thompson | 821183d | 2012-11-24 18:46:21 -0800 | [diff] [blame] | 24 | Interest.RECURSIVE_POSTFIX = "*"; |
| 25 | |
| 26 | Interest.CHILD_SELECTOR_LEFT = 0; |
| 27 | Interest.CHILD_SELECTOR_RIGHT = 1; |
| 28 | Interest.ANSWER_CONTENT_STORE = 1; |
| 29 | Interest.ANSWER_GENERATED = 2; |
| 30 | Interest.ANSWER_STALE = 4; // Stale answer OK |
| 31 | Interest.MARK_STALE = 16; // Must have scope 0. Michael calls this a "hack" |
| 32 | |
| 33 | Interest.DEFAULT_ANSWER_ORIGIN_KIND = Interest.ANSWER_CONTENT_STORE | Interest.ANSWER_GENERATED; |
| 34 | |
| 35 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 36 | Interest.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) { |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 37 | |
| 38 | decoder.readStartElement(CCNProtocolDTags.Interest); |
| 39 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 40 | this.name = new Name(); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 41 | this.name.from_ccnb(decoder); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 42 | |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 43 | if (decoder.peekStartElement(CCNProtocolDTags.MinSuffixComponents)) |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 44 | this.minSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MinSuffixComponents); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 45 | |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 46 | if (decoder.peekStartElement(CCNProtocolDTags.MaxSuffixComponents)) |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 47 | this.maxSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MaxSuffixComponents); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 48 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 49 | if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) { |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 50 | this.publisherPublicKeyDigest = new PublisherPublicKeyDigest(); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 51 | this.publisherPublicKeyDigest.from_ccnb(decoder); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 52 | } |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 53 | |
| 54 | if (decoder.peekStartElement(CCNProtocolDTags.Exclude)) { |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 55 | this.exclude = new Exclude(); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 56 | this.exclude.from_ccnb(decoder); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 59 | if (decoder.peekStartElement(CCNProtocolDTags.ChildSelector)) |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 60 | this.childSelector = decoder.readIntegerElement(CCNProtocolDTags.ChildSelector); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 61 | |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 62 | if (decoder.peekStartElement(CCNProtocolDTags.AnswerOriginKind)) |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 63 | this.answerOriginKind = decoder.readIntegerElement(CCNProtocolDTags.AnswerOriginKind); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 64 | |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 65 | if (decoder.peekStartElement(CCNProtocolDTags.Scope)) |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 66 | this.scope = decoder.readIntegerElement(CCNProtocolDTags.Scope); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 67 | |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 68 | if (decoder.peekStartElement(CCNProtocolDTags.InterestLifetime)) |
Jeff Thompson | 42806a1 | 2012-12-29 18:19:39 -0800 | [diff] [blame] | 69 | this.interestLifetime = 1000.0 * DataUtils.bigEndianToUnsignedInt |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 70 | (decoder.readBinaryElement(CCNProtocolDTags.InterestLifetime)) / 4096; |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 71 | |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 72 | if (decoder.peekStartElement(CCNProtocolDTags.Nonce)) |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 73 | this.nonce = decoder.readBinaryElement(CCNProtocolDTags.Nonce); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 74 | |
| 75 | decoder.readEndElement(); |
| 76 | }; |
| 77 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 78 | Interest.prototype.to_ccnb = function(/*XMLEncoder*/ encoder){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 79 | //Could check if name is present |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 80 | |
| 81 | encoder.writeStartElement(CCNProtocolDTags.Interest); |
| 82 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 83 | this.name.to_ccnb(encoder); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 84 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 85 | if (null != this.minSuffixComponents) |
| 86 | encoder.writeElement(CCNProtocolDTags.MinSuffixComponents, this.minSuffixComponents); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 87 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 88 | if (null != this.maxSuffixComponents) |
| 89 | encoder.writeElement(CCNProtocolDTags.MaxSuffixComponents, this.maxSuffixComponents); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 90 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 91 | if (null != this.publisherPublicKeyDigest) |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 92 | this.publisherPublicKeyDigest.to_ccnb(encoder); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 93 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 94 | if (null != this.exclude) |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 95 | this.exclude.to_ccnb(encoder); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 96 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 97 | if (null != this.childSelector) |
| 98 | encoder.writeElement(CCNProtocolDTags.ChildSelector, this.childSelector); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 99 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 100 | if (this.DEFAULT_ANSWER_ORIGIN_KIND != this.answerOriginKind && this.answerOriginKind!=null) |
| 101 | encoder.writeElement(CCNProtocolDTags.AnswerOriginKind, this.answerOriginKind); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 102 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 103 | if (null != this.scope) |
| 104 | encoder.writeElement(CCNProtocolDTags.Scope, this.scope); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 105 | |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 106 | if (null != this.interestLifetime) |
| 107 | encoder.writeElement(CCNProtocolDTags.InterestLifetime, |
Jeff Thompson | 42806a1 | 2012-12-29 18:19:39 -0800 | [diff] [blame] | 108 | DataUtils.nonNegativeIntToBigEndian((this.interestLifetime / 1000.0) * 4096)); |
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 109 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 110 | if (null != this.nonce) |
| 111 | encoder.writeElement(CCNProtocolDTags.Nonce, this.nonce); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 112 | |
| 113 | encoder.writeEndElement(); |
| 114 | |
| 115 | }; |
| 116 | |
Jeff Thompson | 3f4be55 | 2013-01-05 22:36:40 -0800 | [diff] [blame] | 117 | Interest.prototype.matches_name = function(/*Name*/ name) { |
| 118 | return this.name.match(name); |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Jeff Thompson | 08d41b7 | 2013-02-03 23:09:29 -0800 | [diff] [blame] | 121 | /* |
| 122 | * Handle the interest Exclude element. |
| 123 | * _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] | 124 | */ |
Jeff Thompson | 08d41b7 | 2013-02-03 23:09:29 -0800 | [diff] [blame] | 125 | var Exclude = function Exclude(_values) { |
| 126 | this.values = (_values || []); |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Jeff Thompson | 08d41b7 | 2013-02-03 23:09:29 -0800 | [diff] [blame] | 129 | Exclude.ANY = "*"; |
| 130 | |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 131 | Exclude.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) { |
Jeff Thompson | 08d41b7 | 2013-02-03 23:09:29 -0800 | [diff] [blame] | 132 | decoder.readStartElement(CCNProtocolDTags.Exclude); |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 133 | |
Jeff Thompson | 08d41b7 | 2013-02-03 23:09:29 -0800 | [diff] [blame] | 134 | while (true) { |
| 135 | if (decoder.peekStartElement(CCNProtocolDTags.Component)) |
| 136 | this.values.push(decoder.readBinaryElement(CCNProtocolDTags.Component)); |
| 137 | else if (decoder.peekStartElement(CCNProtocolDTags.Any)) { |
| 138 | decoder.readStartElement(CCNProtocolDTags.Any); |
| 139 | decoder.readEndElement(); |
| 140 | this.values.push(Exclude.ANY); |
| 141 | } |
| 142 | else if (decoder.peekStartElement(CCNProtocolDTags.Bloom)) { |
| 143 | // Skip the Bloom and treat it as Any. |
| 144 | decoder.readBinaryElement(CCNProtocolDTags.Bloom); |
| 145 | this.values.push(Exclude.ANY); |
| 146 | } |
| 147 | else |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | decoder.readEndElement(); |
| 152 | }; |
| 153 | |
| 154 | Exclude.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) { |
| 155 | if (this.values == null || this.values.length == 0) |
| 156 | return; |
| 157 | |
| 158 | encoder.writeStartElement(CCNProtocolDTags.Exclude); |
| 159 | |
| 160 | // TODO: Do we want to order the components (except for ANY)? |
| 161 | for (var i = 0; i < this.values.length; ++i) { |
| 162 | if (this.values[i] == Exclude.ANY) { |
| 163 | encoder.writeStartElement(CCNProtocolDTags.Any); |
| 164 | encoder.writeEndElement(); |
| 165 | } |
| 166 | else |
| 167 | encoder.writeElement(CCNProtocolDTags.Component, this.values[i]); |
| 168 | } |
| 169 | |
| 170 | encoder.writeEndElement(); |
| 171 | }; |
| 172 | |
| 173 | // Return a string with elements separated by "," and Exclude.ANY shown as "*". |
| 174 | Exclude.prototype.to_uri = function() { |
| 175 | if (this.values == null || this.values.length == 0) |
| 176 | return ""; |
| 177 | |
| 178 | var result = ""; |
| 179 | for (var i = 0; i < this.values.length; ++i) { |
| 180 | if (i > 0) |
| 181 | result += ","; |
Jeff Thompson | fced4c2 | 2013-01-27 16:34:13 -0800 | [diff] [blame] | 182 | |
Jeff Thompson | 08d41b7 | 2013-02-03 23:09:29 -0800 | [diff] [blame] | 183 | if (this.values[i] == Exclude.ANY) |
| 184 | result += "*"; |
| 185 | else |
| 186 | result += Name.toEscapedString(this.values[i]); |
| 187 | } |
| 188 | return result; |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 189 | }; |