blob: fef02c8af8539e055be3385fb166e02d30de65f0 [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyev4975f732011-12-20 17:52:19 -08004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyev4975f732011-12-20 17:52:19 -08007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * 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 Afanasyev4975f732011-12-20 17:52:19 -080011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * 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 Afanasyev4975f732011-12-20 17:52:19 -080015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * 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 Afanasyev4975f732011-12-20 17:52:19 -080019
Alexander Afanasyev0c395372014-12-20 15:54:02 -080020#include "ndn-app-helper.hpp"
Alexander Afanasyev4975f732011-12-20 17:52:19 -080021#include "ns3/log.h"
22#include "ns3/string.h"
23#include "ns3/names.h"
Mickey Sweatt89046c12014-11-16 20:32:27 -080024
25#include "apps/ndn-app.hpp"
Alexander Afanasyev6995bb72019-02-23 15:53:26 -050026#include "ndn-stack-helper.hpp"
Alexander Afanasyev4975f732011-12-20 17:52:19 -080027
Alexander Afanasyev89e02c42012-07-27 13:42:28 -070028#ifdef NS3_MPI
29#include "ns3/mpi-interface.h"
30#endif
31
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080032NS_LOG_COMPONENT_DEFINE("ndn.AppHelper");
Alexander Afanasyev4975f732011-12-20 17:52:19 -080033
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070034namespace ns3 {
35namespace ndn {
Alexander Afanasyev4975f732011-12-20 17:52:19 -080036
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080037AppHelper::AppHelper(const std::string& app)
Alexander Afanasyev4975f732011-12-20 17:52:19 -080038{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039 m_factory.SetTypeId(app);
Alexander Afanasyev4975f732011-12-20 17:52:19 -080040}
41
42void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080043AppHelper::SetPrefix(const std::string& prefix)
Alexander Afanasyev4975f732011-12-20 17:52:19 -080044{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080045 m_factory.Set("Prefix", StringValue(prefix));
Alexander Afanasyev4975f732011-12-20 17:52:19 -080046}
47
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080048void
49AppHelper::SetAttribute(std::string name, const AttributeValue& value)
Alexander Afanasyev4975f732011-12-20 17:52:19 -080050{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080051 m_factory.Set(name, value);
Alexander Afanasyev4975f732011-12-20 17:52:19 -080052}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080053
Alexander Afanasyev4975f732011-12-20 17:52:19 -080054ApplicationContainer
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080055AppHelper::Install(Ptr<Node> node)
Alexander Afanasyev4975f732011-12-20 17:52:19 -080056{
Alexander Afanasyev89e02c42012-07-27 13:42:28 -070057 ApplicationContainer apps;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080058 Ptr<Application> app = InstallPriv(node);
Alexander Afanasyev89e02c42012-07-27 13:42:28 -070059 if (app != 0)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080060 apps.Add(app);
61
Alexander Afanasyev89e02c42012-07-27 13:42:28 -070062 return apps;
Alexander Afanasyev4975f732011-12-20 17:52:19 -080063}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080064
Alexander Afanasyev4975f732011-12-20 17:52:19 -080065ApplicationContainer
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080066AppHelper::Install(std::string nodeName)
Alexander Afanasyev4975f732011-12-20 17:52:19 -080067{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068 Ptr<Node> node = Names::Find<Node>(nodeName);
69 return Install(node);
Alexander Afanasyev4975f732011-12-20 17:52:19 -080070}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080071
Alexander Afanasyev4975f732011-12-20 17:52:19 -080072ApplicationContainer
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080073AppHelper::Install(NodeContainer c)
Alexander Afanasyev4975f732011-12-20 17:52:19 -080074{
75 ApplicationContainer apps;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080076 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 Afanasyev4975f732011-12-20 17:52:19 -080082 return apps;
83}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080084
Alexander Afanasyev4975f732011-12-20 17:52:19 -080085Ptr<Application>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086AppHelper::InstallPriv(Ptr<Node> node)
Alexander Afanasyev4975f732011-12-20 17:52:19 -080087{
Alexander Afanasyevdc3c3a32019-02-17 20:17:32 -050088 Ptr<Application> app;
89 Simulator::ScheduleWithContext(node->GetId(), Seconds(0), MakeEvent([=, &app] {
Alexander Afanasyev89e02c42012-07-27 13:42:28 -070090#ifdef NS3_MPI
Alexander Afanasyevdc3c3a32019-02-17 20:17:32 -050091 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 Afanasyev89e02c42012-07-27 13:42:28 -070095#endif
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080096
Alexander Afanasyevdc3c3a32019-02-17 20:17:32 -050097 app = m_factory.Create<Application>();
98 node->AddApplication(app);
99 }));
Alexander Afanasyev6995bb72019-02-23 15:53:26 -0500100 StackHelper::ProcessWarmupEvents();
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800101
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800102 return app;
103}
104
Alexander Afanasyevf19fffb2015-08-21 17:34:23 -0700105////////////////////////////////////////////////////////////////////////////
106
107FactoryCallbackApp::FactoryCallbackApp(const FactoryCallback& factory)
108 : m_factory(factory)
109{
110}
111
112ApplicationContainer
113FactoryCallbackApp::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
122void
123FactoryCallbackApp::StartApplication()
124{
125 m_impl = m_factory();
126}
127
128void
129FactoryCallbackApp::StopApplication()
130{
131 m_impl.reset();
132}
133
134
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700135} // namespace ndn
136} // namespace ns3