model: PacketHeader<T> and interface with NS-3

This commit creates a compatible interface to convert ndn-cxx Interest
and Data structures into NS-3's Ptr<Packet> and back.

Refs: #2048, #2230
diff --git a/model/ndn-header.cpp b/model/ndn-header.cpp
new file mode 100644
index 0000000..89c9251
--- /dev/null
+++ b/model/ndn-header.cpp
@@ -0,0 +1,158 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2011-2014  Regents of the University of California.
+ *
+ * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
+ * contributors.
+ *
+ * ndnSIM is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndnSIM, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+#include "ndn-header.hpp"
+
+#include <iosfwd>
+#include <boost/iostreams/concepts.hpp>
+#include <boost/iostreams/stream.hpp>
+
+namespace io = boost::iostreams;
+
+namespace ns3 {
+namespace ndn {
+
+template<>
+ns3::TypeId
+PacketHeader<Interest>::GetTypeId()
+{
+  static ns3::TypeId tid =
+    ns3::TypeId("ns3::ndn::Interest")
+    .SetGroupName("Ndn")
+    .SetParent<Header>()
+    .AddConstructor<PacketHeader<Interest>>()
+    ;
+
+  return tid;
+}
+
+template<>
+ns3::TypeId
+PacketHeader<Data>::GetTypeId()
+{
+  static ns3::TypeId tid =
+    ns3::TypeId("ns3::ndn::Data")
+    .SetGroupName("Ndn")
+    .SetParent<Header>()
+    .AddConstructor<PacketHeader<Data>>()
+    ;
+  return tid;
+}
+
+template<class Pkt>
+TypeId
+PacketHeader<Pkt>::GetInstanceTypeId(void) const
+{
+  return GetTypeId();
+}
+
+template<class Pkt>
+PacketHeader<Pkt>::PacketHeader()
+{
+}
+
+template<class Pkt>
+PacketHeader<Pkt>::PacketHeader(const Pkt& packet)
+  : m_packet(packet.shared_from_this())
+{
+}
+
+template<class Pkt>
+uint32_t
+PacketHeader<Pkt>::GetSerializedSize(void) const
+{
+  return m_packet->wireEncode().size();
+}
+
+template<class Pkt>
+void
+PacketHeader<Pkt>::Serialize(Buffer::Iterator start) const
+{
+  start.Write(m_packet->wireEncode().wire(), m_packet->wireEncode().size());
+}
+
+class Ns3BufferIteratorSource : public io::source {
+public:
+  Ns3BufferIteratorSource(Buffer::Iterator& is)
+    : m_is(is)
+  {
+  }
+
+  std::streamsize
+  read(char* buf, std::streamsize nMaxRead)
+  {
+    std::streamsize i = 0;
+    for (; i < nMaxRead && !m_is.IsEnd(); ++i) {
+      buf[i] = m_is.ReadU8();
+    }
+    if (i == 0) {
+      return -1;
+    }
+    else {
+      return i;
+    }
+  }
+
+private:
+  Buffer::Iterator& m_is;
+};
+
+template<class Pkt>
+uint32_t
+PacketHeader<Pkt>::Deserialize(Buffer::Iterator start)
+{
+  auto packet = make_shared<Pkt>();
+  io::stream<Ns3BufferIteratorSource> is(start);
+  packet->wireDecode(::ndn::Block::fromStream(is));
+  m_packet = packet;
+  return packet->wireEncode().size();
+}
+
+template<>
+void
+PacketHeader<Interest>::Print(std::ostream& os) const
+{
+  os << "I: " << *m_packet;
+}
+
+template<>
+void
+PacketHeader<Data>::Print(std::ostream& os) const
+{
+  os << "D: " << *m_packet;
+}
+
+template<class Pkt>
+shared_ptr<const Pkt>
+PacketHeader<Pkt>::getPacket()
+{
+  return m_packet;
+}
+
+typedef PacketHeader<Interest> InterestHeader;
+typedef PacketHeader<Data> DataHeader;
+
+NS_OBJECT_ENSURE_REGISTERED(InterestHeader);
+NS_OBJECT_ENSURE_REGISTERED(DataHeader);
+
+template class PacketHeader<Interest>;
+template class PacketHeader<Data>;
+
+} // namespace ndn
+} // namespace ns3
diff --git a/model/ndn-header.hpp b/model/ndn-header.hpp
new file mode 100644
index 0000000..86395d2
--- /dev/null
+++ b/model/ndn-header.hpp
@@ -0,0 +1,65 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2011-2014  Regents of the University of California.
+ *
+ * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
+ * contributors.
+ *
+ * ndnSIM is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndnSIM, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+#ifndef NDNSIM_NDN_HEADER_HPP
+#define NDNSIM_NDN_HEADER_HPP
+
+#include "ns3/header.h"
+
+#include "ndn-common.hpp"
+
+namespace ns3 {
+namespace ndn {
+
+template<class Pkt>
+class PacketHeader : public Header {
+public:
+  static ns3::TypeId
+  GetTypeId();
+
+  virtual TypeId
+  GetInstanceTypeId(void) const;
+
+  PacketHeader();
+
+  PacketHeader(const Pkt& packet);
+
+  virtual uint32_t
+  GetSerializedSize(void) const;
+
+  virtual void
+  Serialize(ns3::Buffer::Iterator start) const;
+
+  virtual uint32_t
+  Deserialize(ns3::Buffer::Iterator start);
+
+  virtual void
+  Print(std::ostream& os) const;
+
+  shared_ptr<const Pkt>
+  getPacket();
+
+private:
+  shared_ptr<const Pkt> m_packet;
+};
+
+} // namespace ndn
+} // namespace ns3
+
+#endif // NDNSIM_NDN_HEADER_HPP
diff --git a/model/ndn-ns3.cpp b/model/ndn-ns3.cpp
new file mode 100644
index 0000000..c7d6c29
--- /dev/null
+++ b/model/ndn-ns3.cpp
@@ -0,0 +1,95 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2011-2014  Regents of the University of California.
+ *
+ * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
+ * contributors.
+ *
+ * ndnSIM is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndnSIM, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+#include "ndn-ns3.hpp"
+
+#include <ndn-cxx/encoding/block.hpp>
+#include <ndn-cxx/interest.hpp>
+#include <ndn-cxx/data.hpp>
+
+#include "ndn-header.hpp"
+#include "../utils/ndn-ns3-packet-tag.hpp"
+
+namespace ns3 {
+namespace ndn {
+
+template<class T>
+std::shared_ptr<const T>
+Convert::FromPacket(Ptr<Packet> packet)
+{
+  PacketHeader<T> header;
+  packet->RemoveHeader(header);
+
+  auto pkt = header.getPacket();
+  pkt->setTag(make_shared<Ns3PacketTag>(packet));
+
+  return pkt;
+}
+
+template std::shared_ptr<const Interest>
+Convert::FromPacket<Interest>(Ptr<Packet> packet);
+
+template std::shared_ptr<const Data>
+Convert::FromPacket<Data>(Ptr<Packet> packet);
+
+template<class T>
+Ptr<Packet>
+Convert::ToPacket(const T& pkt)
+{
+  PacketHeader<T> header(pkt);
+
+  Ptr<Packet> packet;
+
+  auto tag = pkt.template getTag<Ns3PacketTag>();
+  if (tag != nullptr) {
+    packet = tag->getPacket()->Copy();
+  }
+  else {
+    packet = Create<Packet>();
+  }
+
+  packet->AddHeader(header);
+  return packet;
+}
+
+template Ptr<Packet>
+Convert::ToPacket<Interest>(const Interest& packet);
+
+template Ptr<Packet>
+Convert::ToPacket<Data>(const Data& packet);
+
+uint32_t
+Convert::getPacketType(Ptr<const Packet> packet)
+{
+  uint8_t type;
+  uint32_t nRead = packet->CopyData(&type, 1);
+  if (nRead != 1) {
+    throw ::ndn::tlv::Error("Unknown header");
+  }
+
+  if (type == ::ndn::tlv::Interest || type == ::ndn::tlv::Data) {
+    return type;
+  }
+  else {
+    throw ::ndn::tlv::Error("Unknown header");
+  }
+}
+
+} // namespace ndn
+} // namespace ns3
diff --git a/model/ndn-ns3.hpp b/model/ndn-ns3.hpp
new file mode 100644
index 0000000..1f8172f
--- /dev/null
+++ b/model/ndn-ns3.hpp
@@ -0,0 +1,46 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2011-2014  Regents of the University of California.
+ *
+ * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
+ * contributors.
+ *
+ * ndnSIM is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndnSIM, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+#ifndef NDN_NS3_HPP
+#define NDN_NS3_HPP
+
+#include "ns3/packet.h"
+#include "ns3/ptr.h"
+
+namespace ns3 {
+namespace ndn {
+
+class Convert {
+public:
+  template<class T>
+  static std::shared_ptr<const T>
+  FromPacket(Ptr<Packet> packet);
+
+  template<class T>
+  static Ptr<Packet>
+  ToPacket(const T& pkt);
+
+  static uint32_t
+  getPacketType(Ptr<const Packet> packet);
+};
+
+} // namespace ndn
+} // namespace ns3
+
+#endif
diff --git a/utils/ndn-ns3-packet-tag.hpp b/utils/ndn-ns3-packet-tag.hpp
new file mode 100644
index 0000000..af9213f
--- /dev/null
+++ b/utils/ndn-ns3-packet-tag.hpp
@@ -0,0 +1,56 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2011-2014  Regents of the University of California.
+ *
+ * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
+ * contributors.
+ *
+ * ndnSIM is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndnSIM, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+#ifndef NDN_NS3_PACKET_TAG_HPP
+#define NDN_NS3_PACKET_TAG_HPP
+
+#include "ns3/packet.h"
+#include "ns3/ptr.h"
+#include <ndn-cxx/tag.hpp>
+
+namespace ns3 {
+namespace ndn {
+
+class Ns3PacketTag : public ::ndn::Tag {
+public:
+  static size_t
+  getTypeId()
+  {
+    return 0xaee87802; // md5("Ns3PacketTag")[0:8]
+  }
+
+  Ns3PacketTag(Ptr<const Packet> packet)
+    : m_packet(packet)
+  {
+  }
+
+  Ptr<const Packet>
+  getPacket() const
+  {
+    return m_packet;
+  }
+
+private:
+  Ptr<const Packet> m_packet;
+};
+
+} // namespace ndn
+} // namespace ns3
+
+#endif // NDN_NS3_PACKET_TAG_HPP