exclude: Implementing TLV encoding/decoding
Change-Id: I8f624e8d2c519a088bf986864acb83cc79f5192e
diff --git a/include/ndn-cpp/exclude.hpp b/include/ndn-cpp/exclude.hpp
index 8fee29e..276aa5b 100644
--- a/include/ndn-cpp/exclude.hpp
+++ b/include/ndn-cpp/exclude.hpp
@@ -17,16 +17,15 @@
namespace ndn {
-namespace error {
-struct Exclude : public std::runtime_error { Exclude(const std::string &what) : std::runtime_error(what) {} };
-}
-
/**
* @brief Class to represent Exclude component in NDN interests
*/
class Exclude
{
public:
+ struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
+
+
typedef std::map< Name::Component, bool /*any*/, std::greater<Name::Component> > exclude_type;
typedef exclude_type::iterator iterator;
@@ -88,7 +87,7 @@
*
* If there is an error with ranges (e.g., order of components is wrong) an exception is thrown
*/
- void
+ inline void
appendExclude (const Name::Component &name, bool any);
/**
@@ -132,6 +131,20 @@
*/
inline std::string
toUri () const;
+
+ /**
+ * Encode this Interest for a particular wire format.
+ * @return The encoded byte array.
+ */
+ const Block&
+ wireEncode() const;
+
+ /**
+ * Decode the input using a particular wire format and update this Interest.
+ * @param input The input byte array to be decoded.
+ */
+ void
+ wireDecode(const Block &wire);
private:
Exclude &
@@ -139,6 +152,8 @@
private:
exclude_type m_exclude;
+
+ mutable Block wire_;
};
std::ostream&
@@ -150,6 +165,12 @@
return excludeRange (Name::Component (), to);
}
+inline void
+Exclude::appendExclude (const Name::Component &name, bool any)
+{
+ m_exclude[name] = any;
+}
+
inline bool
Exclude::empty () const
{
diff --git a/src/exclude.cpp b/src/exclude.cpp
index 4d65b6c..446db79 100644
--- a/src/exclude.cpp
+++ b/src/exclude.cpp
@@ -87,10 +87,10 @@
{
if (from >= to)
{
- throw error::Exclude ("Invalid exclude range [" +
- from.toEscapedString() + ", " +
- to.toEscapedString() +
- "] (for single name exclude use Exclude::excludeOne)");
+ throw Error ("Invalid exclude range [" +
+ from.toEscapedString() + ", " +
+ to.toEscapedString() +
+ "] (for single name exclude use Exclude::excludeOne)");
}
iterator newFrom = m_exclude.lower_bound (from);
@@ -172,5 +172,78 @@
return os;
}
+const Block&
+Exclude::wireEncode() const
+{
+ if (wire_.hasWire())
+ return wire_;
+
+ wire_ = Block(Tlv::Exclude);
+
+ for (Exclude::const_reverse_iterator i = m_exclude.rbegin (); i != m_exclude.rend (); i++)
+ {
+ if (!i->first.empty())
+ {
+ OBufferStream os;
+ Tlv::writeVarNumber(os, Tlv::NameComponent);
+ Tlv::writeVarNumber(os, i->first.getValue().size());
+ os.write(reinterpret_cast<const char *>(i->first.getValue().buf()), i->first.getValue().size());
+
+ wire_.push_back(Block(os.buf()));
+
+ }
+ if (i->second)
+ {
+ OBufferStream os;
+ Tlv::writeVarNumber(os, Tlv::Any);
+ Tlv::writeVarNumber(os, 0);
+ wire_.push_back(Block(os.buf()));
+ }
+ }
+
+ wire_.encode();
+ return wire_;
+}
+
+void
+Exclude::wireDecode(const Block &wire)
+{
+ wire_ = wire;
+ wire_.parse();
+
+ Block::element_const_iterator i = wire_.getAll().begin();
+ if (i->type() == Tlv::Any)
+ {
+ appendExclude("/", true);
+ ++i;
+ }
+
+ while (i != wire_.getAll().end())
+ {
+ if (i->type() != Tlv::NameComponent)
+ throw Error("Incorrect format of Exclude filter");
+
+ Name::Component excludedComponent (i->value(), i->value_size());
+ ++i;
+
+ if (i != wire_.getAll().end())
+ {
+ if (i->type() == Tlv::Any)
+ {
+ appendExclude(excludedComponent, true);
+ ++i;
+ }
+ else
+ {
+ appendExclude(excludedComponent, false);
+ }
+ }
+ else
+ {
+ appendExclude(excludedComponent, false);
+ }
+ }
+}
+
} // ndn