blob: 9b5bc89b92a45f728e1abc588aacbf8f6a965675 [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 Afanasyevea9b3e62012-08-13 19:02:54 -070058 .AddAttribute ("Limits", "Limits object",
59 PointerValue (CreateObject<Limits> ()),
60 MakePointerAccessor (&Face::m_limits),
61 MakePointerChecker<Limits> ())
Alexander Afanasyev98d85e12012-08-09 10:06:05 -070062
Alexander Afanasyevde7601a2012-05-31 12:14:54 -070063 // .AddAttribute ("MetricTagging", "Enable metric tagging (path-stretch calculation)",
64 // BooleanValue (false),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070065 // MakeBooleanAccessor (&Face::m_enableMetricTagging),
Alexander Afanasyevde7601a2012-05-31 12:14:54 -070066 // MakeBooleanChecker ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -070067
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070068 .AddTraceSource ("NdnTx", "Transmitted packet trace",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070069 MakeTraceSourceAccessor (&Face::m_txTrace))
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070070 .AddTraceSource ("NdnRx", "Received packet trace",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070071 MakeTraceSourceAccessor (&Face::m_rxTrace))
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070072 .AddTraceSource ("NdnDrop", "Dropped packet trace",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070073 MakeTraceSourceAccessor (&Face::m_dropTrace))
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080074 ;
75 return tid;
76}
77
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070078/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070079 * By default, Ndn face are created in the "down" state
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070080 * with no IP addresses. Before becoming useable, the user must
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070081 * invoke SetUp on them once an Ndn address and mask have been set.
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070082 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070083Face::Face (Ptr<Node> node)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080084 : m_node (node)
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070085 , m_protocolHandler (MakeNullCallback<void,const Ptr<Face>&,const Ptr<const Packet>&> ())
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070086 , m_ifup (false)
87 , m_id ((uint32_t)-1)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080088 , m_metric (0)
Alexander Afanasyev30c33e32012-06-05 21:28:44 -070089 // , m_enableMetricTagging (false)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070090{
91 NS_LOG_FUNCTION (this);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080092
93 NS_ASSERT_MSG (node != 0, "node cannot be NULL. Check the code");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070094}
95
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070096Face::~Face ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070097{
98 NS_LOG_FUNCTION_NOARGS ();
99}
100
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700101Face::Face (const Face &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700102{
103}
104
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700105Face& Face::operator= (const Face &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700106{
107 return *this;
108}
109
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800110Ptr<Node>
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700111Face::GetNode () const
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800112{
113 return m_node;
114}
115
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800116void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700117Face::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700118{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800119 NS_LOG_FUNCTION_NOARGS ();
120
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800121 m_protocolHandler = handler;
122}
123
124bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700125Face::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800126{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800127 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyevd459ec32012-04-30 13:58:20 -0700128 NS_LOG_DEBUG (*packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800129
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800130 if (!IsUp ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700131 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700132 m_dropTrace (packet);
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700133 return false;
134 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800135
Alexander Afanasyevde7601a2012-05-31 12:14:54 -0700136 // if (m_enableMetricTagging)
137 // {
138 // // update path information
139 // Ptr<const WeightsPathStretchTag> origTag = packet->RemovePacketTag<WeightsPathStretchTag> ();
140 // Ptr<WeightsPathStretchTag> tag;
141 // if (origTag == 0)
142 // {
143 // tag = CreateObject<WeightsPathStretchTag> (); // create a new tag
144 // }
145 // else
146 // {
147 // tag = CreateObject<WeightsPathStretchTag> (*origTag); // will update existing tag
148 // }
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800149
Alexander Afanasyevde7601a2012-05-31 12:14:54 -0700150 // tag->AddPathInfo (m_node, GetMetric ());
151 // packet->AddPacketTag (tag);
152 // }
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700153
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700154 bool ok = SendImpl (packet);
155 if (ok)
156 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700157 m_txTrace (packet);
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700158 return true;
159 }
160 else
161 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700162 m_dropTrace (packet);
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700163 return false;
164 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800165}
166
167bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700168Face::Receive (const Ptr<const Packet> &packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800169{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800170 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800171
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800172 if (!IsUp ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700173 {
174 // no tracing here. If we were off while receiving, we shouldn't even know that something was there
175 return false;
176 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800177
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700178 m_rxTrace (packet);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800179 m_protocolHandler (this, packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800180
181 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700182}
183
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800184void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700185Face::SetMetric (uint16_t metric)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800186{
187 NS_LOG_FUNCTION (metric);
188 m_metric = metric;
189}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700190
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800191uint16_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700192Face::GetMetric (void) const
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800193{
194 NS_LOG_FUNCTION_NOARGS ();
195 return m_metric;
196}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700197
198/**
Alexander Afanasyev98256102011-08-14 01:00:02 -0700199 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700200 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700201 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700202 */
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800203
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700204bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700205Face::IsUp (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700206{
207 NS_LOG_FUNCTION_NOARGS ();
208 return m_ifup;
209}
210
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700211void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700212Face::SetUp (bool up/* = true*/)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700213{
214 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800215 m_ifup = up;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700216}
217
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700218bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700219Face::operator== (const Face &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700220{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800221 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
222 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700223
224 return (m_id == face.m_id);
225}
226
227bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700228Face::operator< (const Face &face) const
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700229{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800230 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
231 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700232
233 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700234}
235
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700236std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700237Face::Print (std::ostream &os) const
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700238{
239 os << "id=" << GetId ();
240 return os;
241}
242
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700243std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700244operator<< (std::ostream& os, const Face &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700245{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700246 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700247 return os;
248}
249
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700250} // namespace ndn
251} // namespace ns3
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700252