blob: 46303b11a714a74ec853f95b32efd50dd4219295 [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 Thompson86aea882012-09-29 17:32:48 -070039 this.name = new ContentName();
40 this.name.decode(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)) {
51 this.publisherPublicKeyDigest = new PublisherPublicKeyDigest();
52 this.publisherPublicKeyDigest.decode(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();
57 this.exclude.decode(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 Thompson86aea882012-09-29 17:32:48 -070089 this.name.encode(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)
98 this.publisherPublicKeyDigest.encode(encoder);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070099
Jeff Thompson86aea882012-09-29 17:32:48 -0700100 if (null != this.exclude)
101 this.exclude.encode(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 Thompson86aea882012-09-29 17:32:48 -0700120Interest.prototype.matches_name = function(/*ContentName*/ name){
121 var i_name = this.name.Components;
122 var o_name = name.Components;
123
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