Spyridon Mastorakis | ded1aa6 | 2014-10-29 16:36:40 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
| 26 | namespace io = boost::iostreams; |
| 27 | |
| 28 | namespace ns3 { |
| 29 | namespace ndn { |
| 30 | |
| 31 | template<> |
| 32 | ns3::TypeId |
| 33 | PacketHeader<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 | |
| 45 | template<> |
| 46 | ns3::TypeId |
| 47 | PacketHeader<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 | |
| 58 | template<class Pkt> |
| 59 | TypeId |
| 60 | PacketHeader<Pkt>::GetInstanceTypeId(void) const |
| 61 | { |
| 62 | return GetTypeId(); |
| 63 | } |
| 64 | |
| 65 | template<class Pkt> |
| 66 | PacketHeader<Pkt>::PacketHeader() |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | template<class Pkt> |
| 71 | PacketHeader<Pkt>::PacketHeader(const Pkt& packet) |
| 72 | : m_packet(packet.shared_from_this()) |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | template<class Pkt> |
| 77 | uint32_t |
| 78 | PacketHeader<Pkt>::GetSerializedSize(void) const |
| 79 | { |
| 80 | return m_packet->wireEncode().size(); |
| 81 | } |
| 82 | |
| 83 | template<class Pkt> |
| 84 | void |
| 85 | PacketHeader<Pkt>::Serialize(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(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 | Buffer::Iterator& m_is; |
| 114 | }; |
| 115 | |
| 116 | template<class Pkt> |
| 117 | uint32_t |
| 118 | PacketHeader<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 | |
| 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> |
| 143 | PacketHeader<Pkt>::getPacket() |
| 144 | { |
| 145 | return m_packet; |
| 146 | } |
| 147 | |
| 148 | typedef PacketHeader<Interest> InterestHeader; |
| 149 | typedef PacketHeader<Data> DataHeader; |
| 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 |