blob: 72b490e898dd5b030eb85bf9690fde51abd6c755 [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 Afanasyev7b923f32013-07-16 13:23:04 -070037
38#include "ns3/ndn-wire.h"
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080039
Alexander Afanasyev23d2b542011-12-07 18:54:46 -080040#include <boost/ref.hpp>
41
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070042NS_LOG_COMPONENT_DEFINE ("ndn.Face");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070043
44namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070045namespace ndn {
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070046
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070047NS_OBJECT_ENSURE_REGISTERED (Face);
Alexander Afanasyev30c33e32012-06-05 21:28:44 -070048
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080049TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070050Face::GetTypeId ()
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080051{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070052 static TypeId tid = TypeId ("ns3::ndn::Face")
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080053 .SetParent<Object> ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070054 .SetGroupName ("Ndn")
55 .AddAttribute ("Id", "Face id (unique integer for the Ndn stack on this node)",
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080056 TypeId::ATTR_GET, // allow only getting it.
57 UintegerValue (0),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070058 MakeUintegerAccessor (&Face::m_id),
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080059 MakeUintegerChecker<uint32_t> ())
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080060 ;
61 return tid;
62}
63
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -080064/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070065 * By default, Ndn face are created in the "down" state
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -080066 * with no IP addresses. Before becoming useable, the user must
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070067 * invoke SetUp on them once an Ndn address and mask have been set.
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070068 */
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -080069Face::Face (Ptr<Node> node)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080070 : m_node (node)
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070071 , m_upstreamInterestHandler (MakeNullCallback< void, Ptr<Face>, Ptr<Interest> > ())
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070072 , m_upstreamDataHandler (MakeNullCallback< void, Ptr<Face>, Ptr<Data> > ())
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070073 , m_ifup (false)
74 , m_id ((uint32_t)-1)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080075 , m_metric (0)
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -070076 , m_flags (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070077{
78 NS_LOG_FUNCTION (this);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080079
80 NS_ASSERT_MSG (node != 0, "node cannot be NULL. Check the code");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070081}
82
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070083Face::~Face ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070084{
85 NS_LOG_FUNCTION_NOARGS ();
86}
87
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070088Face::Face (const Face &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070089{
90}
91
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070092Face& Face::operator= (const Face &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070093{
94 return *this;
95}
96
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080097Ptr<Node>
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070098Face::GetNode () const
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080099{
100 return m_node;
101}
102
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800103void
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700104Face::RegisterProtocolHandlers (const InterestHandler &interestHandler, const DataHandler &dataHandler)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700105{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800106 NS_LOG_FUNCTION_NOARGS ();
107
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700108 m_upstreamInterestHandler = interestHandler;
109 m_upstreamDataHandler = dataHandler;
110}
111
112void
113Face::UnRegisterProtocolHandlers ()
114{
115 NS_LOG_FUNCTION_NOARGS ();
116
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700117 m_upstreamInterestHandler = MakeNullCallback< void, Ptr<Face>, Ptr<Interest> > ();
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700118 m_upstreamDataHandler = MakeNullCallback< void, Ptr<Face>, Ptr<Data> > ();
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700119}
120
121
122bool
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700123Face::SendInterest (Ptr<const Interest> interest)
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700124{
Alexander Afanasyeve4795ae2013-07-11 20:01:31 -0700125 NS_LOG_FUNCTION (this << boost::cref (*this) << interest->GetName ());
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700126
127 if (!IsUp ())
128 {
129 return false;
130 }
131
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700132 return Send (Wire::FromInterest (interest));
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700133}
134
135bool
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700136Face::SendData (Ptr<const Data> 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 Afanasyev7b923f32013-07-16 13:23:04 -0700145 return Send (Wire::FromData (data));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800146}
147
148bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700149Face::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800150{
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800151 FwHopCountTag hopCount;
152 bool tagExists = packet->RemovePacketTag (hopCount);
153 if (tagExists)
154 {
155 hopCount.Increment ();
156 packet->AddPacketTag (hopCount);
157 }
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700158
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700159 return true;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800160}
161
162bool
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700163Face::Receive (Ptr<const Packet> p)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800164{
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700165 NS_LOG_FUNCTION (this << p << p->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800166
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800167 if (!IsUp ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700168 {
169 // no tracing here. If we were off while receiving, we shouldn't even know that something was there
170 return false;
171 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800172
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700173 Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
174 try
175 {
176 HeaderHelper::Type type = HeaderHelper::GetNdnHeaderType (packet);
177 switch (type)
178 {
179 case HeaderHelper::INTEREST_NDNSIM:
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700180 return ReceiveInterest (Wire::ToInterest (packet, Wire::WIRE_FORMAT_NDNSIM));
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700181 case HeaderHelper::INTEREST_CCNB:
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700182 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));
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700185 case HeaderHelper::CONTENT_OBJECT_CCNB:
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700186 return ReceiveData (Wire::ToData (packet, Wire::WIRE_FORMAT_CCNB));
Alexander Afanasyev37a61242013-07-26 11:19:18 -0700187 default:
188 NS_FATAL_ERROR ("Not supported NDN header");
189 return false;
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700190 }
191
192 // exception will be thrown if packet is not recognized
193 }
194 catch (UnknownHeaderException)
195 {
Alexander Afanasyev7b923f32013-07-16 13:23:04 -0700196 NS_FATAL_ERROR ("Unknown NDN header. Should not happen");
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700197 return false;
198 }
Alexander Afanasyev37a61242013-07-26 11:19:18 -0700199
200 return false;
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700201}
202
203bool
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700204Face::ReceiveInterest (Ptr<Interest> interest)
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700205{
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
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700212 m_upstreamInterestHandler (this, interest);
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700213 return true;
214}
215
216bool
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700217Face::ReceiveData (Ptr<Data> data)
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700218{
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
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700225 m_upstreamDataHandler (this, data);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800226 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700227}
228
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800229void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700230Face::SetMetric (uint16_t metric)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800231{
232 NS_LOG_FUNCTION (metric);
233 m_metric = metric;
234}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700235
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800236uint16_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700237Face::GetMetric (void) const
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800238{
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800239 return m_metric;
240}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700241
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700242void
243Face::SetFlags (uint32_t flags)
244{
245 m_flags = flags;
246}
247
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700248bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700249Face::operator== (const Face &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700250{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800251 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800252 "Faces of different nodes should not be compared to each other: " << *this << " == " << face);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700253
254 return (m_id == face.m_id);
255}
256
257bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700258Face::operator< (const Face &face) const
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700259{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800260 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800261 "Faces of different nodes should not be compared to each other: " << *this << " == " << face);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700262
263 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700264}
265
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700266std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700267Face::Print (std::ostream &os) const
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700268{
269 os << "id=" << GetId ();
270 return os;
271}
272
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700273std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700274operator<< (std::ostream& os, const Face &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700275{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700276 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700277 return os;
278}
279
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700280} // namespace ndn
281} // namespace ns3
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700282