blob: 51a75dc57f8acd25797702b9f10b96478d137db9 [file] [log] [blame]
Alexander Afanasyev33b72772014-01-26 23:22:58 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NFD_FW_FORWARDER_HPP
8#define NFD_FW_FORWARDER_HPP
9
10#include "common.hpp"
Junxiao Shid3c792f2014-01-30 00:46:13 -070011#include "core/scheduler.hpp"
Junxiao Shib289cc12014-03-15 12:19:05 -070012#include "forwarder-counter.hpp"
Junxiao Shia4f2be82014-03-02 22:56:41 -070013#include "face-table.hpp"
Junxiao Shid3c792f2014-01-30 00:46:13 -070014#include "table/fib.hpp"
15#include "table/pit.hpp"
16#include "table/cs.hpp"
Junxiao Shidbe71732014-02-21 22:23:28 -070017#include "table/measurements.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070018#include "table/strategy-choice.hpp"
Alexander Afanasyev33b72772014-01-26 23:22:58 -080019
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080020namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080021
Junxiao Shi2d9bdc82014-03-02 20:55:42 -070022namespace fw {
23class Strategy;
24} // namespace fw
25
26/** \brief main class of NFD
Junxiao Shic041ca32014-02-25 20:01:15 -070027 *
Junxiao Shi2d9bdc82014-03-02 20:55:42 -070028 * Forwarder owns all faces and tables, and implements forwarding pipelines.
Alexander Afanasyev33b72772014-01-26 23:22:58 -080029 */
30class Forwarder
31{
32public:
Junxiao Shic041ca32014-02-25 20:01:15 -070033 Forwarder();
Alexander Afanasyev33b72772014-01-26 23:22:58 -080034
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060035 VIRTUAL_WITH_TESTS
36 ~Forwarder();
37
Junxiao Shib289cc12014-03-15 12:19:05 -070038 const ForwarderCounters&
39 getCounters() const;
40
Junxiao Shia4f2be82014-03-02 22:56:41 -070041public: // faces
42 FaceTable&
43 getFaceTable();
44
45 /** \brief get existing Face
46 *
47 * shortcut to .getFaceTable().get(face)
48 */
49 shared_ptr<Face>
50 getFace(FaceId id) const;
51
52 /** \brief add new Face
53 *
54 * shortcut to .getFaceTable().add(face)
55 */
Junxiao Shi8c8d2182014-01-30 22:33:00 -070056 void
57 addFace(shared_ptr<Face> face);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080058
Junxiao Shia4f2be82014-03-02 22:56:41 -070059public: // forwarding entrypoints and tables
Alexander Afanasyev33b72772014-01-26 23:22:58 -080060 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070061 onInterest(Face& face, const Interest& interest);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080062
63 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070064 onData(Face& face, const Data& data);
Junxiao Shic041ca32014-02-25 20:01:15 -070065
Junxiao Shiea48d8b2014-03-16 13:53:47 -070066 NameTree&
67 getNameTree();
68
Junxiao Shi8c8d2182014-01-30 22:33:00 -070069 Fib&
70 getFib();
Junxiao Shic041ca32014-02-25 20:01:15 -070071
Junxiao Shi8c8d2182014-01-30 22:33:00 -070072 Pit&
73 getPit();
Junxiao Shic041ca32014-02-25 20:01:15 -070074
Junxiao Shi8c8d2182014-01-30 22:33:00 -070075 Cs&
76 getCs();
Junxiao Shic041ca32014-02-25 20:01:15 -070077
Junxiao Shidbe71732014-02-21 22:23:28 -070078 Measurements&
79 getMeasurements();
Junxiao Shi8c8d2182014-01-30 22:33:00 -070080
Junxiao Shibb5105f2014-03-03 12:06:45 -070081 StrategyChoice&
82 getStrategyChoice();
83
Junxiao Shi88884492014-02-15 15:57:43 -070084PUBLIC_WITH_TESTS_ELSE_PRIVATE: // pipelines
Junxiao Shid3c792f2014-01-30 00:46:13 -070085 /** \brief incoming Interest pipeline
86 */
Junxiao Shi88884492014-02-15 15:57:43 -070087 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -070088 onIncomingInterest(Face& inFace, const Interest& interest);
89
90 /** \brief Interest loop pipeline
91 */
Junxiao Shi88884492014-02-15 15:57:43 -070092 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -070093 onInterestLoop(Face& inFace, const Interest& interest,
94 shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -070095
Junxiao Shid3c792f2014-01-30 00:46:13 -070096 /** \brief outgoing Interest pipeline
97 */
Junxiao Shi88884492014-02-15 15:57:43 -070098 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -070099 onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700100
Junxiao Shi09498f02014-02-26 19:41:08 -0700101 /** \brief Interest reject pipeline
Junxiao Shid3c792f2014-01-30 00:46:13 -0700102 */
Junxiao Shi88884492014-02-15 15:57:43 -0700103 VIRTUAL_WITH_TESTS void
Junxiao Shi09498f02014-02-26 19:41:08 -0700104 onInterestReject(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700105
Junxiao Shid3c792f2014-01-30 00:46:13 -0700106 /** \brief Interest unsatisfied pipeline
107 */
Junxiao Shi88884492014-02-15 15:57:43 -0700108 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700109 onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700110
Junxiao Shid3c792f2014-01-30 00:46:13 -0700111 /** \brief incoming Data pipeline
112 */
Junxiao Shi88884492014-02-15 15:57:43 -0700113 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700114 onIncomingData(Face& inFace, const Data& data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700115
Junxiao Shid3c792f2014-01-30 00:46:13 -0700116 /** \brief Data unsolicited pipeline
117 */
Junxiao Shi88884492014-02-15 15:57:43 -0700118 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700119 onDataUnsolicited(Face& inFace, const Data& data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700120
Junxiao Shid3c792f2014-01-30 00:46:13 -0700121 /** \brief outgoing Data pipeline
122 */
Junxiao Shi88884492014-02-15 15:57:43 -0700123 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700124 onOutgoingData(const Data& data, Face& outFace);
125
Junxiao Shi88884492014-02-15 15:57:43 -0700126PROTECTED_WITH_TESTS_ELSE_PRIVATE:
127 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700128 setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700129
Junxiao Shi88884492014-02-15 15:57:43 -0700130 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700131 setStragglerTimer(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700132
Junxiao Shi88884492014-02-15 15:57:43 -0700133 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700134 cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700135
Junxiao Shif3c07812014-03-11 21:48:49 -0700136 /// call trigger (method) on the effective strategy of pitEntry
137#ifdef WITH_TESTS
138 virtual void
139 dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, function<void(fw::Strategy*)> trigger);
140#else
141 template<class Function>
142 void
143 dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, Function trigger);
144#endif
Junxiao Shid3c792f2014-01-30 00:46:13 -0700145
146private:
Junxiao Shib289cc12014-03-15 12:19:05 -0700147 ForwarderCounters m_counters;
148
Junxiao Shia4f2be82014-03-02 22:56:41 -0700149 FaceTable m_faceTable;
HangZhangad4afd12014-03-01 11:03:08 +0800150
Junxiao Shibb5105f2014-03-03 12:06:45 -0700151 // tables
152 NameTree m_nameTree;
153 Fib m_fib;
154 Pit m_pit;
155 Cs m_cs;
156 Measurements m_measurements;
157 StrategyChoice m_strategyChoice;
158
Junxiao Shif3c07812014-03-11 21:48:49 -0700159 static const Name LOCALHOST_NAME;
Junxiao Shic041ca32014-02-25 20:01:15 -0700160
Junxiao Shid3c792f2014-01-30 00:46:13 -0700161 // allow Strategy (base class) to enter pipelines
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700162 friend class fw::Strategy;
Alexander Afanasyev33b72772014-01-26 23:22:58 -0800163};
164
Junxiao Shib289cc12014-03-15 12:19:05 -0700165inline const ForwarderCounters&
166Forwarder::getCounters() const
167{
168 return m_counters;
169}
170
Junxiao Shia4f2be82014-03-02 22:56:41 -0700171inline FaceTable&
172Forwarder::getFaceTable()
173{
174 return m_faceTable;
175}
176
177inline shared_ptr<Face>
178Forwarder::getFace(FaceId id) const
179{
180 return m_faceTable.get(id);
181}
182
183inline void
184Forwarder::addFace(shared_ptr<Face> face)
185{
186 m_faceTable.add(face);
187}
188
189inline void
Junxiao Shia4f2be82014-03-02 22:56:41 -0700190Forwarder::onInterest(Face& face, const Interest& interest)
191{
192 this->onIncomingInterest(face, interest);
193}
194
195inline void
196Forwarder::onData(Face& face, const Data& data)
197{
198 this->onIncomingData(face, data);
199}
200
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700201inline NameTree&
202Forwarder::getNameTree()
203{
204 return m_nameTree;
205}
206
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700207inline Fib&
208Forwarder::getFib()
209{
210 return m_fib;
211}
212
213inline Pit&
214Forwarder::getPit()
215{
216 return m_pit;
217}
218
219inline Cs&
220Forwarder::getCs()
221{
222 return m_cs;
223}
224
Junxiao Shidbe71732014-02-21 22:23:28 -0700225inline Measurements&
226Forwarder::getMeasurements()
227{
228 return m_measurements;
229}
230
Junxiao Shibb5105f2014-03-03 12:06:45 -0700231inline StrategyChoice&
232Forwarder::getStrategyChoice()
233{
234 return m_strategyChoice;
235}
Junxiao Shia4f2be82014-03-02 22:56:41 -0700236
Junxiao Shif3c07812014-03-11 21:48:49 -0700237#ifdef WITH_TESTS
238inline void
239Forwarder::dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, function<void(fw::Strategy*)> trigger)
240#else
241template<class Function>
242inline void
243Forwarder::dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, Function trigger)
244#endif
245{
246 fw::Strategy& strategy = m_strategyChoice.findEffectiveStrategy(*pitEntry);
247 trigger(&strategy);
248}
249
Junxiao Shid3c792f2014-01-30 00:46:13 -0700250} // namespace nfd
Alexander Afanasyev33b72772014-01-26 23:22:58 -0800251
252#endif // NFD_FW_FORWARDER_HPP