blob: 5b720441892290c3892bc0231006f8ea0077f120 [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 Afanasyevb989b122013-07-10 17:15:46 -070036#include "ns3/ndnSIM/model/wire/ndnsim.h"
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080037
Alexander Afanasyev23d2b542011-12-07 18:54:46 -080038#include <boost/ref.hpp>
39
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070040NS_LOG_COMPONENT_DEFINE ("ndn.Face");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070041
42namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070043namespace ndn {
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070044
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070045NS_OBJECT_ENSURE_REGISTERED (Face);
Alexander Afanasyev30c33e32012-06-05 21:28:44 -070046
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080047TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070048Face::GetTypeId ()
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080049{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070050 static TypeId tid = TypeId ("ns3::ndn::Face")
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080051 .SetParent<Object> ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070052 .SetGroupName ("Ndn")
53 .AddAttribute ("Id", "Face id (unique integer for the Ndn stack on this node)",
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080054 TypeId::ATTR_GET, // allow only getting it.
55 UintegerValue (0),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070056 MakeUintegerAccessor (&Face::m_id),
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080057 MakeUintegerChecker<uint32_t> ())
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080058 ;
59 return tid;
60}
61
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -080062/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070063 * By default, Ndn face are created in the "down" state
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -080064 * with no IP addresses. Before becoming useable, the user must
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070065 * invoke SetUp on them once an Ndn address and mask have been set.
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070066 */
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -080067Face::Face (Ptr<Node> node)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080068 : m_node (node)
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070069 , m_protocolHandler (MakeNullCallback<void,const Ptr<Face>&,const Ptr<const Packet>&> ())
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070070 , m_ifup (false)
71 , m_id ((uint32_t)-1)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080072 , m_metric (0)
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -070073 , m_flags (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070074{
75 NS_LOG_FUNCTION (this);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080076
77 NS_ASSERT_MSG (node != 0, "node cannot be NULL. Check the code");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070078}
79
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070080Face::~Face ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070081{
82 NS_LOG_FUNCTION_NOARGS ();
83}
84
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070085Face::Face (const Face &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070086{
87}
88
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070089Face& Face::operator= (const Face &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070090{
91 return *this;
92}
93
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080094Ptr<Node>
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070095Face::GetNode () const
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080096{
97 return m_node;
98}
99
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800100void
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700101Face::RegisterProtocolHandlers (const InterestHandler &interestHandler, const DataHandler &dataHandler)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700102{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800103 NS_LOG_FUNCTION_NOARGS ();
104
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700105 m_upstreamInterestHandler = interestHandler;
106 m_upstreamDataHandler = dataHandler;
107}
108
109void
110Face::UnRegisterProtocolHandlers ()
111{
112 NS_LOG_FUNCTION_NOARGS ();
113
114 m_upstreamInterestHandler = MakeNullCallback< void, const Ptr<Face>&, Ptr<Interest>, Ptr<Packet> > ();
115 m_upstreamDataHandler = MakeNullCallback< void, const Ptr<Face>&, Ptr<ContentObject>, Ptr<Packet> > ();
116}
117
118
119bool
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700120Face::SendInterest (Ptr<const Interest> interest)
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700121{
122 NS_LOG_FUNCTION (this << interest << packet);
123
124 if (!IsUp ())
125 {
126 return false;
127 }
128
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700129 Ptr<Packet> packet = wire::ndnSIM::Interest::ToWire (interest);
130 return Send (packet);
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700131}
132
133bool
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700134Face::SendData (Ptr<const ContentObject> data)
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700135{
136 NS_LOG_FUNCTION (this << data << packet);
137
138 if (!IsUp ())
139 {
140 return false;
141 }
142
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700143 Ptr<Packet> packet = wire::ndnSIM::Data::ToWire (data);
144 return Send (packet);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800145}
146
147bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700148Face::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800149{
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800150 FwHopCountTag hopCount;
151 bool tagExists = packet->RemovePacketTag (hopCount);
152 if (tagExists)
153 {
154 hopCount.Increment ();
155 packet->AddPacketTag (hopCount);
156 }
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700157
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700158 return true;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800159}
160
161bool
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700162Face::Receive (Ptr<const Packet> p)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800163{
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700164 NS_LOG_FUNCTION (this << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800165
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800166 if (!IsUp ())
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700167 {
168 // no tracing here. If we were off while receiving, we shouldn't even know that something was there
169 return false;
170 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800171
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700172 Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
173 try
174 {
175 HeaderHelper::Type type = HeaderHelper::GetNdnHeaderType (packet);
176 switch (type)
177 {
178 case HeaderHelper::INTEREST_NDNSIM:
179 {
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700180 Ptr<Interest> interest = wire::ndnSIM::Interest::FromWire (packet);
181 return ReceiveInterest (interest);
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700182 }
183 case HeaderHelper::CONTENT_OBJECT_NDNSIM:
184 {
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700185 Ptr<ContentObject> data = wire::ndnSIM::Data::FromWire (packet);
186 return ReceiveData (data);
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700187 }
188 case HeaderHelper::INTEREST_CCNB:
189 case HeaderHelper::CONTENT_OBJECT_CCNB:
190 NS_FATAL_ERROR ("ccnb support is broken in this implementation");
191 return false;
192 }
193
194 // exception will be thrown if packet is not recognized
195 }
196 catch (UnknownHeaderException)
197 {
198 NS_ASSERT_MSG (false, "Unknown NDN header. Should not happen");
199 NS_LOG_ERROR ("Unknown NDN header. Should not happen");
200 return false;
201 }
202}
203
204bool
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700205Face::ReceiveInterest (Ptr<Interest> interest)
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700206{
207 if (!IsUp ())
208 {
209 // no tracing here. If we were off while receiving, we shouldn't even know that something was there
210 return false;
211 }
212
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700213 m_upstreamInterestHandler (this, interest);
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700214 return true;
215}
216
217bool
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700218Face::ReceiveData (Ptr<ContentObject> data)
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700219{
220 if (!IsUp ())
221 {
222 // no tracing here. If we were off while receiving, we shouldn't even know that something was there
223 return false;
224 }
225
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700226 m_upstreamDataHandler (this, data);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800227 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700228}
229
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800230void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700231Face::SetMetric (uint16_t metric)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800232{
233 NS_LOG_FUNCTION (metric);
234 m_metric = metric;
235}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700236
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800237uint16_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700238Face::GetMetric (void) const
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800239{
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800240 return m_metric;
241}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700242
243/**
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800244 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700245 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700246 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700247 */
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800248
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700249void
250Face::SetFlags (uint32_t flags)
251{
252 m_flags = flags;
253}
254
255
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700256bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700257Face::operator== (const Face &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700258{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800259 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800260 "Faces of different nodes should not be compared to each other: " << *this << " == " << face);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700261
262 return (m_id == face.m_id);
263}
264
265bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700266Face::operator< (const Face &face) const
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700267{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800268 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800269 "Faces of different nodes should not be compared to each other: " << *this << " == " << face);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700270
271 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700272}
273
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700274std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700275Face::Print (std::ostream &os) const
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700276{
277 os << "id=" << GetId ();
278 return os;
279}
280
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700281std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700282operator<< (std::ostream& os, const Face &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700283{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700284 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700285 return os;
286}
287
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700288} // namespace ndn
289} // namespace ns3
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700290