NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ndn-net-device-face.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
20 #include "ndn-net-device-face.hpp"
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.NetDeviceFace");
38 
39 namespace ns3 {
40 namespace ndn {
41 
42 NetDeviceFace::NetDeviceFace(Ptr<Node> node, const Ptr<NetDevice>& netDevice)
43  : Face(FaceUri("netDeviceFace://"), FaceUri("netDeviceFace://"))
44  , m_node(node)
45  , m_netDevice(netDevice)
46 {
47  NS_LOG_FUNCTION(this << netDevice);
48 
49  setMetric(1); // default metric
50 
51  NS_ASSERT_MSG(m_netDevice != 0, "NetDeviceFace needs to be assigned a valid NetDevice");
52 
53  m_node->RegisterProtocolHandler(MakeCallback(&NetDeviceFace::receiveFromNetDevice, this),
55  true /*promiscuous mode*/);
56 }
57 
59 {
60  NS_LOG_FUNCTION_NOARGS();
61  close();
62 }
63 
64 void
66 {
67  m_node->UnregisterProtocolHandler(MakeCallback(&NetDeviceFace::receiveFromNetDevice, this));
68  this->fail("Close connection");
69 }
70 
71 Ptr<NetDevice>
73 {
74  return m_netDevice;
75 }
76 
77 void
78 NetDeviceFace::send(Ptr<Packet> packet)
79 {
80  NS_ASSERT_MSG(packet->GetSize() <= m_netDevice->GetMtu(),
81  "Packet size " << packet->GetSize() << " exceeds device MTU "
82  << m_netDevice->GetMtu());
83 
84  FwHopCountTag tag;
85  packet->RemovePacketTag(tag);
86  tag.Increment();
87  packet->AddPacketTag(tag);
88 
89  m_netDevice->Send(packet, m_netDevice->GetBroadcast(), L3Protocol::ETHERNET_FRAME_TYPE);
90 }
91 
92 void
94 {
95  NS_LOG_FUNCTION(this << &interest);
96 
97  this->onSendInterest(interest);
98 
99  Ptr<Packet> packet = Convert::ToPacket(interest);
100  send(packet);
101 }
102 
103 void
105 {
106  NS_LOG_FUNCTION(this << &data);
107 
108  this->onSendData(data);
109 
110  Ptr<Packet> packet = Convert::ToPacket(data);
111  send(packet);
112 }
113 
114 // callback
115 void
116 NetDeviceFace::receiveFromNetDevice(Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol,
117  const Address& from, const Address& to,
118  NetDevice::PacketType packetType)
119 {
120  NS_LOG_FUNCTION(device << p << protocol << from << to << packetType);
121 
122  Ptr<Packet> packet = p->Copy();
123  try {
124  uint32_t type = Convert::getPacketType(p);
125  if (type == ::ndn::tlv::Interest) {
126  shared_ptr<const Interest> i = Convert::FromPacket<Interest>(packet);
127  this->onReceiveInterest(*i);
128  }
129  else if (type == ::ndn::tlv::Data) {
130  shared_ptr<const Data> d = Convert::FromPacket<Data>(packet);
131  this->onReceiveData(*d);
132  }
133  else {
134  NS_LOG_ERROR("Unsupported TLV packet");
135  }
136  }
137  catch (::ndn::tlv::Error&) {
138  NS_LOG_ERROR("Unrecognized TLV packet");
139  }
140 }
141 
142 } // namespace ndn
143 } // namespace ns3
static Ptr< Packet > ToPacket(const T &pkt)
Definition: ndn-ns3.cpp:53
virtual void close()
Close the face.
Packet tag that is used to track hop count for Interest-Data pairs.
Ptr< NetDevice > GetNetDevice() const
Get NetDevice associated with the face.
static const uint16_t ETHERNET_FRAME_TYPE
Ethernet Frame Type of Ndn.
EventEmitter< Data > onReceiveData
fires when a Data is received
Definition: face.hpp:84
EventEmitter< Interest > onSendInterest
fires when an Interest is sent out
Definition: face.hpp:87
represents a face
Definition: face.hpp:59
static uint32_t getPacketType(Ptr< const Packet > packet)
Definition: ndn-ns3.cpp:78
void fail(const std::string &reason)
fail the face and raise onFail event if it's UP; otherwise do nothing
Definition: face.cpp:116
NetDeviceFace(Ptr< Node > node, const Ptr< NetDevice > &netDevice)
Constructor.
EventEmitter< Data > onSendData
fires when a Data is sent out
Definition: face.hpp:90
void setMetric(uint64_t metric)
Definition: face.hpp:258
virtual void sendData(const Data &data)
void Increment()
Increment hop count.
EventEmitter< Interest > onReceiveInterest
fires when an Interest is received
Definition: face.hpp:81
virtual void sendInterest(const Interest &interest)