NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
ndn-net-device-link-service.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
21 #include "ndn-l3-protocol.hpp"
22 
23 #include "ndn-ns3.hpp"
24 
25 #include "ns3/net-device.h"
26 #include "ns3/log.h"
27 #include "ns3/packet.h"
28 #include "ns3/node.h"
29 #include "ns3/pointer.h"
30 
31 // #include "ns3/address.h"
32 #include "ns3/point-to-point-net-device.h"
33 #include "ns3/channel.h"
34 
35 #include "../utils/ndn-fw-hop-count-tag.hpp"
36 
37 NS_LOG_COMPONENT_DEFINE("ndn.NetDeviceLinkService");
38 
39 namespace ns3 {
40 namespace ndn {
41 
42 NetDeviceLinkService::NetDeviceLinkService(Ptr<Node> node, const Ptr<NetDevice>& netDevice)
43  : m_node(node)
44  , m_netDevice(netDevice)
45 {
46  NS_LOG_FUNCTION(this << netDevice);
47 
48  NS_ASSERT_MSG(m_netDevice != 0, "NetDeviceFace needs to be assigned a valid NetDevice");
49 
50  m_node->RegisterProtocolHandler(MakeCallback(&NetDeviceLinkService::receiveFromNetDevice, this),
52  true /*promiscuous mode*/);
53 }
54 
56 {
57  NS_LOG_FUNCTION_NOARGS();
58 }
59 
60 Ptr<Node>
62 {
63  return m_node;
64 }
65 
66 Ptr<NetDevice>
68 {
69  return m_netDevice;
70 }
71 
72 void
73 NetDeviceLinkService::doSendInterest(const Interest& interest)
74 {
75  NS_LOG_FUNCTION(this << &interest);
76 
77  Ptr<Packet> packet = Convert::ToPacket(interest);
78  send(packet);
79 }
80 
81 void
82 NetDeviceLinkService::doSendData(const Data& data)
83 {
84  NS_LOG_FUNCTION(this << &data);
85 
86  Ptr<Packet> packet = Convert::ToPacket(data);
87  send(packet);
88 }
89 
90 void
91 NetDeviceLinkService::doSendNack(const lp::Nack& nack)
92 {
93  NS_LOG_FUNCTION(this << &nack);
94 
95  // TODO
96  // Ptr<Packet> packet = Convert::ToPacket(nack);
97  // send(packet);
98 }
99 
100 // callback
101 void
102 NetDeviceLinkService::receiveFromNetDevice(Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol,
103  const Address& from, const Address& to,
104  NetDevice::PacketType packetType)
105 {
106  NS_LOG_FUNCTION(device << p << protocol << from << to << packetType);
107 
108  Ptr<Packet> packet = p->Copy();
109  try {
110  switch (Convert::getPacketType(p)) {
112  shared_ptr<const Interest> i = Convert::FromPacket<Interest>(packet);
113  this->receiveInterest(*i);
114  break;
115  }
117  shared_ptr<const Data> d = Convert::FromPacket<Data>(packet);
118  this->receiveData(*d);
119  break;
120  }
121  // case ::ndn::tlv::Nack: {
122  // shared_ptr<const Nack> n = Convert::FromPacket<Nack>(packet);
123  // this->onReceiveNack(*n);
124  // }
125  default:
126  NS_LOG_ERROR("Unsupported TLV packet");
127  }
128  }
129  catch (const ::ndn::tlv::Error& e) {
130  NS_LOG_ERROR("Unrecognized TLV packet " << e.what());
131  }
132 }
133 
134 void
135 NetDeviceLinkService::send(Ptr<Packet> packet)
136 {
137  NS_ASSERT_MSG(packet->GetSize() <= m_netDevice->GetMtu(),
138  "Packet size " << packet->GetSize() << " exceeds device MTU "
139  << m_netDevice->GetMtu());
140 
141  FwHopCountTag tag;
142  packet->RemovePacketTag(tag);
143  tag.Increment();
144  packet->AddPacketTag(tag);
145 
146  m_netDevice->Send(packet, m_netDevice->GetBroadcast(), L3Protocol::ETHERNET_FRAME_TYPE);
147 }
148 
149 } // namespace face
150 } // namespace nfd
static Ptr< Packet > ToPacket(const T &pkt)
Definition: ndn-ns3.cpp:53
Copyright (c) 2011-2015 Regents of the University of California.
Packet tag that is used to track hop count for Interest-Data pairs.
static const uint16_t ETHERNET_FRAME_TYPE
Ethernet Frame Type of Ndn.
represents a Network Nack
Definition: nack.hpp:40
static uint32_t getPacketType(Ptr< const Packet > packet)
Definition: ndn-ns3.cpp:78
Ptr< NetDevice > GetNetDevice() const
Get NetDevice associated with the LinkService.
Copyright (c) 2011-2015 Regents of the University of California.
void Increment()
Increment hop count.
Ptr< Node > GetNode() const
Get Node associated with the LinkService.
void receiveData(const Data &data)
delivers received Data to forwarding
NetDeviceLinkService(Ptr< Node > node, const Ptr< NetDevice > &netDevice)
Constructor.
void receiveInterest(const Interest &interest)
delivers received Interest to forwarding