blob: 813cce088f226e0283cd577c53d6fd0a91411d87 [file] [log] [blame]
Junxiao Shid3c792f2014-01-30 00:46:13 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi82e7f582014-09-07 15:15:40 -07003 * 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 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 Shibb5105f2014-03-03 12:06:45 -070037class Strategy : public enable_shared_from_this<Strategy>, noncopyable
Junxiao Shid3c792f2014-01-30 00:46:13 -070038{
39public:
Junxiao Shie93d6a32014-09-07 16:13:22 -070040 /** \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 Shibb5105f2014-03-03 12:06:45 -070047 Strategy(Forwarder& forwarder, const Name& name);
Junxiao Shidbe71732014-02-21 22:23:28 -070048
Junxiao Shid3c792f2014-01-30 00:46:13 -070049 virtual
50 ~Strategy();
Junxiao Shidbe71732014-02-21 22:23:28 -070051
Junxiao Shibb5105f2014-03-03 12:06:45 -070052 /// a Name that represent the Strategy program
53 const Name&
54 getName() const;
55
Junxiao Shi679e9272014-02-15 20:10:21 -070056public: // 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 Shi09498f02014-02-26 19:41:08 -070069 * invoke this->rejectPendingInterest so that PIT entry will be deleted shortly
Junxiao Shi82e7f582014-09-07 15:15:40 -070070 *
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 Shi679e9272014-02-15 20:10:21 -070076 */
Junxiao Shid3c792f2014-01-30 00:46:13 -070077 virtual void
78 afterReceiveInterest(const Face& inFace,
79 const Interest& interest,
80 shared_ptr<fib::Entry> fibEntry,
Junxiao Shi82e7f582014-09-07 15:15:40 -070081 shared_ptr<pit::Entry> pitEntry) = 0;
Junxiao Shidbe71732014-02-21 22:23:28 -070082
Junxiao Shi22be22c2014-02-16 22:53:48 -070083 /** \brief trigger before PIT entry is satisfied
84 *
Junxiao Shi82e7f582014-09-07 15:15:40 -070085 * 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 Shi22be22c2014-02-16 22:53:48 -070088 * In this base class this method does nothing.
Junxiao Shi82e7f582014-09-07 15:15:40 -070089 *
90 * \note The strategy is permitted to store a shared reference to pitEntry.
91 * pitEntry is passed by value to reflect this fact.
Junxiao Shi22be22c2014-02-16 22:53:48 -070092 */
93 virtual void
Junxiao Shi82e7f582014-09-07 15:15:40 -070094 beforeSatisfyInterest(shared_ptr<pit::Entry> pitEntry,
95 const Face& inFace, const Data& data);
Junxiao Shidbe71732014-02-21 22:23:28 -070096
Junxiao Shi679e9272014-02-15 20:10:21 -070097 /** \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 Shi82e7f582014-09-07 15:15:40 -0700105 *
106 * \note The strategy is permitted to store a shared reference to pitEntry.
107 * pitEntry is passed by value to reflect this fact.
Junxiao Shi679e9272014-02-15 20:10:21 -0700108 */
109 virtual void
110 beforeExpirePendingInterest(shared_ptr<pit::Entry> pitEntry);
Junxiao Shidbe71732014-02-21 22:23:28 -0700111
Junxiao Shid3c792f2014-01-30 00:46:13 -0700112protected: // actions
113 /// send Interest to outFace
Junxiao Shi727ed292014-02-19 23:26:45 -0700114 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700115 sendInterest(shared_ptr<pit::Entry> pitEntry,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700116 shared_ptr<Face> outFace,
117 bool wantNewNonce = false);
Junxiao Shidbe71732014-02-21 22:23:28 -0700118
Junxiao Shid3c792f2014-01-30 00:46:13 -0700119 /** \brief decide that a pending Interest cannot be forwarded
Junxiao Shi679e9272014-02-15 20:10:21 -0700120 *
Junxiao Shid3c792f2014-01-30 00:46:13 -0700121 * This shall not be called if the pending Interest has been
122 * forwarded earlier, and does not need to be resent now.
123 */
Junxiao Shi727ed292014-02-19 23:26:45 -0700124 VIRTUAL_WITH_TESTS void
Junxiao Shi09498f02014-02-26 19:41:08 -0700125 rejectPendingInterest(shared_ptr<pit::Entry> pitEntry);
Junxiao Shidbe71732014-02-21 22:23:28 -0700126
127protected: // accessors
128 MeasurementsAccessor&
129 getMeasurements();
130
Junxiao Shi2d9bdc82014-03-02 20:55:42 -0700131 shared_ptr<Face>
132 getFace(FaceId id);
133
Junxiao Shi49e11e72014-12-14 19:46:05 -0700134 const FaceTable&
135 getFaceTable();
136
137protected: // accessors
138 signal::Signal<FaceTable, shared_ptr<Face>>& afterAddFace;
139 signal::Signal<FaceTable, shared_ptr<Face>>& beforeRemoveFace;
140
Junxiao Shid3c792f2014-01-30 00:46:13 -0700141private:
Junxiao Shibb5105f2014-03-03 12:06:45 -0700142 Name m_name;
143
Junxiao Shi679e9272014-02-15 20:10:21 -0700144 /** \brief reference to the forwarder
145 *
146 * Triggers can access forwarder indirectly via actions.
147 */
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700148 Forwarder& m_forwarder;
Junxiao Shidbe71732014-02-21 22:23:28 -0700149
150 MeasurementsAccessor m_measurements;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700151};
152
Junxiao Shibb5105f2014-03-03 12:06:45 -0700153inline const Name&
154Strategy::getName() const
155{
156 return m_name;
157}
158
Junxiao Shi2d9bdc82014-03-02 20:55:42 -0700159inline void
160Strategy::sendInterest(shared_ptr<pit::Entry> pitEntry,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700161 shared_ptr<Face> outFace,
162 bool wantNewNonce)
Junxiao Shi2d9bdc82014-03-02 20:55:42 -0700163{
Junxiao Shid938a6b2014-05-11 23:40:29 -0700164 m_forwarder.onOutgoingInterest(pitEntry, *outFace, wantNewNonce);
Junxiao Shi2d9bdc82014-03-02 20:55:42 -0700165}
166
167inline void
168Strategy::rejectPendingInterest(shared_ptr<pit::Entry> pitEntry)
169{
170 m_forwarder.onInterestReject(pitEntry);
171}
172
Junxiao Shidbe71732014-02-21 22:23:28 -0700173inline MeasurementsAccessor&
174Strategy::getMeasurements()
175{
176 return m_measurements;
177}
178
Junxiao Shi2d9bdc82014-03-02 20:55:42 -0700179inline shared_ptr<Face>
180Strategy::getFace(FaceId id)
181{
182 return m_forwarder.getFace(id);
183}
184
Junxiao Shi49e11e72014-12-14 19:46:05 -0700185inline const FaceTable&
186Strategy::getFaceTable()
187{
188 return m_forwarder.getFaceTable();
189}
190
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700191} // namespace fw
Junxiao Shid3c792f2014-01-30 00:46:13 -0700192} // namespace nfd
193
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700194#endif // NFD_DAEMON_FW_STRATEGY_HPP