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