blob: 7e0996ae3c4985228d0ddc1eaac8e8cffa955e8c [file] [log] [blame]
Spyridon Mastorakisded1aa62014-10-29 16:36:40 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08003 * Copyright (c) 2011-2015 Regents of the University of California.
Spyridon Mastorakisded1aa62014-10-29 16:36:40 -07004 *
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
29namespace ns3 {
30namespace ndn {
31
32template<class T>
33std::shared_ptr<const T>
34Convert::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
45template std::shared_ptr<const Interest>
46Convert::FromPacket<Interest>(Ptr<Packet> packet);
47
48template std::shared_ptr<const Data>
49Convert::FromPacket<Data>(Ptr<Packet> packet);
50
51template<class T>
52Ptr<Packet>
53Convert::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
71template Ptr<Packet>
72Convert::ToPacket<Interest>(const Interest& packet);
73
74template Ptr<Packet>
75Convert::ToPacket<Data>(const Data& packet);
76
77uint32_t
78Convert::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