blob: a753f1568c79ef837f0c6e86bc6788ca123797f5 [file] [log] [blame]
Alexander Afanasyev33b72772014-01-26 23:22:58 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shia110f262014-10-12 12:35:20 -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 Shia110f262014-10-12 12:35:20 -070024 */
Alexander Afanasyev33b72772014-01-26 23:22:58 -080025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_FW_FORWARDER_HPP
27#define NFD_DAEMON_FW_FORWARDER_HPP
Alexander Afanasyev33b72772014-01-26 23:22:58 -080028
29#include "common.hpp"
Junxiao Shid3c792f2014-01-30 00:46:13 -070030#include "core/scheduler.hpp"
Junxiao Shi33152f12014-07-16 19:54:32 -070031#include "forwarder-counters.hpp"
Junxiao Shia4f2be82014-03-02 22:56:41 -070032#include "face-table.hpp"
Junxiao Shid3c792f2014-01-30 00:46:13 -070033#include "table/fib.hpp"
34#include "table/pit.hpp"
35#include "table/cs.hpp"
Junxiao Shidbe71732014-02-21 22:23:28 -070036#include "table/measurements.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070037#include "table/strategy-choice.hpp"
Junxiao Shia110f262014-10-12 12:35:20 -070038#include "table/dead-nonce-list.hpp"
Alexander Afanasyev33b72772014-01-26 23:22:58 -080039
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080040namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080041
Junxiao Shi2d9bdc82014-03-02 20:55:42 -070042namespace fw {
43class Strategy;
44} // namespace fw
45
46/** \brief main class of NFD
Junxiao Shic041ca32014-02-25 20:01:15 -070047 *
Junxiao Shi2d9bdc82014-03-02 20:55:42 -070048 * Forwarder owns all faces and tables, and implements forwarding pipelines.
Alexander Afanasyev33b72772014-01-26 23:22:58 -080049 */
50class Forwarder
51{
52public:
Junxiao Shic041ca32014-02-25 20:01:15 -070053 Forwarder();
Alexander Afanasyev33b72772014-01-26 23:22:58 -080054
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060055 VIRTUAL_WITH_TESTS
56 ~Forwarder();
57
Junxiao Shib289cc12014-03-15 12:19:05 -070058 const ForwarderCounters&
59 getCounters() const;
60
Junxiao Shia4f2be82014-03-02 22:56:41 -070061public: // faces
62 FaceTable&
63 getFaceTable();
64
65 /** \brief get existing Face
66 *
67 * shortcut to .getFaceTable().get(face)
68 */
69 shared_ptr<Face>
70 getFace(FaceId id) const;
71
72 /** \brief add new Face
73 *
74 * shortcut to .getFaceTable().add(face)
75 */
Junxiao Shi8c8d2182014-01-30 22:33:00 -070076 void
77 addFace(shared_ptr<Face> face);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080078
Junxiao Shia4f2be82014-03-02 22:56:41 -070079public: // forwarding entrypoints and tables
Alexander Afanasyev33b72772014-01-26 23:22:58 -080080 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070081 onInterest(Face& face, const Interest& interest);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080082
83 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070084 onData(Face& face, const Data& data);
Junxiao Shic041ca32014-02-25 20:01:15 -070085
Junxiao Shiea48d8b2014-03-16 13:53:47 -070086 NameTree&
87 getNameTree();
88
Junxiao Shi8c8d2182014-01-30 22:33:00 -070089 Fib&
90 getFib();
Junxiao Shic041ca32014-02-25 20:01:15 -070091
Junxiao Shi8c8d2182014-01-30 22:33:00 -070092 Pit&
93 getPit();
Junxiao Shic041ca32014-02-25 20:01:15 -070094
Junxiao Shi8c8d2182014-01-30 22:33:00 -070095 Cs&
96 getCs();
Junxiao Shic041ca32014-02-25 20:01:15 -070097
Junxiao Shidbe71732014-02-21 22:23:28 -070098 Measurements&
99 getMeasurements();
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700100
Junxiao Shibb5105f2014-03-03 12:06:45 -0700101 StrategyChoice&
102 getStrategyChoice();
103
Junxiao Shia110f262014-10-12 12:35:20 -0700104 DeadNonceList&
105 getDeadNonceList();
106
Junxiao Shi88884492014-02-15 15:57:43 -0700107PUBLIC_WITH_TESTS_ELSE_PRIVATE: // pipelines
Junxiao Shid3c792f2014-01-30 00:46:13 -0700108 /** \brief incoming Interest pipeline
109 */
Junxiao Shi88884492014-02-15 15:57:43 -0700110 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700111 onIncomingInterest(Face& inFace, const Interest& interest);
112
113 /** \brief Interest loop pipeline
114 */
Junxiao Shi88884492014-02-15 15:57:43 -0700115 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700116 onInterestLoop(Face& inFace, const Interest& interest,
117 shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700118
Junxiao Shid3c792f2014-01-30 00:46:13 -0700119 /** \brief outgoing Interest pipeline
120 */
Junxiao Shi88884492014-02-15 15:57:43 -0700121 VIRTUAL_WITH_TESTS void
Junxiao Shid938a6b2014-05-11 23:40:29 -0700122 onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace,
123 bool wantNewNonce = false);
Junxiao Shic041ca32014-02-25 20:01:15 -0700124
Junxiao Shi09498f02014-02-26 19:41:08 -0700125 /** \brief Interest reject pipeline
Junxiao Shid3c792f2014-01-30 00:46:13 -0700126 */
Junxiao Shi88884492014-02-15 15:57:43 -0700127 VIRTUAL_WITH_TESTS void
Junxiao Shi09498f02014-02-26 19:41:08 -0700128 onInterestReject(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700129
Junxiao Shid3c792f2014-01-30 00:46:13 -0700130 /** \brief Interest unsatisfied pipeline
131 */
Junxiao Shi88884492014-02-15 15:57:43 -0700132 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700133 onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700134
Junxiao Shia110f262014-10-12 12:35:20 -0700135 /** \brief Interest finalize pipeline
136 * \param isSatisfied whether the Interest has been satisfied
137 * \param dataFreshnessPeriod FreshnessPeriod of satisfying Data
138 */
139 VIRTUAL_WITH_TESTS void
140 onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
141 const time::milliseconds& dataFreshnessPeriod = time::milliseconds(-1));
142
Junxiao Shid3c792f2014-01-30 00:46:13 -0700143 /** \brief incoming Data pipeline
144 */
Junxiao Shi88884492014-02-15 15:57:43 -0700145 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700146 onIncomingData(Face& inFace, const Data& data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700147
Junxiao Shid3c792f2014-01-30 00:46:13 -0700148 /** \brief Data unsolicited pipeline
149 */
Junxiao Shi88884492014-02-15 15:57:43 -0700150 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700151 onDataUnsolicited(Face& inFace, const Data& data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700152
Junxiao Shid3c792f2014-01-30 00:46:13 -0700153 /** \brief outgoing Data pipeline
154 */
Junxiao Shi88884492014-02-15 15:57:43 -0700155 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700156 onOutgoingData(const Data& data, Face& outFace);
157
Junxiao Shi88884492014-02-15 15:57:43 -0700158PROTECTED_WITH_TESTS_ELSE_PRIVATE:
159 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700160 setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700161
Junxiao Shi88884492014-02-15 15:57:43 -0700162 VIRTUAL_WITH_TESTS void
Junxiao Shia110f262014-10-12 12:35:20 -0700163 setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
164 const time::milliseconds& dataFreshnessPeriod = time::milliseconds(-1));
Junxiao Shic041ca32014-02-25 20:01:15 -0700165
Junxiao Shi88884492014-02-15 15:57:43 -0700166 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700167 cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700168
Junxiao Shia110f262014-10-12 12:35:20 -0700169 /** \brief insert Nonce to Dead Nonce List if necessary
170 * \param upstream if null, insert Nonces from all OutRecords;
171 * if not null, insert Nonce only on the OutRecord of this face
172 */
173 VIRTUAL_WITH_TESTS void
174 insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
175 const time::milliseconds& dataFreshnessPeriod,
176 Face* upstream);
177
Junxiao Shif3c07812014-03-11 21:48:49 -0700178 /// call trigger (method) on the effective strategy of pitEntry
179#ifdef WITH_TESTS
180 virtual void
181 dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, function<void(fw::Strategy*)> trigger);
182#else
183 template<class Function>
184 void
185 dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, Function trigger);
186#endif
Junxiao Shid3c792f2014-01-30 00:46:13 -0700187
188private:
Junxiao Shib289cc12014-03-15 12:19:05 -0700189 ForwarderCounters m_counters;
190
Junxiao Shia4f2be82014-03-02 22:56:41 -0700191 FaceTable m_faceTable;
HangZhangad4afd12014-03-01 11:03:08 +0800192
Junxiao Shibb5105f2014-03-03 12:06:45 -0700193 // tables
194 NameTree m_nameTree;
195 Fib m_fib;
196 Pit m_pit;
197 Cs m_cs;
198 Measurements m_measurements;
199 StrategyChoice m_strategyChoice;
Junxiao Shia110f262014-10-12 12:35:20 -0700200 DeadNonceList m_deadNonceList;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700201
Junxiao Shif3c07812014-03-11 21:48:49 -0700202 static const Name LOCALHOST_NAME;
Junxiao Shic041ca32014-02-25 20:01:15 -0700203
Junxiao Shid3c792f2014-01-30 00:46:13 -0700204 // allow Strategy (base class) to enter pipelines
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700205 friend class fw::Strategy;
Alexander Afanasyev33b72772014-01-26 23:22:58 -0800206};
207
Junxiao Shib289cc12014-03-15 12:19:05 -0700208inline const ForwarderCounters&
209Forwarder::getCounters() const
210{
211 return m_counters;
212}
213
Junxiao Shia4f2be82014-03-02 22:56:41 -0700214inline FaceTable&
215Forwarder::getFaceTable()
216{
217 return m_faceTable;
218}
219
220inline shared_ptr<Face>
221Forwarder::getFace(FaceId id) const
222{
223 return m_faceTable.get(id);
224}
225
226inline void
227Forwarder::addFace(shared_ptr<Face> face)
228{
229 m_faceTable.add(face);
230}
231
232inline void
Junxiao Shia4f2be82014-03-02 22:56:41 -0700233Forwarder::onInterest(Face& face, const Interest& interest)
234{
235 this->onIncomingInterest(face, interest);
236}
237
238inline void
239Forwarder::onData(Face& face, const Data& data)
240{
241 this->onIncomingData(face, data);
242}
243
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700244inline NameTree&
245Forwarder::getNameTree()
246{
247 return m_nameTree;
248}
249
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700250inline Fib&
251Forwarder::getFib()
252{
253 return m_fib;
254}
255
256inline Pit&
257Forwarder::getPit()
258{
259 return m_pit;
260}
261
262inline Cs&
263Forwarder::getCs()
264{
265 return m_cs;
266}
267
Junxiao Shidbe71732014-02-21 22:23:28 -0700268inline Measurements&
269Forwarder::getMeasurements()
270{
271 return m_measurements;
272}
273
Junxiao Shibb5105f2014-03-03 12:06:45 -0700274inline StrategyChoice&
275Forwarder::getStrategyChoice()
276{
277 return m_strategyChoice;
278}
Junxiao Shia4f2be82014-03-02 22:56:41 -0700279
Junxiao Shia110f262014-10-12 12:35:20 -0700280inline DeadNonceList&
281Forwarder::getDeadNonceList()
282{
283 return m_deadNonceList;
284}
285
Junxiao Shif3c07812014-03-11 21:48:49 -0700286#ifdef WITH_TESTS
287inline void
288Forwarder::dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, function<void(fw::Strategy*)> trigger)
289#else
290template<class Function>
291inline void
292Forwarder::dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, Function trigger)
293#endif
294{
295 fw::Strategy& strategy = m_strategyChoice.findEffectiveStrategy(*pitEntry);
296 trigger(&strategy);
297}
298
Junxiao Shid3c792f2014-01-30 00:46:13 -0700299} // namespace nfd
Alexander Afanasyev33b72772014-01-26 23:22:58 -0800300
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700301#endif // NFD_DAEMON_FW_FORWARDER_HPP