blob: 5b10cc983fd89496789cc6b213875949dc3ff580 [file] [log] [blame]
Jeff Thompson86bcd022013-07-26 17:55:03 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 * This class represents Interest Objects
5 */
6
7/**
Jeff Thompson83c4a962013-07-31 18:05:37 -07008 * A BinaryXmlWireFormat implements the WireFormat interface for encoding and decoding in binary XML.
Jeff Thompson2b14c7e2013-07-29 15:09:56 -07009 * @constructor
Jeff Thompson86bcd022013-07-26 17:55:03 -070010 */
Jeff Thompson83c4a962013-07-31 18:05:37 -070011var BinaryXmlWireFormat = function BinaryXmlWireFormat() {
Jeff Thompson0e474152013-07-31 18:30:37 -070012 // Inherit from WireFormat.
13 WireFormat.call(this);
Jeff Thompson86bcd022013-07-26 17:55:03 -070014};
15
16/**
17 * Encode the interest and return a Uint8Array.
18 * @param {Interest} interest
19 * @returns {UInt8Array}
20 */
Jeff Thompson83c4a962013-07-31 18:05:37 -070021BinaryXmlWireFormat.prototype.encodeInterest = function(interest) {
Jeff Thompson86bcd022013-07-26 17:55:03 -070022 var encoder = new BinaryXMLEncoder();
Jeff Thompson83c4a962013-07-31 18:05:37 -070023 BinaryXmlWireFormat.encodeInterest(interest, encoder);
Jeff Thompson86bcd022013-07-26 17:55:03 -070024 return encoder.getReducedOstream();
25};
26
27/**
28 * Decode the input and put the result in interest.
29 * @param {Interest} interest
30 * @param {Uint8Array} input
31 */
Jeff Thompson83c4a962013-07-31 18:05:37 -070032BinaryXmlWireFormat.prototype.decodeInterest = function(interest, input) {
Jeff Thompson86bcd022013-07-26 17:55:03 -070033 var decoder = new BinaryXMLDecoder(input);
Jeff Thompson83c4a962013-07-31 18:05:37 -070034 BinaryXmlWireFormat.decodeInterest(interest, decoder);
Jeff Thompson86bcd022013-07-26 17:55:03 -070035};
36
37/**
38 * Encode the contentObject and return a Uint8Array.
39 * @param {ContentObject} contentObject
40 * @returns {Uint8Array}
41 */
Jeff Thompson83c4a962013-07-31 18:05:37 -070042BinaryXmlWireFormat.prototype.encodeContentObject = function(contentObject) {
Jeff Thompson86bcd022013-07-26 17:55:03 -070043 var encoder = new BinaryXMLEncoder();
Jeff Thompson83c4a962013-07-31 18:05:37 -070044 BinaryXmlWireFormat.encodeContentObject(contentObject, encoder);
Jeff Thompson86bcd022013-07-26 17:55:03 -070045 return encoder.getReducedOstream();
46};
47
48/**
49 * Decode the input and put the result in contentObject.
50 * @param {ContentObject} contentObject
51 * @param {Uint8Array} input
52 */
Jeff Thompson83c4a962013-07-31 18:05:37 -070053BinaryXmlWireFormat.prototype.decodeContentObject = function(contentObject, input) {
Jeff Thompson86bcd022013-07-26 17:55:03 -070054 var decoder = new BinaryXMLDecoder(input);
Jeff Thompson83c4a962013-07-31 18:05:37 -070055 BinaryXmlWireFormat.decodeContentObject(contentObject, decoder);
Jeff Thompson86bcd022013-07-26 17:55:03 -070056};
57
58// Default object.
Jeff Thompson83c4a962013-07-31 18:05:37 -070059BinaryXmlWireFormat.instance = new BinaryXmlWireFormat();
Jeff Thompson86bcd022013-07-26 17:55:03 -070060
61/**
62 * Encode the interest by calling the operations on the encoder.
63 * @param {Interest} interest
64 * @param {BinaryXMLEncoder} encoder
65 */
Jeff Thompson83c4a962013-07-31 18:05:37 -070066BinaryXmlWireFormat.encodeInterest = function(interest, encoder) {
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070067 encoder.writeStartElement(NDNProtocolDTags.Interest);
Jeff Thompson86bcd022013-07-26 17:55:03 -070068
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070069 interest.name.to_ndnb(encoder);
Jeff Thompson86bcd022013-07-26 17:55:03 -070070
71 if (null != interest.minSuffixComponents)
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070072 encoder.writeElement(NDNProtocolDTags.MinSuffixComponents, interest.minSuffixComponents);
Jeff Thompson86bcd022013-07-26 17:55:03 -070073
74 if (null != interest.maxSuffixComponents)
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070075 encoder.writeElement(NDNProtocolDTags.MaxSuffixComponents, interest.maxSuffixComponents);
Jeff Thompson86bcd022013-07-26 17:55:03 -070076
77 if (null != interest.publisherPublicKeyDigest)
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070078 interest.publisherPublicKeyDigest.to_ndnb(encoder);
Jeff Thompson86bcd022013-07-26 17:55:03 -070079
80 if (null != interest.exclude)
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070081 interest.exclude.to_ndnb(encoder);
Jeff Thompson86bcd022013-07-26 17:55:03 -070082
83 if (null != interest.childSelector)
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070084 encoder.writeElement(NDNProtocolDTags.ChildSelector, interest.childSelector);
Jeff Thompson86bcd022013-07-26 17:55:03 -070085
86 if (interest.DEFAULT_ANSWER_ORIGIN_KIND != interest.answerOriginKind && interest.answerOriginKind!=null)
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070087 encoder.writeElement(NDNProtocolDTags.AnswerOriginKind, interest.answerOriginKind);
Jeff Thompson86bcd022013-07-26 17:55:03 -070088
89 if (null != interest.scope)
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070090 encoder.writeElement(NDNProtocolDTags.Scope, interest.scope);
Jeff Thompson86bcd022013-07-26 17:55:03 -070091
92 if (null != interest.interestLifetime)
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070093 encoder.writeElement(NDNProtocolDTags.InterestLifetime,
Jeff Thompson86bcd022013-07-26 17:55:03 -070094 DataUtils.nonNegativeIntToBigEndian((interest.interestLifetime / 1000.0) * 4096));
95
96 if (null != interest.nonce)
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070097 encoder.writeElement(NDNProtocolDTags.Nonce, interest.nonce);
Jeff Thompson86bcd022013-07-26 17:55:03 -070098
99 encoder.writeEndElement();
100};
101
102/**
103 * Use the decoder to place the result in interest.
104 * @param {Interest} interest
105 * @param {BinaryXMLDecoder} decoder
106 */
Jeff Thompson83c4a962013-07-31 18:05:37 -0700107BinaryXmlWireFormat.decodeInterest = function(interest, decoder) {
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700108 decoder.readStartElement(NDNProtocolDTags.Interest);
Jeff Thompson86bcd022013-07-26 17:55:03 -0700109
110 interest.name = new Name();
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700111 interest.name.from_ndnb(decoder);
Jeff Thompson86bcd022013-07-26 17:55:03 -0700112
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700113 if (decoder.peekStartElement(NDNProtocolDTags.MinSuffixComponents))
114 interest.minSuffixComponents = decoder.readIntegerElement(NDNProtocolDTags.MinSuffixComponents);
Jeff Thompson86bcd022013-07-26 17:55:03 -0700115 else
116 interest.minSuffixComponents = null;
117
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700118 if (decoder.peekStartElement(NDNProtocolDTags.MaxSuffixComponents))
119 interest.maxSuffixComponents = decoder.readIntegerElement(NDNProtocolDTags.MaxSuffixComponents);
Jeff Thompson86bcd022013-07-26 17:55:03 -0700120 else
121 interest.maxSuffixComponents = null;
122
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700123 if (decoder.peekStartElement(NDNProtocolDTags.PublisherPublicKeyDigest)) {
Jeff Thompson86bcd022013-07-26 17:55:03 -0700124 interest.publisherPublicKeyDigest = new PublisherPublicKeyDigest();
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700125 interest.publisherPublicKeyDigest.from_ndnb(decoder);
Jeff Thompson86bcd022013-07-26 17:55:03 -0700126 }
127 else
128 interest.publisherPublicKeyDigest = null;
129
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700130 if (decoder.peekStartElement(NDNProtocolDTags.Exclude)) {
Jeff Thompson86bcd022013-07-26 17:55:03 -0700131 interest.exclude = new Exclude();
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700132 interest.exclude.from_ndnb(decoder);
Jeff Thompson86bcd022013-07-26 17:55:03 -0700133 }
134 else
135 interest.exclude = null;
136
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700137 if (decoder.peekStartElement(NDNProtocolDTags.ChildSelector))
138 interest.childSelector = decoder.readIntegerElement(NDNProtocolDTags.ChildSelector);
Jeff Thompson86bcd022013-07-26 17:55:03 -0700139 else
140 interest.childSelector = null;
141
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700142 if (decoder.peekStartElement(NDNProtocolDTags.AnswerOriginKind))
143 interest.answerOriginKind = decoder.readIntegerElement(NDNProtocolDTags.AnswerOriginKind);
Jeff Thompson86bcd022013-07-26 17:55:03 -0700144 else
145 interest.answerOriginKind = null;
146
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700147 if (decoder.peekStartElement(NDNProtocolDTags.Scope))
148 interest.scope = decoder.readIntegerElement(NDNProtocolDTags.Scope);
Jeff Thompson86bcd022013-07-26 17:55:03 -0700149 else
150 interest.scope = null;
151
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700152 if (decoder.peekStartElement(NDNProtocolDTags.InterestLifetime))
Jeff Thompson86bcd022013-07-26 17:55:03 -0700153 interest.interestLifetime = 1000.0 * DataUtils.bigEndianToUnsignedInt
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700154 (decoder.readBinaryElement(NDNProtocolDTags.InterestLifetime)) / 4096;
Jeff Thompson86bcd022013-07-26 17:55:03 -0700155 else
156 interest.interestLifetime = null;
157
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700158 if (decoder.peekStartElement(NDNProtocolDTags.Nonce))
159 interest.nonce = decoder.readBinaryElement(NDNProtocolDTags.Nonce);
Jeff Thompson86bcd022013-07-26 17:55:03 -0700160 else
161 interest.nonce = null;
162
163 decoder.readEndElement();
164};
165
166/**
167 * Encode the contentObject by calling the operations on the encoder.
168 * @param {ContentObject} contentObject
169 * @param {BinaryXMLEncoder} encoder
170 */
Jeff Thompson83c4a962013-07-31 18:05:37 -0700171BinaryXmlWireFormat.encodeContentObject = function(contentObject, encoder) {
Jeff Thompson86bcd022013-07-26 17:55:03 -0700172 //TODO verify name, SignedInfo and Signature is present
173 encoder.writeStartElement(contentObject.getElementLabel());
174
175 if (null != contentObject.signature)
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700176 contentObject.signature.to_ndnb(encoder);
Jeff Thompson86bcd022013-07-26 17:55:03 -0700177
178 contentObject.startSIG = encoder.offset;
179
180 if (null != contentObject.name)
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700181 contentObject.name.to_ndnb(encoder);
Jeff Thompson86bcd022013-07-26 17:55:03 -0700182
183 if (null != contentObject.signedInfo)
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700184 contentObject.signedInfo.to_ndnb(encoder);
Jeff Thompson86bcd022013-07-26 17:55:03 -0700185
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700186 encoder.writeElement(NDNProtocolDTags.Content, contentObject.content);
Jeff Thompson86bcd022013-07-26 17:55:03 -0700187
188 contentObject.endSIG = encoder.offset;
189
190 encoder.writeEndElement();
191
192 contentObject.saveRawData(encoder.ostream);
193};
194
195/**
196 * Use the decoder to place the result in contentObject.
197 * @param {ContentObject} contentObject
198 * @param {BinaryXMLDecoder} decoder
199 */
Jeff Thompson83c4a962013-07-31 18:05:37 -0700200BinaryXmlWireFormat.decodeContentObject = function(contentObject, decoder) {
Jeff Thompson86bcd022013-07-26 17:55:03 -0700201 // TODO VALIDATE THAT ALL FIELDS EXCEPT SIGNATURE ARE PRESENT
202 decoder.readStartElement(contentObject.getElementLabel());
203
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700204 if( decoder.peekStartElement(NDNProtocolDTags.Signature) ){
Jeff Thompson86bcd022013-07-26 17:55:03 -0700205 contentObject.signature = new Signature();
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700206 contentObject.signature.from_ndnb(decoder);
Jeff Thompson86bcd022013-07-26 17:55:03 -0700207 }
208 else
209 contentObject.signature = null;
210
211 contentObject.startSIG = decoder.offset;
212
213 contentObject.name = new Name();
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700214 contentObject.name.from_ndnb(decoder);
Jeff Thompson86bcd022013-07-26 17:55:03 -0700215
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700216 if( decoder.peekStartElement(NDNProtocolDTags.SignedInfo) ){
Jeff Thompson86bcd022013-07-26 17:55:03 -0700217 contentObject.signedInfo = new SignedInfo();
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700218 contentObject.signedInfo.from_ndnb(decoder);
Jeff Thompson86bcd022013-07-26 17:55:03 -0700219 }
220 else
221 contentObject.signedInfo = null;
222
Alexander Afanasyev03d3f742013-08-14 17:47:28 -0700223 contentObject.content = decoder.readBinaryElement(NDNProtocolDTags.Content, null, true);
Jeff Thompson86bcd022013-07-26 17:55:03 -0700224
225 contentObject.endSIG = decoder.offset;
226
227 decoder.readEndElement();
228
229 contentObject.saveRawData(decoder.input);
230};