NS-3 based Named Data Networking (NDN) simulator
ndnSIM: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
ndn-net-device-face.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2 /*
3  * Copyright (c) 2011 University of California, Los Angeles
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19  *
20  */
21 
22 #include "ndn-net-device-face.h"
23 #include "ndn-l3-protocol.h"
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 #include "ns3/ndn-name.h"
35 
36 NS_LOG_COMPONENT_DEFINE ("ndn.NetDeviceFace");
37 
38 namespace ns3 {
39 namespace ndn {
40 
41 NS_OBJECT_ENSURE_REGISTERED (NetDeviceFace);
42 
43 TypeId
44 NetDeviceFace::GetTypeId ()
45 {
46  static TypeId tid = TypeId ("ns3::ndn::NetDeviceFace")
47  .SetParent<Face> ()
48  .SetGroupName ("Ndn")
49  ;
50  return tid;
51 }
52 
57 NetDeviceFace::NetDeviceFace (Ptr<Node> node, const Ptr<NetDevice> &netDevice)
58  : Face (node)
59  , m_netDevice (netDevice)
60 {
61  NS_LOG_FUNCTION (this << netDevice);
62 
63  SetMetric (1); // default metric
64 
65  NS_ASSERT_MSG (m_netDevice != 0, "NetDeviceFace needs to be assigned a valid NetDevice");
66 }
67 
68 NetDeviceFace::~NetDeviceFace ()
69 {
70  NS_LOG_FUNCTION_NOARGS ();
71 }
72 
73 NetDeviceFace& NetDeviceFace::operator= (const NetDeviceFace &)
74 {
75  return *this;
76 }
77 
78 Ptr<NetDevice>
80 {
81  return m_netDevice;
82 }
83 
84 void
85 NetDeviceFace::RegisterProtocolHandlers (const InterestHandler &interestHandler, const DataHandler &dataHandler)
86 {
87  NS_LOG_FUNCTION (this);
88 
89  Face::RegisterProtocolHandlers (interestHandler, dataHandler);
90 
91  m_node->RegisterProtocolHandler (MakeCallback (&NetDeviceFace::ReceiveFromNetDevice, this),
92  L3Protocol::ETHERNET_FRAME_TYPE, m_netDevice, true/*promiscuous mode*/);
93 }
94 
95 void
97 {
98  m_node->UnregisterProtocolHandler (MakeCallback (&NetDeviceFace::ReceiveFromNetDevice, this));
100 }
101 
102 bool
103 NetDeviceFace::Send (Ptr<Packet> packet)
104 {
105  if (!Face::Send (packet))
106  {
107  return false;
108  }
109 
110  NS_LOG_FUNCTION (this << packet);
111 
112  NS_ASSERT_MSG (packet->GetSize () <= m_netDevice->GetMtu (),
113  "Packet size " << packet->GetSize () << " exceeds device MTU "
114  << m_netDevice->GetMtu ()
115  << " for Ndn; fragmentation not supported");
116 
117  bool ok = m_netDevice->Send (packet, m_netDevice->GetBroadcast (),
119  return ok;
120 }
121 
122 // callback
123 void
124 NetDeviceFace::ReceiveFromNetDevice (Ptr<NetDevice> device,
125  Ptr<const Packet> p,
126  uint16_t protocol,
127  const Address &from,
128  const Address &to,
129  NetDevice::PacketType packetType)
130 {
131  NS_LOG_FUNCTION (device << p << protocol << from << to << packetType);
132  Receive (p);
133 }
134 
135 
136 std::ostream&
137 NetDeviceFace::Print (std::ostream& os) const
138 {
139 #ifdef NS3_LOG_ENABLE
140  os << "dev[" << GetNode ()->GetId () << "]=net(" << GetId ();
141 
142  if (DynamicCast<PointToPointNetDevice> (m_netDevice))
143  {
144  // extra debugging information which available ONLY for PointToPointNetDevice's
145  os << ",";
146  os << DynamicCast<PointToPointNetDevice> (m_netDevice)->GetChannel ()->GetDevice (0)->GetNode ()->GetId ();
147  os << "-";
148  os << DynamicCast<PointToPointNetDevice> (m_netDevice)->GetChannel ()->GetDevice (1)->GetNode ()->GetId ();
149  }
150  os << ")";
151 #else
152  os << "dev=net(" << GetId () << ")";
153 #endif
154  return os;
155 }
156 
157 } // namespace ndnsim
158 } // namespace ns3
159 
virtual bool Send(Ptr< Packet > p)
Send packet down to the stack (towards app or network)
Ptr< Node > m_node
Smart pointer to Node.
Definition: ndn-face.h:269
virtual std::ostream & Print(std::ostream &os) const
Print out name of the NdnFace to the stream.
uint32_t GetId() const
Get face Id.
Definition: ndn-face.h:314
virtual void UnRegisterProtocolHandlers()
Un-Register callback to call when new packet arrives on the face.
Definition: ndn-face.cc:113
virtual void RegisterProtocolHandlers(const InterestHandler &interestHandler, const DataHandler &dataHandler)
Register callback to call when new packet arrives on the face.
virtual void RegisterProtocolHandlers(const InterestHandler &interestHandler, const DataHandler &dataHandler)
Register callback to call when new packet arrives on the face.
Definition: ndn-face.cc:104
Virtual class defining NDN face.
Definition: ndn-face.h:58
virtual void SetMetric(uint16_t metric)
Assign routing/forwarding metric with face.
Definition: ndn-face.cc:230
virtual bool Receive(Ptr< const Packet > p)
Send packet up to the stack (towards forwarding strategy)
Definition: ndn-face.cc:163
static const uint16_t ETHERNET_FRAME_TYPE
Ethernet Frame Type of Ndn.
virtual bool Send(Ptr< Packet > packet)
Send packet down to the stack (towards app or network)
Definition: ndn-face.cc:149
Face(Ptr< Node > node)
Default constructor.
Definition: ndn-face.cc:69
Ptr< Node > GetNode() const
Get node to which this face is associated.
Definition: ndn-face.cc:98
Callback< void, Ptr< Face >, Ptr< Interest > > InterestHandler
NDN protocol handlers.
Definition: ndn-face.h:71
NetDeviceFace(Ptr< Node > node, const Ptr< NetDevice > &netDevice)
Constructor.
virtual void UnRegisterProtocolHandlers()
Un-Register callback to call when new packet arrives on the face.
Ptr< NetDevice > GetNetDevice() const
Get NetDevice associated with the face.