Added support for ForwardingEntry.
diff --git a/ndn-cpp/c/encoding/binary-xml-forwarding-entry.c b/ndn-cpp/c/encoding/binary-xml-forwarding-entry.c
new file mode 100644
index 0000000..982ef23
--- /dev/null
+++ b/ndn-cpp/c/encoding/binary-xml-forwarding-entry.c
@@ -0,0 +1,67 @@
+/**
+ * @author: Jeff Thompson
+ * Derived from ForwardingEntry.js by Meki Cheraoui.
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "binary-xml.h"
+#include "binary-xml-forwarding-entry.h"
+
+ndn_Error ndn_encodeBinaryXmlForwardingEntry(struct ndn_ForwardingEntry *forwardingEntry, struct ndn_BinaryXmlEncoder *encoder)
+{
+ ndn_Error error;
+ if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_ForwardingEntry)))
+ return error;
+
+ if ((error = ndn_BinaryXmlEncoder_writeOptionalUDataDTagElement
+ (encoder, ndn_BinaryXml_DTag_Action, forwardingEntry->action, forwardingEntry->actionLength)))
+ return error;
+ if ((error = ndn_encodeBinaryXmlName(&forwardingEntry->prefix, encoder)))
+ return error;
+ // This will skip encoding if there is no publisherPublicKeyDigest.
+ if ((error = ndn_encodeBinaryXmlPublisherPublicKeyDigest(&forwardingEntry->publisherPublicKeyDigest, encoder)))
+ return error;
+ if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
+ (encoder, ndn_BinaryXml_DTag_FaceID, forwardingEntry->faceId)))
+ return error;
+ if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
+ (encoder, ndn_BinaryXml_DTag_ForwardingFlags, forwardingEntry->forwardingFlags)))
+ return error;
+ if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
+ (encoder, ndn_BinaryXml_DTag_FreshnessSeconds, forwardingEntry->freshnessSeconds)))
+ return error;
+
+ if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
+ return error;
+
+ return NDN_ERROR_success;
+}
+
+ndn_Error ndn_decodeBinaryXmlForwardingEntry(struct ndn_ForwardingEntry *forwardingEntry, struct ndn_BinaryXmlDecoder *decoder)
+{
+ ndn_Error error;
+ if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_ForwardingEntry)))
+ return error;
+
+ if ((error = ndn_BinaryXmlDecoder_readOptionalUDataDTagElement
+ (decoder, ndn_BinaryXml_DTag_Action, &forwardingEntry->action, &forwardingEntry->actionLength)))
+ return error;
+ if ((error = ndn_decodeBinaryXmlName(&forwardingEntry->prefix, decoder)))
+ return error;
+ if ((error = ndn_decodeOptionalBinaryXmlPublisherPublicKeyDigest(&forwardingEntry->publisherPublicKeyDigest, decoder)))
+ return error;
+ if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
+ (decoder, ndn_BinaryXml_DTag_FaceID, &forwardingEntry->faceId)))
+ return error;
+ if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
+ (decoder, ndn_BinaryXml_DTag_ForwardingFlags, &forwardingEntry->forwardingFlags)))
+ return error;
+ if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
+ (decoder, ndn_BinaryXml_DTag_FreshnessSeconds, &forwardingEntry->freshnessSeconds)))
+ return error;
+
+ if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
+ return error;
+
+ return NDN_ERROR_success;
+}
diff --git a/ndn-cpp/c/encoding/binary-xml-forwarding-entry.h b/ndn-cpp/c/encoding/binary-xml-forwarding-entry.h
new file mode 100644
index 0000000..ff5ef5b
--- /dev/null
+++ b/ndn-cpp/c/encoding/binary-xml-forwarding-entry.h
@@ -0,0 +1,38 @@
+/**
+ * @author: Jeff Thompson
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NDN_BINARY_XML_FORWARDING_ENTRY_H
+#define NDN_BINARY_XML_FORWARDING_ENTRY_H
+
+#include "../errors.h"
+#include "../forwarding-entry.h"
+#include "binary-xml-encoder.h"
+#include "binary-xml-decoder.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * Encode the ndn_ForwardingEntry struct using Binary XML.
+ * @param forwardingEntry pointer to the ndn_ForwardingEntry struct
+ * @param encoder pointer to the ndn_BinaryXmlEncoder struct
+ * @return 0 for success, else an error code
+ */
+ndn_Error ndn_encodeBinaryXmlForwardingEntry(struct ndn_ForwardingEntry *forwardingEntry, struct ndn_BinaryXmlEncoder *encoder);
+
+/**
+ * Expect the next element to be a Binary XML ForwardingEntry and decode into the ndn_ForwardingEntry struct.
+ * @param forwardingEntry pointer to the ndn_ForwardingEntry struct
+ * @param decoder pointer to the ndn_BinaryXmlDecoder struct
+ * @return 0 for success, else an error code, including if the next element is not KeyLocator.
+ */
+ndn_Error ndn_decodeBinaryXmlForwardingEntry(struct ndn_ForwardingEntry *forwardingEntry, struct ndn_BinaryXmlDecoder *decoder);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/ndn-cpp/c/forwarding-entry.h b/ndn-cpp/c/forwarding-entry.h
new file mode 100644
index 0000000..be82fee
--- /dev/null
+++ b/ndn-cpp/c/forwarding-entry.h
@@ -0,0 +1,52 @@
+/**
+ * @author: Jeff Thompson
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NDN_FORWARDING_ENTRY_H
+#define NDN_FORWARDING_ENTRY_H
+
+#include "name.h"
+#include "publisher-public-key-digest.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * An ndn_ForwardingEntry holds fields for a ForwardingEntry which is used to register a prefix with a hub.
+ */
+struct ndn_ForwardingEntry {
+ unsigned char *action; /**< pointer to pre-allocated buffer. 0 for none. */
+ unsigned int actionLength; /**< length of action. 0 for none. */
+ struct ndn_Name prefix;
+ struct ndn_PublisherPublicKeyDigest publisherPublicKeyDigest;
+ int faceId; /**< -1 for none. */
+ int forwardingFlags; /**< -1 for none. */
+ int freshnessSeconds; /**< -1 for none. */
+};
+
+/**
+ * Initialize an ndn_ForwardingEntry struct with the pre-allocated prefixNameComponents,
+ * and defaults for all the values.
+ * @param self pointer to the ndn_Interest struct
+ * @param prefixNameComponents the pre-allocated array of ndn_NameComponent
+ * @param maxPrefixNameComponents the number of elements in the allocated prefixNameComponents array
+ */
+static inline void ndn_ForwardingEntry_init
+ (struct ndn_ForwardingEntry *self, struct ndn_NameComponent *prefixNameComponents, unsigned int maxPrefixNameComponents)
+{
+ self->action = 0;
+ self->actionLength = 0;
+ ndn_Name_init(&self->prefix, prefixNameComponents, maxPrefixNameComponents);
+ ndn_PublisherPublicKeyDigest_init(&self->publisherPublicKeyDigest);
+ self->faceId = -1;
+ self->forwardingFlags = -1;
+ self->freshnessSeconds = -1;
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/ndn-cpp/encoding/binary-xml-wire-format.cpp b/ndn-cpp/encoding/binary-xml-wire-format.cpp
index 8cb5643..950d653 100644
--- a/ndn-cpp/encoding/binary-xml-wire-format.cpp
+++ b/ndn-cpp/encoding/binary-xml-wire-format.cpp
@@ -6,8 +6,10 @@
#include <stdexcept>
#include "../c/encoding/binary-xml-interest.h"
#include "../c/encoding/binary-xml-data.h"
+#include "../c/encoding/binary-xml-forwarding-entry.h"
#include "../interest.hpp"
#include "../data.hpp"
+#include "../forwarding-entry.hpp"
#include "binary-xml-encoder.hpp"
#include "binary-xml-decoder.hpp"
#include "binary-xml-wire-format.hpp"
@@ -94,4 +96,35 @@
data.set(dataStruct);
}
+ptr_lib::shared_ptr<vector<unsigned char> > BinaryXmlWireFormat::encodeForwardingEntry(const ForwardingEntry &forwardingEntry)
+{
+ struct ndn_NameComponent prefixNameComponents[100];
+ struct ndn_ForwardingEntry forwardingEntryStruct;
+ ndn_ForwardingEntry_init
+ (&forwardingEntryStruct, prefixNameComponents, sizeof(prefixNameComponents) / sizeof(prefixNameComponents[0]));
+ forwardingEntry.get(forwardingEntryStruct);
+
+ BinaryXmlEncoder encoder;
+ ndn_Error error;
+ if ((error = ndn_encodeBinaryXmlForwardingEntry(&forwardingEntryStruct, &encoder)))
+ throw std::runtime_error(ndn_getErrorString(error));
+
+ return encoder.getOutput();
+}
+
+void BinaryXmlWireFormat::decodeForwardingEntry(ForwardingEntry &forwardingEntry, const unsigned char *input, unsigned int inputLength)
+{
+ struct ndn_NameComponent prefixNameComponents[100];
+ struct ndn_ForwardingEntry forwardingEntryStruct;
+ ndn_ForwardingEntry_init
+ (&forwardingEntryStruct, prefixNameComponents, sizeof(prefixNameComponents) / sizeof(prefixNameComponents[0]));
+
+ BinaryXmlDecoder decoder(input, inputLength);
+ ndn_Error error;
+ if ((error = ndn_decodeBinaryXmlForwardingEntry(&forwardingEntryStruct, &decoder)))
+ throw std::runtime_error(ndn_getErrorString(error));
+
+ forwardingEntry.set(forwardingEntryStruct);
+}
+
}
diff --git a/ndn-cpp/encoding/binary-xml-wire-format.hpp b/ndn-cpp/encoding/binary-xml-wire-format.hpp
index 7266957..68be550 100644
--- a/ndn-cpp/encoding/binary-xml-wire-format.hpp
+++ b/ndn-cpp/encoding/binary-xml-wire-format.hpp
@@ -16,7 +16,19 @@
*/
class BinaryXmlWireFormat : public WireFormat {
public:
+ /**
+ * Encode interest in binary XML and return the encoding.
+ * @param interest The Interest object to encode.
+ * @return A shared_ptr with the vector<unsigned char> containing the encoding.
+ */
virtual ptr_lib::shared_ptr<std::vector<unsigned char> > encodeInterest(const Interest &interest);
+
+ /**
+ * Decode input as an interest in binary XML and set the fields of the interest object.
+ * @param interest The Interest object whose fields are updated.
+ * @param input A pointer to the input buffer to decode.
+ * @param inputLength The number of bytes in input.
+ */
virtual void decodeInterest(Interest &interest, const unsigned char *input, unsigned int inputLength);
/**
@@ -45,6 +57,21 @@
*/
virtual void decodeData
(Data &data, const unsigned char *input, unsigned int inputLength, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset);
+
+ /**
+ * Encode forwardingEntry in binary XML and return the encoding.
+ * @param forwardingEntry The ForwardingEntry object to encode.
+ * @return A shared_ptr with the vector<unsigned char> containing the encoding.
+ */
+ virtual ptr_lib::shared_ptr<std::vector<unsigned char> > encodeForwardingEntry(const ForwardingEntry &forwardingEntry);
+
+ /**
+ * Decode input as a forwarding entry in binary XML and set the fields of the forwardingEntry object.
+ * @param forwardingEntry The ForwardingEntry object whose fields are updated.
+ * @param input A pointer to the input buffer to decode.
+ * @param inputLength The number of bytes in input.
+ */
+ virtual void decodeForwardingEntry(ForwardingEntry &forwardingEntry, const unsigned char *input, unsigned int inputLength);
};
}
diff --git a/ndn-cpp/encoding/wire-format.cpp b/ndn-cpp/encoding/wire-format.cpp
index 703795a..036262d 100644
--- a/ndn-cpp/encoding/wire-format.cpp
+++ b/ndn-cpp/encoding/wire-format.cpp
@@ -46,4 +46,13 @@
throw logic_error("unimplemented");
}
+ptr_lib::shared_ptr<vector<unsigned char> > WireFormat::encodeForwardingEntry(const ForwardingEntry &forwardingEntry)
+{
+ throw logic_error("unimplemented");
+}
+void WireFormat::decodeForwardingEntry(ForwardingEntry &forwardingEntry, const unsigned char *input, unsigned int inputLength)
+{
+ throw logic_error("unimplemented");
+}
+
}
diff --git a/ndn-cpp/encoding/wire-format.hpp b/ndn-cpp/encoding/wire-format.hpp
index 939c00c..bfd1a1d 100644
--- a/ndn-cpp/encoding/wire-format.hpp
+++ b/ndn-cpp/encoding/wire-format.hpp
@@ -13,10 +13,25 @@
class Interest;
class Data;
+class ForwardingEntry;
class WireFormat {
public:
+ /**
+ * Encode interest and return the encoding. Your derived class should override.
+ * @param interest The Interest object to encode.
+ * @return A shared_ptr with the vector<unsigned char> containing the encoding.
+ * @throw logic_error for unimplemented if the derived class does not override.
+ */
virtual ptr_lib::shared_ptr<std::vector<unsigned char> > encodeInterest(const Interest &interest);
+
+ /**
+ * Decode input as an interest and set the fields of the interest object. Your derived class should override.
+ * @param interest The Interest object whose fields are updated.
+ * @param input A pointer to the input buffer to decode.
+ * @param inputLength The number of bytes in input.
+ * @throw logic_error for unimplemented if the derived class does not override.
+ */
virtual void decodeInterest(Interest &interest, const unsigned char *input, unsigned int inputLength);
/**
@@ -67,6 +82,23 @@
}
/**
+ * Encode forwardingEntry and return the encoding. Your derived class should override.
+ * @param forwardingEntry The ForwardingEntry object to encode.
+ * @return A shared_ptr with the vector<unsigned char> containing the encoding.
+ * @throw logic_error for unimplemented if the derived class does not override.
+ */
+ virtual ptr_lib::shared_ptr<std::vector<unsigned char> > encodeForwardingEntry(const ForwardingEntry &forwardingEntry);
+
+ /**
+ * Decode input as a forwarding entry and set the fields of the forwardingEntry object. Your derived class should override.
+ * @param forwardingEntry The ForwardingEntry object whose fields are updated.
+ * @param input A pointer to the input buffer to decode.
+ * @param inputLength The number of bytes in input.
+ * @throw logic_error for unimplemented if the derived class does not override.
+ */
+ virtual void decodeForwardingEntry(ForwardingEntry &forwardingEntry, const unsigned char *input, unsigned int inputLength);
+
+ /**
* Set the static default WireFormat used by default encoding and decoding methods.
* @param wireFormat A Pointer to an object of a subclass of WireFormat. This does not make a copy and
* the caller must ensure that the object remains allocated.
diff --git a/ndn-cpp/forwarding-entry.cpp b/ndn-cpp/forwarding-entry.cpp
new file mode 100644
index 0000000..0e8c6d7
--- /dev/null
+++ b/ndn-cpp/forwarding-entry.cpp
@@ -0,0 +1,42 @@
+/**
+ * @author: Jeff Thompson
+ * See COPYING for copyright and distribution information.
+ */
+#include <stdexcept>
+#include "common.hpp"
+#include "forwarding-entry.hpp"
+
+using namespace std;
+
+namespace ndn {
+
+void ForwardingEntry::set(const struct ndn_ForwardingEntry &forwardingEntryStruct)
+{
+ if (forwardingEntryStruct.action && forwardingEntryStruct.actionLength > 0)
+ action_ = string(forwardingEntryStruct.action, forwardingEntryStruct.action + forwardingEntryStruct.actionLength);
+ else
+ action_ = "";
+
+ prefix_.set(forwardingEntryStruct.prefix);
+ publisherPublicKeyDigest_.set(forwardingEntryStruct.publisherPublicKeyDigest);
+ faceId_ = forwardingEntryStruct.faceId;
+ forwardingFlags_ = forwardingEntryStruct.forwardingFlags;
+ freshnessSeconds_ = forwardingEntryStruct.freshnessSeconds;
+}
+
+void ForwardingEntry::get(struct ndn_ForwardingEntry &forwardingEntryStruct) const
+{
+ prefix_.get(forwardingEntryStruct.prefix);
+ publisherPublicKeyDigest_.get(forwardingEntryStruct.publisherPublicKeyDigest);
+ forwardingEntryStruct.faceId = faceId_;
+ forwardingEntryStruct.forwardingFlags = forwardingFlags_;
+ forwardingEntryStruct.freshnessSeconds = freshnessSeconds_;
+
+ forwardingEntryStruct.actionLength = action_.size();
+ if (action_.size() > 0)
+ forwardingEntryStruct.action = (unsigned char *)&action_[0];
+ else
+ forwardingEntryStruct.action = 0;
+}
+
+}
diff --git a/ndn-cpp/forwarding-entry.hpp b/ndn-cpp/forwarding-entry.hpp
new file mode 100644
index 0000000..8c652a7
--- /dev/null
+++ b/ndn-cpp/forwarding-entry.hpp
@@ -0,0 +1,104 @@
+/**
+ * @author: Jeff Thompson
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NDN_FORWARDING_ENTRY_HPP
+#define NDN_FORWARDING_ENTRY_HPP
+
+#include <string>
+#include "name.hpp"
+#include "publisher-public-key-digest.hpp"
+#include "c/forwarding-entry.h"
+
+namespace ndn {
+
+/**
+ * An ForwardingEntry holds an action and Name prefix and other fields for an forwarding entry.
+ */
+class ForwardingEntry {
+public:
+ ForwardingEntry
+ (const std::string &action, const Name &prefix, const PublisherPublicKeyDigest publisherPublicKeyDigest,
+ int faceId, int forwardingFlags, int freshnessSeconds)
+ : action_(action), prefix_(prefix), publisherPublicKeyDigest_(publisherPublicKeyDigest),
+ faceId_(faceId), forwardingFlags_(forwardingFlags), freshnessSeconds_(freshnessSeconds)
+ {
+ }
+
+ ForwardingEntry()
+ : faceId_(-1), forwardingFlags_(-1), freshnessSeconds_(-1)
+ {
+ }
+
+ ptr_lib::shared_ptr<std::vector<unsigned char> > wireEncode(WireFormat &wireFormat) const
+ {
+ return wireFormat.encodeForwardingEntry(*this);
+ }
+ ptr_lib::shared_ptr<std::vector<unsigned char> > wireEncode() const
+ {
+ return wireEncode(*WireFormat::getDefaultWireFormat());
+ }
+ void wireDecode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat)
+ {
+ wireFormat.decodeForwardingEntry(*this, input, inputLength);
+ }
+ void wireDecode(const unsigned char *input, unsigned int inputLength)
+ {
+ wireDecode(input, inputLength, *WireFormat::getDefaultWireFormat());
+ }
+ void wireDecode(const std::vector<unsigned char> &input, WireFormat &wireFormat)
+ {
+ wireDecode(&input[0], input.size(), wireFormat);
+ }
+ void wireDecode(const std::vector<unsigned char> &input)
+ {
+ wireDecode(&input[0], input.size());
+ }
+
+ /**
+ * Set the forwardingEntryStruct to point to the components in this forwarding entry, without copying any memory.
+ * WARNING: The resulting pointers in forwardingEntryStruct are invalid after a further use of this object which could reallocate memory.
+ * @param forwardingEntryStruct a C ndn_ForwardingEntry struct where the prefix name components array is already allocated.
+ */
+ void get(struct ndn_ForwardingEntry &forwardingEntryStruct) const;
+
+ const std::string &getAction() const { return action_; }
+
+ Name &getPrefix() { return prefix_; }
+ const Name &getPrefix() const { return prefix_; }
+
+ PublisherPublicKeyDigest &getPublisherPublicKeyDigest() { return publisherPublicKeyDigest_; }
+ const PublisherPublicKeyDigest &getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; }
+
+ int getFaceId() const { return faceId_; }
+
+ int getForwardingFlags() const { return forwardingFlags_; }
+
+ int getFreshnessSeconds() const { return freshnessSeconds_; }
+
+ /**
+ * Clear this forwarding entry, and set the values by copying from forwardingEntryStruct.
+ * @param forwardingEntryStruct a C ndn_ForwardingEntry struct.
+ */
+ void set(const struct ndn_ForwardingEntry &forwardingEntryStruct);
+
+ void setAction(const std::string &value) { action_ = value; }
+
+ void setFaceId(int value) { faceId_ = value; }
+
+ void setForwardingFlags(int value) { forwardingFlags_ = value; }
+
+ void setFreshnessSeconds(int value) { freshnessSeconds_ = value; }
+
+ std::string action_; /**< empty for none. */
+ Name prefix_;
+ PublisherPublicKeyDigest publisherPublicKeyDigest_;
+ int faceId_; /**< -1 for none. */
+ int forwardingFlags_; /**< -1 for none. */
+ int freshnessSeconds_; /**< -1 for none. */
+};
+
+}
+
+#endif