Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * @author: ucla-cs |
| 3 | * This class represents Interest Objects |
| 4 | */ |
| 5 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 6 | var Interest = function Interest(_name,_faceInstance,_minSuffixComponents,_maxSuffixComponents,_publisherPublicKeyDigest, _exclude, _childSelector,_answerOriginKind,_scope,_interestLifetime,_nonce){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 7 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 8 | this.name = _name; |
| 9 | this.faceInstance = _faceInstance; |
| 10 | this.maxSuffixComponents = _maxSuffixComponents; |
| 11 | this.minSuffixComponents = _minSuffixComponents; |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 12 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 13 | 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 Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 20 | |
| 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 Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 29 | this.MARK_STALE = 16; // Must have scope 0. Michael calls this a "hack" |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 30 | |
| 31 | this.DEFAULT_ANSWER_ORIGIN_KIND = this.ANSWER_CONTENT_STORE | this.ANSWER_GENERATED; |
| 32 | |
| 33 | }; |
| 34 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 35 | Interest.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) { |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 36 | |
| 37 | decoder.readStartElement(CCNProtocolDTags.Interest); |
| 38 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 39 | this.name = new ContentName(); |
| 40 | this.name.decode(decoder); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 41 | |
| 42 | if (decoder.peekStartElement(CCNProtocolDTags.MinSuffixComponents)) { |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 43 | this.minSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MinSuffixComponents); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | if (decoder.peekStartElement(CCNProtocolDTags.MaxSuffixComponents)) { |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 47 | this.maxSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MaxSuffixComponents); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 50 | if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) { |
| 51 | this.publisherPublicKeyDigest = new PublisherPublicKeyDigest(); |
| 52 | this.publisherPublicKeyDigest.decode(decoder); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 53 | } |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 54 | |
| 55 | if (decoder.peekStartElement(CCNProtocolDTags.Exclude)) { |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 56 | this.exclude = new Exclude(); |
| 57 | this.exclude.decode(decoder); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | if (decoder.peekStartElement(CCNProtocolDTags.ChildSelector)) { |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 61 | this.childSelector = decoder.readIntegerElement(CCNProtocolDTags.ChildSelector); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | if (decoder.peekStartElement(CCNProtocolDTags.AnswerOriginKind)) { |
| 65 | // call setter to handle defaulting |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 66 | this.answerOriginKind = decoder.readIntegerElement(CCNProtocolDTags.AnswerOriginKind); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | if (decoder.peekStartElement(CCNProtocolDTags.Scope)) { |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 70 | this.scope = decoder.readIntegerElement(CCNProtocolDTags.Scope); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | if (decoder.peekStartElement(CCNProtocolDTags.InterestLifetime)) { |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 74 | this.interestLifetime = decoder.readBinaryElement(CCNProtocolDTags.InterestLifetime); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | if (decoder.peekStartElement(CCNProtocolDTags.Nonce)) { |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 78 | this.nonce = decoder.readBinaryElement(CCNProtocolDTags.Nonce); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | decoder.readEndElement(); |
| 82 | }; |
| 83 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 84 | Interest.prototype.to_ccnb = function(/*XMLEncoder*/ encoder){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 85 | //Could check if name is present |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 86 | |
| 87 | encoder.writeStartElement(CCNProtocolDTags.Interest); |
| 88 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 89 | this.name.encode(encoder); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 90 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 91 | if (null != this.minSuffixComponents) |
| 92 | encoder.writeElement(CCNProtocolDTags.MinSuffixComponents, this.minSuffixComponents); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 93 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 94 | if (null != this.maxSuffixComponents) |
| 95 | encoder.writeElement(CCNProtocolDTags.MaxSuffixComponents, this.maxSuffixComponents); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 96 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 97 | if (null != this.publisherPublicKeyDigest) |
| 98 | this.publisherPublicKeyDigest.encode(encoder); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 99 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 100 | if (null != this.exclude) |
| 101 | this.exclude.encode(encoder); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 102 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 103 | if (null != this.childSelector) |
| 104 | encoder.writeElement(CCNProtocolDTags.ChildSelector, this.childSelector); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 105 | |
| 106 | //TODO Encode OriginKind |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 107 | if (this.DEFAULT_ANSWER_ORIGIN_KIND != this.answerOriginKind && this.answerOriginKind!=null) |
| 108 | encoder.writeElement(CCNProtocolDTags.AnswerOriginKind, this.answerOriginKind); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 109 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 110 | if (null != this.scope) |
| 111 | encoder.writeElement(CCNProtocolDTags.Scope, this.scope); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 112 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 113 | if (null != this.nonce) |
| 114 | encoder.writeElement(CCNProtocolDTags.Nonce, this.nonce); |
Meki Cherkaoui | 88d59cd | 2012-05-14 07:34:58 -0700 | [diff] [blame] | 115 | |
| 116 | encoder.writeEndElement(); |
| 117 | |
| 118 | }; |
| 119 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 120 | Interest.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 | |