blob: 3120d8cb8850f5eb4d1cf126d9c3d7e19631fec6 [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 Thompson86bcd022013-07-26 17:55:03 -070012};
13
14/**
15 * Encode the interest and return a Uint8Array.
16 * @param {Interest} interest
17 * @returns {UInt8Array}
18 */
Jeff Thompson83c4a962013-07-31 18:05:37 -070019BinaryXmlWireFormat.prototype.encodeInterest = function(interest) {
Jeff Thompson86bcd022013-07-26 17:55:03 -070020 var encoder = new BinaryXMLEncoder();
Jeff Thompson83c4a962013-07-31 18:05:37 -070021 BinaryXmlWireFormat.encodeInterest(interest, encoder);
Jeff Thompson86bcd022013-07-26 17:55:03 -070022 return encoder.getReducedOstream();
23};
24
25/**
26 * Decode the input and put the result in interest.
27 * @param {Interest} interest
28 * @param {Uint8Array} input
29 */
Jeff Thompson83c4a962013-07-31 18:05:37 -070030BinaryXmlWireFormat.prototype.decodeInterest = function(interest, input) {
Jeff Thompson86bcd022013-07-26 17:55:03 -070031 var decoder = new BinaryXMLDecoder(input);
Jeff Thompson83c4a962013-07-31 18:05:37 -070032 BinaryXmlWireFormat.decodeInterest(interest, decoder);
Jeff Thompson86bcd022013-07-26 17:55:03 -070033};
34
35/**
36 * Encode the contentObject and return a Uint8Array.
37 * @param {ContentObject} contentObject
38 * @returns {Uint8Array}
39 */
Jeff Thompson83c4a962013-07-31 18:05:37 -070040BinaryXmlWireFormat.prototype.encodeContentObject = function(contentObject) {
Jeff Thompson86bcd022013-07-26 17:55:03 -070041 var encoder = new BinaryXMLEncoder();
Jeff Thompson83c4a962013-07-31 18:05:37 -070042 BinaryXmlWireFormat.encodeContentObject(contentObject, encoder);
Jeff Thompson86bcd022013-07-26 17:55:03 -070043 return encoder.getReducedOstream();
44};
45
46/**
47 * Decode the input and put the result in contentObject.
48 * @param {ContentObject} contentObject
49 * @param {Uint8Array} input
50 */
Jeff Thompson83c4a962013-07-31 18:05:37 -070051BinaryXmlWireFormat.prototype.decodeContentObject = function(contentObject, input) {
Jeff Thompson86bcd022013-07-26 17:55:03 -070052 var decoder = new BinaryXMLDecoder(input);
Jeff Thompson83c4a962013-07-31 18:05:37 -070053 BinaryXmlWireFormat.decodeContentObject(contentObject, decoder);
Jeff Thompson86bcd022013-07-26 17:55:03 -070054};
55
56// Default object.
Jeff Thompson83c4a962013-07-31 18:05:37 -070057BinaryXmlWireFormat.instance = new BinaryXmlWireFormat();
Jeff Thompson86bcd022013-07-26 17:55:03 -070058
59/**
60 * Encode the interest by calling the operations on the encoder.
61 * @param {Interest} interest
62 * @param {BinaryXMLEncoder} encoder
63 */
Jeff Thompson83c4a962013-07-31 18:05:37 -070064BinaryXmlWireFormat.encodeInterest = function(interest, encoder) {
Jeff Thompson86bcd022013-07-26 17:55:03 -070065 encoder.writeStartElement(CCNProtocolDTags.Interest);
66
67 interest.name.to_ccnb(encoder);
68
69 if (null != interest.minSuffixComponents)
70 encoder.writeElement(CCNProtocolDTags.MinSuffixComponents, interest.minSuffixComponents);
71
72 if (null != interest.maxSuffixComponents)
73 encoder.writeElement(CCNProtocolDTags.MaxSuffixComponents, interest.maxSuffixComponents);
74
75 if (null != interest.publisherPublicKeyDigest)
76 interest.publisherPublicKeyDigest.to_ccnb(encoder);
77
78 if (null != interest.exclude)
79 interest.exclude.to_ccnb(encoder);
80
81 if (null != interest.childSelector)
82 encoder.writeElement(CCNProtocolDTags.ChildSelector, interest.childSelector);
83
84 if (interest.DEFAULT_ANSWER_ORIGIN_KIND != interest.answerOriginKind && interest.answerOriginKind!=null)
85 encoder.writeElement(CCNProtocolDTags.AnswerOriginKind, interest.answerOriginKind);
86
87 if (null != interest.scope)
88 encoder.writeElement(CCNProtocolDTags.Scope, interest.scope);
89
90 if (null != interest.interestLifetime)
91 encoder.writeElement(CCNProtocolDTags.InterestLifetime,
92 DataUtils.nonNegativeIntToBigEndian((interest.interestLifetime / 1000.0) * 4096));
93
94 if (null != interest.nonce)
95 encoder.writeElement(CCNProtocolDTags.Nonce, interest.nonce);
96
97 encoder.writeEndElement();
98};
99
100/**
101 * Use the decoder to place the result in interest.
102 * @param {Interest} interest
103 * @param {BinaryXMLDecoder} decoder
104 */
Jeff Thompson83c4a962013-07-31 18:05:37 -0700105BinaryXmlWireFormat.decodeInterest = function(interest, decoder) {
Jeff Thompson86bcd022013-07-26 17:55:03 -0700106 decoder.readStartElement(CCNProtocolDTags.Interest);
107
108 interest.name = new Name();
109 interest.name.from_ccnb(decoder);
110
111 if (decoder.peekStartElement(CCNProtocolDTags.MinSuffixComponents))
112 interest.minSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MinSuffixComponents);
113 else
114 interest.minSuffixComponents = null;
115
116 if (decoder.peekStartElement(CCNProtocolDTags.MaxSuffixComponents))
117 interest.maxSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MaxSuffixComponents);
118 else
119 interest.maxSuffixComponents = null;
120
121 if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) {
122 interest.publisherPublicKeyDigest = new PublisherPublicKeyDigest();
123 interest.publisherPublicKeyDigest.from_ccnb(decoder);
124 }
125 else
126 interest.publisherPublicKeyDigest = null;
127
128 if (decoder.peekStartElement(CCNProtocolDTags.Exclude)) {
129 interest.exclude = new Exclude();
130 interest.exclude.from_ccnb(decoder);
131 }
132 else
133 interest.exclude = null;
134
135 if (decoder.peekStartElement(CCNProtocolDTags.ChildSelector))
136 interest.childSelector = decoder.readIntegerElement(CCNProtocolDTags.ChildSelector);
137 else
138 interest.childSelector = null;
139
140 if (decoder.peekStartElement(CCNProtocolDTags.AnswerOriginKind))
141 interest.answerOriginKind = decoder.readIntegerElement(CCNProtocolDTags.AnswerOriginKind);
142 else
143 interest.answerOriginKind = null;
144
145 if (decoder.peekStartElement(CCNProtocolDTags.Scope))
146 interest.scope = decoder.readIntegerElement(CCNProtocolDTags.Scope);
147 else
148 interest.scope = null;
149
150 if (decoder.peekStartElement(CCNProtocolDTags.InterestLifetime))
151 interest.interestLifetime = 1000.0 * DataUtils.bigEndianToUnsignedInt
152 (decoder.readBinaryElement(CCNProtocolDTags.InterestLifetime)) / 4096;
153 else
154 interest.interestLifetime = null;
155
156 if (decoder.peekStartElement(CCNProtocolDTags.Nonce))
157 interest.nonce = decoder.readBinaryElement(CCNProtocolDTags.Nonce);
158 else
159 interest.nonce = null;
160
161 decoder.readEndElement();
162};
163
164/**
165 * Encode the contentObject by calling the operations on the encoder.
166 * @param {ContentObject} contentObject
167 * @param {BinaryXMLEncoder} encoder
168 */
Jeff Thompson83c4a962013-07-31 18:05:37 -0700169BinaryXmlWireFormat.encodeContentObject = function(contentObject, encoder) {
Jeff Thompson86bcd022013-07-26 17:55:03 -0700170 //TODO verify name, SignedInfo and Signature is present
171 encoder.writeStartElement(contentObject.getElementLabel());
172
173 if (null != contentObject.signature)
174 contentObject.signature.to_ccnb(encoder);
175
176 contentObject.startSIG = encoder.offset;
177
178 if (null != contentObject.name)
179 contentObject.name.to_ccnb(encoder);
180
181 if (null != contentObject.signedInfo)
182 contentObject.signedInfo.to_ccnb(encoder);
183
184 encoder.writeElement(CCNProtocolDTags.Content, contentObject.content);
185
186 contentObject.endSIG = encoder.offset;
187
188 encoder.writeEndElement();
189
190 contentObject.saveRawData(encoder.ostream);
191};
192
193/**
194 * Use the decoder to place the result in contentObject.
195 * @param {ContentObject} contentObject
196 * @param {BinaryXMLDecoder} decoder
197 */
Jeff Thompson83c4a962013-07-31 18:05:37 -0700198BinaryXmlWireFormat.decodeContentObject = function(contentObject, decoder) {
Jeff Thompson86bcd022013-07-26 17:55:03 -0700199 // TODO VALIDATE THAT ALL FIELDS EXCEPT SIGNATURE ARE PRESENT
200 decoder.readStartElement(contentObject.getElementLabel());
201
202 if( decoder.peekStartElement(CCNProtocolDTags.Signature) ){
203 contentObject.signature = new Signature();
204 contentObject.signature.from_ccnb(decoder);
205 }
206 else
207 contentObject.signature = null;
208
209 contentObject.startSIG = decoder.offset;
210
211 contentObject.name = new Name();
212 contentObject.name.from_ccnb(decoder);
213
214 if( decoder.peekStartElement(CCNProtocolDTags.SignedInfo) ){
215 contentObject.signedInfo = new SignedInfo();
216 contentObject.signedInfo.from_ccnb(decoder);
217 }
218 else
219 contentObject.signedInfo = null;
220
221 contentObject.content = decoder.readBinaryElement(CCNProtocolDTags.Content, null, true);
222
223 contentObject.endSIG = decoder.offset;
224
225 decoder.readEndElement();
226
227 contentObject.saveRawData(decoder.input);
228};