blob: 3d91de5230942c78029925fcb708d8496ac1a231 [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 Thompson08d41b72013-02-03 23:09:29 -0800121/*
122 * Handle the interest Exclude element.
123 * _values is an array where each element is either Uint8Array component or Exclude.ANY.
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700124 */
Jeff Thompson08d41b72013-02-03 23:09:29 -0800125var Exclude = function Exclude(_values) {
126 this.values = (_values || []);
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700127}
128
Jeff Thompson08d41b72013-02-03 23:09:29 -0800129Exclude.ANY = "*";
130
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700131Exclude.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) {
Jeff Thompson08d41b72013-02-03 23:09:29 -0800132 decoder.readStartElement(CCNProtocolDTags.Exclude);
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700133
Jeff Thompson08d41b72013-02-03 23:09:29 -0800134 while (true) {
135 if (decoder.peekStartElement(CCNProtocolDTags.Component))
136 this.values.push(decoder.readBinaryElement(CCNProtocolDTags.Component));
137 else if (decoder.peekStartElement(CCNProtocolDTags.Any)) {
138 decoder.readStartElement(CCNProtocolDTags.Any);
139 decoder.readEndElement();
140 this.values.push(Exclude.ANY);
141 }
142 else if (decoder.peekStartElement(CCNProtocolDTags.Bloom)) {
143 // Skip the Bloom and treat it as Any.
144 decoder.readBinaryElement(CCNProtocolDTags.Bloom);
145 this.values.push(Exclude.ANY);
146 }
147 else
148 break;
149 }
150
151 decoder.readEndElement();
152};
153
154Exclude.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) {
155 if (this.values == null || this.values.length == 0)
156 return;
157
158 encoder.writeStartElement(CCNProtocolDTags.Exclude);
159
160 // TODO: Do we want to order the components (except for ANY)?
161 for (var i = 0; i < this.values.length; ++i) {
162 if (this.values[i] == Exclude.ANY) {
163 encoder.writeStartElement(CCNProtocolDTags.Any);
164 encoder.writeEndElement();
165 }
166 else
167 encoder.writeElement(CCNProtocolDTags.Component, this.values[i]);
168 }
169
170 encoder.writeEndElement();
171};
172
173// Return a string with elements separated by "," and Exclude.ANY shown as "*".
174Exclude.prototype.to_uri = function() {
175 if (this.values == null || this.values.length == 0)
176 return "";
177
178 var result = "";
179 for (var i = 0; i < this.values.length; ++i) {
180 if (i > 0)
181 result += ",";
Jeff Thompsonfced4c22013-01-27 16:34:13 -0800182
Jeff Thompson08d41b72013-02-03 23:09:29 -0800183 if (this.values[i] == Exclude.ANY)
184 result += "*";
185 else
186 result += Name.toEscapedString(this.values[i]);
187 }
188 return result;
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700189};