blob: 62c2e4801fe74e4f40483cfda57ff9bd2e770a37 [file] [log] [blame]
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -07001 /*
Jeff Thompson146d7de2012-11-17 16:15:28 -08002 * @author: Meki Cheraoui
Jeff Thompson745026e2012-10-13 12:49:20 -07003 * See COPYING for copyright and distribution information.
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -07004 * This class represents Interest Objects
5 */
6
Jeff Thompson86aea882012-09-29 17:32:48 -07007var Interest = function Interest(_name,_faceInstance,_minSuffixComponents,_maxSuffixComponents,_publisherPublicKeyDigest, _exclude, _childSelector,_answerOriginKind,_scope,_interestLifetime,_nonce){
Meki Cherkaoui8f173612012-06-06 01:05:40 -07008
Jeff Thompson86aea882012-09-29 17:32:48 -07009 this.name = _name;
10 this.faceInstance = _faceInstance;
11 this.maxSuffixComponents = _maxSuffixComponents;
12 this.minSuffixComponents = _minSuffixComponents;
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070013
Jeff Thompson86aea882012-09-29 17:32:48 -070014 this.publisherPublicKeyDigest = _publisherPublicKeyDigest;
15 this.exclude = _exclude;
16 this.childSelector = _childSelector;
17 this.answerOriginKind = _answerOriginKind;
18 this.scope = _scope;
Jeff Thompson5fc9b672012-11-24 10:00:56 -080019 this.interestLifetime = _interestLifetime; // number of seconds
20 this.nonce = _nonce;
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070021};
22
Jeff Thompson821183d2012-11-24 18:46:21 -080023Interest.RECURSIVE_POSTFIX = "*";
24
25Interest.CHILD_SELECTOR_LEFT = 0;
26Interest.CHILD_SELECTOR_RIGHT = 1;
27Interest.ANSWER_CONTENT_STORE = 1;
28Interest.ANSWER_GENERATED = 2;
29Interest.ANSWER_STALE = 4; // Stale answer OK
30Interest.MARK_STALE = 16; // Must have scope 0. Michael calls this a "hack"
31
32Interest.DEFAULT_ANSWER_ORIGIN_KIND = Interest.ANSWER_CONTENT_STORE | Interest.ANSWER_GENERATED;
33
34
Jeff Thompson86aea882012-09-29 17:32:48 -070035Interest.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) {
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070036
37 decoder.readStartElement(CCNProtocolDTags.Interest);
38
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070039 this.name = new Name();
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070040 this.name.from_ccnb(decoder);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070041
Jeff Thompson5fc9b672012-11-24 10:00:56 -080042 if (decoder.peekStartElement(CCNProtocolDTags.MinSuffixComponents))
Jeff Thompson86aea882012-09-29 17:32:48 -070043 this.minSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MinSuffixComponents);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070044
Jeff Thompson5fc9b672012-11-24 10:00:56 -080045 if (decoder.peekStartElement(CCNProtocolDTags.MaxSuffixComponents))
Jeff Thompson86aea882012-09-29 17:32:48 -070046 this.maxSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MaxSuffixComponents);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070047
Jeff Thompson86aea882012-09-29 17:32:48 -070048 if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) {
Jeff Thompson5fc9b672012-11-24 10:00:56 -080049 this.publisherPublicKeyDigest = new PublisherPublicKeyDigest();
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070050 this.publisherPublicKeyDigest.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070051 }
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070052
53 if (decoder.peekStartElement(CCNProtocolDTags.Exclude)) {
Jeff Thompson86aea882012-09-29 17:32:48 -070054 this.exclude = new Exclude();
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070055 this.exclude.from_ccnb(decoder);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070056 }
57
Jeff Thompson5fc9b672012-11-24 10:00:56 -080058 if (decoder.peekStartElement(CCNProtocolDTags.ChildSelector))
Jeff Thompson86aea882012-09-29 17:32:48 -070059 this.childSelector = decoder.readIntegerElement(CCNProtocolDTags.ChildSelector);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070060
Jeff Thompson5fc9b672012-11-24 10:00:56 -080061 if (decoder.peekStartElement(CCNProtocolDTags.AnswerOriginKind))
Jeff Thompson86aea882012-09-29 17:32:48 -070062 this.answerOriginKind = decoder.readIntegerElement(CCNProtocolDTags.AnswerOriginKind);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070063
Jeff Thompson5fc9b672012-11-24 10:00:56 -080064 if (decoder.peekStartElement(CCNProtocolDTags.Scope))
Jeff Thompson86aea882012-09-29 17:32:48 -070065 this.scope = decoder.readIntegerElement(CCNProtocolDTags.Scope);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070066
Jeff Thompson5fc9b672012-11-24 10:00:56 -080067 if (decoder.peekStartElement(CCNProtocolDTags.InterestLifetime))
68 this.interestLifetime = DataUtils.bigEndianToUnsignedInt
69 (decoder.readBinaryElement(CCNProtocolDTags.InterestLifetime)) / 4096;
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070070
Jeff Thompson5fc9b672012-11-24 10:00:56 -080071 if (decoder.peekStartElement(CCNProtocolDTags.Nonce))
Jeff Thompson86aea882012-09-29 17:32:48 -070072 this.nonce = decoder.readBinaryElement(CCNProtocolDTags.Nonce);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070073
74 decoder.readEndElement();
75};
76
Jeff Thompson86aea882012-09-29 17:32:48 -070077Interest.prototype.to_ccnb = function(/*XMLEncoder*/ encoder){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070078 //Could check if name is present
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070079
80 encoder.writeStartElement(CCNProtocolDTags.Interest);
81
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070082 this.name.to_ccnb(encoder);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070083
Jeff Thompson86aea882012-09-29 17:32:48 -070084 if (null != this.minSuffixComponents)
85 encoder.writeElement(CCNProtocolDTags.MinSuffixComponents, this.minSuffixComponents);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070086
Jeff Thompson86aea882012-09-29 17:32:48 -070087 if (null != this.maxSuffixComponents)
88 encoder.writeElement(CCNProtocolDTags.MaxSuffixComponents, this.maxSuffixComponents);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070089
Jeff Thompson86aea882012-09-29 17:32:48 -070090 if (null != this.publisherPublicKeyDigest)
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070091 this.publisherPublicKeyDigest.to_ccnb(encoder);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070092
Jeff Thompson86aea882012-09-29 17:32:48 -070093 if (null != this.exclude)
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070094 this.exclude.to_ccnb(encoder);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070095
Jeff Thompson86aea882012-09-29 17:32:48 -070096 if (null != this.childSelector)
97 encoder.writeElement(CCNProtocolDTags.ChildSelector, this.childSelector);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070098
Jeff Thompson86aea882012-09-29 17:32:48 -070099 if (this.DEFAULT_ANSWER_ORIGIN_KIND != this.answerOriginKind && this.answerOriginKind!=null)
100 encoder.writeElement(CCNProtocolDTags.AnswerOriginKind, this.answerOriginKind);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700101
Jeff Thompson86aea882012-09-29 17:32:48 -0700102 if (null != this.scope)
103 encoder.writeElement(CCNProtocolDTags.Scope, this.scope);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700104
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800105 if (null != this.interestLifetime)
106 encoder.writeElement(CCNProtocolDTags.InterestLifetime,
107 DataUtils.nonNegativeIntToBigEndian(this.interestLifetime * 4096));
108
Jeff Thompson86aea882012-09-29 17:32:48 -0700109 if (null != this.nonce)
110 encoder.writeElement(CCNProtocolDTags.Nonce, this.nonce);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700111
112 encoder.writeEndElement();
113
114};
115
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700116Interest.prototype.matches_name = function(/*Name*/ name){
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700117 var i_name = this.name.components;
118 var o_name = name.components;
Jeff Thompson86aea882012-09-29 17:32:48 -0700119
120 // The intrest name is longer than the name we are checking it against.
121 if (i_name.length > o_name.length)
122 return false;
123
124 // Check if at least one of given components doesn't match.
125 for (var i = 0; i < i_name.length; ++i) {
126 if (!DataUtils.arraysEqual(i_name[i], o_name[i]))
127 return false;
128 }
129
130 return true;
131}
132
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700133/**
134 * Exclude
135 */
136var Exclude = function Exclude(_values){
137
138 this.OPTIMUM_FILTER_SIZE = 100;
139
140
141 this.values = _values; //array of elements
142
143}
144
145Exclude.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) {
146
147
148
149 decoder.readStartElement(this.getElementLabel());
150
151 //TODO APPLY FILTERS/EXCLUDE
152
153 //TODO
154 /*var component;
155 var any = false;
156 while ((component = decoder.peekStartElement(CCNProtocolDTags.Component)) ||
157 (any = decoder.peekStartElement(CCNProtocolDTags.Any)) ||
158 decoder.peekStartElement(CCNProtocolDTags.Bloom)) {
159 var ee = component?new ExcludeComponent(): any ? new ExcludeAny() : new BloomFilter();
160 ee.decode(decoder);
161 _values.add(ee);
162 }*/
163
164 decoder.readEndElement();
165
166};
167
168Exclude.prototype.to_ccnb=function(/*XMLEncoder*/ encoder) {
169 if (!validate()) {
170 throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing.");
171 }
172
173 if (empty())
174 return;
175
176 encoder.writeStartElement(getElementLabel());
177
178 encoder.writeEndElement();
179
180 };
181
182Exclude.prototype.getElementLabel = function() { return CCNProtocolDTags.Exclude; };
183
184
185/**
186 * ExcludeAny
187 */
188var ExcludeAny = function ExcludeAny() {
189
190};
191
192ExcludeAny.prototype.from_ccnb = function(decoder) {
193 decoder.readStartElement(this.getElementLabel());
194 decoder.readEndElement();
195};
196
197
198ExcludeAny.prototype.to_ccnb = function( encoder) {
199 encoder.writeStartElement(this.getElementLabel());
200 encoder.writeEndElement();
201};
202
203ExcludeAny.prototype.getElementLabel=function() { return CCNProtocolDTags.Any; };
204
205
206/**
207 * ExcludeComponent
208 */
209var ExcludeComponent = function ExcludeComponent(_body) {
210
211 //TODO Check BODY is an Array of componenets.
212
213 this.body = _body
214};
215
216ExcludeComponent.prototype.from_ccnb = function( decoder) {
217 this.body = decoder.readBinaryElement(this.getElementLabel());
218};
219
220ExcludeComponent.prototype.to_ccnb = function(encoder) {
221 encoder.writeElement(this.getElementLabel(), this.body);
222};
223
224ExcludeComponent.prototype.getElementLabel = function() { return CCNProtocolDTags.Component; };