blob: 505c2b4cae12e302aa374d0d43b0e162297717f0 [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 Afanasyev0c395372014-12-20 15:54:02 -080022#include "ndn-face.hpp"
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 Afanasyev0c395372014-12-20 15:54:02 -080035#include "ns3/ndn-header-helper.hpp"
36#include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.hpp"
Alexander Afanasyev7b923f32013-07-16 13:23:04 -070037
38#include "ns3/ndn-wire.h"
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080039
Alexander Afanasyev23d2b542011-12-07 18:54:46 -080040#include <boost/ref.hpp>
41
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080042NS_LOG_COMPONENT_DEFINE("ndn.Face");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070043
44namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070045namespace ndn {
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070046
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080047NS_OBJECT_ENSURE_REGISTERED(Face);
Alexander Afanasyev30c33e32012-06-05 21:28:44 -070048
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080049TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080050Face::GetTypeId()
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080051{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080052 static TypeId tid =
53 TypeId("ns3::ndn::Face")
54 .SetParent<Object>()
55 .SetGroupName("Ndn")
56 .AddAttribute("Id", "Face id (unique integer for the Ndn stack on this node)",
57 TypeId::ATTR_GET, // allow only getting it.
58 UintegerValue(0), MakeUintegerAccessor(&Face::m_id),
59 MakeUintegerChecker<uint32_t>());
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080060 return tid;
61}
62
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -080063/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070064 * By default, Ndn face are created in the "down" state
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -080065 * with no IP addresses. Before becoming useable, the user must
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070066 * invoke SetUp on them once an Ndn address and mask have been set.
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070067 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068Face::Face(Ptr<Node> node)
69 : m_node(node)
70 , m_upstreamInterestHandler(MakeNullCallback<void, Ptr<Face>, Ptr<Interest>>())
71 , m_upstreamDataHandler(MakeNullCallback<void, Ptr<Face>, Ptr<Data>>())
72 , m_ifup(false)
73 , m_id((uint32_t)-1)
74 , m_metric(0)
75 , m_flags(0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070076{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080077 NS_LOG_FUNCTION(this << node);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080078
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079 NS_ASSERT_MSG(node != 0, "node cannot be NULL. Check the code");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070080}
81
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080082Face::~Face()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070083{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080084 NS_LOG_FUNCTION_NOARGS();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070085}
86
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080087Face::Face(const Face&)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070088{
89}
90
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070091Face& Face::operator= (const Face &)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070092{
93 return *this;
94}
95
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080096Ptr<Node>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097Face::GetNode() const
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080098{
99 return m_node;
100}
101
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800102void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103Face::RegisterProtocolHandlers(const InterestHandler& interestHandler,
104 const DataHandler& dataHandler)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700105{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800106 NS_LOG_FUNCTION_NOARGS();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800107
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700108 m_upstreamInterestHandler = interestHandler;
109 m_upstreamDataHandler = dataHandler;
110}
111
112void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800113Face::UnRegisterProtocolHandlers()
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700114{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800115 NS_LOG_FUNCTION_NOARGS();
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700116
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800117 m_upstreamInterestHandler = MakeNullCallback<void, Ptr<Face>, Ptr<Interest>>();
118 m_upstreamDataHandler = MakeNullCallback<void, Ptr<Face>, Ptr<Data>>();
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700119}
120
121bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800122Face::SendInterest(Ptr<const Interest> interest)
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700123{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800124 NS_LOG_FUNCTION(this << boost::cref(*this) << interest->GetName());
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700125
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800126 if (!IsUp()) {
127 return false;
128 }
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700129
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800130 return Send(Wire::FromInterest(interest));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800131}
132
133bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800134Face::SendData(Ptr<const Data> data)
135{
136 NS_LOG_FUNCTION(this << data);
137
138 if (!IsUp()) {
139 return false;
140 }
141
142 return Send(Wire::FromData(data));
143}
144
145bool
146Face::Send(Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800147{
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800148 FwHopCountTag hopCount;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800149 bool tagExists = packet->RemovePacketTag(hopCount);
150 if (tagExists) {
151 hopCount.Increment();
152 packet->AddPacketTag(hopCount);
153 }
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700154
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700155 return true;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800156}
157
158bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800159Face::Receive(Ptr<const Packet> p)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800160{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800161 NS_LOG_FUNCTION(this << p << p->GetSize());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800162
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800163 if (!IsUp()) {
164 // no tracing here. If we were off while receiving, we shouldn't even know that something was
165 // there
166 return false;
167 }
168
169 Ptr<Packet> packet = p->Copy(); // give upper layers a rw copy of the packet
170 try {
171 HeaderHelper::Type type = HeaderHelper::GetNdnHeaderType(packet);
172 switch (type) {
173 case HeaderHelper::INTEREST_NDNSIM:
174 return ReceiveInterest(Wire::ToInterest(packet, Wire::WIRE_FORMAT_NDNSIM));
175 case HeaderHelper::INTEREST_CCNB:
176 return ReceiveInterest(Wire::ToInterest(packet, Wire::WIRE_FORMAT_CCNB));
177 case HeaderHelper::CONTENT_OBJECT_NDNSIM:
178 return ReceiveData(Wire::ToData(packet, Wire::WIRE_FORMAT_NDNSIM));
179 case HeaderHelper::CONTENT_OBJECT_CCNB:
180 return ReceiveData(Wire::ToData(packet, Wire::WIRE_FORMAT_CCNB));
181 default:
182 NS_FATAL_ERROR("Not supported NDN header");
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700183 return false;
184 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800185
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800186 // exception will be thrown if packet is not recognized
187 }
188 catch (UnknownHeaderException) {
189 NS_FATAL_ERROR("Unknown NDN header. Should not happen");
190 return false;
191 }
Alexander Afanasyev37a61242013-07-26 11:19:18 -0700192
193 return false;
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700194}
195
196bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800197Face::ReceiveInterest(Ptr<Interest> interest)
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700198{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800199 if (!IsUp()) {
200 // no tracing here. If we were off while receiving, we shouldn't even know that something was
201 // there
202 return false;
203 }
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700204
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800205 m_upstreamInterestHandler(this, interest);
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700206 return true;
207}
208
209bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800210Face::ReceiveData(Ptr<Data> data)
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700211{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800212 if (!IsUp()) {
213 // no tracing here. If we were off while receiving, we shouldn't even know that something was
214 // there
215 return false;
216 }
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700217
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800218 m_upstreamDataHandler(this, data);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800219 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700220}
221
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800222void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800223Face::SetMetric(uint16_t metric)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800224{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800225 NS_LOG_FUNCTION(metric);
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800226 m_metric = metric;
227}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700228
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800229uint16_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800230Face::GetMetric(void) const
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800231{
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800232 return m_metric;
233}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700234
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700235void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800236Face::SetFlags(uint32_t flags)
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700237{
238 m_flags = flags;
239}
240
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700241bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700242Face::operator== (const Face &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700243{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800244 NS_ASSERT_MSG(m_node->GetId() == face.m_node->GetId(),
245 "Faces of different nodes should not be compared to each other: " << *this << " == "
246 << face);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700247
248 return (m_id == face.m_id);
249}
250
251bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700252Face::operator< (const Face &face) const
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700253{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800254 NS_ASSERT_MSG(m_node->GetId() == face.m_node->GetId(),
255 "Faces of different nodes should not be compared to each other: " << *this << " == "
256 << face);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700257
258 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700259}
260
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700261std::ostream&
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800262Face::Print(std::ostream& os) const
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700263{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800264 os << "id=" << GetId();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700265 return os;
266}
267
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700268std::ostream&
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800269operator<<(std::ostream& os, const Face& face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700270{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800271 face.Print(os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700272 return os;
273}
274
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700275} // namespace ndn
276} // namespace ns3