NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
ndn-header.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
20 #include "ndn-header.hpp"
21 
22 #include <iosfwd>
23 #include <boost/iostreams/concepts.hpp>
24 #include <boost/iostreams/stream.hpp>
25 
26 namespace io = boost::iostreams;
27 
28 namespace ns3 {
29 namespace ndn {
30 
31 template<>
32 ns3::TypeId
34 {
35  static ns3::TypeId tid =
36  ns3::TypeId("ns3::ndn::Interest")
37  .SetGroupName("Ndn")
38  .SetParent<Header>()
39  .AddConstructor<PacketHeader<Interest>>()
40  ;
41 
42  return tid;
43 }
44 
45 template<>
46 ns3::TypeId
48 {
49  static ns3::TypeId tid =
50  ns3::TypeId("ns3::ndn::Data")
51  .SetGroupName("Ndn")
52  .SetParent<Header>()
53  .AddConstructor<PacketHeader<Data>>()
54  ;
55  return tid;
56 }
57 
58 template<class Pkt>
59 TypeId
61 {
62  return GetTypeId();
63 }
64 
65 template<class Pkt>
67 {
68 }
69 
70 template<class Pkt>
72  : m_packet(packet.shared_from_this())
73 {
74 }
75 
76 template<class Pkt>
77 uint32_t
79 {
80  return m_packet->wireEncode().size();
81 }
82 
83 template<class Pkt>
84 void
85 PacketHeader<Pkt>::Serialize(ns3::Buffer::Iterator start) const
86 {
87  start.Write(m_packet->wireEncode().wire(), m_packet->wireEncode().size());
88 }
89 
90 class Ns3BufferIteratorSource : public io::source {
91 public:
92  Ns3BufferIteratorSource(ns3::Buffer::Iterator& is)
93  : m_is(is)
94  {
95  }
96 
97  std::streamsize
98  read(char* buf, std::streamsize nMaxRead)
99  {
100  std::streamsize i = 0;
101  for (; i < nMaxRead && !m_is.IsEnd(); ++i) {
102  buf[i] = m_is.ReadU8();
103  }
104  if (i == 0) {
105  return -1;
106  }
107  else {
108  return i;
109  }
110  }
111 
112 private:
113  ns3::Buffer::Iterator& m_is;
114 };
115 
116 template<class Pkt>
117 uint32_t
118 PacketHeader<Pkt>::Deserialize(ns3::Buffer::Iterator start)
119 {
120  auto packet = make_shared<Pkt>();
121  io::stream<Ns3BufferIteratorSource> is(start);
122  packet->wireDecode(::ndn::Block::fromStream(is));
123  m_packet = packet;
124  return packet->wireEncode().size();
125 }
126 
127 template<>
128 void
129 PacketHeader<Interest>::Print(std::ostream& os) const
130 {
131  os << "I: " << *m_packet;
132 }
133 
134 template<>
135 void
136 PacketHeader<Data>::Print(std::ostream& os) const
137 {
138  os << "D: " << *m_packet;
139 }
140 
141 template<class Pkt>
142 shared_ptr<const Pkt>
144 {
145  return m_packet;
146 }
147 
150 
151 NS_OBJECT_ENSURE_REGISTERED(InterestHeader);
152 NS_OBJECT_ENSURE_REGISTERED(DataHeader);
153 
154 template class PacketHeader<Interest>;
155 template class PacketHeader<Data>;
156 
157 } // namespace ndn
158 } // namespace ns3
static Block fromStream(std::istream &is)
Create a Block from an input stream.
Definition: block.cpp:223
Copyright (c) 2011-2015 Regents of the University of California.
virtual uint32_t Deserialize(ns3::Buffer::Iterator start)
Definition: ndn-header.cpp:118
static ns3::TypeId GetTypeId()
shared_ptr< const Pkt > getPacket()
Definition: ndn-header.cpp:143
NS_OBJECT_ENSURE_REGISTERED(ContentStore)
virtual TypeId GetInstanceTypeId(void) const
Definition: ndn-header.cpp:60
Ns3BufferIteratorSource(ns3::Buffer::Iterator &is)
Definition: ndn-header.cpp:92
PacketHeader< Data > DataHeader
Definition: ndn-header.cpp:149
virtual void Print(std::ostream &os) const
virtual void Serialize(ns3::Buffer::Iterator start) const
Definition: ndn-header.cpp:85
Copyright (c) 2011-2015 Regents of the University of California.
PacketHeader< Interest > InterestHeader
Definition: ndn-header.cpp:148
virtual uint32_t GetSerializedSize(void) const
Definition: ndn-header.cpp:78
std::streamsize read(char *buf, std::streamsize nMaxRead)
Definition: ndn-header.cpp:98