blob: 7e6ddc47505d9ea5eba3aabfcaaab6bf4a131c8c [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;
19 this.interestLifetime = null; // For now we don't have the ability to set an interest lifetime
20 this.nonce = _nonce;
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070021
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 Thompson86aea882012-09-29 17:32:48 -070030 this.MARK_STALE = 16; // Must have scope 0. Michael calls this a "hack"
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070031
32 this.DEFAULT_ANSWER_ORIGIN_KIND = this.ANSWER_CONTENT_STORE | this.ANSWER_GENERATED;
33
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
43 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 }
46
47 if (decoder.peekStartElement(CCNProtocolDTags.MaxSuffixComponents)) {
Jeff Thompson86aea882012-09-29 17:32:48 -070048 this.maxSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MaxSuffixComponents);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070049 }
50
Jeff Thompson86aea882012-09-29 17:32:48 -070051 if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070052 this.publisherPublicKeyDigest = new publisherPublicKeyDigest();
53 this.publisherPublicKeyDigest.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070054 }
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070055
56 if (decoder.peekStartElement(CCNProtocolDTags.Exclude)) {
Jeff Thompson86aea882012-09-29 17:32:48 -070057 this.exclude = new Exclude();
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070058 this.exclude.from_ccnb(decoder);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070059 }
60
61 if (decoder.peekStartElement(CCNProtocolDTags.ChildSelector)) {
Jeff Thompson86aea882012-09-29 17:32:48 -070062 this.childSelector = decoder.readIntegerElement(CCNProtocolDTags.ChildSelector);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070063 }
64
65 if (decoder.peekStartElement(CCNProtocolDTags.AnswerOriginKind)) {
66 // call setter to handle defaulting
Jeff Thompson86aea882012-09-29 17:32:48 -070067 this.answerOriginKind = decoder.readIntegerElement(CCNProtocolDTags.AnswerOriginKind);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070068 }
69
70 if (decoder.peekStartElement(CCNProtocolDTags.Scope)) {
Jeff Thompson86aea882012-09-29 17:32:48 -070071 this.scope = decoder.readIntegerElement(CCNProtocolDTags.Scope);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070072 }
73
74 if (decoder.peekStartElement(CCNProtocolDTags.InterestLifetime)) {
Jeff Thompson86aea882012-09-29 17:32:48 -070075 this.interestLifetime = decoder.readBinaryElement(CCNProtocolDTags.InterestLifetime);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070076 }
77
78 if (decoder.peekStartElement(CCNProtocolDTags.Nonce)) {
Jeff Thompson86aea882012-09-29 17:32:48 -070079 this.nonce = decoder.readBinaryElement(CCNProtocolDTags.Nonce);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070080 }
81
82 decoder.readEndElement();
83};
84
Jeff Thompson86aea882012-09-29 17:32:48 -070085Interest.prototype.to_ccnb = function(/*XMLEncoder*/ encoder){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070086 //Could check if name is present
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070087
88 encoder.writeStartElement(CCNProtocolDTags.Interest);
89
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070090 this.name.to_ccnb(encoder);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070091
Jeff Thompson86aea882012-09-29 17:32:48 -070092 if (null != this.minSuffixComponents)
93 encoder.writeElement(CCNProtocolDTags.MinSuffixComponents, this.minSuffixComponents);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070094
Jeff Thompson86aea882012-09-29 17:32:48 -070095 if (null != this.maxSuffixComponents)
96 encoder.writeElement(CCNProtocolDTags.MaxSuffixComponents, this.maxSuffixComponents);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070097
Jeff Thompson86aea882012-09-29 17:32:48 -070098 if (null != this.publisherPublicKeyDigest)
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070099 this.publisherPublicKeyDigest.to_ccnb(encoder);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700100
Jeff Thompson86aea882012-09-29 17:32:48 -0700101 if (null != this.exclude)
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700102 this.exclude.to_ccnb(encoder);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700103
Jeff Thompson86aea882012-09-29 17:32:48 -0700104 if (null != this.childSelector)
105 encoder.writeElement(CCNProtocolDTags.ChildSelector, this.childSelector);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700106
107 //TODO Encode OriginKind
Jeff Thompson86aea882012-09-29 17:32:48 -0700108 if (this.DEFAULT_ANSWER_ORIGIN_KIND != this.answerOriginKind && this.answerOriginKind!=null)
109 encoder.writeElement(CCNProtocolDTags.AnswerOriginKind, this.answerOriginKind);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700110
Jeff Thompson86aea882012-09-29 17:32:48 -0700111 if (null != this.scope)
112 encoder.writeElement(CCNProtocolDTags.Scope, this.scope);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700113
Jeff Thompson86aea882012-09-29 17:32:48 -0700114 if (null != this.nonce)
115 encoder.writeElement(CCNProtocolDTags.Nonce, this.nonce);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700116
117 encoder.writeEndElement();
118
119};
120
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700121Interest.prototype.matches_name = function(/*Name*/ name){
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700122 var i_name = this.name.components;
123 var o_name = name.components;
Jeff Thompson86aea882012-09-29 17:32:48 -0700124
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 Thompsonb9ce4582012-09-30 17:52:51 -0700138/**
139 * Exclude
140 */
141var Exclude = function Exclude(_values){
142
143 this.OPTIMUM_FILTER_SIZE = 100;
144
145
146 this.values = _values; //array of elements
147
148}
149
150Exclude.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
173Exclude.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
187Exclude.prototype.getElementLabel = function() { return CCNProtocolDTags.Exclude; };
188
189
190/**
191 * ExcludeAny
192 */
193var ExcludeAny = function ExcludeAny() {
194
195};
196
197ExcludeAny.prototype.from_ccnb = function(decoder) {
198 decoder.readStartElement(this.getElementLabel());
199 decoder.readEndElement();
200};
201
202
203ExcludeAny.prototype.to_ccnb = function( encoder) {
204 encoder.writeStartElement(this.getElementLabel());
205 encoder.writeEndElement();
206};
207
208ExcludeAny.prototype.getElementLabel=function() { return CCNProtocolDTags.Any; };
209
210
211/**
212 * ExcludeComponent
213 */
214var ExcludeComponent = function ExcludeComponent(_body) {
215
216 //TODO Check BODY is an Array of componenets.
217
218 this.body = _body
219};
220
221ExcludeComponent.prototype.from_ccnb = function( decoder) {
222 this.body = decoder.readBinaryElement(this.getElementLabel());
223};
224
225ExcludeComponent.prototype.to_ccnb = function(encoder) {
226 encoder.writeElement(this.getElementLabel(), this.body);
227};
228
229ExcludeComponent.prototype.getElementLabel = function() { return CCNProtocolDTags.Component; };