interest: Convert Interest to TLV
Change-Id: I9b6ccd840289d7fd22fc6f652ac4bd7d0fbee63d
diff --git a/src/interest.cpp b/src/interest.cpp
index fe8faf3..2caffb1 100644
--- a/src/interest.cpp
+++ b/src/interest.cpp
@@ -8,139 +8,76 @@
#include <stdexcept>
#include <ndn-cpp/common.hpp>
#include <ndn-cpp/interest.hpp>
-#include "c/interest.h"
using namespace std;
namespace ndn {
-
-void
-Exclude::Entry::get(struct ndn_ExcludeEntry& excludeEntryStruct) const
+
+bool
+Interest::matchesName(const Name &name) const
{
- excludeEntryStruct.type = type_;
- if (type_ == ndn_Exclude_COMPONENT)
- component_.get(excludeEntryStruct.component);
+ if (!name_.isPrefixOf(name))
+ return false;
+
+ if (minSuffixComponents_ >= 0 &&
+ // Add 1 for the implicit digest.
+ !(name.size() + 1 - name_.size() >= minSuffixComponents_))
+ return false;
+
+ if (maxSuffixComponents_ >= 0 &&
+ // Add 1 for the implicit digest.
+ !(name.size() + 1 - name_.size() <= maxSuffixComponents_))
+ return false;
+
+ if (!exclude_.empty() && name.size() > name_.size() &&
+ exclude_.isExcluded(name[name_.size()]))
+ return false;
+
+ return true;
}
-void
-Exclude::get(struct ndn_Exclude& excludeStruct) const
+std::ostream &
+operator << (std::ostream &os, const Interest &interest)
{
- if (excludeStruct.maxEntries < entries_.size())
- throw runtime_error("excludeStruct.maxEntries must be >= this exclude getEntryCount()");
-
- excludeStruct.nEntries = entries_.size();
- for (size_t i = 0; i < excludeStruct.nEntries; ++i)
- entries_[i].get(excludeStruct.entries[i]);
-}
+ os << interest.getName();
-void
-Exclude::set(const struct ndn_Exclude& excludeStruct)
-{
- entries_.clear();
- for (size_t i = 0; i < excludeStruct.nEntries; ++i) {
- ndn_ExcludeEntry *entry = &excludeStruct.entries[i];
-
- if (entry->type == ndn_Exclude_COMPONENT)
- appendComponent(entry->component.value.value, entry->component.value.length);
- else if (entry->type == ndn_Exclude_ANY)
- appendAny();
- else
- throw runtime_error("unrecognized ndn_ExcludeType");
+ char delim = '?';
+
+ if (interest.getMinSuffixComponents() >= 0) {
+ os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents();
+ delim = '&';
}
-}
-
-string
-Exclude::toUri() const
-{
- if (entries_.size() == 0)
- return "";
-
- ostringstream result;
- for (unsigned i = 0; i < entries_.size(); ++i) {
- if (i > 0)
- result << ",";
-
- if (entries_[i].getType() == ndn_Exclude_ANY)
- result << "*";
- else
- Name::toEscapedString(*entries_[i].getComponent().getValue(), result);
+ if (interest.getMaxSuffixComponents() >= 0) {
+ os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents();
+ delim = '&';
}
-
- return result.str();
-}
-
-void
-Interest::set(const struct ndn_Interest& interestStruct)
-{
- name_.set(interestStruct.name);
- minSuffixComponents_ = interestStruct.minSuffixComponents;
- maxSuffixComponents_ = interestStruct.maxSuffixComponents;
-
- publisherPublicKeyDigest_.set(interestStruct.publisherPublicKeyDigest);
-
- exclude_.set(interestStruct.exclude);
- childSelector_ = interestStruct.childSelector;
- answerOriginKind_ = interestStruct.answerOriginKind;
- scope_ = interestStruct.scope;
- interestLifetimeMilliseconds_ = interestStruct.interestLifetimeMilliseconds;
- nonce_ = Blob(interestStruct.nonce);
-}
-
-void
-Interest::get(struct ndn_Interest& interestStruct) const
-{
- name_.get(interestStruct.name);
- interestStruct.minSuffixComponents = minSuffixComponents_;
- interestStruct.maxSuffixComponents = maxSuffixComponents_;
- publisherPublicKeyDigest_.get(interestStruct.publisherPublicKeyDigest);
- exclude_.get(interestStruct.exclude);
- interestStruct.childSelector = childSelector_;
- interestStruct.answerOriginKind = answerOriginKind_;
- interestStruct.scope = scope_;
- interestStruct.interestLifetimeMilliseconds = interestLifetimeMilliseconds_;
- nonce_.get(interestStruct.nonce);
-}
-
-string
-Interest::toUri() const
-{
- ostringstream selectors;
-
- if (minSuffixComponents_ >= 0)
- selectors << "&ndn.MinSuffixComponents=" << minSuffixComponents_;
- if (maxSuffixComponents_ >= 0)
- selectors << "&ndn.MaxSuffixComponents=" << maxSuffixComponents_;
- if (childSelector_ >= 0)
- selectors << "&ndn.ChildSelector=" << childSelector_;
- if (answerOriginKind_ >= 0)
- selectors << "&ndn.AnswerOriginKind=" << answerOriginKind_;
- if (scope_ >= 0)
- selectors << "&ndn.Scope=" << scope_;
- if (interestLifetimeMilliseconds_ >= 0)
- selectors << "&ndn.InterestLifetime=" << interestLifetimeMilliseconds_;
- if (publisherPublicKeyDigest_.getPublisherPublicKeyDigest().size() > 0) {
- selectors << "&ndn.PublisherPublicKeyDigest=";
- Name::toEscapedString(*publisherPublicKeyDigest_.getPublisherPublicKeyDigest(), selectors);
+ if (interest.getChildSelector() >= 0) {
+ os << delim << "ndn.ChildSelector=" << interest.getChildSelector();
+ delim = '&';
}
- if (nonce_.size() > 0) {
- selectors << "&ndn.Nonce=";
- Name::toEscapedString(*nonce_, selectors);
+ if (interest.getMustBeFresh()) {
+ os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh();
+ delim = '&';
}
- if (exclude_.size() > 0)
- selectors << "&ndn.Exclude=" << exclude_.toUri();
-
- ostringstream result;
-
- result << name_.toUri();
- string selectorsString(selectors.str());
- if (selectorsString.size() > 0) {
- // Replace the first & with ?.
- result << "?";
- result.write(&selectorsString[1], selectorsString.size() - 1);
+ if (interest.getScope() >= 0) {
+ os << delim << "ndn.Scope=" << interest.getScope();
+ delim = '&';
}
-
- return result.str();
+ if (interest.getInterestLifetime() >= 0) {
+ os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime();
+ delim = '&';
+ }
+
+ if (interest.getNonce() > 0) {
+ os << delim << "ndn.Nonce=" << interest.getNonce();
+ delim = '&';
+ }
+ if (!interest.getExclude().empty()) {
+ os << delim << "ndn.Exclude=" << interest.getExclude();
+ delim = '&';
+ }
+
+ return os;
}
}
-