Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 | faf3eb0 | 2015-02-16 10:50:36 -0700 | [diff] [blame] | 30 | #include "strategy-registry.hpp" |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 31 | #include "table/measurements-accessor.hpp" |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 32 | |
| 33 | namespace nfd { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 34 | namespace fw { |
| 35 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 36 | /** \brief represents a forwarding strategy |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 37 | */ |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 38 | class Strategy : public enable_shared_from_this<Strategy>, noncopyable |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 39 | { |
| 40 | public: |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 41 | /** \brief construct a strategy instance |
| 42 | * \param forwarder a reference to the Forwarder, used to enable actions and accessors. |
| 43 | * Strategy subclasses should pass this reference, |
| 44 | * and should not keep a reference themselves. |
| 45 | * \param name the strategy Name. |
| 46 | * It's recommended to include a version number as the last component. |
| 47 | */ |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 48 | Strategy(Forwarder& forwarder, const Name& name); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 49 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 50 | virtual |
| 51 | ~Strategy(); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 52 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 53 | /// a Name that represent the Strategy program |
| 54 | const Name& |
| 55 | getName() const; |
| 56 | |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 57 | public: // triggers |
| 58 | /** \brief trigger after Interest is received |
| 59 | * |
| 60 | * The Interest: |
| 61 | * - does not violate Scope |
| 62 | * - is not looped |
| 63 | * - cannot be satisfied by ContentStore |
| 64 | * - is under a namespace managed by this strategy |
| 65 | * |
| 66 | * The strategy should decide whether and where to forward this Interest. |
| 67 | * - If the strategy decides to forward this Interest, |
| 68 | * invoke this->sendInterest one or more times, either now or shortly after |
| 69 | * - If strategy concludes that this Interest cannot be forwarded, |
Junxiao Shi | 09498f0 | 2014-02-26 19:41:08 -0700 | [diff] [blame] | 70 | * invoke this->rejectPendingInterest so that PIT entry will be deleted shortly |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 71 | * |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 72 | * \note The strategy is permitted to store a shared reference to pitEntry. |
| 73 | * pitEntry is passed by value to reflect this fact. |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 74 | */ |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 75 | virtual void |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 76 | afterReceiveInterest(const Face& inFace, const Interest& interest, |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 77 | shared_ptr<pit::Entry> pitEntry) = 0; |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 78 | |
Junxiao Shi | 22be22c | 2014-02-16 22:53:48 -0700 | [diff] [blame] | 79 | /** \brief trigger before PIT entry is satisfied |
| 80 | * |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 81 | * This trigger is invoked when an incoming Data satisfies the PIT entry. |
| 82 | * It can be invoked even if the PIT entry has already been satisfied. |
| 83 | * |
Junxiao Shi | 22be22c | 2014-02-16 22:53:48 -0700 | [diff] [blame] | 84 | * In this base class this method does nothing. |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 85 | * |
| 86 | * \note The strategy is permitted to store a shared reference to pitEntry. |
| 87 | * pitEntry is passed by value to reflect this fact. |
Junxiao Shi | 22be22c | 2014-02-16 22:53:48 -0700 | [diff] [blame] | 88 | */ |
| 89 | virtual void |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 90 | beforeSatisfyInterest(shared_ptr<pit::Entry> pitEntry, |
| 91 | const Face& inFace, const Data& data); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 92 | |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 93 | /** \brief trigger before PIT entry expires |
| 94 | * |
| 95 | * PIT entry expires when InterestLifetime has elapsed for all InRecords, |
| 96 | * and it is not satisfied by an incoming Data. |
| 97 | * |
| 98 | * This trigger is not invoked for PIT entry already satisfied. |
| 99 | * |
| 100 | * In this base class this method does nothing. |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 101 | * |
| 102 | * \note The strategy is permitted to store a shared reference to pitEntry. |
| 103 | * pitEntry is passed by value to reflect this fact. |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 104 | */ |
| 105 | virtual void |
| 106 | beforeExpirePendingInterest(shared_ptr<pit::Entry> pitEntry); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 107 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 108 | /** \brief trigger after Nack is received |
| 109 | * |
| 110 | * This trigger is invoked when an incoming Nack is received in response to |
| 111 | * an forwarded Interest. |
| 112 | * The Nack has been confirmed to be a response to the last Interest forwarded |
| 113 | * to that upstream, i.e. the PIT out-record exists and has a matching Nonce. |
| 114 | * The NackHeader has been recorded in the PIT out-record. |
| 115 | * |
| 116 | * In this base class this method does nothing. |
| 117 | * |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 118 | * \note The strategy is permitted to store a shared reference to pitEntry. |
| 119 | * pitEntry is passed by value to reflect this fact. |
| 120 | */ |
| 121 | virtual void |
| 122 | afterReceiveNack(const Face& inFace, const lp::Nack& nack, |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 123 | shared_ptr<pit::Entry> pitEntry); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 124 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 125 | protected: // actions |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 126 | /** \brief send Interest to outFace |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 127 | * \param pitEntry PIT entry |
| 128 | * \param outFace face through which to send out the Interest |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 129 | * \param wantNewNonce if true, a new Nonce will be generated, |
| 130 | * rather than reusing a Nonce from one of the PIT in-records |
| 131 | */ |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 132 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame^] | 133 | sendInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace, |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 134 | bool wantNewNonce = false); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 135 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 136 | /** \brief decide that a pending Interest cannot be forwarded |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 137 | * \param pitEntry PIT entry |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 138 | * |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 139 | * This shall not be called if the pending Interest has been |
| 140 | * forwarded earlier, and does not need to be resent now. |
| 141 | */ |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 142 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | 09498f0 | 2014-02-26 19:41:08 -0700 | [diff] [blame] | 143 | rejectPendingInterest(shared_ptr<pit::Entry> pitEntry); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 144 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 145 | /** \brief send Nack to outFace |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 146 | * \param pitEntry PIT entry |
| 147 | * \param outFace face through which to send out the Nack |
| 148 | * \param header Nack header |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 149 | * |
| 150 | * The outFace must have a PIT in-record, otherwise this method has no effect. |
| 151 | */ |
| 152 | VIRTUAL_WITH_TESTS void |
| 153 | sendNack(shared_ptr<pit::Entry> pitEntry, const Face& outFace, |
| 154 | const lp::NackHeader& header); |
| 155 | |
| 156 | /** \brief send Nack to every face that has an in-record, |
| 157 | * except those in \p exceptFaces |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 158 | * \param pitEntry PIT entry |
| 159 | * \param header NACK header |
| 160 | * \param exceptFaces list of faces that should be excluded from sending Nacks |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 161 | * \note This is not an action, but a helper that invokes the sendNack action. |
| 162 | */ |
| 163 | void |
| 164 | sendNacks(shared_ptr<pit::Entry> pitEntry, const lp::NackHeader& header, |
| 165 | std::initializer_list<const Face*> exceptFaces = std::initializer_list<const Face*>()); |
| 166 | |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 167 | protected: // accessors |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 168 | const fib::Entry& |
| 169 | lookupFib(const pit::Entry& pitEntry); |
| 170 | |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 171 | MeasurementsAccessor& |
| 172 | getMeasurements(); |
| 173 | |
Junxiao Shi | 2d9bdc8 | 2014-03-02 20:55:42 -0700 | [diff] [blame] | 174 | shared_ptr<Face> |
| 175 | getFace(FaceId id); |
| 176 | |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 177 | const FaceTable& |
| 178 | getFaceTable(); |
| 179 | |
| 180 | protected: // accessors |
| 181 | signal::Signal<FaceTable, shared_ptr<Face>>& afterAddFace; |
| 182 | signal::Signal<FaceTable, shared_ptr<Face>>& beforeRemoveFace; |
| 183 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 184 | private: |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 185 | Name m_name; |
| 186 | |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 187 | /** \brief reference to the forwarder |
| 188 | * |
| 189 | * Triggers can access forwarder indirectly via actions. |
| 190 | */ |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 191 | Forwarder& m_forwarder; |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 192 | |
| 193 | MeasurementsAccessor m_measurements; |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 194 | }; |
| 195 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 196 | inline const Name& |
| 197 | Strategy::getName() const |
| 198 | { |
| 199 | return m_name; |
| 200 | } |
| 201 | |
Junxiao Shi | 2d9bdc8 | 2014-03-02 20:55:42 -0700 | [diff] [blame] | 202 | inline void |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame^] | 203 | Strategy::sendInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace, bool wantNewNonce) |
Junxiao Shi | 2d9bdc8 | 2014-03-02 20:55:42 -0700 | [diff] [blame] | 204 | { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame^] | 205 | m_forwarder.onOutgoingInterest(pitEntry, outFace, wantNewNonce); |
Junxiao Shi | 2d9bdc8 | 2014-03-02 20:55:42 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | inline void |
| 209 | Strategy::rejectPendingInterest(shared_ptr<pit::Entry> pitEntry) |
| 210 | { |
| 211 | m_forwarder.onInterestReject(pitEntry); |
| 212 | } |
| 213 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 214 | inline void |
| 215 | Strategy::sendNack(shared_ptr<pit::Entry> pitEntry, const Face& outFace, |
| 216 | const lp::NackHeader& header) |
| 217 | { |
| 218 | m_forwarder.onOutgoingNack(pitEntry, outFace, header); |
| 219 | } |
| 220 | |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 221 | inline const fib::Entry& |
| 222 | Strategy::lookupFib(const pit::Entry& pitEntry) |
| 223 | { |
| 224 | return m_forwarder.lookupFib(pitEntry); |
| 225 | } |
| 226 | |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 227 | inline MeasurementsAccessor& |
| 228 | Strategy::getMeasurements() |
| 229 | { |
| 230 | return m_measurements; |
| 231 | } |
| 232 | |
Junxiao Shi | 2d9bdc8 | 2014-03-02 20:55:42 -0700 | [diff] [blame] | 233 | inline shared_ptr<Face> |
| 234 | Strategy::getFace(FaceId id) |
| 235 | { |
| 236 | return m_forwarder.getFace(id); |
| 237 | } |
| 238 | |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 239 | inline const FaceTable& |
| 240 | Strategy::getFaceTable() |
| 241 | { |
| 242 | return m_forwarder.getFaceTable(); |
| 243 | } |
| 244 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 245 | } // namespace fw |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 246 | } // namespace nfd |
| 247 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 248 | #endif // NFD_DAEMON_FW_STRATEGY_HPP |