blob: 5f8d75c128479880b72553dd5605378880252be0 [file] [log] [blame]
Alexander Afanasyevc74a6022011-08-15 20:01:35 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev08d984e2011-08-13 19:20:22 -07002/*
Ilya Moiseenko956d0542012-01-02 15:26:40 -08003 * Copyright (c) 2011 University of California, Los Angeles
Alexander Afanasyev08d984e2011-08-13 19:20:22 -07004 *
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 *
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070018 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070019 *
20 */
21
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070022#include "ndn-face.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070023
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080024#include "ns3/packet.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070025#include "ns3/log.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070026#include "ns3/node.h"
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070027#include "ns3/assert.h"
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080028#include "ns3/uinteger.h"
29#include "ns3/double.h"
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080030#include "ns3/boolean.h"
Alexander Afanasyev4975f732011-12-20 17:52:19 -080031#include "ns3/simulator.h"
Alexander Afanasyev98d85e12012-08-09 10:06:05 -070032#include "ns3/random-variable.h"
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070033#include "ns3/pointer.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070034
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070035#include "ns3/ndn-header-helper.h"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -080036#include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.h"
Alexander Afanasyevb989b122013-07-10 17:15:46 -070037#include "ns3/ndnSIM/model/wire/ndnsim.h"
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080038
Alexander Afanasyev23d2b542011-12-07 18:54:46 -080039#include <boost/ref.hpp>
40
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070041NS_LOG_COMPONENT_DEFINE ("ndn.Face");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070042
43namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070044namespace ndn {
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070045
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070046NS_OBJECT_ENSURE_REGISTERED (Face);
Alexander Afanasyev30c33e32012-06-05 21:28:44 -070047
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080048TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070049Face::GetTypeId ()
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080050{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070051 static TypeId tid = TypeId ("ns3::ndn::Face")
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080052 .SetParent<Object> ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070053 .SetGroupName ("Ndn")
54 .AddAttribute ("Id", "Face id (unique integer for the Ndn stack on this node)",
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080055 TypeId::ATTR_GET, // allow only getting it.
56 UintegerValue (0),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070057 MakeUintegerAccessor (&Face::m_id),
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080058 MakeUintegerChecker<uint32_t> ())
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080059 ;
60 return tid;
61}
62
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -080063/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070064 * By default, Ndn face are created in the "down" state
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -080065 * with no IP addresses. Before becoming useable, the user must
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070066 * invoke SetUp on them once an Ndn address and mask have been set.
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070067 */
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -080068Face::Face (Ptr<Node> node)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080069 : m_node (node)
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070070 , m_upstreamInterestHandler (MakeNullCallback< void, Ptr<Face>, Ptr<Interest> > ())
71 , m_upstreamDataHandler (MakeNullCallback< void, Ptr<Face>, Ptr<ContentObject> > ())
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070072 , m_ifup (false)
73 , m_id ((uint32_t)-1)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080074 , m_metric (0)
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -070075 , m_flags (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070076{
77 NS_LOG_FUNCTION (this);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080078
79 NS_ASSERT_MSG (node != 0, "node cannot be NULL. Check the code");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070080}
81
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070082Face::~Face ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070083{
84 NS_LOG_FUNCTION_NOARGS ();
85}
86
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070087Face::Face (const Face &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070088{
89}
90
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070091Face& Face::operator= (const Face &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070092{
93 return *this;
94}
95
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080096Ptr<Node>
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070097Face::GetNode () const
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080098{
99 return m_node;
100}
101
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800102void
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700103Face::RegisterProtocolHandlers (const InterestHandler &interestHandler, const DataHandler &dataHandler)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700104{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800105 NS_LOG_FUNCTION_NOARGS ();
106
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700107 m_upstreamInterestHandler = interestHandler;
108 m_upstreamDataHandler = dataHandler;
109}
110
111void
112Face::UnRegisterProtocolHandlers ()
113{
114 NS_LOG_FUNCTION_NOARGS ();
115
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700116 m_upstreamInterestHandler = MakeNullCallback< void, Ptr<Face>, Ptr<Interest> > ();
117 m_upstreamDataHandler = MakeNullCallback< void, Ptr<Face>, Ptr<ContentObject> > ();
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700118}
119
120
121bool
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700122Face::SendInterest (Ptr<const Interest> interest)
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700123{
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700124 NS_LOG_FUNCTION (this << interest);
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700125
126 if (!IsUp ())
127 {
128 return false;
129 }
130
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700131 Ptr<Packet> packet = wire::ndnSIM::Interest::ToWire (interest);
132 return Send (packet);
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700133}
134
135bool
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700136Face::SendData (Ptr<const ContentObject> data)
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700137{
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700138 NS_LOG_FUNCTION (this << data);
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700139
140 if (!IsUp ())
141 {
142 return false;
143 }
144
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700145 Ptr<Packet> packet = wire::ndnSIM::Data::ToWire (data);
146 return Send (packet);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800147}
148
149bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700150Face::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800151{
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800152 FwHopCountTag hopCount;
153 bool tagExists = packet->RemovePacketTag (hopCount);
154 if (tagExists)
155 {
156 hopCount.Increment ();
157 packet->AddPacketTag (hopCount);
158 }
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700159
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700160 return true;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800161}
162
163bool
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700164Face::Receive (Ptr<const Packet> p)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800165{
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700166 NS_LOG_FUNCTION (this << p << p->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800167
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800168 if (!IsUp ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700169 {
170 // no tracing here. If we were off while receiving, we shouldn't even know that something was there
171 return false;
172 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800173
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700174 Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
175 try
176 {
177 HeaderHelper::Type type = HeaderHelper::GetNdnHeaderType (packet);
178 switch (type)
179 {
180 case HeaderHelper::INTEREST_NDNSIM:
181 {
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700182 Ptr<Interest> interest = wire::ndnSIM::Interest::FromWire (packet);
183 return ReceiveInterest (interest);
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700184 }
185 case HeaderHelper::CONTENT_OBJECT_NDNSIM:
186 {
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700187 Ptr<ContentObject> data = wire::ndnSIM::Data::FromWire (packet);
188 return ReceiveData (data);
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700189 }
190 case HeaderHelper::INTEREST_CCNB:
191 case HeaderHelper::CONTENT_OBJECT_CCNB:
192 NS_FATAL_ERROR ("ccnb support is broken in this implementation");
193 return false;
194 }
195
196 // exception will be thrown if packet is not recognized
197 }
198 catch (UnknownHeaderException)
199 {
200 NS_ASSERT_MSG (false, "Unknown NDN header. Should not happen");
201 NS_LOG_ERROR ("Unknown NDN header. Should not happen");
202 return false;
203 }
204}
205
206bool
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700207Face::ReceiveInterest (Ptr<Interest> interest)
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700208{
209 if (!IsUp ())
210 {
211 // no tracing here. If we were off while receiving, we shouldn't even know that something was there
212 return false;
213 }
214
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700215 m_upstreamInterestHandler (this, interest);
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700216 return true;
217}
218
219bool
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700220Face::ReceiveData (Ptr<ContentObject> data)
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700221{
222 if (!IsUp ())
223 {
224 // no tracing here. If we were off while receiving, we shouldn't even know that something was there
225 return false;
226 }
227
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700228 m_upstreamDataHandler (this, data);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800229 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700230}
231
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800232void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700233Face::SetMetric (uint16_t metric)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800234{
235 NS_LOG_FUNCTION (metric);
236 m_metric = metric;
237}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700238
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800239uint16_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700240Face::GetMetric (void) const
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800241{
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800242 return m_metric;
243}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700244
245/**
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800246 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700247 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700248 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700249 */
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800250
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700251void
252Face::SetFlags (uint32_t flags)
253{
254 m_flags = flags;
255}
256
257
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700258bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700259Face::operator== (const Face &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700260{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800261 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800262 "Faces of different nodes should not be compared to each other: " << *this << " == " << face);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700263
264 return (m_id == face.m_id);
265}
266
267bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700268Face::operator< (const Face &face) const
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700269{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800270 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800271 "Faces of different nodes should not be compared to each other: " << *this << " == " << face);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700272
273 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700274}
275
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700276std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700277Face::Print (std::ostream &os) const
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700278{
279 os << "id=" << GetId ();
280 return os;
281}
282
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700283std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700284operator<< (std::ostream& os, const Face &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700285{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700286 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700287 return os;
288}
289
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700290} // namespace ndn
291} // namespace ns3
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700292