Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 2 | /* |
Teng Liang | 6308644 | 2018-02-25 20:39:42 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Junxiao Shi | faf3eb0 | 2015-02-16 10:50:36 -0700 | [diff] [blame] | 4 | * 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_FW_STRATEGY_HPP |
| 27 | #define NFD_DAEMON_FW_STRATEGY_HPP |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 28 | |
Junxiao Shi | 2d9bdc8 | 2014-03-02 20:55:42 -0700 | [diff] [blame] | 29 | #include "forwarder.hpp" |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 30 | #include "table/measurements-accessor.hpp" |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 31 | |
| 32 | namespace nfd { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 33 | namespace fw { |
| 34 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 35 | /** \brief represents a forwarding strategy |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 36 | */ |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 37 | class Strategy : noncopyable |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 38 | { |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 39 | public: // registry |
| 40 | /** \brief register a strategy type |
| 41 | * \tparam S subclass of Strategy |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 42 | * \param strategyName strategy program name, must contain version |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 43 | * \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 Shi | 037f4ab | 2016-12-13 04:27:06 +0000 | [diff] [blame] | 48 | registerType(const Name& strategyName = S::getStrategyName()) |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 49 | { |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 50 | BOOST_ASSERT(strategyName.size() > 1); |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 51 | BOOST_ASSERT(strategyName.at(-1).isVersion()); |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 52 | Registry& registry = getRegistry(); |
| 53 | BOOST_ASSERT(registry.count(strategyName) == 0); |
| 54 | registry[strategyName] = &make_unique<S, Forwarder&, const Name&>; |
| 55 | } |
| 56 | |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 57 | /** \return whether a strategy instance can be created from \p instanceName |
| 58 | * \param instanceName strategy instance name, may contain version and parameters |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 59 | * \note This function finds a strategy type using same rules as \p create , |
| 60 | * but does not attempt to construct an instance. |
| 61 | */ |
| 62 | static bool |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 63 | canCreate(const Name& instanceName); |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 64 | |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 65 | /** \return a strategy instance created from \p instanceName |
| 66 | * \retval nullptr if !canCreate(instanceName) |
| 67 | * \throw std::invalid_argument strategy type constructor does not accept |
| 68 | * specified version or parameters |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 69 | */ |
| 70 | static unique_ptr<Strategy> |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 71 | create(const Name& instanceName, Forwarder& forwarder); |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 72 | |
Junxiao Shi | 55e21b9 | 2017-01-23 03:27:47 +0000 | [diff] [blame] | 73 | /** \return whether \p instanceNameA and \p instanceNameA will initiate same strategy type |
| 74 | */ |
| 75 | static bool |
| 76 | areSameType(const Name& instanceNameA, const Name& instanceNameB); |
| 77 | |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 78 | /** \return registered versioned strategy names |
| 79 | */ |
| 80 | static std::set<Name> |
| 81 | listRegistered(); |
| 82 | |
| 83 | public: // constructor, destructor, strategy name |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 84 | /** \brief construct a strategy instance |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 85 | * \param forwarder a reference to the forwarder, used to enable actions and accessors. |
| 86 | * \note Strategy subclass constructor should not retain a reference to the forwarder. |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 87 | */ |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 88 | explicit |
| 89 | Strategy(Forwarder& forwarder); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 90 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 91 | virtual |
| 92 | ~Strategy(); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 93 | |
Junxiao Shi | 037f4ab | 2016-12-13 04:27:06 +0000 | [diff] [blame] | 94 | #ifdef DOXYGEN |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 95 | /** \return strategy program name |
| 96 | * |
| 97 | * The strategy name is defined by the strategy program. |
| 98 | * It must end with a version component. |
Junxiao Shi | 037f4ab | 2016-12-13 04:27:06 +0000 | [diff] [blame] | 99 | */ |
| 100 | static const Name& |
| 101 | getStrategyName(); |
| 102 | #endif |
| 103 | |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 104 | /** \return strategy instance name |
| 105 | * |
| 106 | * The instance name is assigned during instantiation. |
| 107 | * It contains a version component, and may have extra parameter components. |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 108 | */ |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 109 | const Name& |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 110 | getInstanceName() const |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 111 | { |
| 112 | return m_name; |
| 113 | } |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 114 | |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 115 | public: // triggers |
| 116 | /** \brief trigger after Interest is received |
| 117 | * |
| 118 | * The Interest: |
| 119 | * - does not violate Scope |
| 120 | * - is not looped |
| 121 | * - cannot be satisfied by ContentStore |
| 122 | * - is under a namespace managed by this strategy |
| 123 | * |
Teng Liang | 7003e0b | 2018-03-03 16:03:30 -0700 | [diff] [blame] | 124 | * The PIT entry is set to expire after InterestLifetime has elapsed at each downstream. |
| 125 | * |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 126 | * The strategy should decide whether and where to forward this Interest. |
| 127 | * - If the strategy decides to forward this Interest, |
Teng Liang | 7003e0b | 2018-03-03 16:03:30 -0700 | [diff] [blame] | 128 | * invoke \c sendInterest for each upstream, either now or shortly after via a scheduler event, |
| 129 | * but before PIT entry expires. |
| 130 | * Optionally, the strategy can invoke \c setExpiryTimer to adjust how long it would wait for a response. |
| 131 | * - If the strategy has already forwarded this Interest previously and decides to continue waiting, |
| 132 | * do nothing. |
| 133 | * Optionally, the strategy can invoke \c setExpiryTimer to adjust how long it would wait for a response. |
| 134 | * - If the strategy concludes that this Interest cannot be satisfied, |
| 135 | * invoke \c rejectPendingInterest to erase the PIT entry. |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 136 | * |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 137 | * \warning The strategy must not retain shared_ptr<pit::Entry>, otherwise undefined behavior |
| 138 | * may occur. However, the strategy is allowed to store weak_ptr<pit::Entry>. |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 139 | */ |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 140 | virtual void |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 141 | afterReceiveInterest(const Face& inFace, const Interest& interest, |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 142 | const shared_ptr<pit::Entry>& pitEntry) = 0; |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 143 | |
Junxiao Shi | 22be22c | 2014-02-16 22:53:48 -0700 | [diff] [blame] | 144 | /** \brief trigger before PIT entry is satisfied |
| 145 | * |
Teng Liang | 43bb231 | 2018-03-26 04:16:42 -0700 | [diff] [blame] | 146 | * This trigger is invoked when an incoming Data satisfies more than one PIT entry. |
| 147 | * The strategy can collect measurements information, but cannot manipulate Data forwarding. |
| 148 | * When an incoming Data satisfies only one PIT entry, \c afterReceiveData is invoked instead |
| 149 | * and given full control over Data forwarding. If a strategy does not override \c afterReceiveData, |
| 150 | * the default implementation invokes \c beforeSatisfyInterest. |
Teng Liang | 7003e0b | 2018-03-03 16:03:30 -0700 | [diff] [blame] | 151 | * |
Teng Liang | 43bb231 | 2018-03-26 04:16:42 -0700 | [diff] [blame] | 152 | * Normally, PIT entries would be erased after receiving the first matching Data. |
Teng Liang | 7003e0b | 2018-03-03 16:03:30 -0700 | [diff] [blame] | 153 | * If the strategy wishes to collect responses from additional upstream nodes, |
Teng Liang | 43bb231 | 2018-03-26 04:16:42 -0700 | [diff] [blame] | 154 | * it should invoke \c setExpiryTimer within this function to prolong the PIT entry lifetime. |
Teng Liang | 7003e0b | 2018-03-03 16:03:30 -0700 | [diff] [blame] | 155 | * If a Data arrives from another upstream during the extended PIT entry lifetime, this trigger will be invoked again. |
| 156 | * At that time, this function must invoke \c setExpiryTimer again to continue collecting more responses. |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 157 | * |
Junxiao Shi | 22be22c | 2014-02-16 22:53:48 -0700 | [diff] [blame] | 158 | * In this base class this method does nothing. |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 159 | * |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 160 | * \warning The strategy must not retain shared_ptr<pit::Entry>, otherwise undefined behavior |
| 161 | * may occur. However, the strategy is allowed to store weak_ptr<pit::Entry>. |
Junxiao Shi | 22be22c | 2014-02-16 22:53:48 -0700 | [diff] [blame] | 162 | */ |
| 163 | virtual void |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 164 | beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry, |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 165 | const Face& inFace, const Data& data); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 166 | |
Teng Liang | 85a3663 | 2018-03-21 05:59:34 -0700 | [diff] [blame] | 167 | /** \brief trigger after a Data is matched in CS |
| 168 | * |
| 169 | * In the base class this method sends \p data to \p inFace |
| 170 | */ |
| 171 | virtual void |
| 172 | afterContentStoreHit(const shared_ptr<pit::Entry>& pitEntry, |
| 173 | const Face& inFace, const Data& data); |
| 174 | |
Teng Liang | 43bb231 | 2018-03-26 04:16:42 -0700 | [diff] [blame] | 175 | /** \brief trigger after Data is received |
| 176 | * |
| 177 | * This trigger is invoked when an incoming Data satisfies exactly one PIT entry, |
| 178 | * and gives the strategy full control over Data forwarding. |
| 179 | * |
| 180 | * When this trigger is invoked: |
| 181 | * - The Data has been verified to satisfy the PIT entry. |
| 182 | * - The PIT entry expiry timer is set to now |
| 183 | * |
| 184 | * Within this function: |
| 185 | * - A strategy should return Data to downstream nodes via \c sendData or \c sendDataToAll. |
| 186 | * - A strategy can modify the Data as long as it still satisfies the PIT entry, such as |
| 187 | * adding or removing congestion marks. |
| 188 | * - A strategy can delay Data forwarding by prolonging the PIT entry lifetime via \c setExpiryTimer, |
| 189 | * and forward Data before the PIT entry is erased. |
| 190 | * - A strategy can collect measurements about the upstream. |
| 191 | * - A strategy can collect responses from additional upstream nodes by prolonging the PIT entry |
| 192 | * lifetime via \c setExpiryTimer every time a Data is received. Note that only one Data should |
| 193 | * be returned to each downstream node. |
| 194 | * |
| 195 | * In the base class this method invokes \c beforeSatisfyInterest trigger and then returns |
| 196 | * the Data to downstream faces via \c sendDataToAll. |
| 197 | */ |
| 198 | virtual void |
| 199 | afterReceiveData(const shared_ptr<pit::Entry>& pitEntry, |
| 200 | const Face& inFace, const Data& data); |
| 201 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 202 | /** \brief trigger after Nack is received |
| 203 | * |
| 204 | * This trigger is invoked when an incoming Nack is received in response to |
| 205 | * an forwarded Interest. |
| 206 | * The Nack has been confirmed to be a response to the last Interest forwarded |
| 207 | * to that upstream, i.e. the PIT out-record exists and has a matching Nonce. |
| 208 | * The NackHeader has been recorded in the PIT out-record. |
| 209 | * |
Teng Liang | 7003e0b | 2018-03-03 16:03:30 -0700 | [diff] [blame] | 210 | * If the PIT entry is not yet satisfied, its expiry timer remains unchanged. |
| 211 | * Otherwise, the PIT entry normally would expire immediately after this function returns. |
| 212 | * |
| 213 | * If the strategy wishes to collect responses from additional upstream nodes, |
| 214 | * it should invoke \c setExpiryTimer within this function to retain the PIT entry. |
| 215 | * If a Nack arrives from another upstream during the extended PIT entry lifetime, this trigger will be invoked again. |
| 216 | * At that time, this function must invoke \c setExpiryTimer again to continue collecting more responses. |
| 217 | * |
Teng Liang | 85a3663 | 2018-03-21 05:59:34 -0700 | [diff] [blame] | 218 | * In the base class this method does nothing. |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 219 | * |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 220 | * \warning The strategy must not retain shared_ptr<pit::Entry>, otherwise undefined behavior |
| 221 | * may occur. However, the strategy is allowed to store weak_ptr<pit::Entry>. |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 222 | */ |
| 223 | virtual void |
| 224 | afterReceiveNack(const Face& inFace, const lp::Nack& nack, |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 225 | const shared_ptr<pit::Entry>& pitEntry); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 226 | |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 227 | /** \brief trigger after Interest dropped for exceeding allowed retransmissions |
| 228 | * |
| 229 | * In the base class this method does nothing. |
| 230 | */ |
| 231 | virtual void |
| 232 | onDroppedInterest(const Face& outFace, const Interest& interest); |
| 233 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 234 | protected: // actions |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 235 | /** \brief send Interest to outFace |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 236 | * \param pitEntry PIT entry |
| 237 | * \param outFace face through which to send out the Interest |
Junxiao Shi | c5f651f | 2016-11-17 22:58:12 +0000 | [diff] [blame] | 238 | * \param interest the Interest packet |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 239 | */ |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 240 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 241 | sendInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace, |
Junxiao Shi | c5f651f | 2016-11-17 22:58:12 +0000 | [diff] [blame] | 242 | const Interest& interest) |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 243 | { |
Junxiao Shi | c5f651f | 2016-11-17 22:58:12 +0000 | [diff] [blame] | 244 | m_forwarder.onOutgoingInterest(pitEntry, outFace, interest); |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 245 | } |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 246 | |
Teng Liang | 85a3663 | 2018-03-21 05:59:34 -0700 | [diff] [blame] | 247 | /** \brief send \p data to \p outFace |
| 248 | * \param pitEntry PIT entry |
| 249 | * \param data the Data packet |
| 250 | * \param outFace face through which to send out the Data |
| 251 | */ |
| 252 | VIRTUAL_WITH_TESTS void |
Teng Liang | 43bb231 | 2018-03-26 04:16:42 -0700 | [diff] [blame] | 253 | sendData(const shared_ptr<pit::Entry>& pitEntry, const Data& data, const Face& outFace); |
Teng Liang | 85a3663 | 2018-03-21 05:59:34 -0700 | [diff] [blame] | 254 | |
Teng Liang | 43bb231 | 2018-03-26 04:16:42 -0700 | [diff] [blame] | 255 | /** \brief send \p data to all matched and qualified faces |
| 256 | * |
| 257 | * A matched face is qualified if it is ad-hoc or it is NOT \p inFace |
| 258 | * |
| 259 | * \param pitEntry PIT entry |
| 260 | * \param inFace face through which the Data comes from |
| 261 | * \param data the Data packet |
| 262 | */ |
| 263 | VIRTUAL_WITH_TESTS void |
| 264 | sendDataToAll(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace, const Data& data); |
Teng Liang | 85a3663 | 2018-03-21 05:59:34 -0700 | [diff] [blame] | 265 | |
Teng Liang | 7003e0b | 2018-03-03 16:03:30 -0700 | [diff] [blame] | 266 | /** \brief schedule the PIT entry for immediate deletion |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 267 | * |
Teng Liang | 7003e0b | 2018-03-03 16:03:30 -0700 | [diff] [blame] | 268 | * 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 Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 271 | */ |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 272 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 273 | rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry) |
| 274 | { |
Teng Liang | 7003e0b | 2018-03-03 16:03:30 -0700 | [diff] [blame] | 275 | this->setExpiryTimer(pitEntry, 0_ms); |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 276 | } |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 277 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 278 | /** \brief send Nack to outFace |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 279 | * \param pitEntry PIT entry |
| 280 | * \param outFace face through which to send out the Nack |
| 281 | * \param header Nack header |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 282 | * |
| 283 | * The outFace must have a PIT in-record, otherwise this method has no effect. |
| 284 | */ |
| 285 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 286 | sendNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace, |
| 287 | const lp::NackHeader& header) |
| 288 | { |
| 289 | m_forwarder.onOutgoingNack(pitEntry, outFace, header); |
| 290 | } |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 291 | |
| 292 | /** \brief send Nack to every face that has an in-record, |
| 293 | * except those in \p exceptFaces |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 294 | * \param pitEntry PIT entry |
| 295 | * \param header NACK header |
| 296 | * \param exceptFaces list of faces that should be excluded from sending Nacks |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 297 | * \note This is not an action, but a helper that invokes the sendNack action. |
| 298 | */ |
| 299 | void |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 300 | sendNacks(const shared_ptr<pit::Entry>& pitEntry, const lp::NackHeader& header, |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 301 | std::initializer_list<const Face*> exceptFaces = std::initializer_list<const Face*>()); |
| 302 | |
Teng Liang | 7003e0b | 2018-03-03 16:03:30 -0700 | [diff] [blame] | 303 | /** \brief Schedule the PIT entry to be erased after \p duration |
| 304 | */ |
| 305 | void |
| 306 | setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration) |
| 307 | { |
| 308 | m_forwarder.setExpiryTimer(pitEntry, duration); |
| 309 | } |
| 310 | |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 311 | protected: // accessors |
Junxiao Shi | cf0f3ce | 2016-09-02 13:01:59 +0000 | [diff] [blame] | 312 | /** \brief performs a FIB lookup, considering Link object if present |
| 313 | */ |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 314 | const fib::Entry& |
Junxiao Shi | cf0f3ce | 2016-09-02 13:01:59 +0000 | [diff] [blame] | 315 | lookupFib(const pit::Entry& pitEntry) const; |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 316 | |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 317 | MeasurementsAccessor& |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 318 | getMeasurements() |
| 319 | { |
| 320 | return m_measurements; |
| 321 | } |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 322 | |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 323 | Face* |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 324 | getFace(FaceId id) const |
| 325 | { |
| 326 | return m_forwarder.getFace(id); |
| 327 | } |
Junxiao Shi | 2d9bdc8 | 2014-03-02 20:55:42 -0700 | [diff] [blame] | 328 | |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 329 | const FaceTable& |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 330 | getFaceTable() const |
| 331 | { |
| 332 | return m_forwarder.getFaceTable(); |
| 333 | } |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 334 | |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 335 | protected: // instance name |
| 336 | struct ParsedInstanceName |
| 337 | { |
| 338 | Name strategyName; ///< strategy name without parameters |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 339 | optional<uint64_t> version; ///< whether strategyName contains a version component |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 340 | PartialName parameters; ///< parameter components |
| 341 | }; |
| 342 | |
| 343 | /** \brief parse a strategy instance name |
| 344 | * \param input strategy instance name, may contain version and parameters |
| 345 | * \throw std::invalid_argument input format is unacceptable |
| 346 | */ |
| 347 | static ParsedInstanceName |
| 348 | parseInstanceName(const Name& input); |
| 349 | |
| 350 | /** \brief construct a strategy instance name |
| 351 | * \param input strategy instance name, may contain version and parameters |
| 352 | * \param strategyName strategy name with version but without parameters; |
| 353 | * typically this should be \p getStrategyName() |
| 354 | * |
| 355 | * If \p input contains a version component, return \p input unchanged. |
| 356 | * Otherwise, return \p input plus the version component taken from \p strategyName. |
| 357 | * This allows a strategy instance to be constructed with an unversioned name, |
| 358 | * but its final instance name should contain the version. |
| 359 | */ |
| 360 | static Name |
| 361 | makeInstanceName(const Name& input, const Name& strategyName); |
| 362 | |
| 363 | /** \brief set strategy instance name |
| 364 | * \note This must be called by strategy subclass constructor. |
| 365 | */ |
| 366 | void |
| 367 | setInstanceName(const Name& name) |
| 368 | { |
| 369 | m_name = name; |
| 370 | } |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 371 | |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 372 | private: // registry |
| 373 | typedef std::function<unique_ptr<Strategy>(Forwarder& forwarder, const Name& strategyName)> CreateFunc; |
| 374 | typedef std::map<Name, CreateFunc> Registry; // indexed by strategy name |
| 375 | |
| 376 | static Registry& |
| 377 | getRegistry(); |
| 378 | |
| 379 | static Registry::const_iterator |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 380 | find(const Name& instanceName); |
| 381 | |
| 382 | protected: // accessors |
| 383 | signal::Signal<FaceTable, Face&>& afterAddFace; |
| 384 | signal::Signal<FaceTable, Face&>& beforeRemoveFace; |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 385 | |
| 386 | private: // instance fields |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 387 | Name m_name; |
| 388 | |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 389 | /** \brief reference to the forwarder |
| 390 | * |
| 391 | * Triggers can access forwarder indirectly via actions. |
| 392 | */ |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 393 | Forwarder& m_forwarder; |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 394 | |
| 395 | MeasurementsAccessor m_measurements; |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 396 | }; |
| 397 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 398 | } // namespace fw |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 399 | } // namespace nfd |
| 400 | |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 401 | /** \brief registers a strategy |
| 402 | * |
| 403 | * This macro should appear once in .cpp of each strategy. |
| 404 | */ |
| 405 | #define NFD_REGISTER_STRATEGY(S) \ |
| 406 | static class NfdAuto ## S ## StrategyRegistrationClass \ |
| 407 | { \ |
| 408 | public: \ |
| 409 | NfdAuto ## S ## StrategyRegistrationClass() \ |
| 410 | { \ |
| 411 | ::nfd::fw::Strategy::registerType<S>(); \ |
| 412 | } \ |
| 413 | } g_nfdAuto ## S ## StrategyRegistrationVariable |
| 414 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 415 | #endif // NFD_DAEMON_FW_STRATEGY_HPP |