Jeff Thompson | 86bcd02 | 2013-07-26 17:55:03 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | * This class represents Interest Objects |
| 5 | */ |
| 6 | |
| 7 | /** |
Jeff Thompson | 83c4a96 | 2013-07-31 18:05:37 -0700 | [diff] [blame] | 8 | * A BinaryXmlWireFormat implements the WireFormat interface for encoding and decoding in binary XML. |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 9 | * @constructor |
Jeff Thompson | 86bcd02 | 2013-07-26 17:55:03 -0700 | [diff] [blame] | 10 | */ |
Jeff Thompson | 83c4a96 | 2013-07-31 18:05:37 -0700 | [diff] [blame] | 11 | var BinaryXmlWireFormat = function BinaryXmlWireFormat() { |
Jeff Thompson | 86bcd02 | 2013-07-26 17:55:03 -0700 | [diff] [blame] | 12 | }; |
| 13 | |
| 14 | /** |
| 15 | * Encode the interest and return a Uint8Array. |
| 16 | * @param {Interest} interest |
| 17 | * @returns {UInt8Array} |
| 18 | */ |
Jeff Thompson | 83c4a96 | 2013-07-31 18:05:37 -0700 | [diff] [blame] | 19 | BinaryXmlWireFormat.prototype.encodeInterest = function(interest) { |
Jeff Thompson | 86bcd02 | 2013-07-26 17:55:03 -0700 | [diff] [blame] | 20 | var encoder = new BinaryXMLEncoder(); |
Jeff Thompson | 83c4a96 | 2013-07-31 18:05:37 -0700 | [diff] [blame] | 21 | BinaryXmlWireFormat.encodeInterest(interest, encoder); |
Jeff Thompson | 86bcd02 | 2013-07-26 17:55:03 -0700 | [diff] [blame] | 22 | 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 Thompson | 83c4a96 | 2013-07-31 18:05:37 -0700 | [diff] [blame] | 30 | BinaryXmlWireFormat.prototype.decodeInterest = function(interest, input) { |
Jeff Thompson | 86bcd02 | 2013-07-26 17:55:03 -0700 | [diff] [blame] | 31 | var decoder = new BinaryXMLDecoder(input); |
Jeff Thompson | 83c4a96 | 2013-07-31 18:05:37 -0700 | [diff] [blame] | 32 | BinaryXmlWireFormat.decodeInterest(interest, decoder); |
Jeff Thompson | 86bcd02 | 2013-07-26 17:55:03 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | /** |
| 36 | * Encode the contentObject and return a Uint8Array. |
| 37 | * @param {ContentObject} contentObject |
| 38 | * @returns {Uint8Array} |
| 39 | */ |
Jeff Thompson | 83c4a96 | 2013-07-31 18:05:37 -0700 | [diff] [blame] | 40 | BinaryXmlWireFormat.prototype.encodeContentObject = function(contentObject) { |
Jeff Thompson | 86bcd02 | 2013-07-26 17:55:03 -0700 | [diff] [blame] | 41 | var encoder = new BinaryXMLEncoder(); |
Jeff Thompson | 83c4a96 | 2013-07-31 18:05:37 -0700 | [diff] [blame] | 42 | BinaryXmlWireFormat.encodeContentObject(contentObject, encoder); |
Jeff Thompson | 86bcd02 | 2013-07-26 17:55:03 -0700 | [diff] [blame] | 43 | 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 Thompson | 83c4a96 | 2013-07-31 18:05:37 -0700 | [diff] [blame] | 51 | BinaryXmlWireFormat.prototype.decodeContentObject = function(contentObject, input) { |
Jeff Thompson | 86bcd02 | 2013-07-26 17:55:03 -0700 | [diff] [blame] | 52 | var decoder = new BinaryXMLDecoder(input); |
Jeff Thompson | 83c4a96 | 2013-07-31 18:05:37 -0700 | [diff] [blame] | 53 | BinaryXmlWireFormat.decodeContentObject(contentObject, decoder); |
Jeff Thompson | 86bcd02 | 2013-07-26 17:55:03 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | // Default object. |
Jeff Thompson | 83c4a96 | 2013-07-31 18:05:37 -0700 | [diff] [blame] | 57 | BinaryXmlWireFormat.instance = new BinaryXmlWireFormat(); |
Jeff Thompson | 86bcd02 | 2013-07-26 17:55:03 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Encode the interest by calling the operations on the encoder. |
| 61 | * @param {Interest} interest |
| 62 | * @param {BinaryXMLEncoder} encoder |
| 63 | */ |
Jeff Thompson | 83c4a96 | 2013-07-31 18:05:37 -0700 | [diff] [blame] | 64 | BinaryXmlWireFormat.encodeInterest = function(interest, encoder) { |
Jeff Thompson | 86bcd02 | 2013-07-26 17:55:03 -0700 | [diff] [blame] | 65 | 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 Thompson | 83c4a96 | 2013-07-31 18:05:37 -0700 | [diff] [blame] | 105 | BinaryXmlWireFormat.decodeInterest = function(interest, decoder) { |
Jeff Thompson | 86bcd02 | 2013-07-26 17:55:03 -0700 | [diff] [blame] | 106 | 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 Thompson | 83c4a96 | 2013-07-31 18:05:37 -0700 | [diff] [blame] | 169 | BinaryXmlWireFormat.encodeContentObject = function(contentObject, encoder) { |
Jeff Thompson | 86bcd02 | 2013-07-26 17:55:03 -0700 | [diff] [blame] | 170 | //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 Thompson | 83c4a96 | 2013-07-31 18:05:37 -0700 | [diff] [blame] | 198 | BinaryXmlWireFormat.decodeContentObject = function(contentObject, decoder) { |
Jeff Thompson | 86bcd02 | 2013-07-26 17:55:03 -0700 | [diff] [blame] | 199 | // 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 | }; |