Added BinaryXMLWireFormat.js and moved to_ccnb and from_ccnb into there from Interest.js and ContentObject.js. In Interest and ContentObject, added encode and decode methods which take a WireFormat object.
diff --git a/js/ContentObject.js b/js/ContentObject.js
index d95940c..7c66f3c 100644
--- a/js/ContentObject.js
+++ b/js/ContentObject.js
@@ -114,74 +114,34 @@
this.rawSignatureData = sigBits;
};
+// Deprecated. Use BinaryXMLWireFormat.decodeContentObject.
ContentObject.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) {
- // TODO VALIDATE THAT ALL FIELDS EXCEPT SIGNATURE ARE PRESENT
- decoder.readStartElement(this.getElementLabel());
-
- if( decoder.peekStartElement(CCNProtocolDTags.Signature) ){
- this.signature = new Signature();
- this.signature.from_ccnb(decoder);
- }
- else
- this.signature = null;
-
- this.startSIG = decoder.offset;
-
- this.name = new Name();
- this.name.from_ccnb(decoder);
-
- if( decoder.peekStartElement(CCNProtocolDTags.SignedInfo) ){
- this.signedInfo = new SignedInfo();
- this.signedInfo.from_ccnb(decoder);
- }
- else
- this.signedInfo = null;
-
- this.content = decoder.readBinaryElement(CCNProtocolDTags.Content, null, true);
-
- this.endSIG = decoder.offset;
-
- decoder.readEndElement();
-
- this.saveRawData(decoder.input);
+ BinaryXMLWireFormat.decodeContentObject(this, decoder);
};
+// Deprecated. Use BinaryXMLWireFormat.encodeContentObject.
ContentObject.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) {
+ BinaryXMLWireFormat.encodeContentObject(this, encoder);
+};
- //TODO verify name, SignedInfo and Signature is present
+/**
+ * Encode this ContentObject for a particular wire format.
+ * @param {WireFormat} wireFormat if null, use BinaryXMLWireFormat.
+ * @returns {Uint8Array}
+ */
+ContentObject.prototype.encode = function(wireFormat) {
+ wireFormat = (wireFormat || BinaryXMLWireFormat.instance);
+ return wireFormat.encodeContentObject(this);
+};
-
- encoder.writeStartElement(this.getElementLabel());
-
-
-
-
- if(null!=this.signature) this.signature.to_ccnb(encoder);
-
-
- this.startSIG = encoder.offset;
-
-
- if(null!=this.name) this.name.to_ccnb(encoder);
-
- //this.endSIG = encoder.offset;
- //this.startSignedInfo = encoder.offset;
-
-
- if(null!=this.signedInfo) this.signedInfo.to_ccnb(encoder);
-
- encoder.writeElement(CCNProtocolDTags.Content, this.content);
-
-
- this.endSIG = encoder.offset;
-
- //this.endContent = encoder.offset;
-
-
- encoder.writeEndElement();
-
- this.saveRawData(encoder.ostream);
-
+/**
+ * Decode the input using a particular wire format and update this ContentObject.
+ * @param {Uint8Array} input
+ * @param {WireFormat} wireFormat if null, use BinaryXMLWireFormat.
+ */
+ContentObject.prototype.decode = function(input, wireFormat) {
+ wireFormat = (wireFormat || BinaryXMLWireFormat.instance);
+ wireFormat.decodeContentObject(this, input);
};
ContentObject.prototype.getElementLabel= function(){return CCNProtocolDTags.ContentObject;};
diff --git a/js/Interest.js b/js/Interest.js
index e8d2891..692b90c 100644
--- a/js/Interest.js
+++ b/js/Interest.js
@@ -34,104 +34,34 @@
Interest.DEFAULT_ANSWER_ORIGIN_KIND = Interest.ANSWER_CONTENT_STORE | Interest.ANSWER_GENERATED;
-
+// Deprecated. Use BinaryXMLWireFormat.decodeInterest.
Interest.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) {
-
- decoder.readStartElement(CCNProtocolDTags.Interest);
-
- this.name = new Name();
- this.name.from_ccnb(decoder);
-
- if (decoder.peekStartElement(CCNProtocolDTags.MinSuffixComponents))
- this.minSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MinSuffixComponents);
- else
- this.minSuffixComponents = null;
-
- if (decoder.peekStartElement(CCNProtocolDTags.MaxSuffixComponents))
- this.maxSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MaxSuffixComponents);
- else
- this.maxSuffixComponents = null;
-
- if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) {
- this.publisherPublicKeyDigest = new PublisherPublicKeyDigest();
- this.publisherPublicKeyDigest.from_ccnb(decoder);
- }
- else
- this.publisherPublicKeyDigest = null;
-
- if (decoder.peekStartElement(CCNProtocolDTags.Exclude)) {
- this.exclude = new Exclude();
- this.exclude.from_ccnb(decoder);
- }
- else
- this.exclude = null;
-
- if (decoder.peekStartElement(CCNProtocolDTags.ChildSelector))
- this.childSelector = decoder.readIntegerElement(CCNProtocolDTags.ChildSelector);
- else
- this.childSelector = null;
-
- if (decoder.peekStartElement(CCNProtocolDTags.AnswerOriginKind))
- this.answerOriginKind = decoder.readIntegerElement(CCNProtocolDTags.AnswerOriginKind);
- else
- this.answerOriginKind = null;
-
- if (decoder.peekStartElement(CCNProtocolDTags.Scope))
- this.scope = decoder.readIntegerElement(CCNProtocolDTags.Scope);
- else
- this.scope = null;
-
- if (decoder.peekStartElement(CCNProtocolDTags.InterestLifetime))
- this.interestLifetime = 1000.0 * DataUtils.bigEndianToUnsignedInt
- (decoder.readBinaryElement(CCNProtocolDTags.InterestLifetime)) / 4096;
- else
- this.interestLifetime = null;
-
- if (decoder.peekStartElement(CCNProtocolDTags.Nonce))
- this.nonce = decoder.readBinaryElement(CCNProtocolDTags.Nonce);
- else
- this.nonce = null;
-
- decoder.readEndElement();
+ BinaryXMLWireFormat.decodeInterest(this, decoder);
};
+// Deprecated. Use BinaryXMLWireFormat.encodeInterest.
Interest.prototype.to_ccnb = function(/*XMLEncoder*/ encoder){
- //Could check if name is present
-
- encoder.writeStartElement(CCNProtocolDTags.Interest);
-
- this.name.to_ccnb(encoder);
-
- if (null != this.minSuffixComponents)
- encoder.writeElement(CCNProtocolDTags.MinSuffixComponents, this.minSuffixComponents);
+ BinaryXMLWireFormat.encodeInterest(this, encoder);
+};
- if (null != this.maxSuffixComponents)
- encoder.writeElement(CCNProtocolDTags.MaxSuffixComponents, this.maxSuffixComponents);
+/**
+ * Encode this Interest for a particular wire format.
+ * @param {WireFormat} wireFormat if null, use BinaryXMLWireFormat.
+ * @returns {Uint8Array}
+ */
+Interest.prototype.encode = function(wireFormat) {
+ wireFormat = (wireFormat || BinaryXMLWireFormat.instance);
+ return wireFormat.encodeInterest(this);
+};
- if (null != this.publisherPublicKeyDigest)
- this.publisherPublicKeyDigest.to_ccnb(encoder);
-
- if (null != this.exclude)
- this.exclude.to_ccnb(encoder);
-
- if (null != this.childSelector)
- encoder.writeElement(CCNProtocolDTags.ChildSelector, this.childSelector);
-
- if (this.DEFAULT_ANSWER_ORIGIN_KIND != this.answerOriginKind && this.answerOriginKind!=null)
- encoder.writeElement(CCNProtocolDTags.AnswerOriginKind, this.answerOriginKind);
-
- if (null != this.scope)
- encoder.writeElement(CCNProtocolDTags.Scope, this.scope);
-
- if (null != this.interestLifetime)
- encoder.writeElement(CCNProtocolDTags.InterestLifetime,
- DataUtils.nonNegativeIntToBigEndian((this.interestLifetime / 1000.0) * 4096));
-
- if (null != this.nonce)
- encoder.writeElement(CCNProtocolDTags.Nonce, this.nonce);
-
- encoder.writeEndElement();
-
+/**
+ * Decode the input using a particular wire format and update this Interest.
+ * @param {Uint8Array} input
+ * @param {WireFormat} wireFormat if null, use BinaryXMLWireFormat.
+ */
+Interest.prototype.decode = function(input, wireFormat) {
+ wireFormat = (wireFormat || BinaryXMLWireFormat.instance);
+ wireFormat.decodeInterest(this, input);
};
/*
diff --git a/js/encoding/BinaryXMLWireFormat.js b/js/encoding/BinaryXMLWireFormat.js
new file mode 100644
index 0000000..5cc2a5f
--- /dev/null
+++ b/js/encoding/BinaryXMLWireFormat.js
@@ -0,0 +1,229 @@
+/**
+ * @author: Jeff Thompson
+ * See COPYING for copyright and distribution information.
+ * This class represents Interest Objects
+ */
+
+/**
+ * A BinaryXMLWireFormat implements the WireFormat interface for encoding and decoding in binary XML.
+ */
+var BinaryXMLWireFormat = function BinaryXMLWireFormat() {
+};
+
+/**
+ * Encode the interest and return a Uint8Array.
+ * @param {Interest} interest
+ * @returns {UInt8Array}
+ */
+BinaryXMLWireFormat.prototype.encodeInterest = function(interest) {
+ var encoder = new BinaryXMLEncoder();
+ interest.to_ccnb(encoder);
+
+ return encoder.getReducedOstream();
+};
+
+/**
+ * Decode the input and put the result in interest.
+ * @param {Interest} interest
+ * @param {Uint8Array} input
+ */
+BinaryXMLWireFormat.prototype.decodeInterest = function(interest, input) {
+ var decoder = new BinaryXMLDecoder(input);
+ BinaryXMLWireFormat.decodeInterest(interest, decoder);
+};
+
+/**
+ * Encode the contentObject and return a Uint8Array.
+ * @param {ContentObject} contentObject
+ * @returns {Uint8Array}
+ */
+BinaryXMLWireFormat.prototype.encodeContentObject = function(contentObject) {
+ var encoder = new BinaryXMLEncoder();
+ BinaryXMLWireFormat(contentObject, encoder);
+
+ return encoder.getReducedOstream();
+};
+
+/**
+ * Decode the input and put the result in contentObject.
+ * @param {ContentObject} contentObject
+ * @param {Uint8Array} input
+ */
+BinaryXMLWireFormat.prototype.decodeContentObject = function(contentObject, input) {
+ var decoder = new BinaryXMLDecoder(input);
+ contentObject.from_ccnb(decoder);
+};
+
+// Default object.
+BinaryXMLWireFormat.instance = new BinaryXMLWireFormat();
+
+/**
+ * Encode the interest by calling the operations on the encoder.
+ * @param {Interest} interest
+ * @param {BinaryXMLEncoder} encoder
+ */
+BinaryXMLWireFormat.encodeInterest = function(interest, encoder) {
+ encoder.writeStartElement(CCNProtocolDTags.Interest);
+
+ interest.name.to_ccnb(encoder);
+
+ if (null != interest.minSuffixComponents)
+ encoder.writeElement(CCNProtocolDTags.MinSuffixComponents, interest.minSuffixComponents);
+
+ if (null != interest.maxSuffixComponents)
+ encoder.writeElement(CCNProtocolDTags.MaxSuffixComponents, interest.maxSuffixComponents);
+
+ if (null != interest.publisherPublicKeyDigest)
+ interest.publisherPublicKeyDigest.to_ccnb(encoder);
+
+ if (null != interest.exclude)
+ interest.exclude.to_ccnb(encoder);
+
+ if (null != interest.childSelector)
+ encoder.writeElement(CCNProtocolDTags.ChildSelector, interest.childSelector);
+
+ if (interest.DEFAULT_ANSWER_ORIGIN_KIND != interest.answerOriginKind && interest.answerOriginKind!=null)
+ encoder.writeElement(CCNProtocolDTags.AnswerOriginKind, interest.answerOriginKind);
+
+ if (null != interest.scope)
+ encoder.writeElement(CCNProtocolDTags.Scope, interest.scope);
+
+ if (null != interest.interestLifetime)
+ encoder.writeElement(CCNProtocolDTags.InterestLifetime,
+ DataUtils.nonNegativeIntToBigEndian((interest.interestLifetime / 1000.0) * 4096));
+
+ if (null != interest.nonce)
+ encoder.writeElement(CCNProtocolDTags.Nonce, interest.nonce);
+
+ encoder.writeEndElement();
+};
+
+/**
+ * Use the decoder to place the result in interest.
+ * @param {Interest} interest
+ * @param {BinaryXMLDecoder} decoder
+ */
+BinaryXMLWireFormat.decodeInterest = function(interest, decoder) {
+ decoder.readStartElement(CCNProtocolDTags.Interest);
+
+ interest.name = new Name();
+ interest.name.from_ccnb(decoder);
+
+ if (decoder.peekStartElement(CCNProtocolDTags.MinSuffixComponents))
+ interest.minSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MinSuffixComponents);
+ else
+ interest.minSuffixComponents = null;
+
+ if (decoder.peekStartElement(CCNProtocolDTags.MaxSuffixComponents))
+ interest.maxSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MaxSuffixComponents);
+ else
+ interest.maxSuffixComponents = null;
+
+ if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) {
+ interest.publisherPublicKeyDigest = new PublisherPublicKeyDigest();
+ interest.publisherPublicKeyDigest.from_ccnb(decoder);
+ }
+ else
+ interest.publisherPublicKeyDigest = null;
+
+ if (decoder.peekStartElement(CCNProtocolDTags.Exclude)) {
+ interest.exclude = new Exclude();
+ interest.exclude.from_ccnb(decoder);
+ }
+ else
+ interest.exclude = null;
+
+ if (decoder.peekStartElement(CCNProtocolDTags.ChildSelector))
+ interest.childSelector = decoder.readIntegerElement(CCNProtocolDTags.ChildSelector);
+ else
+ interest.childSelector = null;
+
+ if (decoder.peekStartElement(CCNProtocolDTags.AnswerOriginKind))
+ interest.answerOriginKind = decoder.readIntegerElement(CCNProtocolDTags.AnswerOriginKind);
+ else
+ interest.answerOriginKind = null;
+
+ if (decoder.peekStartElement(CCNProtocolDTags.Scope))
+ interest.scope = decoder.readIntegerElement(CCNProtocolDTags.Scope);
+ else
+ interest.scope = null;
+
+ if (decoder.peekStartElement(CCNProtocolDTags.InterestLifetime))
+ interest.interestLifetime = 1000.0 * DataUtils.bigEndianToUnsignedInt
+ (decoder.readBinaryElement(CCNProtocolDTags.InterestLifetime)) / 4096;
+ else
+ interest.interestLifetime = null;
+
+ if (decoder.peekStartElement(CCNProtocolDTags.Nonce))
+ interest.nonce = decoder.readBinaryElement(CCNProtocolDTags.Nonce);
+ else
+ interest.nonce = null;
+
+ decoder.readEndElement();
+};
+
+/**
+ * Encode the contentObject by calling the operations on the encoder.
+ * @param {ContentObject} contentObject
+ * @param {BinaryXMLEncoder} encoder
+ */
+BinaryXMLWireFormat.encodeContentObject = function(contentObject, encoder) {
+ //TODO verify name, SignedInfo and Signature is present
+ encoder.writeStartElement(contentObject.getElementLabel());
+
+ if (null != contentObject.signature)
+ contentObject.signature.to_ccnb(encoder);
+
+ contentObject.startSIG = encoder.offset;
+
+ if (null != contentObject.name)
+ contentObject.name.to_ccnb(encoder);
+
+ if (null != contentObject.signedInfo)
+ contentObject.signedInfo.to_ccnb(encoder);
+
+ encoder.writeElement(CCNProtocolDTags.Content, contentObject.content);
+
+ contentObject.endSIG = encoder.offset;
+
+ encoder.writeEndElement();
+
+ contentObject.saveRawData(encoder.ostream);
+};
+
+/**
+ * Use the decoder to place the result in contentObject.
+ * @param {ContentObject} contentObject
+ * @param {BinaryXMLDecoder} decoder
+ */
+BinaryXMLWireFormat.decodeContentObject = function(contentObject, decoder) {
+ // TODO VALIDATE THAT ALL FIELDS EXCEPT SIGNATURE ARE PRESENT
+ decoder.readStartElement(contentObject.getElementLabel());
+
+ if( decoder.peekStartElement(CCNProtocolDTags.Signature) ){
+ contentObject.signature = new Signature();
+ contentObject.signature.from_ccnb(decoder);
+ }
+ else
+ contentObject.signature = null;
+
+ contentObject.startSIG = decoder.offset;
+
+ contentObject.name = new Name();
+ contentObject.name.from_ccnb(decoder);
+
+ if( decoder.peekStartElement(CCNProtocolDTags.SignedInfo) ){
+ contentObject.signedInfo = new SignedInfo();
+ contentObject.signedInfo.from_ccnb(decoder);
+ }
+ else
+ contentObject.signedInfo = null;
+
+ contentObject.content = decoder.readBinaryElement(CCNProtocolDTags.Content, null, true);
+
+ contentObject.endSIG = decoder.offset;
+
+ decoder.readEndElement();
+
+ contentObject.saveRawData(decoder.input);
+};