model: Enabling serialization/deserialization of Exclude filter in ndnSIM wire format

Refs #1009 (http://redmine.named-data.net/issues/1009)
diff --git a/model/ndn-interest.cc b/model/ndn-interest.cc
index 5d76362..3b09fde 100644
--- a/model/ndn-interest.cc
+++ b/model/ndn-interest.cc
@@ -35,6 +35,7 @@
   , m_interestLifetime (Seconds (0))
   , m_nonce (0)
   , m_nackType (NORMAL_INTEREST)
+  , m_exclude (0)
   , m_payload (payload)
   , m_wire (0)
 {
@@ -50,6 +51,7 @@
   , m_interestLifetime (interest.m_interestLifetime)
   , m_nonce            (interest.m_nonce)
   , m_nackType         (interest.m_nackType)
+  , m_exclude          (interest.m_exclude ? Create<Exclude> (*interest.GetExclude ()) : 0)
   , m_payload          (interest.GetPayload ()->Copy ())
   , m_wire             (0)
 {
@@ -136,6 +138,19 @@
 }
 
 void
+Interest::SetExclude (Ptr<Exclude> exclude)
+{
+  m_exclude = exclude;
+  m_wire = 0;
+}
+
+Ptr<const Exclude>
+Interest::GetExclude () const
+{
+  return m_exclude;
+}
+
+void
 Interest::SetPayload (Ptr<Packet> payload)
 {
   m_payload = payload;
diff --git a/model/ndn-interest.h b/model/ndn-interest.h
index 16e8fc3..748e2d9 100644
--- a/model/ndn-interest.h
+++ b/model/ndn-interest.h
@@ -27,7 +27,8 @@
 #include "ns3/packet.h"
 #include "ns3/ptr.h"
 
-#include <ns3/ndn-name.h>
+#include <ns3/ndnSIM/ndn.cxx/name.h>
+#include <ns3/ndnSIM/ndn.cxx/exclude.h>
 
 namespace ns3 {
 
@@ -178,7 +179,21 @@
   GetNack () const;
 
   /**
-   * @brief Get virtual "payload" of interest packet
+   * @brief Set exclude filter of interest packet
+   *
+   * Empty or 0 means no exclude filter
+   */
+  void
+  SetExclude (Ptr<Exclude> exclude);
+
+  /**
+   * @brief Get exclude filter of interest packet
+   */
+  Ptr<const Exclude>
+  GetExclude () const;
+  
+  /**
+   * @brief Set virtual "payload" of interest packet
    *
    * This payload can carry packet tags
    */
@@ -186,7 +201,7 @@
   SetPayload (Ptr<Packet> payload);
 
   /**
-   * @brief Set virtual "payload" to interest packet
+   * @brief Get virtual "payload" to interest packet
    *
    * This payload can carry packet tags
    */
@@ -224,6 +239,8 @@
   Time  m_interestLifetime; ///< @brief InterestLifetime
   uint32_t m_nonce;         ///< @brief Nonce. not used if zero
   uint8_t  m_nackType;      ///< @brief Negative Acknowledgement type
+
+  Ptr<Exclude> m_exclude;   ///< @brief Exclude filter
   Ptr<Packet> m_payload;    ///< @brief virtual payload
 
   mutable Ptr<const Packet> m_wire;
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