blob: 2d4d3252a284b1b5dbbf064607d00a01bd79dc7b [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 Name LOCALHOST_NAME;
Junxiao Shic041ca32014-02-25 20:01:15 -0700161
Junxiao Shid3c792f2014-01-30 00:46:13 -0700162 // allow Strategy (base class) to enter pipelines
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700163 friend class fw::Strategy;
Alexander Afanasyev33b72772014-01-26 23:22:58 -0800164};
165
Junxiao Shib289cc12014-03-15 12:19:05 -0700166inline const ForwarderCounters&
167Forwarder::getCounters() const
168{
169 return m_counters;
170}
171
Junxiao Shia4f2be82014-03-02 22:56:41 -0700172inline FaceTable&
173Forwarder::getFaceTable()
174{
175 return m_faceTable;
176}
177
178inline shared_ptr<Face>
179Forwarder::getFace(FaceId id) const
180{
181 return m_faceTable.get(id);
182}
183
184inline void
185Forwarder::addFace(shared_ptr<Face> face)
186{
187 m_faceTable.add(face);
188}
189
190inline void
191Forwarder::removeFace(shared_ptr<Face> face)
192{
193 m_faceTable.remove(face);
194}
195
196inline void
197Forwarder::onInterest(Face& face, const Interest& interest)
198{
199 this->onIncomingInterest(face, interest);
200}
201
202inline void
203Forwarder::onData(Face& face, const Data& data)
204{
205 this->onIncomingData(face, data);
206}
207
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700208inline Fib&
209Forwarder::getFib()
210{
211 return m_fib;
212}
213
214inline Pit&
215Forwarder::getPit()
216{
217 return m_pit;
218}
219
220inline Cs&
221Forwarder::getCs()
222{
223 return m_cs;
224}
225
Junxiao Shidbe71732014-02-21 22:23:28 -0700226inline Measurements&
227Forwarder::getMeasurements()
228{
229 return m_measurements;
230}
231
Junxiao Shibb5105f2014-03-03 12:06:45 -0700232inline StrategyChoice&
233Forwarder::getStrategyChoice()
234{
235 return m_strategyChoice;
236}
Junxiao Shia4f2be82014-03-02 22:56:41 -0700237
Junxiao Shif3c07812014-03-11 21:48:49 -0700238#ifdef WITH_TESTS
239inline void
240Forwarder::dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, function<void(fw::Strategy*)> trigger)
241#else
242template<class Function>
243inline void
244Forwarder::dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, Function trigger)
245#endif
246{
247 fw::Strategy& strategy = m_strategyChoice.findEffectiveStrategy(*pitEntry);
248 trigger(&strategy);
249}
250
Junxiao Shid3c792f2014-01-30 00:46:13 -0700251} // namespace nfd
Alexander Afanasyev33b72772014-01-26 23:22:58 -0800252
253#endif // NFD_FW_FORWARDER_HPP