blob: f51b1dc806fdd84243eb5bf5fa710d0e775471c2 [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 Thompsonf3bd3592012-09-29 23:25:30 -0700117Interest.prototype.matches_name = function(/*Name*/ name){
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700118 var i_name = this.name.components;
119 var o_name = name.components;
Jeff Thompson86aea882012-09-29 17:32:48 -0700120
121 // The intrest name is longer than the name we are checking it against.
122 if (i_name.length > o_name.length)
123 return false;
124
125 // Check if at least one of given components doesn't match.
126 for (var i = 0; i < i_name.length; ++i) {
127 if (!DataUtils.arraysEqual(i_name[i], o_name[i]))
128 return false;
129 }
130
131 return true;
132}
133
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700134/**
135 * Exclude
136 */
137var Exclude = function Exclude(_values){
138
139 this.OPTIMUM_FILTER_SIZE = 100;
140
141
142 this.values = _values; //array of elements
143
144}
145
146Exclude.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) {
147
148
149
150 decoder.readStartElement(this.getElementLabel());
151
152 //TODO APPLY FILTERS/EXCLUDE
153
154 //TODO
155 /*var component;
156 var any = false;
157 while ((component = decoder.peekStartElement(CCNProtocolDTags.Component)) ||
158 (any = decoder.peekStartElement(CCNProtocolDTags.Any)) ||
159 decoder.peekStartElement(CCNProtocolDTags.Bloom)) {
160 var ee = component?new ExcludeComponent(): any ? new ExcludeAny() : new BloomFilter();
161 ee.decode(decoder);
162 _values.add(ee);
163 }*/
164
165 decoder.readEndElement();
166
167};
168
169Exclude.prototype.to_ccnb=function(/*XMLEncoder*/ encoder) {
170 if (!validate()) {
171 throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing.");
172 }
173
174 if (empty())
175 return;
176
177 encoder.writeStartElement(getElementLabel());
178
179 encoder.writeEndElement();
180
181 };
182
183Exclude.prototype.getElementLabel = function() { return CCNProtocolDTags.Exclude; };
184
185
186/**
187 * ExcludeAny
188 */
189var ExcludeAny = function ExcludeAny() {
190
191};
192
193ExcludeAny.prototype.from_ccnb = function(decoder) {
194 decoder.readStartElement(this.getElementLabel());
195 decoder.readEndElement();
196};
197
198
199ExcludeAny.prototype.to_ccnb = function( encoder) {
200 encoder.writeStartElement(this.getElementLabel());
201 encoder.writeEndElement();
202};
203
204ExcludeAny.prototype.getElementLabel=function() { return CCNProtocolDTags.Any; };
205
206
207/**
208 * ExcludeComponent
209 */
210var ExcludeComponent = function ExcludeComponent(_body) {
211
212 //TODO Check BODY is an Array of componenets.
213
214 this.body = _body
215};
216
217ExcludeComponent.prototype.from_ccnb = function( decoder) {
218 this.body = decoder.readBinaryElement(this.getElementLabel());
219};
220
221ExcludeComponent.prototype.to_ccnb = function(encoder) {
222 encoder.writeElement(this.getElementLabel(), this.body);
223};
224
225ExcludeComponent.prototype.getElementLabel = function() { return CCNProtocolDTags.Component; };