blob: b3a43b0a989efe8563df040a956baacd7b22482c [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 Afanasyev1a0fff62013-01-19 14:29:51 -080035#include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.h"
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080036
Alexander Afanasyev23d2b542011-12-07 18:54:46 -080037#include <boost/ref.hpp>
38
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070039NS_LOG_COMPONENT_DEFINE ("ndn.Face");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070040
41namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070042namespace ndn {
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070043
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070044NS_OBJECT_ENSURE_REGISTERED (Face);
Alexander Afanasyev30c33e32012-06-05 21:28:44 -070045
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080046TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070047Face::GetTypeId ()
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080048{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070049 static TypeId tid = TypeId ("ns3::ndn::Face")
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080050 .SetParent<Object> ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070051 .SetGroupName ("Ndn")
52 .AddAttribute ("Id", "Face id (unique integer for the Ndn stack on this node)",
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080053 TypeId::ATTR_GET, // allow only getting it.
54 UintegerValue (0),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070055 MakeUintegerAccessor (&Face::m_id),
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080056 MakeUintegerChecker<uint32_t> ())
57
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070058 .AddTraceSource ("NdnTx", "Transmitted packet trace",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070059 MakeTraceSourceAccessor (&Face::m_txTrace))
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070060 .AddTraceSource ("NdnRx", "Received packet trace",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070061 MakeTraceSourceAccessor (&Face::m_rxTrace))
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070062 .AddTraceSource ("NdnDrop", "Dropped packet trace",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070063 MakeTraceSourceAccessor (&Face::m_dropTrace))
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080064 ;
65 return tid;
66}
67
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -080068/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070069 * By default, Ndn face are created in the "down" state
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -080070 * with no IP addresses. Before becoming useable, the user must
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070071 * invoke SetUp on them once an Ndn address and mask have been set.
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070072 */
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -080073Face::Face (Ptr<Node> node)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080074 : m_node (node)
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070075 , m_protocolHandler (MakeNullCallback<void,const Ptr<Face>&,const Ptr<const Packet>&> ())
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070076 , m_ifup (false)
77 , m_id ((uint32_t)-1)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080078 , m_metric (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070079{
80 NS_LOG_FUNCTION (this);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080081
82 NS_ASSERT_MSG (node != 0, "node cannot be NULL. Check the code");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070083}
84
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070085Face::~Face ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070086{
87 NS_LOG_FUNCTION_NOARGS ();
88}
89
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070090Face::Face (const Face &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070091{
92}
93
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070094Face& Face::operator= (const Face &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070095{
96 return *this;
97}
98
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080099Ptr<Node>
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700100Face::GetNode () const
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800101{
102 return m_node;
103}
104
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800105void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700106Face::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700107{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800108 NS_LOG_FUNCTION_NOARGS ();
109
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800110 m_protocolHandler = handler;
111}
112
113bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700114Face::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800115{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800116 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyevd459ec32012-04-30 13:58:20 -0700117 NS_LOG_DEBUG (*packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800118
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800119 if (!IsUp ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700120 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700121 m_dropTrace (packet);
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700122 return false;
123 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800124
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800125 FwHopCountTag hopCount;
126 bool tagExists = packet->RemovePacketTag (hopCount);
127 if (tagExists)
128 {
129 hopCount.Increment ();
130 packet->AddPacketTag (hopCount);
131 }
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700132
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700133 bool ok = SendImpl (packet);
134 if (ok)
135 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700136 m_txTrace (packet);
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700137 return true;
138 }
139 else
140 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700141 m_dropTrace (packet);
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700142 return false;
143 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800144}
145
146bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700147Face::Receive (const Ptr<const Packet> &packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800148{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800149 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800150
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800151 if (!IsUp ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700152 {
153 // no tracing here. If we were off while receiving, we shouldn't even know that something was there
154 return false;
155 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800156
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700157 m_rxTrace (packet);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800158 m_protocolHandler (this, packet);
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800159
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800160 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700161}
162
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800163void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700164Face::SetMetric (uint16_t metric)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800165{
166 NS_LOG_FUNCTION (metric);
167 m_metric = metric;
168}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700169
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800170uint16_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700171Face::GetMetric (void) const
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800172{
173 NS_LOG_FUNCTION_NOARGS ();
174 return m_metric;
175}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700176
177/**
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800178 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700179 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700180 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700181 */
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800182
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800183bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700184Face::IsUp (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700185{
186 NS_LOG_FUNCTION_NOARGS ();
187 return m_ifup;
188}
189
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800190void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700191Face::SetUp (bool up/* = true*/)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700192{
193 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800194 m_ifup = up;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700195}
196
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700197bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700198Face::operator== (const Face &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700199{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800200 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800201 "Faces of different nodes should not be compared to each other: " << *this << " == " << face);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700202
203 return (m_id == face.m_id);
204}
205
206bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700207Face::operator< (const Face &face) const
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700208{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800209 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800210 "Faces of different nodes should not be compared to each other: " << *this << " == " << face);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700211
212 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700213}
214
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700215std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700216Face::Print (std::ostream &os) const
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700217{
218 os << "id=" << GetId ();
219 return os;
220}
221
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700222std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700223operator<< (std::ostream& os, const Face &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700224{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700225 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700226 return os;
227}
228
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700229} // namespace ndn
230} // namespace ns3
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700231