blob: f9103837850f5b35f437fa80581e686c0b7a0830 [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 Shibb5105f2014-03-03 12:06:45 -070040 Strategy(Forwarder& forwarder, const Name& name);
Junxiao Shidbe71732014-02-21 22:23:28 -070041
Junxiao Shid3c792f2014-01-30 00:46:13 -070042 virtual
43 ~Strategy();
Junxiao Shidbe71732014-02-21 22:23:28 -070044
Junxiao Shibb5105f2014-03-03 12:06:45 -070045 /// a Name that represent the Strategy program
46 const Name&
47 getName() const;
48
Junxiao Shi679e9272014-02-15 20:10:21 -070049public: // triggers
50 /** \brief trigger after Interest is received
51 *
52 * The Interest:
53 * - does not violate Scope
54 * - is not looped
55 * - cannot be satisfied by ContentStore
56 * - is under a namespace managed by this strategy
57 *
58 * The strategy should decide whether and where to forward this Interest.
59 * - If the strategy decides to forward this Interest,
60 * invoke this->sendInterest one or more times, either now or shortly after
61 * - If strategy concludes that this Interest cannot be forwarded,
Junxiao Shi09498f02014-02-26 19:41:08 -070062 * invoke this->rejectPendingInterest so that PIT entry will be deleted shortly
Junxiao Shi82e7f582014-09-07 15:15:40 -070063 *
64 * \note The strategy is permitted to store a weak reference to fibEntry.
65 * Do not store a shared reference, because PIT entry may be deleted at any moment.
66 * fibEntry is passed by value to allow obtaining a weak reference from it.
67 * \note The strategy is permitted to store a shared reference to pitEntry.
68 * pitEntry is passed by value to reflect this fact.
Junxiao Shi679e9272014-02-15 20:10:21 -070069 */
Junxiao Shid3c792f2014-01-30 00:46:13 -070070 virtual void
71 afterReceiveInterest(const Face& inFace,
72 const Interest& interest,
73 shared_ptr<fib::Entry> fibEntry,
Junxiao Shi82e7f582014-09-07 15:15:40 -070074 shared_ptr<pit::Entry> pitEntry) = 0;
Junxiao Shidbe71732014-02-21 22:23:28 -070075
Junxiao Shi22be22c2014-02-16 22:53:48 -070076 /** \brief trigger before PIT entry is satisfied
77 *
Junxiao Shi82e7f582014-09-07 15:15:40 -070078 * This trigger is invoked when an incoming Data satisfies the PIT entry.
79 * It can be invoked even if the PIT entry has already been satisfied.
80 *
Junxiao Shi22be22c2014-02-16 22:53:48 -070081 * In this base class this method does nothing.
Junxiao Shi82e7f582014-09-07 15:15:40 -070082 *
83 * \note The strategy is permitted to store a shared reference to pitEntry.
84 * pitEntry is passed by value to reflect this fact.
Junxiao Shi22be22c2014-02-16 22:53:48 -070085 */
86 virtual void
Junxiao Shi82e7f582014-09-07 15:15:40 -070087 beforeSatisfyInterest(shared_ptr<pit::Entry> pitEntry,
88 const Face& inFace, const Data& data);
Junxiao Shidbe71732014-02-21 22:23:28 -070089
Junxiao Shi679e9272014-02-15 20:10:21 -070090 /** \brief trigger before PIT entry expires
91 *
92 * PIT entry expires when InterestLifetime has elapsed for all InRecords,
93 * and it is not satisfied by an incoming Data.
94 *
95 * This trigger is not invoked for PIT entry already satisfied.
96 *
97 * In this base class this method does nothing.
Junxiao Shi82e7f582014-09-07 15:15:40 -070098 *
99 * \note The strategy is permitted to store a shared reference to pitEntry.
100 * pitEntry is passed by value to reflect this fact.
Junxiao Shi679e9272014-02-15 20:10:21 -0700101 */
102 virtual void
103 beforeExpirePendingInterest(shared_ptr<pit::Entry> pitEntry);
Junxiao Shidbe71732014-02-21 22:23:28 -0700104
Junxiao Shid3c792f2014-01-30 00:46:13 -0700105protected: // actions
106 /// send Interest to outFace
Junxiao Shi727ed292014-02-19 23:26:45 -0700107 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700108 sendInterest(shared_ptr<pit::Entry> pitEntry,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700109 shared_ptr<Face> outFace,
110 bool wantNewNonce = false);
Junxiao Shidbe71732014-02-21 22:23:28 -0700111
Junxiao Shid3c792f2014-01-30 00:46:13 -0700112 /** \brief decide that a pending Interest cannot be forwarded
Junxiao Shi679e9272014-02-15 20:10:21 -0700113 *
Junxiao Shid3c792f2014-01-30 00:46:13 -0700114 * This shall not be called if the pending Interest has been
115 * forwarded earlier, and does not need to be resent now.
116 */
Junxiao Shi727ed292014-02-19 23:26:45 -0700117 VIRTUAL_WITH_TESTS void
Junxiao Shi09498f02014-02-26 19:41:08 -0700118 rejectPendingInterest(shared_ptr<pit::Entry> pitEntry);
Junxiao Shidbe71732014-02-21 22:23:28 -0700119
120protected: // accessors
121 MeasurementsAccessor&
122 getMeasurements();
123
Junxiao Shi2d9bdc82014-03-02 20:55:42 -0700124 shared_ptr<Face>
125 getFace(FaceId id);
126
Junxiao Shid3c792f2014-01-30 00:46:13 -0700127private:
Junxiao Shibb5105f2014-03-03 12:06:45 -0700128 Name m_name;
129
Junxiao Shi679e9272014-02-15 20:10:21 -0700130 /** \brief reference to the forwarder
131 *
132 * Triggers can access forwarder indirectly via actions.
133 */
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700134 Forwarder& m_forwarder;
Junxiao Shidbe71732014-02-21 22:23:28 -0700135
136 MeasurementsAccessor m_measurements;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700137};
138
Junxiao Shibb5105f2014-03-03 12:06:45 -0700139inline const Name&
140Strategy::getName() const
141{
142 return m_name;
143}
144
Junxiao Shi2d9bdc82014-03-02 20:55:42 -0700145inline void
146Strategy::sendInterest(shared_ptr<pit::Entry> pitEntry,
Junxiao Shid938a6b2014-05-11 23:40:29 -0700147 shared_ptr<Face> outFace,
148 bool wantNewNonce)
Junxiao Shi2d9bdc82014-03-02 20:55:42 -0700149{
Junxiao Shid938a6b2014-05-11 23:40:29 -0700150 m_forwarder.onOutgoingInterest(pitEntry, *outFace, wantNewNonce);
Junxiao Shi2d9bdc82014-03-02 20:55:42 -0700151}
152
153inline void
154Strategy::rejectPendingInterest(shared_ptr<pit::Entry> pitEntry)
155{
156 m_forwarder.onInterestReject(pitEntry);
157}
158
Junxiao Shidbe71732014-02-21 22:23:28 -0700159inline MeasurementsAccessor&
160Strategy::getMeasurements()
161{
162 return m_measurements;
163}
164
Junxiao Shi2d9bdc82014-03-02 20:55:42 -0700165inline shared_ptr<Face>
166Strategy::getFace(FaceId id)
167{
168 return m_forwarder.getFace(id);
169}
170
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700171} // namespace fw
Junxiao Shid3c792f2014-01-30 00:46:13 -0700172} // namespace nfd
173
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700174#endif // NFD_DAEMON_FW_STRATEGY_HPP