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