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