Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [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 | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 7 | 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] | 8 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 9 | this.name = _name; |
| 10 | this.faceInstance = _faceInstance; |
| 11 | this.maxSuffixComponents = _maxSuffixComponents; |
| 12 | this.minSuffixComponents = _minSuffixComponents; |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 13 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 14 | this.publisherPublicKeyDigest = _publisherPublicKeyDigest; |
| 15 | this.exclude = _exclude; |
| 16 | this.childSelector = _childSelector; |
| 17 | this.answerOriginKind = _answerOriginKind; |
| 18 | this.scope = _scope; |
| 19 | this.interestLifetime = null; // For now we don't have the ability to set an interest lifetime |
| 20 | this.nonce = _nonce; |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 21 | |
| 22 | |
| 23 | this.RECURSIVE_POSTFIX = "*"; |
| 24 | |
| 25 | this.CHILD_SELECTOR_LEFT = 0; |
| 26 | this.CHILD_SELECTOR_RIGHT = 1; |
| 27 | this.ANSWER_CONTENT_STORE = 1; |
| 28 | this.ANSWER_GENERATED = 2; |
| 29 | this.ANSWER_STALE = 4; // Stale answer OK |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 30 | this.MARK_STALE = 16; // Must have scope 0. Michael calls this a "hack" |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 31 | |
| 32 | this.DEFAULT_ANSWER_ORIGIN_KIND = this.ANSWER_CONTENT_STORE | this.ANSWER_GENERATED; |
| 33 | |
| 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 | |
| 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 | } |
| 46 | |
| 47 | if (decoder.peekStartElement(CCNProtocolDTags.MaxSuffixComponents)) { |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 48 | this.maxSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MaxSuffixComponents); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 51 | if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 52 | this.publisherPublicKeyDigest = new publisherPublicKeyDigest(); |
| 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 | |
| 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 | } |
| 64 | |
| 65 | if (decoder.peekStartElement(CCNProtocolDTags.AnswerOriginKind)) { |
| 66 | // call setter to handle defaulting |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 67 | this.answerOriginKind = decoder.readIntegerElement(CCNProtocolDTags.AnswerOriginKind); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | if (decoder.peekStartElement(CCNProtocolDTags.Scope)) { |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 71 | this.scope = decoder.readIntegerElement(CCNProtocolDTags.Scope); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | if (decoder.peekStartElement(CCNProtocolDTags.InterestLifetime)) { |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 75 | this.interestLifetime = decoder.readBinaryElement(CCNProtocolDTags.InterestLifetime); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | if (decoder.peekStartElement(CCNProtocolDTags.Nonce)) { |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 79 | this.nonce = decoder.readBinaryElement(CCNProtocolDTags.Nonce); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | decoder.readEndElement(); |
| 83 | }; |
| 84 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 85 | Interest.prototype.to_ccnb = function(/*XMLEncoder*/ encoder){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 86 | //Could check if name is present |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 87 | |
| 88 | encoder.writeStartElement(CCNProtocolDTags.Interest); |
| 89 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 90 | this.name.to_ccnb(encoder); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 91 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 92 | if (null != this.minSuffixComponents) |
| 93 | encoder.writeElement(CCNProtocolDTags.MinSuffixComponents, this.minSuffixComponents); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 94 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 95 | if (null != this.maxSuffixComponents) |
| 96 | encoder.writeElement(CCNProtocolDTags.MaxSuffixComponents, this.maxSuffixComponents); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 97 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 98 | if (null != this.publisherPublicKeyDigest) |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 99 | this.publisherPublicKeyDigest.to_ccnb(encoder); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 100 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 101 | if (null != this.exclude) |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 102 | this.exclude.to_ccnb(encoder); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 103 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 104 | if (null != this.childSelector) |
| 105 | encoder.writeElement(CCNProtocolDTags.ChildSelector, this.childSelector); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 106 | |
| 107 | //TODO Encode OriginKind |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 108 | if (this.DEFAULT_ANSWER_ORIGIN_KIND != this.answerOriginKind && this.answerOriginKind!=null) |
| 109 | encoder.writeElement(CCNProtocolDTags.AnswerOriginKind, this.answerOriginKind); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 110 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 111 | if (null != this.scope) |
| 112 | encoder.writeElement(CCNProtocolDTags.Scope, this.scope); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 113 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 114 | if (null != this.nonce) |
| 115 | encoder.writeElement(CCNProtocolDTags.Nonce, this.nonce); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 116 | |
| 117 | encoder.writeEndElement(); |
| 118 | |
| 119 | }; |
| 120 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 121 | Interest.prototype.matches_name = function(/*Name*/ name){ |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 122 | var i_name = this.name.components; |
| 123 | var o_name = name.components; |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 124 | |
| 125 | // The intrest name is longer than the name we are checking it against. |
| 126 | if (i_name.length > o_name.length) |
| 127 | return false; |
| 128 | |
| 129 | // Check if at least one of given components doesn't match. |
| 130 | for (var i = 0; i < i_name.length; ++i) { |
| 131 | if (!DataUtils.arraysEqual(i_name[i], o_name[i])) |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | return true; |
| 136 | } |
| 137 | |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 138 | /** |
| 139 | * Exclude |
| 140 | */ |
| 141 | var Exclude = function Exclude(_values){ |
| 142 | |
| 143 | this.OPTIMUM_FILTER_SIZE = 100; |
| 144 | |
| 145 | |
| 146 | this.values = _values; //array of elements |
| 147 | |
| 148 | } |
| 149 | |
| 150 | Exclude.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) { |
| 151 | |
| 152 | |
| 153 | |
| 154 | decoder.readStartElement(this.getElementLabel()); |
| 155 | |
| 156 | //TODO APPLY FILTERS/EXCLUDE |
| 157 | |
| 158 | //TODO |
| 159 | /*var component; |
| 160 | var any = false; |
| 161 | while ((component = decoder.peekStartElement(CCNProtocolDTags.Component)) || |
| 162 | (any = decoder.peekStartElement(CCNProtocolDTags.Any)) || |
| 163 | decoder.peekStartElement(CCNProtocolDTags.Bloom)) { |
| 164 | var ee = component?new ExcludeComponent(): any ? new ExcludeAny() : new BloomFilter(); |
| 165 | ee.decode(decoder); |
| 166 | _values.add(ee); |
| 167 | }*/ |
| 168 | |
| 169 | decoder.readEndElement(); |
| 170 | |
| 171 | }; |
| 172 | |
| 173 | Exclude.prototype.to_ccnb=function(/*XMLEncoder*/ encoder) { |
| 174 | if (!validate()) { |
| 175 | throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing."); |
| 176 | } |
| 177 | |
| 178 | if (empty()) |
| 179 | return; |
| 180 | |
| 181 | encoder.writeStartElement(getElementLabel()); |
| 182 | |
| 183 | encoder.writeEndElement(); |
| 184 | |
| 185 | }; |
| 186 | |
| 187 | Exclude.prototype.getElementLabel = function() { return CCNProtocolDTags.Exclude; }; |
| 188 | |
| 189 | |
| 190 | /** |
| 191 | * ExcludeAny |
| 192 | */ |
| 193 | var ExcludeAny = function ExcludeAny() { |
| 194 | |
| 195 | }; |
| 196 | |
| 197 | ExcludeAny.prototype.from_ccnb = function(decoder) { |
| 198 | decoder.readStartElement(this.getElementLabel()); |
| 199 | decoder.readEndElement(); |
| 200 | }; |
| 201 | |
| 202 | |
| 203 | ExcludeAny.prototype.to_ccnb = function( encoder) { |
| 204 | encoder.writeStartElement(this.getElementLabel()); |
| 205 | encoder.writeEndElement(); |
| 206 | }; |
| 207 | |
| 208 | ExcludeAny.prototype.getElementLabel=function() { return CCNProtocolDTags.Any; }; |
| 209 | |
| 210 | |
| 211 | /** |
| 212 | * ExcludeComponent |
| 213 | */ |
| 214 | var ExcludeComponent = function ExcludeComponent(_body) { |
| 215 | |
| 216 | //TODO Check BODY is an Array of componenets. |
| 217 | |
| 218 | this.body = _body |
| 219 | }; |
| 220 | |
| 221 | ExcludeComponent.prototype.from_ccnb = function( decoder) { |
| 222 | this.body = decoder.readBinaryElement(this.getElementLabel()); |
| 223 | }; |
| 224 | |
| 225 | ExcludeComponent.prototype.to_ccnb = function(encoder) { |
| 226 | encoder.writeElement(this.getElementLabel(), this.body); |
| 227 | }; |
| 228 | |
| 229 | ExcludeComponent.prototype.getElementLabel = function() { return CCNProtocolDTags.Component; }; |