blob: d709cfc6d834168d2e3d5a2c8e6666a62a134c59 [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",
Alexander Afanasyev8067ff12012-08-14 11:24:14 -070059 TypeId::ATTR_GET, // allow only getting it.
60 PointerValue (0), // this is not really used, but needed for the compiler... so sad
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070061 MakePointerAccessor (&Face::m_limits),
62 MakePointerChecker<Limits> ())
Alexander Afanasyev98d85e12012-08-09 10:06:05 -070063
Alexander Afanasyevde7601a2012-05-31 12:14:54 -070064 // .AddAttribute ("MetricTagging", "Enable metric tagging (path-stretch calculation)",
65 // BooleanValue (false),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070066 // MakeBooleanAccessor (&Face::m_enableMetricTagging),
Alexander Afanasyevde7601a2012-05-31 12:14:54 -070067 // MakeBooleanChecker ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -070068
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070069 .AddTraceSource ("NdnTx", "Transmitted packet trace",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070070 MakeTraceSourceAccessor (&Face::m_txTrace))
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070071 .AddTraceSource ("NdnRx", "Received packet trace",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070072 MakeTraceSourceAccessor (&Face::m_rxTrace))
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070073 .AddTraceSource ("NdnDrop", "Dropped packet trace",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070074 MakeTraceSourceAccessor (&Face::m_dropTrace))
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080075 ;
76 return tid;
77}
78
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070079/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070080 * By default, Ndn face are created in the "down" state
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070081 * with no IP addresses. Before becoming useable, the user must
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070082 * invoke SetUp on them once an Ndn address and mask have been set.
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070083 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070084Face::Face (Ptr<Node> node)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080085 : m_node (node)
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070086 , m_protocolHandler (MakeNullCallback<void,const Ptr<Face>&,const Ptr<const Packet>&> ())
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070087 , m_ifup (false)
88 , m_id ((uint32_t)-1)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080089 , m_metric (0)
Alexander Afanasyev8067ff12012-08-14 11:24:14 -070090 , m_limits (CreateObject<Limits> ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -070091 // , m_enableMetricTagging (false)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070092{
93 NS_LOG_FUNCTION (this);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080094
95 NS_ASSERT_MSG (node != 0, "node cannot be NULL. Check the code");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070096}
97
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070098Face::~Face ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070099{
100 NS_LOG_FUNCTION_NOARGS ();
101}
102
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700103Face::Face (const Face &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700104{
105}
106
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700107Face& Face::operator= (const Face &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700108{
109 return *this;
110}
111
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800112Ptr<Node>
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700113Face::GetNode () const
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800114{
115 return m_node;
116}
117
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800118void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700119Face::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700120{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800121 NS_LOG_FUNCTION_NOARGS ();
122
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800123 m_protocolHandler = handler;
124}
125
126bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700127Face::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800128{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800129 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyevd459ec32012-04-30 13:58:20 -0700130 NS_LOG_DEBUG (*packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800131
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800132 if (!IsUp ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700133 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700134 m_dropTrace (packet);
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700135 return false;
136 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800137
Alexander Afanasyevde7601a2012-05-31 12:14:54 -0700138 // if (m_enableMetricTagging)
139 // {
140 // // update path information
141 // Ptr<const WeightsPathStretchTag> origTag = packet->RemovePacketTag<WeightsPathStretchTag> ();
142 // Ptr<WeightsPathStretchTag> tag;
143 // if (origTag == 0)
144 // {
145 // tag = CreateObject<WeightsPathStretchTag> (); // create a new tag
146 // }
147 // else
148 // {
149 // tag = CreateObject<WeightsPathStretchTag> (*origTag); // will update existing tag
150 // }
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800151
Alexander Afanasyevde7601a2012-05-31 12:14:54 -0700152 // tag->AddPathInfo (m_node, GetMetric ());
153 // packet->AddPacketTag (tag);
154 // }
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700155
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700156 bool ok = SendImpl (packet);
157 if (ok)
158 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700159 m_txTrace (packet);
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700160 return true;
161 }
162 else
163 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700164 m_dropTrace (packet);
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700165 return false;
166 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800167}
168
169bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700170Face::Receive (const Ptr<const Packet> &packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800171{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800172 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800173
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800174 if (!IsUp ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700175 {
176 // no tracing here. If we were off while receiving, we shouldn't even know that something was there
177 return false;
178 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800179
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700180 m_rxTrace (packet);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800181 m_protocolHandler (this, packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800182
183 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700184}
185
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800186void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700187Face::SetMetric (uint16_t metric)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800188{
189 NS_LOG_FUNCTION (metric);
190 m_metric = metric;
191}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700192
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800193uint16_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700194Face::GetMetric (void) const
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800195{
196 NS_LOG_FUNCTION_NOARGS ();
197 return m_metric;
198}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700199
200/**
Alexander Afanasyev98256102011-08-14 01:00:02 -0700201 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700202 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700203 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700204 */
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800205
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700206bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700207Face::IsUp (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700208{
209 NS_LOG_FUNCTION_NOARGS ();
210 return m_ifup;
211}
212
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700213void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700214Face::SetUp (bool up/* = true*/)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700215{
216 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800217 m_ifup = up;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700218}
219
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700220bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700221Face::operator== (const Face &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700222{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800223 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
224 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700225
226 return (m_id == face.m_id);
227}
228
229bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700230Face::operator< (const Face &face) const
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700231{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800232 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
233 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700234
235 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700236}
237
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700238std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700239Face::Print (std::ostream &os) const
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700240{
241 os << "id=" << GetId ();
242 return os;
243}
244
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700245std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700246operator<< (std::ostream& os, const Face &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700247{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700248 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700249 return os;
250}
251
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700252} // namespace ndn
253} // namespace ns3
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700254