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 | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, Regents of the University of California, |
| 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 | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 37 | class Strategy : public enable_shared_from_this<Strategy>, noncopyable |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 38 | { |
| 39 | public: |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame^] | 40 | /** \brief construct a strategy instance |
| 41 | * \param forwarder a reference to the Forwarder, used to enable actions and accessors. |
| 42 | * Strategy subclasses should pass this reference, |
| 43 | * and should not keep a reference themselves. |
| 44 | * \param name the strategy Name. |
| 45 | * It's recommended to include a version number as the last component. |
| 46 | */ |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 47 | Strategy(Forwarder& forwarder, const Name& name); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 48 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 49 | virtual |
| 50 | ~Strategy(); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 51 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 52 | /// a Name that represent the Strategy program |
| 53 | const Name& |
| 54 | getName() const; |
| 55 | |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 56 | public: // triggers |
| 57 | /** \brief trigger after Interest is received |
| 58 | * |
| 59 | * The Interest: |
| 60 | * - does not violate Scope |
| 61 | * - is not looped |
| 62 | * - cannot be satisfied by ContentStore |
| 63 | * - is under a namespace managed by this strategy |
| 64 | * |
| 65 | * The strategy should decide whether and where to forward this Interest. |
| 66 | * - If the strategy decides to forward this Interest, |
| 67 | * invoke this->sendInterest one or more times, either now or shortly after |
| 68 | * - If strategy concludes that this Interest cannot be forwarded, |
Junxiao Shi | 09498f0 | 2014-02-26 19:41:08 -0700 | [diff] [blame] | 69 | * invoke this->rejectPendingInterest so that PIT entry will be deleted shortly |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 70 | * |
| 71 | * \note The strategy is permitted to store a weak reference to fibEntry. |
| 72 | * Do not store a shared reference, because PIT entry may be deleted at any moment. |
| 73 | * fibEntry is passed by value to allow obtaining a weak reference from it. |
| 74 | * \note The strategy is permitted to store a shared reference to pitEntry. |
| 75 | * pitEntry is passed by value to reflect this fact. |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 76 | */ |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 77 | virtual void |
| 78 | afterReceiveInterest(const Face& inFace, |
| 79 | const Interest& interest, |
| 80 | shared_ptr<fib::Entry> fibEntry, |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 81 | shared_ptr<pit::Entry> pitEntry) = 0; |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 82 | |
Junxiao Shi | 22be22c | 2014-02-16 22:53:48 -0700 | [diff] [blame] | 83 | /** \brief trigger before PIT entry is satisfied |
| 84 | * |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 85 | * This trigger is invoked when an incoming Data satisfies the PIT entry. |
| 86 | * It can be invoked even if the PIT entry has already been satisfied. |
| 87 | * |
Junxiao Shi | 22be22c | 2014-02-16 22:53:48 -0700 | [diff] [blame] | 88 | * In this base class this method does nothing. |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 89 | * |
| 90 | * \note The strategy is permitted to store a shared reference to pitEntry. |
| 91 | * pitEntry is passed by value to reflect this fact. |
Junxiao Shi | 22be22c | 2014-02-16 22:53:48 -0700 | [diff] [blame] | 92 | */ |
| 93 | virtual void |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 94 | beforeSatisfyInterest(shared_ptr<pit::Entry> pitEntry, |
| 95 | const Face& inFace, const Data& data); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 96 | |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 97 | /** \brief trigger before PIT entry expires |
| 98 | * |
| 99 | * PIT entry expires when InterestLifetime has elapsed for all InRecords, |
| 100 | * and it is not satisfied by an incoming Data. |
| 101 | * |
| 102 | * This trigger is not invoked for PIT entry already satisfied. |
| 103 | * |
| 104 | * In this base class this method does nothing. |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 105 | * |
| 106 | * \note The strategy is permitted to store a shared reference to pitEntry. |
| 107 | * pitEntry is passed by value to reflect this fact. |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 108 | */ |
| 109 | virtual void |
| 110 | beforeExpirePendingInterest(shared_ptr<pit::Entry> pitEntry); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 111 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 112 | protected: // actions |
| 113 | /// send Interest to outFace |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 114 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 115 | sendInterest(shared_ptr<pit::Entry> pitEntry, |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 116 | shared_ptr<Face> outFace, |
| 117 | bool wantNewNonce = false); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 118 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 119 | /** \brief decide that a pending Interest cannot be forwarded |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 120 | * |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 121 | * This shall not be called if the pending Interest has been |
| 122 | * forwarded earlier, and does not need to be resent now. |
| 123 | */ |
Junxiao Shi | 727ed29 | 2014-02-19 23:26:45 -0700 | [diff] [blame] | 124 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | 09498f0 | 2014-02-26 19:41:08 -0700 | [diff] [blame] | 125 | rejectPendingInterest(shared_ptr<pit::Entry> pitEntry); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 126 | |
| 127 | protected: // accessors |
| 128 | MeasurementsAccessor& |
| 129 | getMeasurements(); |
| 130 | |
Junxiao Shi | 2d9bdc8 | 2014-03-02 20:55:42 -0700 | [diff] [blame] | 131 | shared_ptr<Face> |
| 132 | getFace(FaceId id); |
| 133 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 134 | private: |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 135 | Name m_name; |
| 136 | |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 137 | /** \brief reference to the forwarder |
| 138 | * |
| 139 | * Triggers can access forwarder indirectly via actions. |
| 140 | */ |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 141 | Forwarder& m_forwarder; |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 142 | |
| 143 | MeasurementsAccessor m_measurements; |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 146 | inline const Name& |
| 147 | Strategy::getName() const |
| 148 | { |
| 149 | return m_name; |
| 150 | } |
| 151 | |
Junxiao Shi | 2d9bdc8 | 2014-03-02 20:55:42 -0700 | [diff] [blame] | 152 | inline void |
| 153 | Strategy::sendInterest(shared_ptr<pit::Entry> pitEntry, |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 154 | shared_ptr<Face> outFace, |
| 155 | bool wantNewNonce) |
Junxiao Shi | 2d9bdc8 | 2014-03-02 20:55:42 -0700 | [diff] [blame] | 156 | { |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 157 | m_forwarder.onOutgoingInterest(pitEntry, *outFace, wantNewNonce); |
Junxiao Shi | 2d9bdc8 | 2014-03-02 20:55:42 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | inline void |
| 161 | Strategy::rejectPendingInterest(shared_ptr<pit::Entry> pitEntry) |
| 162 | { |
| 163 | m_forwarder.onInterestReject(pitEntry); |
| 164 | } |
| 165 | |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 166 | inline MeasurementsAccessor& |
| 167 | Strategy::getMeasurements() |
| 168 | { |
| 169 | return m_measurements; |
| 170 | } |
| 171 | |
Junxiao Shi | 2d9bdc8 | 2014-03-02 20:55:42 -0700 | [diff] [blame] | 172 | inline shared_ptr<Face> |
| 173 | Strategy::getFace(FaceId id) |
| 174 | { |
| 175 | return m_forwarder.getFace(id); |
| 176 | } |
| 177 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 178 | } // namespace fw |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 179 | } // namespace nfd |
| 180 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 181 | #endif // NFD_DAEMON_FW_STRATEGY_HPP |