blob: 99e4826f91e0ac19984be04ad297d3aa9976394a [file] [log] [blame]
Junxiao Shid3c792f2014-01-30 00:46:13 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry41aba102017-11-01 16:42:13 -07002/*
ashiqopuc7079482019-02-20 05:34:37 +00003 * Copyright (c) 2014-2019, Regents of the University of California,
Junxiao Shifaf3eb02015-02-16 10:50:36 -07004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shi82e7f582014-09-07 15:15:40 -070024 */
Junxiao Shid3c792f2014-01-30 00:46:13 -070025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_FW_STRATEGY_HPP
27#define NFD_DAEMON_FW_STRATEGY_HPP
Junxiao Shid3c792f2014-01-30 00:46:13 -070028
Junxiao Shi2d9bdc82014-03-02 20:55:42 -070029#include "forwarder.hpp"
Junxiao Shidbe71732014-02-21 22:23:28 -070030#include "table/measurements-accessor.hpp"
Junxiao Shid3c792f2014-01-30 00:46:13 -070031
32namespace nfd {
Junxiao Shi8c8d2182014-01-30 22:33:00 -070033namespace fw {
34
Junxiao Shibb5105f2014-03-03 12:06:45 -070035/** \brief represents a forwarding strategy
Junxiao Shid3c792f2014-01-30 00:46:13 -070036 */
Junxiao Shic34d1672016-12-09 15:57:59 +000037class Strategy : noncopyable
Junxiao Shid3c792f2014-01-30 00:46:13 -070038{
Junxiao Shic34d1672016-12-09 15:57:59 +000039public: // registry
40 /** \brief register a strategy type
41 * \tparam S subclass of Strategy
Junxiao Shi18739c42016-12-22 08:03:00 +000042 * \param strategyName strategy program name, must contain version
Junxiao Shic34d1672016-12-09 15:57:59 +000043 * \note It is permitted to register the same strategy type under multiple names,
44 * which is useful in tests and for creating aliases.
45 */
46 template<typename S>
47 static void
Junxiao Shi037f4ab2016-12-13 04:27:06 +000048 registerType(const Name& strategyName = S::getStrategyName())
Junxiao Shic34d1672016-12-09 15:57:59 +000049 {
Junxiao Shi91f6ee02016-12-29 21:44:44 +000050 BOOST_ASSERT(strategyName.size() > 1);
Junxiao Shi18739c42016-12-22 08:03:00 +000051 BOOST_ASSERT(strategyName.at(-1).isVersion());
Junxiao Shic34d1672016-12-09 15:57:59 +000052 Registry& registry = getRegistry();
53 BOOST_ASSERT(registry.count(strategyName) == 0);
Davide Pesavento3dade002019-03-19 11:29:56 -060054 registry[strategyName] = [] (auto&&... args) {
55 return make_unique<S>(std::forward<decltype(args)>(args)...);
56 };
Junxiao Shic34d1672016-12-09 15:57:59 +000057 }
58
Junxiao Shi18739c42016-12-22 08:03:00 +000059 /** \return whether a strategy instance can be created from \p instanceName
60 * \param instanceName strategy instance name, may contain version and parameters
Junxiao Shic34d1672016-12-09 15:57:59 +000061 * \note This function finds a strategy type using same rules as \p create ,
62 * but does not attempt to construct an instance.
63 */
64 static bool
Junxiao Shi18739c42016-12-22 08:03:00 +000065 canCreate(const Name& instanceName);
Junxiao Shic34d1672016-12-09 15:57:59 +000066
Junxiao Shi18739c42016-12-22 08:03:00 +000067 /** \return a strategy instance created from \p instanceName
68 * \retval nullptr if !canCreate(instanceName)
69 * \throw std::invalid_argument strategy type constructor does not accept
70 * specified version or parameters
Junxiao Shic34d1672016-12-09 15:57:59 +000071 */
72 static unique_ptr<Strategy>
Junxiao Shi18739c42016-12-22 08:03:00 +000073 create(const Name& instanceName, Forwarder& forwarder);
Junxiao Shic34d1672016-12-09 15:57:59 +000074
Junxiao Shi55e21b92017-01-23 03:27:47 +000075 /** \return whether \p instanceNameA and \p instanceNameA will initiate same strategy type
76 */
77 static bool
78 areSameType(const Name& instanceNameA, const Name& instanceNameB);
79
Junxiao Shic34d1672016-12-09 15:57:59 +000080 /** \return registered versioned strategy names
81 */
82 static std::set<Name>
83 listRegistered();
84
85public: // constructor, destructor, strategy name
Davide Pesavento3dade002019-03-19 11:29:56 -060086 /** \brief Construct a strategy instance.
Junxiao Shi18739c42016-12-22 08:03:00 +000087 * \param forwarder a reference to the forwarder, used to enable actions and accessors.
Davide Pesavento3dade002019-03-19 11:29:56 -060088 * \note Strategy subclass constructor must not retain a reference to \p forwarder.
Junxiao Shie93d6a32014-09-07 16:13:22 -070089 */
Junxiao Shi18739c42016-12-22 08:03:00 +000090 explicit
91 Strategy(Forwarder& forwarder);
Junxiao Shidbe71732014-02-21 22:23:28 -070092
Junxiao Shid3c792f2014-01-30 00:46:13 -070093 virtual
94 ~Strategy();
Junxiao Shidbe71732014-02-21 22:23:28 -070095
Junxiao Shi037f4ab2016-12-13 04:27:06 +000096#ifdef DOXYGEN
Junxiao Shi18739c42016-12-22 08:03:00 +000097 /** \return strategy program name
98 *
99 * The strategy name is defined by the strategy program.
100 * It must end with a version component.
Junxiao Shi037f4ab2016-12-13 04:27:06 +0000101 */
102 static const Name&
103 getStrategyName();
104#endif
105
Junxiao Shi18739c42016-12-22 08:03:00 +0000106 /** \return strategy instance name
107 *
108 * The instance name is assigned during instantiation.
109 * It contains a version component, and may have extra parameter components.
Junxiao Shib9420cf2016-08-13 04:38:52 +0000110 */
Junxiao Shibb5105f2014-03-03 12:06:45 -0700111 const Name&
Junxiao Shi18739c42016-12-22 08:03:00 +0000112 getInstanceName() const
Junxiao Shib9420cf2016-08-13 04:38:52 +0000113 {
114 return m_name;
115 }
Junxiao Shibb5105f2014-03-03 12:06:45 -0700116
Junxiao Shi679e9272014-02-15 20:10:21 -0700117public: // triggers
118 /** \brief trigger after Interest is received
119 *
120 * The Interest:
121 * - does not violate Scope
122 * - is not looped
123 * - cannot be satisfied by ContentStore
124 * - is under a namespace managed by this strategy
125 *
Teng Liang7003e0b2018-03-03 16:03:30 -0700126 * The PIT entry is set to expire after InterestLifetime has elapsed at each downstream.
127 *
Junxiao Shi679e9272014-02-15 20:10:21 -0700128 * The strategy should decide whether and where to forward this Interest.
129 * - If the strategy decides to forward this Interest,
Teng Liang7003e0b2018-03-03 16:03:30 -0700130 * invoke \c sendInterest for each upstream, either now or shortly after via a scheduler event,
131 * but before PIT entry expires.
132 * Optionally, the strategy can invoke \c setExpiryTimer to adjust how long it would wait for a response.
133 * - If the strategy has already forwarded this Interest previously and decides to continue waiting,
134 * do nothing.
135 * Optionally, the strategy can invoke \c setExpiryTimer to adjust how long it would wait for a response.
136 * - If the strategy concludes that this Interest cannot be satisfied,
137 * invoke \c rejectPendingInterest to erase the PIT entry.
Junxiao Shi82e7f582014-09-07 15:15:40 -0700138 *
Junxiao Shi15e98b02016-08-12 11:21:44 +0000139 * \warning The strategy must not retain shared_ptr<pit::Entry>, otherwise undefined behavior
140 * may occur. However, the strategy is allowed to store weak_ptr<pit::Entry>.
Junxiao Shi679e9272014-02-15 20:10:21 -0700141 */
Junxiao Shid3c792f2014-01-30 00:46:13 -0700142 virtual void
ashiqopuc7079482019-02-20 05:34:37 +0000143 afterReceiveInterest(const FaceEndpoint& ingress, const Interest& interest,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000144 const shared_ptr<pit::Entry>& pitEntry) = 0;
Junxiao Shidbe71732014-02-21 22:23:28 -0700145
Junxiao Shi22be22c2014-02-16 22:53:48 -0700146 /** \brief trigger before PIT entry is satisfied
147 *
Teng Liang43bb2312018-03-26 04:16:42 -0700148 * This trigger is invoked when an incoming Data satisfies more than one PIT entry.
149 * The strategy can collect measurements information, but cannot manipulate Data forwarding.
150 * When an incoming Data satisfies only one PIT entry, \c afterReceiveData is invoked instead
151 * and given full control over Data forwarding. If a strategy does not override \c afterReceiveData,
152 * the default implementation invokes \c beforeSatisfyInterest.
Teng Liang7003e0b2018-03-03 16:03:30 -0700153 *
Teng Liang43bb2312018-03-26 04:16:42 -0700154 * Normally, PIT entries would be erased after receiving the first matching Data.
Teng Liang7003e0b2018-03-03 16:03:30 -0700155 * If the strategy wishes to collect responses from additional upstream nodes,
Teng Liang43bb2312018-03-26 04:16:42 -0700156 * it should invoke \c setExpiryTimer within this function to prolong the PIT entry lifetime.
Teng Liang7003e0b2018-03-03 16:03:30 -0700157 * If a Data arrives from another upstream during the extended PIT entry lifetime, this trigger will be invoked again.
158 * At that time, this function must invoke \c setExpiryTimer again to continue collecting more responses.
Junxiao Shi82e7f582014-09-07 15:15:40 -0700159 *
Junxiao Shi22be22c2014-02-16 22:53:48 -0700160 * In this base class this method does nothing.
Junxiao Shi82e7f582014-09-07 15:15:40 -0700161 *
Junxiao Shi15e98b02016-08-12 11:21:44 +0000162 * \warning The strategy must not retain shared_ptr<pit::Entry>, otherwise undefined behavior
163 * may occur. However, the strategy is allowed to store weak_ptr<pit::Entry>.
Junxiao Shi22be22c2014-02-16 22:53:48 -0700164 */
165 virtual void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000166 beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000167 const FaceEndpoint& ingress, const Data& data);
Junxiao Shidbe71732014-02-21 22:23:28 -0700168
Teng Liang85a36632018-03-21 05:59:34 -0700169 /** \brief trigger after a Data is matched in CS
170 *
ashiqopuc7079482019-02-20 05:34:37 +0000171 * In the base class this method sends \p data to \p ingress
Teng Liang85a36632018-03-21 05:59:34 -0700172 */
173 virtual void
174 afterContentStoreHit(const shared_ptr<pit::Entry>& pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000175 const FaceEndpoint& ingress, const Data& data);
Teng Liang85a36632018-03-21 05:59:34 -0700176
Teng Liang43bb2312018-03-26 04:16:42 -0700177 /** \brief trigger after Data is received
178 *
179 * This trigger is invoked when an incoming Data satisfies exactly one PIT entry,
180 * and gives the strategy full control over Data forwarding.
181 *
182 * When this trigger is invoked:
183 * - The Data has been verified to satisfy the PIT entry.
184 * - The PIT entry expiry timer is set to now
185 *
186 * Within this function:
187 * - A strategy should return Data to downstream nodes via \c sendData or \c sendDataToAll.
188 * - A strategy can modify the Data as long as it still satisfies the PIT entry, such as
189 * adding or removing congestion marks.
190 * - A strategy can delay Data forwarding by prolonging the PIT entry lifetime via \c setExpiryTimer,
191 * and forward Data before the PIT entry is erased.
192 * - A strategy can collect measurements about the upstream.
193 * - A strategy can collect responses from additional upstream nodes by prolonging the PIT entry
194 * lifetime via \c setExpiryTimer every time a Data is received. Note that only one Data should
195 * be returned to each downstream node.
196 *
197 * In the base class this method invokes \c beforeSatisfyInterest trigger and then returns
198 * the Data to downstream faces via \c sendDataToAll.
199 */
200 virtual void
201 afterReceiveData(const shared_ptr<pit::Entry>& pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000202 const FaceEndpoint& ingress, const Data& data);
Teng Liang43bb2312018-03-26 04:16:42 -0700203
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700204 /** \brief trigger after Nack is received
205 *
206 * This trigger is invoked when an incoming Nack is received in response to
207 * an forwarded Interest.
208 * The Nack has been confirmed to be a response to the last Interest forwarded
209 * to that upstream, i.e. the PIT out-record exists and has a matching Nonce.
210 * The NackHeader has been recorded in the PIT out-record.
211 *
Teng Liang7003e0b2018-03-03 16:03:30 -0700212 * If the PIT entry is not yet satisfied, its expiry timer remains unchanged.
213 * Otherwise, the PIT entry normally would expire immediately after this function returns.
214 *
215 * If the strategy wishes to collect responses from additional upstream nodes,
216 * it should invoke \c setExpiryTimer within this function to retain the PIT entry.
217 * If a Nack arrives from another upstream during the extended PIT entry lifetime, this trigger will be invoked again.
218 * At that time, this function must invoke \c setExpiryTimer again to continue collecting more responses.
219 *
Teng Liang85a36632018-03-21 05:59:34 -0700220 * In the base class this method does nothing.
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700221 *
Junxiao Shi15e98b02016-08-12 11:21:44 +0000222 * \warning The strategy must not retain shared_ptr<pit::Entry>, otherwise undefined behavior
223 * may occur. However, the strategy is allowed to store weak_ptr<pit::Entry>.
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700224 */
225 virtual void
ashiqopuc7079482019-02-20 05:34:37 +0000226 afterReceiveNack(const FaceEndpoint& ingress, const lp::Nack& nack,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000227 const shared_ptr<pit::Entry>& pitEntry);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700228
Eric Newberry41aba102017-11-01 16:42:13 -0700229 /** \brief trigger after Interest dropped for exceeding allowed retransmissions
230 *
231 * In the base class this method does nothing.
232 */
233 virtual void
ashiqopuc7079482019-02-20 05:34:37 +0000234 onDroppedInterest(const FaceEndpoint& egress, const Interest& interest);
Eric Newberry41aba102017-11-01 16:42:13 -0700235
Junxiao Shid3c792f2014-01-30 00:46:13 -0700236protected: // actions
ashiqopuc7079482019-02-20 05:34:37 +0000237 /** \brief send Interest to egress
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -0500238 * \param pitEntry PIT entry
ashiqopuc7079482019-02-20 05:34:37 +0000239 * \param egress face through which to send out the Interest and destination endpoint
Junxiao Shic5f651f2016-11-17 22:58:12 +0000240 * \param interest the Interest packet
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700241 */
Junxiao Shi727ed292014-02-19 23:26:45 -0700242 VIRTUAL_WITH_TESTS void
ashiqopuc7079482019-02-20 05:34:37 +0000243 sendInterest(const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600244 const FaceEndpoint& egress, const Interest& interest);
Junxiao Shidbe71732014-02-21 22:23:28 -0700245
ashiqopuc7079482019-02-20 05:34:37 +0000246 /** \brief send \p data to \p egress
Teng Liang85a36632018-03-21 05:59:34 -0700247 * \param pitEntry PIT entry
248 * \param data the Data packet
ashiqopuc7079482019-02-20 05:34:37 +0000249 * \param egress face through which to send out the Data and destination endpoint
Teng Liang85a36632018-03-21 05:59:34 -0700250 */
251 VIRTUAL_WITH_TESTS void
ashiqopuc7079482019-02-20 05:34:37 +0000252 sendData(const shared_ptr<pit::Entry>& pitEntry, const Data& data, const FaceEndpoint& egress);
Teng Liang85a36632018-03-21 05:59:34 -0700253
ashiqopuc7079482019-02-20 05:34:37 +0000254 /** \brief send \p data to all matched and qualified face-endpoint pairs
Teng Liang43bb2312018-03-26 04:16:42 -0700255 *
ashiqopuc7079482019-02-20 05:34:37 +0000256 * A matched face is qualified if it is ad-hoc or it is NOT \p ingress
Teng Liang43bb2312018-03-26 04:16:42 -0700257 *
258 * \param pitEntry PIT entry
ashiqopuc7079482019-02-20 05:34:37 +0000259 * \param ingress face through which the Data comes from and endpoint of the sender
Teng Liang43bb2312018-03-26 04:16:42 -0700260 * \param data the Data packet
261 */
262 VIRTUAL_WITH_TESTS void
ashiqopuc7079482019-02-20 05:34:37 +0000263 sendDataToAll(const shared_ptr<pit::Entry>& pitEntry,
264 const FaceEndpoint& ingress, const Data& data);
Teng Liang85a36632018-03-21 05:59:34 -0700265
Teng Liang7003e0b2018-03-03 16:03:30 -0700266 /** \brief schedule the PIT entry for immediate deletion
Junxiao Shi679e9272014-02-15 20:10:21 -0700267 *
Teng Liang7003e0b2018-03-03 16:03:30 -0700268 * This helper function sets the PIT entry expiry time to zero.
269 * The strategy should invoke this function when it concludes that the Interest cannot
270 * be forwarded and it does not want to wait for responses from existing upstream nodes.
Junxiao Shid3c792f2014-01-30 00:46:13 -0700271 */
Junxiao Shi727ed292014-02-19 23:26:45 -0700272 VIRTUAL_WITH_TESTS void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000273 rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry)
274 {
Teng Liang7003e0b2018-03-03 16:03:30 -0700275 this->setExpiryTimer(pitEntry, 0_ms);
Junxiao Shib9420cf2016-08-13 04:38:52 +0000276 }
Junxiao Shidbe71732014-02-21 22:23:28 -0700277
ashiqopuc7079482019-02-20 05:34:37 +0000278 /** \brief send Nack to egress
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -0500279 * \param pitEntry PIT entry
ashiqopuc7079482019-02-20 05:34:37 +0000280 * \param egress face through which to send out the Nack and destination endpoint
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -0500281 * \param header Nack header
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700282 *
ashiqopuc7079482019-02-20 05:34:37 +0000283 * The egress must have a PIT in-record, otherwise this method has no effect.
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700284 */
285 VIRTUAL_WITH_TESTS void
ashiqopuc7079482019-02-20 05:34:37 +0000286 sendNack(const shared_ptr<pit::Entry>& pitEntry,
287 const FaceEndpoint& egress, const lp::NackHeader& header)
Junxiao Shib9420cf2016-08-13 04:38:52 +0000288 {
ashiqopuc7079482019-02-20 05:34:37 +0000289 m_forwarder.onOutgoingNack(pitEntry, egress, header);
Junxiao Shib9420cf2016-08-13 04:38:52 +0000290 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700291
ashiqopuc7079482019-02-20 05:34:37 +0000292 /** \brief send Nack to every face-endpoint pair that has an in-record, except those in \p exceptFaceEndpoints
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -0500293 * \param pitEntry PIT entry
294 * \param header NACK header
ashiqopuc7079482019-02-20 05:34:37 +0000295 * \param exceptFaceEndpoints list of face-endpoint pairs that should be excluded from sending Nacks
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700296 * \note This is not an action, but a helper that invokes the sendNack action.
297 */
298 void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000299 sendNacks(const shared_ptr<pit::Entry>& pitEntry, const lp::NackHeader& header,
ashiqopuc7079482019-02-20 05:34:37 +0000300 std::initializer_list<FaceEndpoint> exceptFaceEndpoints = {});
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700301
Teng Liang7003e0b2018-03-03 16:03:30 -0700302 /** \brief Schedule the PIT entry to be erased after \p duration
303 */
304 void
305 setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
306 {
307 m_forwarder.setExpiryTimer(pitEntry, duration);
308 }
309
Junxiao Shidbe71732014-02-21 22:23:28 -0700310protected: // accessors
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000311 /** \brief performs a FIB lookup, considering Link object if present
312 */
Junxiao Shi8d843142016-07-11 22:42:42 +0000313 const fib::Entry&
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000314 lookupFib(const pit::Entry& pitEntry) const;
Junxiao Shi8d843142016-07-11 22:42:42 +0000315
Junxiao Shidbe71732014-02-21 22:23:28 -0700316 MeasurementsAccessor&
Junxiao Shib9420cf2016-08-13 04:38:52 +0000317 getMeasurements()
318 {
319 return m_measurements;
320 }
Junxiao Shidbe71732014-02-21 22:23:28 -0700321
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000322 Face*
Junxiao Shib9420cf2016-08-13 04:38:52 +0000323 getFace(FaceId id) const
324 {
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400325 return getFaceTable().get(id);
Junxiao Shib9420cf2016-08-13 04:38:52 +0000326 }
Junxiao Shi2d9bdc82014-03-02 20:55:42 -0700327
Junxiao Shi49e11e72014-12-14 19:46:05 -0700328 const FaceTable&
Junxiao Shib9420cf2016-08-13 04:38:52 +0000329 getFaceTable() const
330 {
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400331 return m_forwarder.m_faceTable;
Junxiao Shib9420cf2016-08-13 04:38:52 +0000332 }
Junxiao Shi49e11e72014-12-14 19:46:05 -0700333
Junxiao Shi18739c42016-12-22 08:03:00 +0000334protected: // instance name
335 struct ParsedInstanceName
336 {
337 Name strategyName; ///< strategy name without parameters
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400338 optional<uint64_t> version; ///< whether strategyName contains a version component
Junxiao Shi18739c42016-12-22 08:03:00 +0000339 PartialName parameters; ///< parameter components
340 };
341
342 /** \brief parse a strategy instance name
343 * \param input strategy instance name, may contain version and parameters
344 * \throw std::invalid_argument input format is unacceptable
345 */
346 static ParsedInstanceName
347 parseInstanceName(const Name& input);
348
349 /** \brief construct a strategy instance name
350 * \param input strategy instance name, may contain version and parameters
351 * \param strategyName strategy name with version but without parameters;
352 * typically this should be \p getStrategyName()
353 *
354 * If \p input contains a version component, return \p input unchanged.
355 * Otherwise, return \p input plus the version component taken from \p strategyName.
356 * This allows a strategy instance to be constructed with an unversioned name,
357 * but its final instance name should contain the version.
358 */
359 static Name
360 makeInstanceName(const Name& input, const Name& strategyName);
361
362 /** \brief set strategy instance name
363 * \note This must be called by strategy subclass constructor.
364 */
365 void
366 setInstanceName(const Name& name)
367 {
368 m_name = name;
369 }
Junxiao Shi49e11e72014-12-14 19:46:05 -0700370
Junxiao Shic34d1672016-12-09 15:57:59 +0000371private: // registry
372 typedef std::function<unique_ptr<Strategy>(Forwarder& forwarder, const Name& strategyName)> CreateFunc;
373 typedef std::map<Name, CreateFunc> Registry; // indexed by strategy name
374
375 static Registry&
376 getRegistry();
377
378 static Registry::const_iterator
Junxiao Shi18739c42016-12-22 08:03:00 +0000379 find(const Name& instanceName);
380
381protected: // accessors
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400382 signal::Signal<FaceTable, Face>& afterAddFace;
383 signal::Signal<FaceTable, Face>& beforeRemoveFace;
Junxiao Shic34d1672016-12-09 15:57:59 +0000384
385private: // instance fields
Junxiao Shibb5105f2014-03-03 12:06:45 -0700386 Name m_name;
387
Junxiao Shi679e9272014-02-15 20:10:21 -0700388 /** \brief reference to the forwarder
389 *
390 * Triggers can access forwarder indirectly via actions.
391 */
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700392 Forwarder& m_forwarder;
Junxiao Shidbe71732014-02-21 22:23:28 -0700393
394 MeasurementsAccessor m_measurements;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700395};
396
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700397} // namespace fw
Junxiao Shid3c792f2014-01-30 00:46:13 -0700398} // namespace nfd
399
Junxiao Shic34d1672016-12-09 15:57:59 +0000400/** \brief registers a strategy
401 *
402 * This macro should appear once in .cpp of each strategy.
403 */
404#define NFD_REGISTER_STRATEGY(S) \
405static class NfdAuto ## S ## StrategyRegistrationClass \
406{ \
407public: \
408 NfdAuto ## S ## StrategyRegistrationClass() \
409 { \
410 ::nfd::fw::Strategy::registerType<S>(); \
411 } \
412} g_nfdAuto ## S ## StrategyRegistrationVariable
413
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700414#endif // NFD_DAEMON_FW_STRATEGY_HPP