model: Enabling serialization/deserialization of Exclude filter in ndnSIM wire format
Refs #1009 (http://redmine.named-data.net/issues/1009)
diff --git a/model/wire/ndnsim/wire-ndnsim.cc b/model/wire/ndnsim/wire-ndnsim.cc
index 66c2c44..013d78d 100644
--- a/model/wire/ndnsim/wire-ndnsim.cc
+++ b/model/wire/ndnsim/wire-ndnsim.cc
@@ -73,6 +73,103 @@
return name;
}
+
+size_t
+NdnSim::SerializeExclude (Buffer::Iterator &i, const Exclude &exclude)
+{
+ Buffer::Iterator start = i;
+
+ i.WriteU16 (static_cast<uint16_t> (SerializedSizeExclude (exclude)-2));
+
+ for (Exclude::const_reverse_iterator item = exclude.rbegin ();
+ item != exclude.rend ();
+ item++)
+ {
+ if (!item->first.empty ())
+ {
+ i.WriteU8 (ExcludeNameType);
+ i.WriteU16 (static_cast<uint16_t> (item->first.size ()));
+ i.Write (reinterpret_cast<const uint8_t*> (item->first.buf ()), item->first.size ());
+ }
+ if (item->second)
+ {
+ i.WriteU8 (ExcludeAnyType);
+ }
+ }
+ return i.GetDistanceFrom (start);
+}
+
+size_t
+NdnSim::SerializedSizeExclude (const Exclude &exclude)
+{
+ size_t excludeSerializedSize = 2;
+
+ for (Exclude::const_reverse_iterator item = exclude.rbegin ();
+ item != exclude.rend ();
+ item++)
+ {
+ if (!item->first.empty ())
+ {
+ excludeSerializedSize += 1 + 2 + item->first.size ();
+ }
+ if (item->second)
+ {
+ excludeSerializedSize += 1;
+ }
+ }
+
+ return excludeSerializedSize;
+}
+
+Ptr<Exclude>
+NdnSim::DeserializeExclude (Buffer::Iterator &i)
+{
+ Ptr<Exclude> exclude = Create<Exclude> ();
+
+ uint16_t excludeLength = i.ReadU16 ();
+ while (excludeLength > 0)
+ {
+ uint8_t type = i.ReadU8 ();
+ excludeLength --;
+
+ if (type == ExcludeAnyType)
+ {
+ exclude->appendExclude (name::Component (), true);
+ }
+ else if (type == ExcludeNameType)
+ {
+ uint16_t length = i.ReadU16 ();
+ excludeLength = excludeLength - 2 - length;
+
+ uint8_t tmp[length];
+ i.Read (tmp, length);
+
+ bool any = false;
+ if (excludeLength > 0)
+ {
+ uint8_t type = i.ReadU8 ();
+ if (type != ExcludeAnyType)
+ {
+ i.Prev ();
+ }
+ else
+ {
+ any = true;
+ excludeLength --;
+ }
+ }
+
+ exclude->appendExclude (name::Component (tmp, length), any);
+ }
+ else
+ {
+ NS_FATAL_ERROR ("Incorrect format of Exclude filter");
+ }
+ }
+ return exclude;
+}
+
+
} // wire
NDN_NAMESPACE_END
diff --git a/model/wire/ndnsim/wire-ndnsim.h b/model/wire/ndnsim/wire-ndnsim.h
index 6e4b1e5..4ddfb3e 100644
--- a/model/wire/ndnsim/wire-ndnsim.h
+++ b/model/wire/ndnsim/wire-ndnsim.h
@@ -16,7 +16,8 @@
#include "ns3/buffer.h"
#include "ns3/ndn-common.h"
-#include "ns3/ndn-name.h"
+#include "ns3/ndnSIM/ndn.cxx/name.h"
+#include "ns3/ndnSIM/ndn.cxx/exclude.h"
NDN_NAMESPACE_BEGIN
@@ -53,6 +54,42 @@
*/
static Ptr<Name>
DeserializeName (Buffer::Iterator &start);
+
+
+ enum Selectors {
+ SelectorExclude = 0x01
+ };
+
+ enum ExcludeTypes {
+ ExcludeNameType = 0x01,
+ ExcludeAnyType = 0x02
+ };
+
+ /**
+ * @brief Append Exclude in ndnSIM encoding
+ * @param start Buffer to store serialized Interest
+ * @param exclude constant reference to Exclude object
+ *
+ * @returns written length
+ */
+ static size_t
+ SerializeExclude (Buffer::Iterator &start, const Exclude &exclude);
+
+ /**
+ * @brief Estimate size of Exclude in ndnSIM encoding
+ * @param exclude constant reference to Exclude object
+ * @returns estimated length
+ */
+ static size_t
+ SerializedSizeExclude (const Exclude &exclude);
+
+ /**
+ * @brief Deserialize Exclude from ndnSIM encodeing
+ * @param start Buffer that stores serialized Interest
+ * @param exclude Exclude object
+ */
+ static Ptr<Exclude>
+ DeserializeExclude (Buffer::Iterator &start);
}; // NdnSim
} // wire