blob: f294fb046bc19a4c9e30ffbcce04e1347f9eede6 [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) {
Jeff Thompson86bcd022013-07-26 17:55:03 -070067 encoder.writeStartElement(CCNProtocolDTags.Interest);
68
69 interest.name.to_ccnb(encoder);
70
71 if (null != interest.minSuffixComponents)
72 encoder.writeElement(CCNProtocolDTags.MinSuffixComponents, interest.minSuffixComponents);
73
74 if (null != interest.maxSuffixComponents)
75 encoder.writeElement(CCNProtocolDTags.MaxSuffixComponents, interest.maxSuffixComponents);
76
77 if (null != interest.publisherPublicKeyDigest)
78 interest.publisherPublicKeyDigest.to_ccnb(encoder);
79
80 if (null != interest.exclude)
81 interest.exclude.to_ccnb(encoder);
82
83 if (null != interest.childSelector)
84 encoder.writeElement(CCNProtocolDTags.ChildSelector, interest.childSelector);
85
86 if (interest.DEFAULT_ANSWER_ORIGIN_KIND != interest.answerOriginKind && interest.answerOriginKind!=null)
87 encoder.writeElement(CCNProtocolDTags.AnswerOriginKind, interest.answerOriginKind);
88
89 if (null != interest.scope)
90 encoder.writeElement(CCNProtocolDTags.Scope, interest.scope);
91
92 if (null != interest.interestLifetime)
93 encoder.writeElement(CCNProtocolDTags.InterestLifetime,
94 DataUtils.nonNegativeIntToBigEndian((interest.interestLifetime / 1000.0) * 4096));
95
96 if (null != interest.nonce)
97 encoder.writeElement(CCNProtocolDTags.Nonce, interest.nonce);
98
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) {
Jeff Thompson86bcd022013-07-26 17:55:03 -0700108 decoder.readStartElement(CCNProtocolDTags.Interest);
109
110 interest.name = new Name();
111 interest.name.from_ccnb(decoder);
112
113 if (decoder.peekStartElement(CCNProtocolDTags.MinSuffixComponents))
114 interest.minSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MinSuffixComponents);
115 else
116 interest.minSuffixComponents = null;
117
118 if (decoder.peekStartElement(CCNProtocolDTags.MaxSuffixComponents))
119 interest.maxSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MaxSuffixComponents);
120 else
121 interest.maxSuffixComponents = null;
122
123 if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) {
124 interest.publisherPublicKeyDigest = new PublisherPublicKeyDigest();
125 interest.publisherPublicKeyDigest.from_ccnb(decoder);
126 }
127 else
128 interest.publisherPublicKeyDigest = null;
129
130 if (decoder.peekStartElement(CCNProtocolDTags.Exclude)) {
131 interest.exclude = new Exclude();
132 interest.exclude.from_ccnb(decoder);
133 }
134 else
135 interest.exclude = null;
136
137 if (decoder.peekStartElement(CCNProtocolDTags.ChildSelector))
138 interest.childSelector = decoder.readIntegerElement(CCNProtocolDTags.ChildSelector);
139 else
140 interest.childSelector = null;
141
142 if (decoder.peekStartElement(CCNProtocolDTags.AnswerOriginKind))
143 interest.answerOriginKind = decoder.readIntegerElement(CCNProtocolDTags.AnswerOriginKind);
144 else
145 interest.answerOriginKind = null;
146
147 if (decoder.peekStartElement(CCNProtocolDTags.Scope))
148 interest.scope = decoder.readIntegerElement(CCNProtocolDTags.Scope);
149 else
150 interest.scope = null;
151
152 if (decoder.peekStartElement(CCNProtocolDTags.InterestLifetime))
153 interest.interestLifetime = 1000.0 * DataUtils.bigEndianToUnsignedInt
154 (decoder.readBinaryElement(CCNProtocolDTags.InterestLifetime)) / 4096;
155 else
156 interest.interestLifetime = null;
157
158 if (decoder.peekStartElement(CCNProtocolDTags.Nonce))
159 interest.nonce = decoder.readBinaryElement(CCNProtocolDTags.Nonce);
160 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)
176 contentObject.signature.to_ccnb(encoder);
177
178 contentObject.startSIG = encoder.offset;
179
180 if (null != contentObject.name)
181 contentObject.name.to_ccnb(encoder);
182
183 if (null != contentObject.signedInfo)
184 contentObject.signedInfo.to_ccnb(encoder);
185
186 encoder.writeElement(CCNProtocolDTags.Content, contentObject.content);
187
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
204 if( decoder.peekStartElement(CCNProtocolDTags.Signature) ){
205 contentObject.signature = new Signature();
206 contentObject.signature.from_ccnb(decoder);
207 }
208 else
209 contentObject.signature = null;
210
211 contentObject.startSIG = decoder.offset;
212
213 contentObject.name = new Name();
214 contentObject.name.from_ccnb(decoder);
215
216 if( decoder.peekStartElement(CCNProtocolDTags.SignedInfo) ){
217 contentObject.signedInfo = new SignedInfo();
218 contentObject.signedInfo.from_ccnb(decoder);
219 }
220 else
221 contentObject.signedInfo = null;
222
223 contentObject.content = decoder.readBinaryElement(CCNProtocolDTags.Content, null, true);
224
225 contentObject.endSIG = decoder.offset;
226
227 decoder.readEndElement();
228
229 contentObject.saveRawData(decoder.input);
230};