binary-xml-wire-format: Partial enabling of binary-xml-wire-format

Due to significant changes, Data abstraction conversion (from and to
wire format) is currently disabled, using empty placeholders instead.

Other data structures should have working conversion (minus
PublisherPublicKeyDigest, which is not part of Interest and
ForwardingEntry data structure anymore).

Change-Id: I51c47495808c35b500fc6b1fcc0c46db217e6598
diff --git a/src/encoding/binary-xml-wire-format.cpp b/src/encoding/binary-xml-wire-format.cpp
index e463e91..73d8fd6 100644
--- a/src/encoding/binary-xml-wire-format.cpp
+++ b/src/encoding/binary-xml-wire-format.cpp
@@ -20,6 +20,204 @@
 
 namespace ndn {
 
+namespace c {
+
+class Exclude : public ndn::Exclude
+{
+public:
+  void
+  get(struct ndn_Exclude& excludeStruct) const
+  {
+    if (excludeStruct.maxEntries < size())
+      throw runtime_error("excludeStruct.maxEntries must be >= this exclude getEntryCount()");
+
+    int entries = 0;
+    for (Exclude::const_reverse_iterator i = rbegin (); i != rend (); i++)
+      {
+        if (!i->first.empty())
+          {
+            excludeStruct.entries[entries].type = ndn_Exclude_COMPONENT;
+            excludeStruct.entries[entries].component.value.value = const_cast<uint8_t*>(i->first.getValue().buf());
+            excludeStruct.entries[entries].component.value.length = i->first.getValue().size();
+            ++entries;
+          }
+        if (i->second)
+          {
+            excludeStruct.entries[entries].type = ndn_Exclude_ANY;
+            ++entries;
+          }
+      }
+
+    excludeStruct.nEntries = entries;
+  }
+
+  void
+  set(const struct ndn_Exclude& excludeStruct)
+  {
+    clear();
+
+    if (excludeStruct.nEntries == 0)
+      return;
+
+    int i = 0;
+    if (excludeStruct.entries[i].type == ndn_Exclude_ANY) {
+      appendExclude("/", true);
+      i++;
+    }
+
+    while (i < excludeStruct.nEntries) {
+      ndn_ExcludeEntry *entry = &excludeStruct.entries[i];
+
+      if (entry->type != ndn_Exclude_COMPONENT)
+        throw runtime_error("unrecognized ndn_ExcludeType");
+
+      Name::Component excludedComponent (entry->component.value.value, entry->component.value.length);
+      ++i;
+      entry = &excludeStruct.entries[i];
+
+      if (i < excludeStruct.nEntries) {
+        if (entry->type == ndn_Exclude_ANY) {
+          appendExclude(excludedComponent, true);
+          ++i;
+        }
+        else
+          appendExclude(excludedComponent, false);
+      }
+      else
+        appendExclude(excludedComponent, false);
+    }
+  }
+};
+
+class Name : public ndn::Name
+{
+public:
+  void 
+  get(struct ndn_Name& nameStruct) const
+  {
+    if (nameStruct.maxComponents < size())
+      throw runtime_error("nameStruct.maxComponents must be >= this name getNComponents()");
+  
+    nameStruct.nComponents = size();
+    for (size_t i = 0; i < nameStruct.nComponents; ++i) {
+      nameStruct.components[i].value.value = const_cast<uint8_t*>(ndn::Name::get(i).getValue().buf());
+      nameStruct.components[i].value.length = ndn::Name::get(i).getValue().size();
+    }
+  }
+  
+  void 
+  set(const struct ndn_Name& nameStruct) 
+  {
+    clear();
+    for (size_t i = 0; i < nameStruct.nComponents; ++i)
+      append(nameStruct.components[i].value.value, nameStruct.components[i].value.length);  
+  }
+};
+
+class Interest : public ndn::Interest
+{
+public:
+  void 
+  get(struct ndn_Interest& interestStruct) const 
+  {
+    reinterpret_cast<const c::Name&>(getName()).get(interestStruct.name);
+    interestStruct.minSuffixComponents = getMinSuffixComponents();
+    interestStruct.maxSuffixComponents = getMaxSuffixComponents();
+    // publisherPublicKeyDigest_.get(interestStruct.publisherPublicKeyDigest);
+    reinterpret_cast<const c::Exclude&>(getExclude()).get(interestStruct.exclude);
+    interestStruct.childSelector = getChildSelector();
+    interestStruct.answerOriginKind = getMustBeFresh() ?
+      (ndn_Interest_ANSWER_CONTENT_STORE | ndn_Interest_ANSWER_GENERATED)
+      :
+      (ndn_Interest_ANSWER_CONTENT_STORE | ndn_Interest_ANSWER_GENERATED | ndn_Interest_ANSWER_STALE);
+    interestStruct.scope = getScope();
+    interestStruct.interestLifetimeMilliseconds = getInterestLifetime();
+
+    interestStruct.nonce.length = 4;
+    interestStruct.nonce.value = const_cast<uint8_t*>(reinterpret_cast<const uint8_t *>(getNonce()));
+  }  
+
+  void 
+  set(const struct ndn_Interest& interestStruct) 
+  {
+    reinterpret_cast<c::Name&>(getName()).set(interestStruct.name);
+    setMinSuffixComponents(interestStruct.minSuffixComponents);
+    setMaxSuffixComponents(interestStruct.maxSuffixComponents);
+  
+    // publisherPublicKeyDigest_.set(interestStruct.publisherPublicKeyDigest);
+  
+    reinterpret_cast<c::Exclude&>(getExclude()).set(interestStruct.exclude);
+    setChildSelector(interestStruct.childSelector);
+    // answerOriginKind_ = interestStruct.answerOriginKind;
+    setScope(interestStruct.scope);
+    setInterestLifetime(interestStruct.interestLifetimeMilliseconds);
+
+    setNonce(*reinterpret_cast<uint32_t*>(interestStruct.nonce.value));
+  }
+};
+
+class Data : public ndn::Data
+{
+public:
+  void 
+  get(struct ndn_Data& dataStruct) const 
+  {
+    // signature_->get(dataStruct.signature);
+    // name_.get(dataStruct.name);
+    // metaInfo_.get(dataStruct.metaInfo);
+    // content_.get(dataStruct.content);
+  }
+
+  void 
+  set(const struct ndn_Data& dataStruct)
+  {
+    // signature_->set(dataStruct.signature);
+    // name_.set(dataStruct.name);
+    // metaInfo_.set(dataStruct.metaInfo);
+    // content_ = Blob(dataStruct.content);
+  }
+};
+
+class ForwardingEntry : public ndn::ForwardingEntry
+{
+public:
+  void 
+  get(struct ndn_ForwardingEntry& forwardingEntryStruct) const 
+  {
+    reinterpret_cast<const c::Name&>(getPrefix()).get(forwardingEntryStruct.prefix);
+    // publisherPublicKeyDigest_.get(forwardingEntryStruct.publisherPublicKeyDigest);
+    forwardingEntryStruct.faceId = getFaceId();
+    // forwardingEntryStruct.forwardingFlags = getForwardingFlags();
+    forwardingEntryStruct.freshnessSeconds = getFreshnessPeriod() / 1000;
+
+    forwardingEntryStruct.action.length = getAction().size();
+    if (getAction().size() > 0)
+      forwardingEntryStruct.action.value = (uint8_t *)&getAction();
+    else
+      forwardingEntryStruct.action.value = 0;
+  }
+
+  void 
+  set(const struct ndn_ForwardingEntry& forwardingEntryStruct) 
+  {
+    if (forwardingEntryStruct.action.value && forwardingEntryStruct.action.length > 0)
+      setAction(string(forwardingEntryStruct.action.value, forwardingEntryStruct.action.value + forwardingEntryStruct.action.length));
+    else
+      setAction("");
+
+    Name prefix;
+    reinterpret_cast<c::Name&>(prefix).set(forwardingEntryStruct.prefix);
+    setPrefix(prefix);
+    // publisherPublicKeyDigest_.set(forwardingEntryStruct.publisherPublicKeyDigest);  
+    setFaceId(forwardingEntryStruct.faceId);
+    // setForwardingFlags(forwardingEntryStruct.forwardingFlags);
+    setFreshnessPeriod(forwardingEntryStruct.freshnessSeconds * 1000);
+  }
+};
+
+}
+
+
 // This is declared in the WireFormat class.
 WireFormat*
 WireFormat::newInitialDefaultWireFormat() 
@@ -36,7 +234,7 @@
   ndn_Interest_initialize
     (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]), 
      excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0]));
-  interest.get(interestStruct);
+  reinterpret_cast<const c::Interest&>(interest).get(interestStruct);
 
   BinaryXmlEncoder encoder;
   ndn_Error error;
@@ -61,7 +259,7 @@
   if ((error = ndn_decodeBinaryXmlInterest(&interestStruct, &decoder)))
     throw runtime_error(ndn_getErrorString(error));
 
-  interest.set(interestStruct);
+  reinterpret_cast<c::Interest&>(interest).set(interestStruct);
 }
 
 Blob 
@@ -73,7 +271,7 @@
   ndn_Data_initialize
     (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]), 
      keyNameComponents, sizeof(keyNameComponents) / sizeof(keyNameComponents[0]));
-  data.get(dataStruct);
+  reinterpret_cast<const c::Data&>(data).get(dataStruct);
 
   BinaryXmlEncoder encoder(1500);
   ndn_Error error;
@@ -99,7 +297,7 @@
   if ((error = ndn_decodeBinaryXmlData(&dataStruct, signedPortionBeginOffset, signedPortionEndOffset, &decoder)))
     throw runtime_error(ndn_getErrorString(error));
 
-  data.set(dataStruct);
+  reinterpret_cast<c::Data&>(data).set(dataStruct);
 }
 
 Blob 
@@ -109,7 +307,7 @@
   struct ndn_ForwardingEntry forwardingEntryStruct;
   ndn_ForwardingEntry_initialize
     (&forwardingEntryStruct, prefixNameComponents, sizeof(prefixNameComponents) / sizeof(prefixNameComponents[0]));
-  forwardingEntry.get(forwardingEntryStruct);
+  reinterpret_cast<const c::ForwardingEntry&>(forwardingEntry).get(forwardingEntryStruct);
 
   BinaryXmlEncoder encoder;
   ndn_Error error;
@@ -132,7 +330,7 @@
   if ((error = ndn_decodeBinaryXmlForwardingEntry(&forwardingEntryStruct, &decoder)))
     throw runtime_error(ndn_getErrorString(error));
 
-  forwardingEntry.set(forwardingEntryStruct);
+  reinterpret_cast<c::ForwardingEntry&>(forwardingEntry).set(forwardingEntryStruct);
 }
 
 }