exclude: Implementing TLV encoding/decoding
Change-Id: I8f624e8d2c519a088bf986864acb83cc79f5192e
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