blob: f91fc1951cba6d835b31151cd511a3a1690a6a45 [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 Afanasyevde7601a2012-05-31 12:14:54 -070035// #include "ns3/weights-path-stretch-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 Afanasyevde7601a2012-05-31 12:14:54 -070058 // .AddAttribute ("MetricTagging", "Enable metric tagging (path-stretch calculation)",
59 // BooleanValue (false),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070060 // MakeBooleanAccessor (&Face::m_enableMetricTagging),
Alexander Afanasyevde7601a2012-05-31 12:14:54 -070061 // MakeBooleanChecker ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -070062
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070063 .AddTraceSource ("NdnTx", "Transmitted packet trace",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070064 MakeTraceSourceAccessor (&Face::m_txTrace))
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070065 .AddTraceSource ("NdnRx", "Received packet trace",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070066 MakeTraceSourceAccessor (&Face::m_rxTrace))
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070067 .AddTraceSource ("NdnDrop", "Dropped packet trace",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070068 MakeTraceSourceAccessor (&Face::m_dropTrace))
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080069 ;
70 return tid;
71}
72
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070073/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070074 * By default, Ndn face are created in the "down" state
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070075 * with no IP addresses. Before becoming useable, the user must
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070076 * invoke SetUp on them once an Ndn address and mask have been set.
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070077 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070078Face::Face (Ptr<Node> node)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080079 : m_node (node)
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070080 , m_protocolHandler (MakeNullCallback<void,const Ptr<Face>&,const Ptr<const Packet>&> ())
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070081 , m_ifup (false)
82 , m_id ((uint32_t)-1)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080083 , m_metric (0)
Alexander Afanasyev30c33e32012-06-05 21:28:44 -070084 // , m_enableMetricTagging (false)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070085{
86 NS_LOG_FUNCTION (this);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080087
88 NS_ASSERT_MSG (node != 0, "node cannot be NULL. Check the code");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070089}
90
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070091Face::~Face ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070092{
93 NS_LOG_FUNCTION_NOARGS ();
94}
95
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070096Face::Face (const Face &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070097{
98}
99
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700100Face& Face::operator= (const Face &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700101{
102 return *this;
103}
104
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800105Ptr<Node>
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700106Face::GetNode () const
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800107{
108 return m_node;
109}
110
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800111void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700112Face::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700113{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800114 NS_LOG_FUNCTION_NOARGS ();
115
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800116 m_protocolHandler = handler;
117}
118
119bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700120Face::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800121{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800122 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyevd459ec32012-04-30 13:58:20 -0700123 NS_LOG_DEBUG (*packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800124
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800125 if (!IsUp ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700126 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700127 m_dropTrace (packet);
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700128 return false;
129 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800130
Alexander Afanasyevde7601a2012-05-31 12:14:54 -0700131 // if (m_enableMetricTagging)
132 // {
133 // // update path information
134 // Ptr<const WeightsPathStretchTag> origTag = packet->RemovePacketTag<WeightsPathStretchTag> ();
135 // Ptr<WeightsPathStretchTag> tag;
136 // if (origTag == 0)
137 // {
138 // tag = CreateObject<WeightsPathStretchTag> (); // create a new tag
139 // }
140 // else
141 // {
142 // tag = CreateObject<WeightsPathStretchTag> (*origTag); // will update existing tag
143 // }
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800144
Alexander Afanasyevde7601a2012-05-31 12:14:54 -0700145 // tag->AddPathInfo (m_node, GetMetric ());
146 // packet->AddPacketTag (tag);
147 // }
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700148
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700149 bool ok = SendImpl (packet);
150 if (ok)
151 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700152 m_txTrace (packet);
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700153 return true;
154 }
155 else
156 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700157 m_dropTrace (packet);
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700158 return false;
159 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800160}
161
162bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700163Face::Receive (const Ptr<const Packet> &packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800164{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800165 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->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 Afanasyev2b4c9472012-08-09 15:00:38 -0700173 m_rxTrace (packet);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800174 m_protocolHandler (this, packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800175
176 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700177}
178
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800179void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700180Face::SetMetric (uint16_t metric)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800181{
182 NS_LOG_FUNCTION (metric);
183 m_metric = metric;
184}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700185
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800186uint16_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700187Face::GetMetric (void) const
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800188{
189 NS_LOG_FUNCTION_NOARGS ();
190 return m_metric;
191}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700192
193/**
Alexander Afanasyev98256102011-08-14 01:00:02 -0700194 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700195 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700196 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700197 */
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800198
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700199bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700200Face::IsUp (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700201{
202 NS_LOG_FUNCTION_NOARGS ();
203 return m_ifup;
204}
205
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700206void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700207Face::SetUp (bool up/* = true*/)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700208{
209 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800210 m_ifup = up;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700211}
212
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700213bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700214Face::operator== (const Face &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700215{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800216 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
217 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700218
219 return (m_id == face.m_id);
220}
221
222bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700223Face::operator< (const Face &face) const
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700224{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800225 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
226 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700227
228 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700229}
230
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700231std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700232Face::Print (std::ostream &os) const
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700233{
234 os << "id=" << GetId ();
235 return os;
236}
237
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700238std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700239operator<< (std::ostream& os, const Face &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700240{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700241 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700242 return os;
243}
244
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700245} // namespace ndn
246} // namespace ns3
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700247