Meki Cherkaoui | b0365a7 | 2012-02-18 00:59:57 -0800 | [diff] [blame^] | 1 | var CCNProtocolDTags = require('./CCNProtocolDTags').CCNProtocolDTags; |
| 2 | |
| 3 | var Interest = function Interest(_Name,_MinSuffixComponents,_MaxSuffixComponents,_PublisherPublicKeyDigest, _Exclude, _ChildSelector,_AnswerOriginKind,_Scope,_InterestLifetime,_Nonce){ |
| 4 | |
| 5 | |
| 6 | this.Name = _Name; |
| 7 | this.MaxSuffixComponents = _MaxSuffixComponents; |
| 8 | this.MinSuffixComponents = _MinSuffixComponents; |
| 9 | |
| 10 | this.PublisherKeyDigest = _PublisherPublicKeyDigest; |
| 11 | this.Exclude = _Exclude; |
| 12 | this.ChildSelector = _ChildSelector; |
| 13 | this.AnswerOriginKind = _AnswerOriginKind; |
| 14 | this.Scope = _Scope; |
| 15 | this.InterestLifetime = null; // For now we don't have the ability to set an interest lifetime |
| 16 | this.Nonce = _Nonce; |
| 17 | |
| 18 | /*Not sure if the rest is needed*/ |
| 19 | this.Publisher = _Publisher; |
| 20 | |
| 21 | this.UserTime=_UserTime; |
| 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 |
| 30 | this.MARK_STALE = 16; // Must have Scope 0. Michael calls this a "hack" |
| 31 | |
| 32 | this.DEFAULT_ANSWER_ORIGIN_KIND = ANSWER_CONTENT_STORE | ANSWER_GENERATED; |
| 33 | |
| 34 | |
| 35 | }; |
| 36 | exports.Interest = Interest; |
| 37 | |
| 38 | Interest.prototype.decode(/*XMLDecoder*/ decoder) { |
| 39 | |
| 40 | decoder.readStartElement(CCNProtocolDTags.Interest); |
| 41 | |
| 42 | this.Name = new ContentName(); |
| 43 | this.Name.decode(decoder); |
| 44 | |
| 45 | if (decoder.peekStartElement(CCNProtocolDTags.MinSuffixComponents)) { |
| 46 | this.MinSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MinSuffixComponents); |
| 47 | } |
| 48 | |
| 49 | if (decoder.peekStartElement(CCNProtocolDTags.MaxSuffixComponents)) { |
| 50 | this.MaxSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MaxSuffixComponents); |
| 51 | } |
| 52 | |
| 53 | //TODO something about that guy |
| 54 | /*if (PublisherID.peek(decoder)) { |
| 55 | this.Publisher = new PublisherID(); |
| 56 | this.Publisher.decode(decoder); |
| 57 | }*/ |
| 58 | |
| 59 | if (decoder.peekStartElement(CCNProtocolDTags.Exclude)) { |
| 60 | this.Exclude = new Exclude(); |
| 61 | this.Exclude.decode(decoder); |
| 62 | } |
| 63 | |
| 64 | if (decoder.peekStartElement(CCNProtocolDTags.ChildSelector)) { |
| 65 | this.ChildSelector = decoder.readIntegerElement(CCNProtocolDTags.ChildSelector); |
| 66 | } |
| 67 | |
| 68 | if (decoder.peekStartElement(CCNProtocolDTags.AnswerOriginKind)) { |
| 69 | // call setter to handle defaulting |
| 70 | this.AnswerOriginKind = decoder.readIntegerElement(CCNProtocolDTags.AnswerOriginKind); |
| 71 | } |
| 72 | |
| 73 | if (decoder.peekStartElement(CCNProtocolDTags.Scope)) { |
| 74 | this.Scope = decoder.readIntegerElement(CCNProtocolDTags.Scope); |
| 75 | } |
| 76 | |
| 77 | if (decoder.peekStartElement(CCNProtocolDTags.InterestLifetime)) { |
| 78 | this.InterestLifetime = decoder.readBinaryElement(CCNProtocolDTags.InterestLifetime); |
| 79 | } |
| 80 | |
| 81 | if (decoder.peekStartElement(CCNProtocolDTags.Nonce)) { |
| 82 | this.Nonce = decoder.readBinaryElement(CCNProtocolDTags.Nonce); |
| 83 | } |
| 84 | |
| 85 | decoder.readEndElement(); |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | Interest.prototype.encode = function(/*XMLEncoder*/ encoder){ |
| 90 | if (!validate()) { |
| 91 | throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing."); |
| 92 | } |
| 93 | |
| 94 | encoder.writeStartElement(CCNProtocolDTags.Interest); |
| 95 | |
| 96 | this.Name.encode(encoder); |
| 97 | |
| 98 | if (null != this.MinSuffixComponents) |
| 99 | encoder.writeElement(CCNProtocolDTags.MinSuffixComponents, this.MinSuffixComponents); |
| 100 | |
| 101 | if (null != this.MaxSuffixComponents) |
| 102 | encoder.writeElement(CCNProtocolDTags.MaxSuffixComponents, this.MaxSuffixComponents); |
| 103 | |
| 104 | //TODO |
| 105 | |
| 106 | /*if (null != this.PublisherID) |
| 107 | publisherID().encode(encoder);*/ |
| 108 | |
| 109 | //if (null != this.Exclude) |
| 110 | //exclude().encode(encoder); |
| 111 | |
| 112 | if (null != this.ChildSelector) |
| 113 | encoder.writeElement(CCNProtocolDTags.ChildSelector, this.ChildSelector); |
| 114 | |
| 115 | if (this.DEFAULT_ANSWER_ORIGIN_KIND != this.AnswerOriginKind) |
| 116 | encoder.writeElement(CCNProtocolDTags.AnswerOriginKind, this.AnswerOriginKind); |
| 117 | |
| 118 | if (null != this.Scope) |
| 119 | encoder.writeElement(CCNProtocolDTags.Scope, this.Scope); |
| 120 | |
| 121 | if (null != this.Nonce) |
| 122 | encoder.writeElement(CCNProtocolDTags.Nonce, this.Nonce); |
| 123 | |
| 124 | encoder.writeEndElement(); |
| 125 | }; |
| 126 | |
| 127 | |
| 128 | |