blob: a1a3292278293226235e41e14c2c27ca4c4ebe7b [file] [log] [blame]
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev45b92d42011-08-14 23:11:38 -07002/*
3 * Copyright (c) 2011 UCLA
4 *
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 *
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -070018 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyev122f3782013-02-02 00:04:40 -080019 * Ilya Moiseenko <iliamo@cs.ucla.edu>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070020 */
21
Alexander Afanasyev0c395372014-12-20 15:54:02 -080022#include "ndn-stack-helper.hpp"
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080023
24#include "ns3/log.h"
25#include "ns3/names.h"
26#include "ns3/point-to-point-net-device.h"
27
28#include "model/ndn-l3-protocol.hpp"
29#include "model/ndn-net-device-face.hpp"
30#include "utils/ndn-time.hpp"
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080031#include "utils/dummy-keychain.hpp"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070032
33#include <limits>
34#include <map>
Alexander Afanasyevb7626842012-01-12 13:43:33 -080035#include <boost/lexical_cast.hpp>
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080036
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080037NS_LOG_COMPONENT_DEFINE("ndn.StackHelper");
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070038
39namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070040namespace ndn {
Alexander Afanasyev122f3782013-02-02 00:04:40 -080041
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080042StackHelper::StackHelper()
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070043{
Spyridon Mastorakis86edf6f2014-11-14 19:27:18 -080044 setCustomNdnCxxClocks();
45
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080046 m_ndnFactory.SetTypeId("ns3::ndn::L3Protocol");
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080047 // m_contentStoreFactory.SetTypeId("ns3::ndn::cs::Lru");
Alexander Afanasyev122f3782013-02-02 00:04:40 -080048
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080049 m_netDeviceCallbacks.push_back(
50 std::make_pair(PointToPointNetDevice::GetTypeId(),
51 MakeCallback(&StackHelper::PointToPointNetDeviceCallback, this)));
Alexander Afanasyev122f3782013-02-02 00:04:40 -080052 // default callback will be fired if non of others callbacks fit or did the job
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -070053}
Alexander Afanasyev122f3782013-02-02 00:04:40 -080054
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080055StackHelper::~StackHelper()
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070056{
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070057}
58
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080059KeyChain&
60StackHelper::getKeyChain()
61{
62 static ::ndn::DummyKeyChain keyChain;
63 return keyChain;
64}
65
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070066void
Spyridon Mastorakis86edf6f2014-11-14 19:27:18 -080067StackHelper::setCustomNdnCxxClocks()
68{
69 ::ndn::time::setCustomClocks(make_shared<ns3::ndn::time::CustomSteadyClock>(),
70 make_shared<ns3::ndn::time::CustomSystemClock>());
71}
72
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070073Ptr<FaceContainer>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080074StackHelper::Install(const NodeContainer& c) const
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070075{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080076 Ptr<FaceContainer> faces = Create<FaceContainer>();
77 for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i) {
78 faces->AddAll(Install(*i));
79 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070080 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070081}
82
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070083Ptr<FaceContainer>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080084StackHelper::InstallAll() const
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070085{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086 return Install(NodeContainer::GetGlobal());
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070087}
88
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070089Ptr<FaceContainer>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090StackHelper::Install(Ptr<Node> node) const
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070091{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080092 Ptr<FaceContainer> faces = Create<FaceContainer>();
Alexander Afanasyev122f3782013-02-02 00:04:40 -080093
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080094 if (node->GetObject<L3Protocol>() != 0) {
95 NS_FATAL_ERROR("StackHelper::Install (): Installing "
96 "a NdnStack to a node with an existing Ndn object");
97 return 0;
98 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070099
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100 Ptr<L3Protocol> ndn = m_ndnFactory.Create<L3Protocol>();
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700101 // Aggregate L3Protocol on node
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800102 node->AggregateObject(ndn);
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800103
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800104 // NFD initialization
105 ndn->initialize();
106
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800107 for (uint32_t index = 0; index < node->GetNDevices(); index++) {
108 Ptr<NetDevice> device = node->GetDevice(index);
109 // This check does not make sense: LoopbackNetDevice is installed only if IP stack is installed,
110 // Normally, ndnSIM works without IP stack, so no reason to check
111 // if (DynamicCast<LoopbackNetDevice> (device) != 0)
112 // continue; // don't create face for a LoopbackNetDevice
Alexander Afanasyev11453142011-11-25 16:13:33 -0800113
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700114 shared_ptr<NetDeviceFace> face;
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800115
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800116 for (const auto& item : m_netDeviceCallbacks) {
117 if (device->GetInstanceTypeId() == item.first ||
118 device->GetInstanceTypeId().IsChildOf(item.first)) {
119 face = item.second(node, ndn, device);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800120 if (face != 0)
121 break;
122 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700123 }
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800124
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800125 if (face == 0) {
126 face = DefaultNetDeviceCallback(node, ndn, device);
127 }
128
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800129 faces->Add(face);
130 }
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800131
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700132 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700133}
134
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800135void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800136StackHelper::AddNetDeviceFaceCreateCallback(TypeId netDeviceType,
137 StackHelper::NetDeviceFaceCreateCallback callback)
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800138{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800139 m_netDeviceCallbacks.push_back(std::make_pair(netDeviceType, callback));
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800140}
141
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700142void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800143StackHelper::UpdateNetDeviceFaceCreateCallback(TypeId netDeviceType,
144 NetDeviceFaceCreateCallback callback)
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700145{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800146 for (auto& i : m_netDeviceCallbacks) {
147 if (i.first == netDeviceType) {
148 i.second = callback;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800149 return;
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700150 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800151 }
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700152}
153
154void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800155StackHelper::RemoveNetDeviceFaceCreateCallback(TypeId netDeviceType,
156 NetDeviceFaceCreateCallback callback)
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700157{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800158 m_netDeviceCallbacks.remove_if([&] (const std::pair<TypeId, NetDeviceFaceCreateCallback>& i) {
159 return (i.first == netDeviceType);
160 });
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700161}
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800162
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700163shared_ptr<NetDeviceFace>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800164StackHelper::DefaultNetDeviceCallback(Ptr<Node> node, Ptr<L3Protocol> ndn,
165 Ptr<NetDevice> netDevice) const
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800166{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800167 NS_LOG_DEBUG("Creating default NetDeviceFace on node " << node->GetId());
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800168
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800169 shared_ptr<NetDeviceFace> face = std::make_shared<NetDeviceFace>(node, netDevice);
Alexander Afanasyevc17e4bd2013-02-17 14:31:56 -0800170
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800171 ndn->addFace(face);
172 NS_LOG_LOGIC("Node " << node->GetId() << ": added NetDeviceFace as face #"
173 << face->getLocalUri());
Alexander Afanasyevc17e4bd2013-02-17 14:31:56 -0800174
175 return face;
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800176}
177
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700178shared_ptr<NetDeviceFace>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800179StackHelper::PointToPointNetDeviceCallback(Ptr<Node> node, Ptr<L3Protocol> ndn,
180 Ptr<NetDevice> device) const
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800181{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800182 NS_LOG_DEBUG("Creating point-to-point NetDeviceFace on node " << node->GetId());
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800183
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800184 shared_ptr<NetDeviceFace> face = std::make_shared<NetDeviceFace>(node, device);
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800185
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800186 ndn->addFace(face);
187 NS_LOG_LOGIC("Node " << node->GetId() << ": added NetDeviceFace as face #"
188 << face->getLocalUri());
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800189
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800190 return face;
191}
192
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700193Ptr<FaceContainer>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800194StackHelper::Install(const std::string& nodeName) const
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700195{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800196 Ptr<Node> node = Names::Find<Node>(nodeName);
197 return Install(node);
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700198}
199
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700200} // namespace ndn
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700201} // namespace ns3