blob: f50d643c12336bfc9f4b93d67519a87099c2f198 [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.
Ilya Moiseenkoa1214112011-08-29 13:03:55 -07004 *
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.
Ilya Moiseenkoa1214112011-08-29 13:03:55 -07007 *
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.
Ilya Moiseenkoa1214112011-08-29 13:03:55 -070011 *
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.
Ilya Moiseenkoa1214112011-08-29 13:03:55 -070015 *
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 **/
Ilya Moiseenkoa1214112011-08-29 13:03:55 -070019
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070020#ifndef NDN_APP_HELPER_H
21#define NDN_APP_HELPER_H
Ilya Moiseenkoa1214112011-08-29 13:03:55 -070022
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070023#include "ns3/ndnSIM/model/ndn-common.hpp"
24
Ilya Moiseenkoa1214112011-08-29 13:03:55 -070025#include "ns3/object-factory.h"
Ilya Moiseenkoa1214112011-08-29 13:03:55 -070026#include "ns3/attribute.h"
Ilya Moiseenkoa1214112011-08-29 13:03:55 -070027#include "ns3/node-container.h"
28#include "ns3/application-container.h"
Alexander Afanasyev07827182011-12-13 01:07:32 -080029#include "ns3/ptr.h"
Ilya Moiseenkoa1214112011-08-29 13:03:55 -070030
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070031namespace ns3 {
32namespace ndn {
Alexander Afanasyev4975f732011-12-20 17:52:19 -080033
Ilya Moiseenkoa1214112011-08-29 13:03:55 -070034/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070035 * @ingroup ndn-helpers
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080036 * @brief A helper to make it easier to instantiate an ns3::ndn::App applications
37 * on a set of nodes
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070038 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039class AppHelper {
Ilya Moiseenkoa1214112011-08-29 13:03:55 -070040public:
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070041 /**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070042 * \brief Create an NdnAppHelper to make it easier to work with Ndn apps
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070043 *
Alexander Afanasyev4975f732011-12-20 17:52:19 -080044 * \param app Class of the application
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070045 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080046 AppHelper(const std::string& prefix);
Alexander Afanasyev120bf312011-12-19 01:24:47 -080047
48 /**
49 * @brief Set the prefix consumer will be requesting
50 */
51 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080052 SetPrefix(const std::string& prefix);
53
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070054 /**
55 * \brief Helper function used to set the underlying application attributes.
56 *
57 * \param name the name of the application attribute to set
58 * \param value the value of the application attribute to set
59 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080060 void
61 SetAttribute(std::string name, const AttributeValue& value);
62
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070063 /**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070064 * Install an ns3::NdnConsumer on each node of the input container
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070065 * configured with all the attributes set with SetAttribute.
66 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080067 * \param c NodeContainer of the set of nodes on which an NdnConsumer
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070068 * will be installed.
69 * \returns Container of Ptr to the applications installed.
70 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080071 ApplicationContainer
72 Install(NodeContainer c);
73
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070074 /**
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080075 * Install an ns3::NdnConsumer on the node configured with all the
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070076 * attributes set with SetAttribute.
77 *
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070078 * \param node The node on which an NdnConsumer will be installed.
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070079 * \returns Container of Ptr to the applications installed.
80 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080081 ApplicationContainer
82 Install(Ptr<Node> node);
83
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070084 /**
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080085 * Install an ns3::NdnConsumer on the node configured with all the
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070086 * attributes set with SetAttribute.
87 *
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070088 * \param nodeName The node on which an NdnConsumer will be installed.
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070089 * \returns Container of Ptr to the applications installed.
90 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080091 ApplicationContainer
92 Install(std::string nodeName);
93
Ilya Moiseenkoa1214112011-08-29 13:03:55 -070094private:
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070095 /**
96 * \internal
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097 * Install an ns3::NdnConsumer on the node configured with all the
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070098 * attributes set with SetAttribute.
99 *
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700100 * \param node The node on which an NdnConsumer will be installed.
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700101 * \returns Ptr to the application installed.
102 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103 Ptr<Application>
104 InstallPriv(Ptr<Node> node);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700105 ObjectFactory m_factory;
106};
Ilya Moiseenkoa1214112011-08-29 13:03:55 -0700107
Alexander Afanasyevf19fffb2015-08-21 17:34:23 -0700108/**
109 * @brief An application that can be created using the supplied callback
110 *
111 * Example:
112 *
113 * class SomeApp
114 * {
115 * public:
116 * SomeApp(size_t initParameter);
117 * ...
118 * };
119 *
120 * FactoryCallbackApp::Install(node, [] () -> shared_ptr<void> {
121 * return make_shared<SomeApp>(42);
122 * })
123 * .Start(Seconds(1.01));
124 */
125class FactoryCallbackApp : public Application
126{
127public:
128 typedef std::function<shared_ptr<void>()> FactoryCallback;
129
130 FactoryCallbackApp(const FactoryCallback& factory);
131
132public:
133 static ApplicationContainer
134 Install(Ptr<Node> node, const FactoryCallback& factory);
135
136protected:
137 // inherited from Application base class.
138 virtual void
139 StartApplication();
140
141 virtual void
142 StopApplication();
143
144private:
145 FactoryCallback m_factory;
146 std::shared_ptr<void> m_impl;
147};
148
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700149} // namespace ndn
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800150} // namespace ns3
Ilya Moiseenkoa1214112011-08-29 13:03:55 -0700151
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700152#endif // NDN_APP_HELPER_H