blob: 25566597dadf443b1465325392682ad3d0f73d87 [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 Afanasyev4975f732011-12-20 17:52:19 -080026
Alexander Afanasyev89e02c42012-07-27 13:42:28 -070027#ifdef NS3_MPI
28#include "ns3/mpi-interface.h"
29#endif
30
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080031NS_LOG_COMPONENT_DEFINE("ndn.AppHelper");
Alexander Afanasyev4975f732011-12-20 17:52:19 -080032
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070033namespace ns3 {
34namespace ndn {
Alexander Afanasyev4975f732011-12-20 17:52:19 -080035
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080036AppHelper::AppHelper(const std::string& app)
Alexander Afanasyev4975f732011-12-20 17:52:19 -080037{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080038 m_factory.SetTypeId(app);
Alexander Afanasyev4975f732011-12-20 17:52:19 -080039}
40
41void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080042AppHelper::SetPrefix(const std::string& prefix)
Alexander Afanasyev4975f732011-12-20 17:52:19 -080043{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080044 m_factory.Set("Prefix", StringValue(prefix));
Alexander Afanasyev4975f732011-12-20 17:52:19 -080045}
46
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080047void
48AppHelper::SetAttribute(std::string name, const AttributeValue& value)
Alexander Afanasyev4975f732011-12-20 17:52:19 -080049{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080050 m_factory.Set(name, value);
Alexander Afanasyev4975f732011-12-20 17:52:19 -080051}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080052
Alexander Afanasyev4975f732011-12-20 17:52:19 -080053ApplicationContainer
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080054AppHelper::Install(Ptr<Node> node)
Alexander Afanasyev4975f732011-12-20 17:52:19 -080055{
Alexander Afanasyev89e02c42012-07-27 13:42:28 -070056 ApplicationContainer apps;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057 Ptr<Application> app = InstallPriv(node);
Alexander Afanasyev89e02c42012-07-27 13:42:28 -070058 if (app != 0)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080059 apps.Add(app);
60
Alexander Afanasyev89e02c42012-07-27 13:42:28 -070061 return apps;
Alexander Afanasyev4975f732011-12-20 17:52:19 -080062}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080063
Alexander Afanasyev4975f732011-12-20 17:52:19 -080064ApplicationContainer
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080065AppHelper::Install(std::string nodeName)
Alexander Afanasyev4975f732011-12-20 17:52:19 -080066{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080067 Ptr<Node> node = Names::Find<Node>(nodeName);
68 return Install(node);
Alexander Afanasyev4975f732011-12-20 17:52:19 -080069}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080070
Alexander Afanasyev4975f732011-12-20 17:52:19 -080071ApplicationContainer
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080072AppHelper::Install(NodeContainer c)
Alexander Afanasyev4975f732011-12-20 17:52:19 -080073{
74 ApplicationContainer apps;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080075 for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i) {
76 Ptr<Application> app = InstallPriv(*i);
77 if (app != 0)
78 apps.Add(app);
79 }
80
Alexander Afanasyev4975f732011-12-20 17:52:19 -080081 return apps;
82}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080083
Alexander Afanasyev4975f732011-12-20 17:52:19 -080084Ptr<Application>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080085AppHelper::InstallPriv(Ptr<Node> node)
Alexander Afanasyev4975f732011-12-20 17:52:19 -080086{
Alexander Afanasyev89e02c42012-07-27 13:42:28 -070087#ifdef NS3_MPI
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080088 if (MpiInterface::IsEnabled() && node->GetSystemId() != MpiInterface::GetSystemId()) {
89 // don't create an app if MPI is enabled and node is not in the correct partition
90 return 0;
91 }
Alexander Afanasyev89e02c42012-07-27 13:42:28 -070092#endif
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080093
94 Ptr<Application> app = m_factory.Create<Application>();
95 node->AddApplication(app);
96
Alexander Afanasyev4975f732011-12-20 17:52:19 -080097 return app;
98}
99
Alexander Afanasyevf19fffb2015-08-21 17:34:23 -0700100////////////////////////////////////////////////////////////////////////////
101
102FactoryCallbackApp::FactoryCallbackApp(const FactoryCallback& factory)
103 : m_factory(factory)
104{
105}
106
107ApplicationContainer
108FactoryCallbackApp::Install(Ptr<Node> node, const FactoryCallback& factory)
109{
110 ApplicationContainer apps;
111 auto app = CreateObject<FactoryCallbackApp>(factory);
112 node->AddApplication(app);
113 apps.Add(app);
114 return apps;
115}
116
117void
118FactoryCallbackApp::StartApplication()
119{
120 m_impl = m_factory();
121}
122
123void
124FactoryCallbackApp::StopApplication()
125{
126 m_impl.reset();
127}
128
129
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700130} // namespace ndn
131} // namespace ns3