blob: 89c9251d65e7b57ef66c734fd661bea632fd3c02 [file] [log] [blame]
Spyridon Mastorakisded1aa62014-10-29 16:36:40 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2014 Regents of the University of California.
4 *
5 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
7 *
8 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20#include "ndn-header.hpp"
21
22#include <iosfwd>
23#include <boost/iostreams/concepts.hpp>
24#include <boost/iostreams/stream.hpp>
25
26namespace io = boost::iostreams;
27
28namespace ns3 {
29namespace ndn {
30
31template<>
32ns3::TypeId
33PacketHeader<Interest>::GetTypeId()
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
45template<>
46ns3::TypeId
47PacketHeader<Data>::GetTypeId()
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
58template<class Pkt>
59TypeId
60PacketHeader<Pkt>::GetInstanceTypeId(void) const
61{
62 return GetTypeId();
63}
64
65template<class Pkt>
66PacketHeader<Pkt>::PacketHeader()
67{
68}
69
70template<class Pkt>
71PacketHeader<Pkt>::PacketHeader(const Pkt& packet)
72 : m_packet(packet.shared_from_this())
73{
74}
75
76template<class Pkt>
77uint32_t
78PacketHeader<Pkt>::GetSerializedSize(void) const
79{
80 return m_packet->wireEncode().size();
81}
82
83template<class Pkt>
84void
85PacketHeader<Pkt>::Serialize(Buffer::Iterator start) const
86{
87 start.Write(m_packet->wireEncode().wire(), m_packet->wireEncode().size());
88}
89
90class Ns3BufferIteratorSource : public io::source {
91public:
92 Ns3BufferIteratorSource(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
112private:
113 Buffer::Iterator& m_is;
114};
115
116template<class Pkt>
117uint32_t
118PacketHeader<Pkt>::Deserialize(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
127template<>
128void
129PacketHeader<Interest>::Print(std::ostream& os) const
130{
131 os << "I: " << *m_packet;
132}
133
134template<>
135void
136PacketHeader<Data>::Print(std::ostream& os) const
137{
138 os << "D: " << *m_packet;
139}
140
141template<class Pkt>
142shared_ptr<const Pkt>
143PacketHeader<Pkt>::getPacket()
144{
145 return m_packet;
146}
147
148typedef PacketHeader<Interest> InterestHeader;
149typedef PacketHeader<Data> DataHeader;
150
151NS_OBJECT_ENSURE_REGISTERED(InterestHeader);
152NS_OBJECT_ENSURE_REGISTERED(DataHeader);
153
154template class PacketHeader<Interest>;
155template class PacketHeader<Data>;
156
157} // namespace ndn
158} // namespace ns3