blob: 6553091ed80033ff940dba2aae61114431ab98d9 [file] [log] [blame]
Wentao Shangbd63e462012-12-03 16:19:33 -08001/**
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 Thompson42806a12012-12-29 18:19:39 -08007// _interestLifetime is in milliseconds.
Jeff Thompson86aea882012-09-29 17:32:48 -07008var Interest = function Interest(_name,_faceInstance,_minSuffixComponents,_maxSuffixComponents,_publisherPublicKeyDigest, _exclude, _childSelector,_answerOriginKind,_scope,_interestLifetime,_nonce){
Meki Cherkaoui8f173612012-06-06 01:05:40 -07009
Jeff Thompson86aea882012-09-29 17:32:48 -070010 this.name = _name;
11 this.faceInstance = _faceInstance;
12 this.maxSuffixComponents = _maxSuffixComponents;
13 this.minSuffixComponents = _minSuffixComponents;
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070014
Jeff Thompson86aea882012-09-29 17:32:48 -070015 this.publisherPublicKeyDigest = _publisherPublicKeyDigest;
16 this.exclude = _exclude;
17 this.childSelector = _childSelector;
18 this.answerOriginKind = _answerOriginKind;
19 this.scope = _scope;
Jeff Thompson42806a12012-12-29 18:19:39 -080020 this.interestLifetime = _interestLifetime; // milli seconds
Jeff Thompson5fc9b672012-11-24 10:00:56 -080021 this.nonce = _nonce;
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070022};
23
Jeff Thompson821183d2012-11-24 18:46:21 -080024Interest.RECURSIVE_POSTFIX = "*";
25
26Interest.CHILD_SELECTOR_LEFT = 0;
27Interest.CHILD_SELECTOR_RIGHT = 1;
28Interest.ANSWER_CONTENT_STORE = 1;
29Interest.ANSWER_GENERATED = 2;
30Interest.ANSWER_STALE = 4; // Stale answer OK
31Interest.MARK_STALE = 16; // Must have scope 0. Michael calls this a "hack"
32
33Interest.DEFAULT_ANSWER_ORIGIN_KIND = Interest.ANSWER_CONTENT_STORE | Interest.ANSWER_GENERATED;
34
35
Jeff Thompson86aea882012-09-29 17:32:48 -070036Interest.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) {
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070037
38 decoder.readStartElement(CCNProtocolDTags.Interest);
39
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070040 this.name = new Name();
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070041 this.name.from_ccnb(decoder);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070042
Jeff Thompson5fc9b672012-11-24 10:00:56 -080043 if (decoder.peekStartElement(CCNProtocolDTags.MinSuffixComponents))
Jeff Thompson86aea882012-09-29 17:32:48 -070044 this.minSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MinSuffixComponents);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070045
Jeff Thompson5fc9b672012-11-24 10:00:56 -080046 if (decoder.peekStartElement(CCNProtocolDTags.MaxSuffixComponents))
Jeff Thompson86aea882012-09-29 17:32:48 -070047 this.maxSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MaxSuffixComponents);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070048
Jeff Thompson86aea882012-09-29 17:32:48 -070049 if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) {
Jeff Thompson5fc9b672012-11-24 10:00:56 -080050 this.publisherPublicKeyDigest = new PublisherPublicKeyDigest();
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070051 this.publisherPublicKeyDigest.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070052 }
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070053
54 if (decoder.peekStartElement(CCNProtocolDTags.Exclude)) {
Jeff Thompson86aea882012-09-29 17:32:48 -070055 this.exclude = new Exclude();
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070056 this.exclude.from_ccnb(decoder);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070057 }
58
Jeff Thompson5fc9b672012-11-24 10:00:56 -080059 if (decoder.peekStartElement(CCNProtocolDTags.ChildSelector))
Jeff Thompson86aea882012-09-29 17:32:48 -070060 this.childSelector = decoder.readIntegerElement(CCNProtocolDTags.ChildSelector);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070061
Jeff Thompson5fc9b672012-11-24 10:00:56 -080062 if (decoder.peekStartElement(CCNProtocolDTags.AnswerOriginKind))
Jeff Thompson86aea882012-09-29 17:32:48 -070063 this.answerOriginKind = decoder.readIntegerElement(CCNProtocolDTags.AnswerOriginKind);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070064
Jeff Thompson5fc9b672012-11-24 10:00:56 -080065 if (decoder.peekStartElement(CCNProtocolDTags.Scope))
Jeff Thompson86aea882012-09-29 17:32:48 -070066 this.scope = decoder.readIntegerElement(CCNProtocolDTags.Scope);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070067
Jeff Thompson5fc9b672012-11-24 10:00:56 -080068 if (decoder.peekStartElement(CCNProtocolDTags.InterestLifetime))
Jeff Thompson42806a12012-12-29 18:19:39 -080069 this.interestLifetime = 1000.0 * DataUtils.bigEndianToUnsignedInt
Jeff Thompson5fc9b672012-11-24 10:00:56 -080070 (decoder.readBinaryElement(CCNProtocolDTags.InterestLifetime)) / 4096;
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070071
Jeff Thompson5fc9b672012-11-24 10:00:56 -080072 if (decoder.peekStartElement(CCNProtocolDTags.Nonce))
Jeff Thompson86aea882012-09-29 17:32:48 -070073 this.nonce = decoder.readBinaryElement(CCNProtocolDTags.Nonce);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070074
75 decoder.readEndElement();
76};
77
Jeff Thompson86aea882012-09-29 17:32:48 -070078Interest.prototype.to_ccnb = function(/*XMLEncoder*/ encoder){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070079 //Could check if name is present
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070080
81 encoder.writeStartElement(CCNProtocolDTags.Interest);
82
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070083 this.name.to_ccnb(encoder);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070084
Jeff Thompson86aea882012-09-29 17:32:48 -070085 if (null != this.minSuffixComponents)
86 encoder.writeElement(CCNProtocolDTags.MinSuffixComponents, this.minSuffixComponents);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070087
Jeff Thompson86aea882012-09-29 17:32:48 -070088 if (null != this.maxSuffixComponents)
89 encoder.writeElement(CCNProtocolDTags.MaxSuffixComponents, this.maxSuffixComponents);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070090
Jeff Thompson86aea882012-09-29 17:32:48 -070091 if (null != this.publisherPublicKeyDigest)
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070092 this.publisherPublicKeyDigest.to_ccnb(encoder);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070093
Jeff Thompson86aea882012-09-29 17:32:48 -070094 if (null != this.exclude)
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070095 this.exclude.to_ccnb(encoder);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070096
Jeff Thompson86aea882012-09-29 17:32:48 -070097 if (null != this.childSelector)
98 encoder.writeElement(CCNProtocolDTags.ChildSelector, this.childSelector);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070099
Jeff Thompson86aea882012-09-29 17:32:48 -0700100 if (this.DEFAULT_ANSWER_ORIGIN_KIND != this.answerOriginKind && this.answerOriginKind!=null)
101 encoder.writeElement(CCNProtocolDTags.AnswerOriginKind, this.answerOriginKind);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700102
Jeff Thompson86aea882012-09-29 17:32:48 -0700103 if (null != this.scope)
104 encoder.writeElement(CCNProtocolDTags.Scope, this.scope);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700105
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800106 if (null != this.interestLifetime)
107 encoder.writeElement(CCNProtocolDTags.InterestLifetime,
Jeff Thompson42806a12012-12-29 18:19:39 -0800108 DataUtils.nonNegativeIntToBigEndian((this.interestLifetime / 1000.0) * 4096));
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800109
Jeff Thompson86aea882012-09-29 17:32:48 -0700110 if (null != this.nonce)
111 encoder.writeElement(CCNProtocolDTags.Nonce, this.nonce);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700112
113 encoder.writeEndElement();
114
115};
116
Jeff Thompson3f4be552013-01-05 22:36:40 -0800117Interest.prototype.matches_name = function(/*Name*/ name) {
118 return this.name.match(name);
Jeff Thompson86aea882012-09-29 17:32:48 -0700119}
120
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700121/**
122 * Exclude
123 */
124var Exclude = function Exclude(_values){
125
126 this.OPTIMUM_FILTER_SIZE = 100;
127
128
129 this.values = _values; //array of elements
130
131}
132
133Exclude.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) {
134
135
136
137 decoder.readStartElement(this.getElementLabel());
138
139 //TODO APPLY FILTERS/EXCLUDE
140
141 //TODO
142 /*var component;
143 var any = false;
144 while ((component = decoder.peekStartElement(CCNProtocolDTags.Component)) ||
145 (any = decoder.peekStartElement(CCNProtocolDTags.Any)) ||
146 decoder.peekStartElement(CCNProtocolDTags.Bloom)) {
147 var ee = component?new ExcludeComponent(): any ? new ExcludeAny() : new BloomFilter();
148 ee.decode(decoder);
149 _values.add(ee);
150 }*/
151
152 decoder.readEndElement();
153
154};
155
156Exclude.prototype.to_ccnb=function(/*XMLEncoder*/ encoder) {
157 if (!validate()) {
158 throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing.");
159 }
160
161 if (empty())
162 return;
163
164 encoder.writeStartElement(getElementLabel());
165
166 encoder.writeEndElement();
167
168 };
169
170Exclude.prototype.getElementLabel = function() { return CCNProtocolDTags.Exclude; };
171
172
173/**
174 * ExcludeAny
175 */
176var ExcludeAny = function ExcludeAny() {
177
178};
179
180ExcludeAny.prototype.from_ccnb = function(decoder) {
181 decoder.readStartElement(this.getElementLabel());
182 decoder.readEndElement();
183};
184
185
186ExcludeAny.prototype.to_ccnb = function( encoder) {
187 encoder.writeStartElement(this.getElementLabel());
188 encoder.writeEndElement();
189};
190
191ExcludeAny.prototype.getElementLabel=function() { return CCNProtocolDTags.Any; };
192
193
194/**
195 * ExcludeComponent
196 */
197var ExcludeComponent = function ExcludeComponent(_body) {
198
199 //TODO Check BODY is an Array of componenets.
200
201 this.body = _body
202};
203
204ExcludeComponent.prototype.from_ccnb = function( decoder) {
205 this.body = decoder.readBinaryElement(this.getElementLabel());
206};
207
208ExcludeComponent.prototype.to_ccnb = function(encoder) {
209 encoder.writeElement(this.getElementLabel(), this.body);
210};
211
212ExcludeComponent.prototype.getElementLabel = function() { return CCNProtocolDTags.Component; };