Implement interest Exclude.
diff --git a/ndn-cpp/c/Interest.h b/ndn-cpp/c/Interest.h
index 57c340f..81d9bce 100644
--- a/ndn-cpp/c/Interest.h
+++ b/ndn-cpp/c/Interest.h
@@ -12,6 +12,55 @@
extern "C" {
#endif
+typedef enum {
+ ndn_Exclude_COMPONENT = 0,
+ ndn_Exclude_ANY = 1
+} ndn_ExcludeType;
+
+/**
+ * An ndn_ExcludeEntry holds an ndn_ExcludeType, and if it is a COMPONENT, it holds a pointer to the component value.
+ */
+struct ndn_ExcludeEntry {
+ ndn_ExcludeType type;
+ unsigned char *component; /**< pointer to the pre-allocated buffer for the component value */
+ unsigned int componentLength; /**< the number of bytes in value */
+};
+
+/**
+ *
+ * @param self pointer to the ndn_NameComponent struct
+ * @param type one of the ndn_ExcludeType enum
+ * @param component the pre-allocated buffer for the component value, only used if type is ndn_Exclude_COMPONENT
+ * @param componentLength the number of bytes in value, only used if type is ndn_Exclude_COMPONENT
+ */
+static inline void ndn_ExcludeEntry_init(struct ndn_ExcludeEntry *self, ndn_ExcludeType type, unsigned char *component, unsigned int componentLength)
+{
+ self->type = type;
+ self->component = component;
+ self->componentLength = componentLength;
+}
+
+/**
+ * An ndn_Exclude holds an array of ndn_ExcludeEntry.
+ */
+struct ndn_Exclude {
+ struct ndn_ExcludeEntry *entries; /**< pointer to the array of entries. */
+ unsigned int maxEntries; /**< the number of elements in the allocated entries array */
+ unsigned int nEntries; /**< the number of entries in the exclude, 0 for no exclude */
+};
+/**
+ * Initialize an ndn_Exclude struct with the entries array.
+ * @param self pointer to the ndn_Exclude struct
+ * @param entries the pre-allocated array of ndn_ExcludeEntry
+ * @param maxEntries the number of elements in the allocated entries array
+ */
+static inline void ndn_Exclude_init(struct ndn_Exclude *self, struct ndn_ExcludeEntry *entries, unsigned int maxEntries)
+{
+ self->entries = entries;
+ self->maxEntries = maxEntries;
+ self->nEntries = 0;
+}
+
enum {
ndn_Interest_CHILD_SELECTOR_LEFT = 0,
ndn_Interest_CHILD_SELECTOR_RIGHT = 1,
@@ -29,7 +78,7 @@
int maxSuffixComponents; /**< -1 for none */
unsigned char *publisherPublicKeyDigest; /**< pointer to pre-allocated buffer. 0 for none */
unsigned int publisherPublicKeyDigestLength; /**< length of publisherPublicKeyDigest. 0 for none */
- // TODO: implement exclude
+ struct ndn_Exclude exclude;
int childSelector; /**< -1 for none */
int answerOriginKind; /**< -1 for none */
int scope; /**< -1 for none */
@@ -38,14 +87,25 @@
unsigned int nonceLength; /**< length of nonce. 0 for none */
};
-static inline void ndn_Interest_init(struct ndn_Interest *self, struct ndn_NameComponent *nameComponents, unsigned int maxNameComponents)
+/**
+ * Initialize an ndn_Interest_init struct with the pre-allocated nameComponents and excludeEntries,
+ * and defaults for all the values.
+ * @param self pointer to the ndn_Interest struct
+ * @param nameComponents the pre-allocated array of ndn_NameComponent
+ * @param maxNameComponents the number of elements in the allocated nameComponents array
+ * @param excludeEntries the pre-allocated array of ndn_ExcludeEntry
+ * @param maxExcludeEntries the number of elements in the allocated excludeEntries array
+ */
+static inline void ndn_Interest_init
+ (struct ndn_Interest *self, struct ndn_NameComponent *nameComponents, unsigned int maxNameComponents,
+ struct ndn_ExcludeEntry *excludeEntries, unsigned int maxExcludeEntries)
{
ndn_Name_init(&self->name, nameComponents, maxNameComponents);
self->minSuffixComponents = -1;
self->maxSuffixComponents = -1;
self->publisherPublicKeyDigest = 0;
self->publisherPublicKeyDigestLength = 0;
- // TODO: implement exclude
+ ndn_Exclude_init(&self->exclude, excludeEntries, maxExcludeEntries);
self->childSelector = -1;
self->answerOriginKind = -1;
self->scope = -1;
diff --git a/ndn-cpp/c/encoding/BinaryXMLInterest.c b/ndn-cpp/c/encoding/BinaryXMLInterest.c
index 7939556..b9de3d6 100644
--- a/ndn-cpp/c/encoding/BinaryXMLInterest.c
+++ b/ndn-cpp/c/encoding/BinaryXMLInterest.c
@@ -9,6 +9,115 @@
#include "BinaryXMLName.h"
#include "BinaryXMLInterest.h"
+static ndn_Error encodeExclude(struct ndn_Exclude *exclude, struct ndn_BinaryXMLEncoder *encoder)
+{
+ if (exclude->nEntries == 0)
+ return;
+
+ ndn_Error error;
+ if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_Exclude))
+ return error;
+
+ // TODO: Do we want to order the components (except for ANY)?
+ unsigned int i;
+ for (i = 0; i < exclude->nEntries; ++i) {
+ struct ndn_ExcludeEntry *entry = &exclude->entries[i];
+
+ if (entry->type == ndn_Exclude_COMPONENT) {
+ if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement
+ (encoder, ndn_BinaryXML_DTag_Component, entry->component, entry->componentLength))
+ return error;
+ }
+ else if (entry->type == ndn_Exclude_ANY) {
+ if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_Any))
+ return error;
+ if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
+ return error;
+ }
+ else
+ return NDN_ERROR_unrecognized_ndn_ExcludeType;
+ }
+
+ if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
+ return error;
+
+ return 0;
+}
+
+static ndn_Error decodeExclude(struct ndn_Exclude *exclude, struct ndn_BinaryXMLDecoder *decoder)
+{
+ ndn_Error error;
+ if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_Exclude))
+ return error;
+
+ exclude->nEntries = 0;
+ while (1) {
+ int gotExpectedTag;
+
+ if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Component, &gotExpectedTag))
+ return error;
+ if (gotExpectedTag) {
+ // Component
+ unsigned char *component;
+ unsigned int componentLen;
+ if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement(decoder, ndn_BinaryXML_DTag_Component, 0, &component, &componentLen))
+ return error;
+
+ // Add the component entry.
+ if (exclude->nEntries >= exclude->maxEntries)
+ return NDN_ERROR_read_an_entry_past_the_maximum_number_of_entries_allowed_in_the_exclude;
+ ndn_ExcludeEntry_init(exclude->entries + exclude->nEntries, ndn_Exclude_COMPONENT, component, componentLen);
+ ++exclude->nEntries;
+
+ continue;
+ }
+
+ if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Any, &gotExpectedTag))
+ return error;
+ if (gotExpectedTag) {
+ // Any
+ if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_Any))
+ return error;
+ if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
+ return error;
+
+ // Add the any entry.
+ if (exclude->nEntries >= exclude->maxEntries)
+ return NDN_ERROR_read_an_entry_past_the_maximum_number_of_entries_allowed_in_the_exclude;
+ ndn_ExcludeEntry_init(exclude->entries + exclude->nEntries, ndn_Exclude_ANY, 0, 0);
+ ++exclude->nEntries;
+
+ continue;
+ }
+
+ if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Bloom, &gotExpectedTag))
+ return error;
+ if (gotExpectedTag) {
+ // Skip the Bloom and treat it as Any.
+ unsigned char *value;
+ unsigned int valueLen;
+ if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement(decoder, ndn_BinaryXML_DTag_Bloom, 0, &value, &valueLen))
+ return error;
+
+ // Add the any entry.
+ if (exclude->nEntries >= exclude->maxEntries)
+ return NDN_ERROR_read_an_entry_past_the_maximum_number_of_entries_allowed_in_the_exclude;
+ ndn_ExcludeEntry_init(exclude->entries + exclude->nEntries, ndn_Exclude_ANY, 0, 0);
+ ++exclude->nEntries;
+
+ continue;
+ }
+
+ // Else no more entries.
+ break;
+ }
+
+ if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
+ return error;
+
+ return 0;
+}
+
ndn_Error ndn_encodeBinaryXMLInterest(struct ndn_Interest *interest, struct ndn_BinaryXMLEncoder *encoder)
{
ndn_Error error;
@@ -35,7 +144,9 @@
return error;
}
- // TODO: Implement exclude
+ // This will check for no exclude.
+ if (error = encodeExclude(&interest->exclude, encoder))
+ return error;
if (interest->childSelector >= 0) {
if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement
@@ -107,7 +218,14 @@
interest->publisherPublicKeyDigestLength = 0;
}
- // TODO: Implement exclude
+ if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Exclude, &gotExpectedTag))
+ return error;
+ if (gotExpectedTag) {
+ if (error = decodeExclude(&interest->exclude, decoder))
+ return error;
+ }
+ else
+ interest->exclude.nEntries = 0;
if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
(decoder, ndn_BinaryXML_DTag_ChildSelector, &interest->childSelector))