blob: b5f7c340ec6587b077ad6deb266d24c0805e7705 [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> ())
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080057 ;
58 return tid;
59}
60
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -080061/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070062 * By default, Ndn face are created in the "down" state
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -080063 * with no IP addresses. Before becoming useable, the user must
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070064 * invoke SetUp on them once an Ndn address and mask have been set.
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070065 */
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -080066Face::Face (Ptr<Node> node)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080067 : m_node (node)
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070068 , m_protocolHandler (MakeNullCallback<void,const Ptr<Face>&,const Ptr<const Packet>&> ())
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070069 , m_ifup (false)
70 , m_id ((uint32_t)-1)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080071 , m_metric (0)
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -070072 , m_flags (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070073{
74 NS_LOG_FUNCTION (this);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080075
76 NS_ASSERT_MSG (node != 0, "node cannot be NULL. Check the code");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070077}
78
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070079Face::~Face ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070080{
81 NS_LOG_FUNCTION_NOARGS ();
82}
83
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070084Face::Face (const Face &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070085{
86}
87
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070088Face& Face::operator= (const Face &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070089{
90 return *this;
91}
92
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080093Ptr<Node>
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070094Face::GetNode () const
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080095{
96 return m_node;
97}
98
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080099void
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700100Face::RegisterProtocolHandlers (const InterestHandler &interestHandler, const DataHandler &dataHandler)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700101{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800102 NS_LOG_FUNCTION_NOARGS ();
103
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700104 m_upstreamInterestHandler = interestHandler;
105 m_upstreamDataHandler = dataHandler;
106}
107
108void
109Face::UnRegisterProtocolHandlers ()
110{
111 NS_LOG_FUNCTION_NOARGS ();
112
113 m_upstreamInterestHandler = MakeNullCallback< void, const Ptr<Face>&, Ptr<Interest>, Ptr<Packet> > ();
114 m_upstreamDataHandler = MakeNullCallback< void, const Ptr<Face>&, Ptr<ContentObject>, Ptr<Packet> > ();
115}
116
117
118bool
119Face::SendInterest (Ptr<const Interest> interest, Ptr<const Packet> packet)
120{
121 NS_LOG_FUNCTION (this << interest << packet);
122
123 if (!IsUp ())
124 {
125 return false;
126 }
127
128 Ptr<Packet> copy = packet->Copy ();
129 copy->AddHeader (*interest);
130
131 return Send (copy);
132}
133
134bool
135Face::SendData (Ptr<const ContentObject> data, Ptr<const Packet> packet)
136{
137 NS_LOG_FUNCTION (this << data << packet);
138
139 if (!IsUp ())
140 {
141 return false;
142 }
143
144 Ptr<Packet> copy = packet->Copy ();
145 copy->AddHeader (*data);
146
147 return Send (copy);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800148}
149
150bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700151Face::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800152{
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800153 FwHopCountTag hopCount;
154 bool tagExists = packet->RemovePacketTag (hopCount);
155 if (tagExists)
156 {
157 hopCount.Increment ();
158 packet->AddPacketTag (hopCount);
159 }
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700160
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700161 return true;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800162}
163
164bool
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700165Face::Receive (Ptr<const Packet> p)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800166{
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700167 NS_LOG_FUNCTION (this << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800168
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800169 if (!IsUp ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700170 {
171 // no tracing here. If we were off while receiving, we shouldn't even know that something was there
172 return false;
173 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800174
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700175 Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
176 try
177 {
178 HeaderHelper::Type type = HeaderHelper::GetNdnHeaderType (packet);
179 switch (type)
180 {
181 case HeaderHelper::INTEREST_NDNSIM:
182 {
183 Ptr<Interest> interest = Create<Interest> ();
184 packet->RemoveHeader (*header);
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800185
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700186 return ReceiveInterest (interest, packet/*payload*/);
187 }
188 case HeaderHelper::CONTENT_OBJECT_NDNSIM:
189 {
190 Ptr<ContentObject> data = Create<ContentObject> ();
191 packet->RemoveHeader (*header);
192
193 return ReceiveData (data, packet);
194 }
195 case HeaderHelper::INTEREST_CCNB:
196 case HeaderHelper::CONTENT_OBJECT_CCNB:
197 NS_FATAL_ERROR ("ccnb support is broken in this implementation");
198 return false;
199 }
200
201 // exception will be thrown if packet is not recognized
202 }
203 catch (UnknownHeaderException)
204 {
205 NS_ASSERT_MSG (false, "Unknown NDN header. Should not happen");
206 NS_LOG_ERROR ("Unknown NDN header. Should not happen");
207 return false;
208 }
209}
210
211bool
212Face::ReceiveInterest (Ptr<Interest> interest, Ptr<Packet> payload)
213{
214 if (!IsUp ())
215 {
216 // no tracing here. If we were off while receiving, we shouldn't even know that something was there
217 return false;
218 }
219
220 m_upstreamInterestHandler (this, interest, payload);
221 return true;
222}
223
224bool
225Face::ReceiveData (Ptr<ContentObject> data, Ptr<Packet> payload)
226{
227 if (!IsUp ())
228 {
229 // no tracing here. If we were off while receiving, we shouldn't even know that something was there
230 return false;
231 }
232
233 m_upstreamDataHandler (this, data, payload);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800234 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700235}
236
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800237void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700238Face::SetMetric (uint16_t metric)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800239{
240 NS_LOG_FUNCTION (metric);
241 m_metric = metric;
242}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700243
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800244uint16_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700245Face::GetMetric (void) const
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800246{
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800247 return m_metric;
248}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700249
250/**
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800251 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700252 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700253 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700254 */
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800255
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700256void
257Face::SetFlags (uint32_t flags)
258{
259 m_flags = flags;
260}
261
262
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700263bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700264Face::operator== (const Face &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700265{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800266 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800267 "Faces of different nodes should not be compared to each other: " << *this << " == " << face);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700268
269 return (m_id == face.m_id);
270}
271
272bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700273Face::operator< (const Face &face) const
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700274{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800275 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800276 "Faces of different nodes should not be compared to each other: " << *this << " == " << face);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700277
278 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700279}
280
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700281std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700282Face::Print (std::ostream &os) const
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700283{
284 os << "id=" << GetId ();
285 return os;
286}
287
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700288std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700289operator<< (std::ostream& os, const Face &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700290{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700291 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700292 return os;
293}
294
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700295} // namespace ndn
296} // namespace ns3
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700297