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