blob: c6e352956884071ad27f2b88b836889c4cbc6057 [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
Junxiao Shib289cc12014-03-15 12:19:05 -070035 const ForwarderCounters&
36 getCounters() const;
37
Junxiao Shia4f2be82014-03-02 22:56:41 -070038public: // faces
39 FaceTable&
40 getFaceTable();
41
42 /** \brief get existing Face
43 *
44 * shortcut to .getFaceTable().get(face)
45 */
46 shared_ptr<Face>
47 getFace(FaceId id) const;
48
49 /** \brief add new Face
50 *
51 * shortcut to .getFaceTable().add(face)
52 */
Junxiao Shi8c8d2182014-01-30 22:33:00 -070053 void
54 addFace(shared_ptr<Face> face);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080055
Junxiao Shia4f2be82014-03-02 22:56:41 -070056 /** \brief remove existing Face
57 *
58 * shortcut to .getFaceTable().remove(face)
59 */
Alexander Afanasyev33b72772014-01-26 23:22:58 -080060 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070061 removeFace(shared_ptr<Face> face);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080062
Junxiao Shia4f2be82014-03-02 22:56:41 -070063public: // forwarding entrypoints and tables
Alexander Afanasyev33b72772014-01-26 23:22:58 -080064 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070065 onInterest(Face& face, const Interest& interest);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080066
67 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070068 onData(Face& face, const Data& data);
Junxiao Shic041ca32014-02-25 20:01:15 -070069
Junxiao Shi8c8d2182014-01-30 22:33:00 -070070 Fib&
71 getFib();
Junxiao Shic041ca32014-02-25 20:01:15 -070072
Junxiao Shi8c8d2182014-01-30 22:33:00 -070073 Pit&
74 getPit();
Junxiao Shic041ca32014-02-25 20:01:15 -070075
Junxiao Shi8c8d2182014-01-30 22:33:00 -070076 Cs&
77 getCs();
Junxiao Shic041ca32014-02-25 20:01:15 -070078
Junxiao Shidbe71732014-02-21 22:23:28 -070079 Measurements&
80 getMeasurements();
Junxiao Shi8c8d2182014-01-30 22:33:00 -070081
Junxiao Shibb5105f2014-03-03 12:06:45 -070082 StrategyChoice&
83 getStrategyChoice();
84
Junxiao Shi88884492014-02-15 15:57:43 -070085PUBLIC_WITH_TESTS_ELSE_PRIVATE: // pipelines
Junxiao Shid3c792f2014-01-30 00:46:13 -070086 /** \brief incoming Interest pipeline
87 */
Junxiao Shi88884492014-02-15 15:57:43 -070088 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -070089 onIncomingInterest(Face& inFace, const Interest& interest);
90
91 /** \brief Interest loop pipeline
92 */
Junxiao Shi88884492014-02-15 15:57:43 -070093 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -070094 onInterestLoop(Face& inFace, const Interest& interest,
95 shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -070096
Junxiao Shid3c792f2014-01-30 00:46:13 -070097 /** \brief outgoing Interest pipeline
98 */
Junxiao Shi88884492014-02-15 15:57:43 -070099 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700100 onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace);
Junxiao Shic041ca32014-02-25 20:01:15 -0700101
Junxiao Shi09498f02014-02-26 19:41:08 -0700102 /** \brief Interest reject pipeline
Junxiao Shid3c792f2014-01-30 00:46:13 -0700103 */
Junxiao Shi88884492014-02-15 15:57:43 -0700104 VIRTUAL_WITH_TESTS void
Junxiao Shi09498f02014-02-26 19:41:08 -0700105 onInterestReject(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700106
Junxiao Shid3c792f2014-01-30 00:46:13 -0700107 /** \brief Interest unsatisfied pipeline
108 */
Junxiao Shi88884492014-02-15 15:57:43 -0700109 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700110 onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700111
Junxiao Shid3c792f2014-01-30 00:46:13 -0700112 /** \brief incoming Data pipeline
113 */
Junxiao Shi88884492014-02-15 15:57:43 -0700114 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700115 onIncomingData(Face& inFace, const Data& data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700116
Junxiao Shid3c792f2014-01-30 00:46:13 -0700117 /** \brief Data unsolicited pipeline
118 */
Junxiao Shi88884492014-02-15 15:57:43 -0700119 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700120 onDataUnsolicited(Face& inFace, const Data& data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700121
Junxiao Shid3c792f2014-01-30 00:46:13 -0700122 /** \brief outgoing Data pipeline
123 */
Junxiao Shi88884492014-02-15 15:57:43 -0700124 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700125 onOutgoingData(const Data& data, Face& outFace);
126
Junxiao Shi88884492014-02-15 15:57:43 -0700127PROTECTED_WITH_TESTS_ELSE_PRIVATE:
128 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700129 setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700130
Junxiao Shi88884492014-02-15 15:57:43 -0700131 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700132 setStragglerTimer(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700133
Junxiao Shi88884492014-02-15 15:57:43 -0700134 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700135 cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700136
Junxiao Shif3c07812014-03-11 21:48:49 -0700137 /// call trigger (method) on the effective strategy of pitEntry
138#ifdef WITH_TESTS
139 virtual void
140 dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, function<void(fw::Strategy*)> trigger);
141#else
142 template<class Function>
143 void
144 dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, Function trigger);
145#endif
Junxiao Shid3c792f2014-01-30 00:46:13 -0700146
147private:
Junxiao Shib289cc12014-03-15 12:19:05 -0700148 ForwarderCounters m_counters;
149
Junxiao Shia4f2be82014-03-02 22:56:41 -0700150 FaceTable m_faceTable;
HangZhangad4afd12014-03-01 11:03:08 +0800151
Junxiao Shibb5105f2014-03-03 12:06:45 -0700152 // tables
153 NameTree m_nameTree;
154 Fib m_fib;
155 Pit m_pit;
156 Cs m_cs;
157 Measurements m_measurements;
158 StrategyChoice m_strategyChoice;
159
Junxiao Shif3c07812014-03-11 21:48:49 -0700160 static const ndn::Milliseconds DEFAULT_INTEREST_LIFETIME;
161 static const Name LOCALHOST_NAME;
Junxiao Shic041ca32014-02-25 20:01:15 -0700162
Junxiao Shid3c792f2014-01-30 00:46:13 -0700163 // allow Strategy (base class) to enter pipelines
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700164 friend class fw::Strategy;
Alexander Afanasyev33b72772014-01-26 23:22:58 -0800165};
166
Junxiao Shib289cc12014-03-15 12:19:05 -0700167inline const ForwarderCounters&
168Forwarder::getCounters() const
169{
170 return m_counters;
171}
172
Junxiao Shia4f2be82014-03-02 22:56:41 -0700173inline FaceTable&
174Forwarder::getFaceTable()
175{
176 return m_faceTable;
177}
178
179inline shared_ptr<Face>
180Forwarder::getFace(FaceId id) const
181{
182 return m_faceTable.get(id);
183}
184
185inline void
186Forwarder::addFace(shared_ptr<Face> face)
187{
188 m_faceTable.add(face);
189}
190
191inline void
192Forwarder::removeFace(shared_ptr<Face> face)
193{
194 m_faceTable.remove(face);
195}
196
197inline void
198Forwarder::onInterest(Face& face, const Interest& interest)
199{
200 this->onIncomingInterest(face, interest);
201}
202
203inline void
204Forwarder::onData(Face& face, const Data& data)
205{
206 this->onIncomingData(face, data);
207}
208
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700209inline Fib&
210Forwarder::getFib()
211{
212 return m_fib;
213}
214
215inline Pit&
216Forwarder::getPit()
217{
218 return m_pit;
219}
220
221inline Cs&
222Forwarder::getCs()
223{
224 return m_cs;
225}
226
Junxiao Shidbe71732014-02-21 22:23:28 -0700227inline Measurements&
228Forwarder::getMeasurements()
229{
230 return m_measurements;
231}
232
Junxiao Shibb5105f2014-03-03 12:06:45 -0700233inline StrategyChoice&
234Forwarder::getStrategyChoice()
235{
236 return m_strategyChoice;
237}
Junxiao Shia4f2be82014-03-02 22:56:41 -0700238
Junxiao Shif3c07812014-03-11 21:48:49 -0700239#ifdef WITH_TESTS
240inline void
241Forwarder::dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, function<void(fw::Strategy*)> trigger)
242#else
243template<class Function>
244inline void
245Forwarder::dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, Function trigger)
246#endif
247{
248 fw::Strategy& strategy = m_strategyChoice.findEffectiveStrategy(*pitEntry);
249 trigger(&strategy);
250}
251
Junxiao Shid3c792f2014-01-30 00:46:13 -0700252} // namespace nfd
Alexander Afanasyev33b72772014-01-26 23:22:58 -0800253
254#endif // NFD_FW_FORWARDER_HPP