Spyridon Mastorakis | ded1aa6 | 2014-10-29 16:36:40 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
Spyridon Mastorakis | ded1aa6 | 2014-10-29 16:36:40 -0700 | [diff] [blame] | 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-ns3.hpp" |
| 21 | |
| 22 | #include <ndn-cxx/encoding/block.hpp> |
| 23 | #include <ndn-cxx/interest.hpp> |
| 24 | #include <ndn-cxx/data.hpp> |
| 25 | |
| 26 | #include "ndn-header.hpp" |
| 27 | #include "../utils/ndn-ns3-packet-tag.hpp" |
| 28 | |
| 29 | namespace ns3 { |
| 30 | namespace ndn { |
| 31 | |
| 32 | template<class T> |
| 33 | std::shared_ptr<const T> |
| 34 | Convert::FromPacket(Ptr<Packet> packet) |
| 35 | { |
| 36 | PacketHeader<T> header; |
| 37 | packet->RemoveHeader(header); |
| 38 | |
| 39 | auto pkt = header.getPacket(); |
| 40 | pkt->setTag(make_shared<Ns3PacketTag>(packet)); |
| 41 | |
| 42 | return pkt; |
| 43 | } |
| 44 | |
| 45 | template std::shared_ptr<const Interest> |
| 46 | Convert::FromPacket<Interest>(Ptr<Packet> packet); |
| 47 | |
| 48 | template std::shared_ptr<const Data> |
| 49 | Convert::FromPacket<Data>(Ptr<Packet> packet); |
| 50 | |
| 51 | template<class T> |
| 52 | Ptr<Packet> |
| 53 | Convert::ToPacket(const T& pkt) |
| 54 | { |
| 55 | PacketHeader<T> header(pkt); |
| 56 | |
| 57 | Ptr<Packet> packet; |
| 58 | |
| 59 | auto tag = pkt.template getTag<Ns3PacketTag>(); |
| 60 | if (tag != nullptr) { |
| 61 | packet = tag->getPacket()->Copy(); |
| 62 | } |
| 63 | else { |
| 64 | packet = Create<Packet>(); |
| 65 | } |
| 66 | |
| 67 | packet->AddHeader(header); |
| 68 | return packet; |
| 69 | } |
| 70 | |
| 71 | template Ptr<Packet> |
| 72 | Convert::ToPacket<Interest>(const Interest& packet); |
| 73 | |
| 74 | template Ptr<Packet> |
| 75 | Convert::ToPacket<Data>(const Data& packet); |
| 76 | |
| 77 | uint32_t |
| 78 | Convert::getPacketType(Ptr<const Packet> packet) |
| 79 | { |
| 80 | uint8_t type; |
| 81 | uint32_t nRead = packet->CopyData(&type, 1); |
| 82 | if (nRead != 1) { |
| 83 | throw ::ndn::tlv::Error("Unknown header"); |
| 84 | } |
| 85 | |
| 86 | if (type == ::ndn::tlv::Interest || type == ::ndn::tlv::Data) { |
| 87 | return type; |
| 88 | } |
| 89 | else { |
| 90 | throw ::ndn::tlv::Error("Unknown header"); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | } // namespace ndn |
| 95 | } // namespace ns3 |