Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 8 | * ndnSIM is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 12 | * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 19 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 20 | #include "ndn-app-helper.hpp" |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 21 | #include "ns3/log.h" |
| 22 | #include "ns3/string.h" |
| 23 | #include "ns3/names.h" |
Mickey Sweatt | 89046c1 | 2014-11-16 20:32:27 -0800 | [diff] [blame] | 24 | |
| 25 | #include "apps/ndn-app.hpp" |
Alexander Afanasyev | 6995bb7 | 2019-02-23 15:53:26 -0500 | [diff] [blame] | 26 | #include "ndn-stack-helper.hpp" |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | 89e02c4 | 2012-07-27 13:42:28 -0700 | [diff] [blame] | 28 | #ifdef NS3_MPI |
| 29 | #include "ns3/mpi-interface.h" |
| 30 | #endif |
| 31 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 32 | NS_LOG_COMPONENT_DEFINE("ndn.AppHelper"); |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 33 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 34 | namespace ns3 { |
| 35 | namespace ndn { |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 37 | AppHelper::AppHelper(const std::string& app) |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 38 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 39 | m_factory.SetTypeId(app); |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 43 | AppHelper::SetPrefix(const std::string& prefix) |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 44 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 45 | m_factory.Set("Prefix", StringValue(prefix)); |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 48 | void |
| 49 | AppHelper::SetAttribute(std::string name, const AttributeValue& value) |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 50 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 51 | m_factory.Set(name, value); |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 52 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 53 | |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 54 | ApplicationContainer |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 55 | AppHelper::Install(Ptr<Node> node) |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 56 | { |
Alexander Afanasyev | 89e02c4 | 2012-07-27 13:42:28 -0700 | [diff] [blame] | 57 | ApplicationContainer apps; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 58 | Ptr<Application> app = InstallPriv(node); |
Alexander Afanasyev | 89e02c4 | 2012-07-27 13:42:28 -0700 | [diff] [blame] | 59 | if (app != 0) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 60 | apps.Add(app); |
| 61 | |
Alexander Afanasyev | 89e02c4 | 2012-07-27 13:42:28 -0700 | [diff] [blame] | 62 | return apps; |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 63 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 64 | |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 65 | ApplicationContainer |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 66 | AppHelper::Install(std::string nodeName) |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 67 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 68 | Ptr<Node> node = Names::Find<Node>(nodeName); |
| 69 | return Install(node); |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 70 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 71 | |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 72 | ApplicationContainer |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 73 | AppHelper::Install(NodeContainer c) |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 74 | { |
| 75 | ApplicationContainer apps; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 76 | for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i) { |
| 77 | Ptr<Application> app = InstallPriv(*i); |
| 78 | if (app != 0) |
| 79 | apps.Add(app); |
| 80 | } |
| 81 | |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 82 | return apps; |
| 83 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 84 | |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 85 | Ptr<Application> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 86 | AppHelper::InstallPriv(Ptr<Node> node) |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 87 | { |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 88 | Ptr<Application> app; |
| 89 | Simulator::ScheduleWithContext(node->GetId(), Seconds(0), MakeEvent([=, &app] { |
Alexander Afanasyev | 89e02c4 | 2012-07-27 13:42:28 -0700 | [diff] [blame] | 90 | #ifdef NS3_MPI |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 91 | if (MpiInterface::IsEnabled() && node->GetSystemId() != MpiInterface::GetSystemId()) { |
| 92 | // don't create an app if MPI is enabled and node is not in the correct partition |
| 93 | return 0; |
| 94 | } |
Alexander Afanasyev | 89e02c4 | 2012-07-27 13:42:28 -0700 | [diff] [blame] | 95 | #endif |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 96 | |
Alexander Afanasyev | dc3c3a3 | 2019-02-17 20:17:32 -0500 | [diff] [blame] | 97 | app = m_factory.Create<Application>(); |
| 98 | node->AddApplication(app); |
| 99 | })); |
Alexander Afanasyev | 6995bb7 | 2019-02-23 15:53:26 -0500 | [diff] [blame] | 100 | StackHelper::ProcessWarmupEvents(); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 101 | |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 102 | return app; |
| 103 | } |
| 104 | |
Alexander Afanasyev | f19fffb | 2015-08-21 17:34:23 -0700 | [diff] [blame] | 105 | //////////////////////////////////////////////////////////////////////////// |
| 106 | |
| 107 | FactoryCallbackApp::FactoryCallbackApp(const FactoryCallback& factory) |
| 108 | : m_factory(factory) |
| 109 | { |
| 110 | } |
| 111 | |
| 112 | ApplicationContainer |
| 113 | FactoryCallbackApp::Install(Ptr<Node> node, const FactoryCallback& factory) |
| 114 | { |
| 115 | ApplicationContainer apps; |
| 116 | auto app = CreateObject<FactoryCallbackApp>(factory); |
| 117 | node->AddApplication(app); |
| 118 | apps.Add(app); |
| 119 | return apps; |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | FactoryCallbackApp::StartApplication() |
| 124 | { |
| 125 | m_impl = m_factory(); |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | FactoryCallbackApp::StopApplication() |
| 130 | { |
| 131 | m_impl.reset(); |
| 132 | } |
| 133 | |
| 134 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 135 | } // namespace ndn |
| 136 | } // namespace ns3 |