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.cc b/model/wire/ndnsim.cc
index 75aaf1e..1c2d778 100644
--- a/model/wire/ndnsim.cc
+++ b/model/wire/ndnsim.cc
@@ -104,7 +104,11 @@
1/*version*/ + 1 /*type*/ + 2/*length*/ +
(4/*nonce*/ + 1/*scope*/ + 1/*nack type*/ + 2/*timestamp*/ +
NdnSim::SerializedSizeName (m_interest->GetName ()) +
- (2 + 0)/* selectors */ +
+
+ (2 +
+ (m_interest->GetExclude () == 0 ? 0 : (1 + NdnSim::SerializedSizeExclude (*m_interest->GetExclude ())))
+ )/* selectors */ +
+
(2 + 0)/* options */);
NS_LOG_INFO ("Serialize size = " << size);
@@ -130,8 +134,18 @@
start.WriteU16 (static_cast<uint16_t> (m_interest->GetInterestLifetime ().ToInteger (Time::S)));
NdnSim::SerializeName (start, m_interest->GetName ());
+
+ if (m_interest->GetExclude () == 0)
+ {
+ start.WriteU16 (0); // no selectors
+ }
+ else
+ {
+ start.WriteU16 (1 + NdnSim::SerializedSizeExclude (*m_interest->GetExclude ()));
+ start.WriteU8 (0x01);
+ NdnSim::SerializeExclude (start, *m_interest->GetExclude ());
+ }
- start.WriteU16 (0); // no selectors
start.WriteU16 (0); // no options
}
@@ -156,7 +170,14 @@
m_interest->SetName (NdnSim::DeserializeName (i));
- i.ReadU16 ();
+ uint32_t selectorsLen = i.ReadU16 ();
+ if (selectorsLen > 0)
+ {
+ if (i.ReadU8 () != 0x01) // exclude filter only
+ throw InterestException ();
+
+ m_interest->SetExclude (NdnSim::DeserializeExclude (i));
+ }
i.ReadU16 ();
NS_ASSERT (GetSerializedSize () == (i.GetDistanceFrom (start)));
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