Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2016 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-net-device-transport.hpp" |
| 21 | |
| 22 | #include "../helper/ndn-stack-helper.hpp" |
| 23 | #include "ndn-block-header.hpp" |
| 24 | #include "../utils/ndn-ns3-packet-tag.hpp" |
| 25 | |
| 26 | #include <ndn-cxx/encoding/block.hpp> |
| 27 | #include <ndn-cxx/interest.hpp> |
| 28 | #include <ndn-cxx/data.hpp> |
| 29 | |
| 30 | NS_LOG_COMPONENT_DEFINE("ndn.NetDeviceTransport"); |
| 31 | |
| 32 | namespace ns3 { |
| 33 | namespace ndn { |
| 34 | |
| 35 | NetDeviceTransport::NetDeviceTransport(Ptr<Node> node, |
| 36 | const Ptr<NetDevice>& netDevice, |
| 37 | const std::string& localUri, |
| 38 | const std::string& remoteUri, |
| 39 | ::ndn::nfd::FaceScope scope, |
| 40 | ::ndn::nfd::FacePersistency persistency, |
| 41 | ::ndn::nfd::LinkType linkType) |
| 42 | : m_netDevice(netDevice) |
| 43 | , m_node(node) |
| 44 | { |
| 45 | this->setLocalUri(FaceUri(localUri)); |
| 46 | this->setRemoteUri(FaceUri(remoteUri)); |
| 47 | this->setScope(scope); |
| 48 | this->setPersistency(persistency); |
| 49 | this->setLinkType(linkType); |
| 50 | // this->setMtu(udp::computeMtu(m_socket.local_endpoint())); // not sure what should be here |
| 51 | |
| 52 | NS_LOG_FUNCTION(this << "Creating an ndnSIM transport instance for netDevice with URI" |
| 53 | << this->getLocalUri()); |
| 54 | |
| 55 | NS_ASSERT_MSG(m_netDevice != 0, "NetDeviceFace needs to be assigned a valid NetDevice"); |
| 56 | |
| 57 | m_node->RegisterProtocolHandler(MakeCallback(&NetDeviceTransport::receiveFromNetDevice, this), |
| 58 | L3Protocol::ETHERNET_FRAME_TYPE, m_netDevice, |
| 59 | true /*promiscuous mode*/); |
| 60 | } |
| 61 | |
| 62 | NetDeviceTransport::~NetDeviceTransport() |
| 63 | { |
| 64 | NS_LOG_FUNCTION_NOARGS(); |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | NetDeviceTransport::beforeChangePersistency(::ndn::nfd::FacePersistency newPersistency) |
| 69 | { |
| 70 | NS_LOG_FUNCTION(this << "Changing persistency for netDevice with URI" |
| 71 | << this->getLocalUri() << "currently does nothing"); |
| 72 | // do nothing for now |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | NetDeviceTransport::doClose() |
| 77 | { |
| 78 | NS_LOG_FUNCTION(this << "Closing transport for netDevice with URI" |
| 79 | << this->getLocalUri()); |
| 80 | |
| 81 | // set the state of the transport to "CLOSED" |
| 82 | this->setState(nfd::face::TransportState::CLOSED); |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | NetDeviceTransport::doSend(Packet&& packet) |
| 87 | { |
| 88 | NS_LOG_FUNCTION(this << "Sending packet from netDevice with URI" |
| 89 | << this->getLocalUri()); |
| 90 | |
| 91 | // convert NFD packet to NS3 packet |
| 92 | BlockHeader header(packet); |
| 93 | |
| 94 | Ptr<ns3::Packet> ns3Packet = Create<ns3::Packet>(); |
| 95 | ns3Packet->AddHeader(header); |
| 96 | |
| 97 | // send the NS3 packet |
| 98 | m_netDevice->Send(ns3Packet, m_netDevice->GetBroadcast(), |
| 99 | L3Protocol::ETHERNET_FRAME_TYPE); |
| 100 | } |
| 101 | |
| 102 | // callback |
| 103 | void |
| 104 | NetDeviceTransport::receiveFromNetDevice(Ptr<NetDevice> device, |
| 105 | Ptr<const ns3::Packet> p, |
| 106 | uint16_t protocol, |
| 107 | const Address& from, const Address& to, |
| 108 | NetDevice::PacketType packetType) |
| 109 | { |
| 110 | NS_LOG_FUNCTION(device << p << protocol << from << to << packetType); |
| 111 | |
| 112 | // Convert NS3 packet to NFD packet |
| 113 | Ptr<ns3::Packet> packet = p->Copy(); |
| 114 | |
| 115 | BlockHeader header; |
| 116 | packet->RemoveHeader(header); |
| 117 | |
| 118 | auto nfdPacket = Packet(std::move(header.getBlock())); |
| 119 | |
| 120 | this->receive(std::move(nfdPacket)); |
| 121 | } |
| 122 | |
| 123 | Ptr<NetDevice> |
| 124 | NetDeviceTransport::GetNetDevice() const |
| 125 | { |
| 126 | return m_netDevice; |
| 127 | } |
| 128 | |
| 129 | } // namespace ndn |
| 130 | } // namespace ns3 |