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-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-face.h"
23 
24 #include "ns3/packet.h"
25 #include "ns3/log.h"
26 #include "ns3/node.h"
27 #include "ns3/assert.h"
28 #include "ns3/uinteger.h"
29 #include "ns3/double.h"
30 #include "ns3/boolean.h"
31 #include "ns3/simulator.h"
32 #include "ns3/random-variable.h"
33 #include "ns3/pointer.h"
34 
35 #include "ns3/ndn-header-helper.h"
36 #include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.h"
37 
38 #include "ns3/ndn-wire.h"
39 
40 #include <boost/ref.hpp>
41 
42 NS_LOG_COMPONENT_DEFINE ("ndn.Face");
43 
44 namespace ns3 {
45 namespace ndn {
46 
47 NS_OBJECT_ENSURE_REGISTERED (Face);
48 
49 TypeId
50 Face::GetTypeId ()
51 {
52  static TypeId tid = TypeId ("ns3::ndn::Face")
53  .SetParent<Object> ()
54  .SetGroupName ("Ndn")
55  .AddAttribute ("Id", "Face id (unique integer for the Ndn stack on this node)",
56  TypeId::ATTR_GET, // allow only getting it.
57  UintegerValue (0),
58  MakeUintegerAccessor (&Face::m_id),
59  MakeUintegerChecker<uint32_t> ())
60  ;
61  return tid;
62 }
63 
69 Face::Face (Ptr<Node> node)
70  : m_node (node)
71  , m_upstreamInterestHandler (MakeNullCallback< void, Ptr<Face>, Ptr<Interest> > ())
72  , m_upstreamDataHandler (MakeNullCallback< void, Ptr<Face>, Ptr<Data> > ())
73  , m_ifup (false)
74  , m_id ((uint32_t)-1)
75  , m_metric (0)
76  , m_flags (0)
77 {
78  NS_LOG_FUNCTION (this << node);
79 
80  NS_ASSERT_MSG (node != 0, "node cannot be NULL. Check the code");
81 }
82 
83 Face::~Face ()
84 {
85  NS_LOG_FUNCTION_NOARGS ();
86 }
87 
88 Face::Face (const Face &)
89 {
90 }
91 
92 Face& Face::operator= (const Face &)
93 {
94  return *this;
95 }
96 
97 Ptr<Node>
98 Face::GetNode () const
99 {
100  return m_node;
101 }
102 
103 void
104 Face::RegisterProtocolHandlers (const InterestHandler &interestHandler, const DataHandler &dataHandler)
105 {
106  NS_LOG_FUNCTION_NOARGS ();
107 
108  m_upstreamInterestHandler = interestHandler;
109  m_upstreamDataHandler = dataHandler;
110 }
111 
112 void
114 {
115  NS_LOG_FUNCTION_NOARGS ();
116 
117  m_upstreamInterestHandler = MakeNullCallback< void, Ptr<Face>, Ptr<Interest> > ();
118  m_upstreamDataHandler = MakeNullCallback< void, Ptr<Face>, Ptr<Data> > ();
119 }
120 
121 
122 bool
123 Face::SendInterest (Ptr<const Interest> interest)
124 {
125  NS_LOG_FUNCTION (this << boost::cref (*this) << interest->GetName ());
126 
127  if (!IsUp ())
128  {
129  return false;
130  }
131 
132  return Send (Wire::FromInterest (interest));
133 }
134 
135 bool
136 Face::SendData (Ptr<const Data> data)
137 {
138  NS_LOG_FUNCTION (this << data);
139 
140  if (!IsUp ())
141  {
142  return false;
143  }
144 
145  return Send (Wire::FromData (data));
146 }
147 
148 bool
149 Face::Send (Ptr<Packet> packet)
150 {
151  FwHopCountTag hopCount;
152  bool tagExists = packet->RemovePacketTag (hopCount);
153  if (tagExists)
154  {
155  hopCount.Increment ();
156  packet->AddPacketTag (hopCount);
157  }
158 
159  return true;
160 }
161 
162 bool
163 Face::Receive (Ptr<const Packet> p)
164 {
165  NS_LOG_FUNCTION (this << p << p->GetSize ());
166 
167  if (!IsUp ())
168  {
169  // no tracing here. If we were off while receiving, we shouldn't even know that something was there
170  return false;
171  }
172 
173  Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
174  try
175  {
177  switch (type)
178  {
179  case HeaderHelper::INTEREST_NDNSIM:
180  return ReceiveInterest (Wire::ToInterest (packet, Wire::WIRE_FORMAT_NDNSIM));
181  case HeaderHelper::INTEREST_CCNB:
182  return ReceiveInterest (Wire::ToInterest (packet, Wire::WIRE_FORMAT_CCNB));
183  case HeaderHelper::CONTENT_OBJECT_NDNSIM:
184  return ReceiveData (Wire::ToData (packet, Wire::WIRE_FORMAT_NDNSIM));
185  case HeaderHelper::CONTENT_OBJECT_CCNB:
186  return ReceiveData (Wire::ToData (packet, Wire::WIRE_FORMAT_CCNB));
187  default:
188  NS_FATAL_ERROR ("Not supported NDN header");
189  return false;
190  }
191 
192  // exception will be thrown if packet is not recognized
193  }
194  catch (UnknownHeaderException)
195  {
196  NS_FATAL_ERROR ("Unknown NDN header. Should not happen");
197  return false;
198  }
199 
200  return false;
201 }
202 
203 bool
204 Face::ReceiveInterest (Ptr<Interest> interest)
205 {
206  if (!IsUp ())
207  {
208  // no tracing here. If we were off while receiving, we shouldn't even know that something was there
209  return false;
210  }
211 
212  m_upstreamInterestHandler (this, interest);
213  return true;
214 }
215 
216 bool
217 Face::ReceiveData (Ptr<Data> data)
218 {
219  if (!IsUp ())
220  {
221  // no tracing here. If we were off while receiving, we shouldn't even know that something was there
222  return false;
223  }
224 
225  m_upstreamDataHandler (this, data);
226  return true;
227 }
228 
229 void
230 Face::SetMetric (uint16_t metric)
231 {
232  NS_LOG_FUNCTION (metric);
233  m_metric = metric;
234 }
235 
236 uint16_t
237 Face::GetMetric (void) const
238 {
239  return m_metric;
240 }
241 
242 void
243 Face::SetFlags (uint32_t flags)
244 {
245  m_flags = flags;
246 }
247 
248 bool
249 Face::operator== (const Face &face) const
250 {
251  NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
252  "Faces of different nodes should not be compared to each other: " << *this << " == " << face);
253 
254  return (m_id == face.m_id);
255 }
256 
257 bool
258 Face::operator< (const Face &face) const
259 {
260  NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
261  "Faces of different nodes should not be compared to each other: " << *this << " == " << face);
262 
263  return (m_id < face.m_id);
264 }
265 
266 std::ostream&
267 Face::Print (std::ostream &os) const
268 {
269  os << "id=" << GetId ();
270  return os;
271 }
272 
273 std::ostream&
274 operator<< (std::ostream& os, const Face &face)
275 {
276  face.Print (os);
277  return os;
278 }
279 
280 } // namespace ndn
281 } // namespace ns3
282 
Ptr< Node > m_node
Smart pointer to Node.
Definition: ndn-face.h:269
virtual std::ostream & Print(std::ostream &os) const
Print information about the face into the stream.
Definition: ndn-face.cc:267
virtual bool SendData(Ptr< const Data > data)
Send out Dat packet through the face.
Definition: ndn-face.cc:136
void Increment()
Increment hop count.
bool operator<(const Face &face) const
Compare two faces.
Definition: ndn-face.cc:258
virtual bool SendInterest(Ptr< const Interest > interest)
Send out interest through the face.
Definition: ndn-face.cc:123
virtual bool ReceiveInterest(Ptr< Interest > interest)
Receive interest from application or another node and forward it up to the NDN stack.
Definition: ndn-face.cc:204
uint32_t GetId() const
Get face Id.
Definition: ndn-face.h:314
Type
enum for Ndn packet types
bool operator==(const Face &face) const
Compare two faces.
Definition: ndn-face.cc:249
virtual void UnRegisterProtocolHandlers()
Un-Register callback to call when new packet arrives on the face.
Definition: ndn-face.cc:113
Data header.
Definition: ndn-data.h:39
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
static Type GetNdnHeaderType(Ptr< const Packet > packet)
Packet ::= Version PacketType (Interest | Data)
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
void SetFlags(uint32_t flags)
Set face flags.
Definition: ndn-face.cc:243
virtual uint16_t GetMetric(void) const
Get routing/forwarding metric assigned to the face.
Definition: ndn-face.cc:237
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
NDN Interest (wire formats are defined in wire)
Definition: ndn-interest.h:43
virtual bool ReceiveData(Ptr< Data > data)
Receive Data packet from application or another node and forward it up to the NDN stack...
Definition: ndn-face.cc:217
Packet tag that is used to track hop count for Interest-Data pairs.
Exception thrown if NDN stack receives unrecognized message type.
Callback< void, Ptr< Face >, Ptr< Interest > > InterestHandler
NDN protocol handlers.
Definition: ndn-face.h:71
bool IsUp() const
Returns true if this face is enabled, false otherwise.
Definition: ndn-face.h:284