blob: a04c463f24bcefc752d8192d4c13f6c6ffccbc1d [file] [log] [blame]
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -07001 /*
2 * @author: ucla-cs
3 * This class represents Interest Objects
4 */
5
Jeff Thompson86aea882012-09-29 17:32:48 -07006var Interest = function Interest(_name,_faceInstance,_minSuffixComponents,_maxSuffixComponents,_publisherPublicKeyDigest, _exclude, _childSelector,_answerOriginKind,_scope,_interestLifetime,_nonce){
Meki Cherkaoui8f173612012-06-06 01:05:40 -07007
Jeff Thompson86aea882012-09-29 17:32:48 -07008 this.name = _name;
9 this.faceInstance = _faceInstance;
10 this.maxSuffixComponents = _maxSuffixComponents;
11 this.minSuffixComponents = _minSuffixComponents;
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070012
Jeff Thompson86aea882012-09-29 17:32:48 -070013 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 Cherkaoui88d59cd2012-05-14 07:34:58 -070020
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 Thompson86aea882012-09-29 17:32:48 -070029 this.MARK_STALE = 16; // Must have scope 0. Michael calls this a "hack"
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070030
31 this.DEFAULT_ANSWER_ORIGIN_KIND = this.ANSWER_CONTENT_STORE | this.ANSWER_GENERATED;
32
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
42 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 }
45
46 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 }
49
Jeff Thompson86aea882012-09-29 17:32:48 -070050 if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070051 this.publisherPublicKeyDigest = new publisherPublicKeyDigest();
52 this.publisherPublicKeyDigest.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070053 }
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070054
55 if (decoder.peekStartElement(CCNProtocolDTags.Exclude)) {
Jeff Thompson86aea882012-09-29 17:32:48 -070056 this.exclude = new Exclude();
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070057 this.exclude.from_ccnb(decoder);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070058 }
59
60 if (decoder.peekStartElement(CCNProtocolDTags.ChildSelector)) {
Jeff Thompson86aea882012-09-29 17:32:48 -070061 this.childSelector = decoder.readIntegerElement(CCNProtocolDTags.ChildSelector);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070062 }
63
64 if (decoder.peekStartElement(CCNProtocolDTags.AnswerOriginKind)) {
65 // call setter to handle defaulting
Jeff Thompson86aea882012-09-29 17:32:48 -070066 this.answerOriginKind = decoder.readIntegerElement(CCNProtocolDTags.AnswerOriginKind);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070067 }
68
69 if (decoder.peekStartElement(CCNProtocolDTags.Scope)) {
Jeff Thompson86aea882012-09-29 17:32:48 -070070 this.scope = decoder.readIntegerElement(CCNProtocolDTags.Scope);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070071 }
72
73 if (decoder.peekStartElement(CCNProtocolDTags.InterestLifetime)) {
Jeff Thompson86aea882012-09-29 17:32:48 -070074 this.interestLifetime = decoder.readBinaryElement(CCNProtocolDTags.InterestLifetime);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070075 }
76
77 if (decoder.peekStartElement(CCNProtocolDTags.Nonce)) {
Jeff Thompson86aea882012-09-29 17:32:48 -070078 this.nonce = decoder.readBinaryElement(CCNProtocolDTags.Nonce);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070079 }
80
81 decoder.readEndElement();
82};
83
Jeff Thompson86aea882012-09-29 17:32:48 -070084Interest.prototype.to_ccnb = function(/*XMLEncoder*/ encoder){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070085 //Could check if name is present
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070086
87 encoder.writeStartElement(CCNProtocolDTags.Interest);
88
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070089 this.name.to_ccnb(encoder);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070090
Jeff Thompson86aea882012-09-29 17:32:48 -070091 if (null != this.minSuffixComponents)
92 encoder.writeElement(CCNProtocolDTags.MinSuffixComponents, this.minSuffixComponents);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070093
Jeff Thompson86aea882012-09-29 17:32:48 -070094 if (null != this.maxSuffixComponents)
95 encoder.writeElement(CCNProtocolDTags.MaxSuffixComponents, this.maxSuffixComponents);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070096
Jeff Thompson86aea882012-09-29 17:32:48 -070097 if (null != this.publisherPublicKeyDigest)
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070098 this.publisherPublicKeyDigest.to_ccnb(encoder);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070099
Jeff Thompson86aea882012-09-29 17:32:48 -0700100 if (null != this.exclude)
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700101 this.exclude.to_ccnb(encoder);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700102
Jeff Thompson86aea882012-09-29 17:32:48 -0700103 if (null != this.childSelector)
104 encoder.writeElement(CCNProtocolDTags.ChildSelector, this.childSelector);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700105
106 //TODO Encode OriginKind
Jeff Thompson86aea882012-09-29 17:32:48 -0700107 if (this.DEFAULT_ANSWER_ORIGIN_KIND != this.answerOriginKind && this.answerOriginKind!=null)
108 encoder.writeElement(CCNProtocolDTags.AnswerOriginKind, this.answerOriginKind);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700109
Jeff Thompson86aea882012-09-29 17:32:48 -0700110 if (null != this.scope)
111 encoder.writeElement(CCNProtocolDTags.Scope, this.scope);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700112
Jeff Thompson86aea882012-09-29 17:32:48 -0700113 if (null != this.nonce)
114 encoder.writeElement(CCNProtocolDTags.Nonce, this.nonce);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700115
116 encoder.writeEndElement();
117
118};
119
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700120Interest.prototype.matches_name = function(/*Name*/ name){
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700121 var i_name = this.name.components;
122 var o_name = name.components;
Jeff Thompson86aea882012-09-29 17:32:48 -0700123
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 Thompsonb9ce4582012-09-30 17:52:51 -0700137/**
138 * Exclude
139 */
140var Exclude = function Exclude(_values){
141
142 this.OPTIMUM_FILTER_SIZE = 100;
143
144
145 this.values = _values; //array of elements
146
147}
148
149Exclude.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
172Exclude.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
186Exclude.prototype.getElementLabel = function() { return CCNProtocolDTags.Exclude; };
187
188
189/**
190 * ExcludeAny
191 */
192var ExcludeAny = function ExcludeAny() {
193
194};
195
196ExcludeAny.prototype.from_ccnb = function(decoder) {
197 decoder.readStartElement(this.getElementLabel());
198 decoder.readEndElement();
199};
200
201
202ExcludeAny.prototype.to_ccnb = function( encoder) {
203 encoder.writeStartElement(this.getElementLabel());
204 encoder.writeEndElement();
205};
206
207ExcludeAny.prototype.getElementLabel=function() { return CCNProtocolDTags.Any; };
208
209
210/**
211 * ExcludeComponent
212 */
213var ExcludeComponent = function ExcludeComponent(_body) {
214
215 //TODO Check BODY is an Array of componenets.
216
217 this.body = _body
218};
219
220ExcludeComponent.prototype.from_ccnb = function( decoder) {
221 this.body = decoder.readBinaryElement(this.getElementLabel());
222};
223
224ExcludeComponent.prototype.to_ccnb = function(encoder) {
225 encoder.writeElement(this.getElementLabel(), this.body);
226};
227
228ExcludeComponent.prototype.getElementLabel = function() { return CCNProtocolDTags.Component; };