blob: c3898c6e14d91b3e99cc5fa014e55994bd3cbc83 [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 -070056public: // forwarding entrypoints and tables
Alexander Afanasyev33b72772014-01-26 23:22:58 -080057 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070058 onInterest(Face& face, const Interest& interest);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080059
60 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070061 onData(Face& face, const Data& data);
Junxiao Shic041ca32014-02-25 20:01:15 -070062
Junxiao Shiea48d8b2014-03-16 13:53:47 -070063 NameTree&
64 getNameTree();
65
Junxiao Shi8c8d2182014-01-30 22:33:00 -070066 Fib&
67 getFib();
Junxiao Shic041ca32014-02-25 20:01:15 -070068
Junxiao Shi8c8d2182014-01-30 22:33:00 -070069 Pit&
70 getPit();
Junxiao Shic041ca32014-02-25 20:01:15 -070071
Junxiao Shi8c8d2182014-01-30 22:33:00 -070072 Cs&
73 getCs();
Junxiao Shic041ca32014-02-25 20:01:15 -070074
Junxiao Shidbe71732014-02-21 22:23:28 -070075 Measurements&
76 getMeasurements();
Junxiao Shi8c8d2182014-01-30 22:33:00 -070077
Junxiao Shibb5105f2014-03-03 12:06:45 -070078 StrategyChoice&
79 getStrategyChoice();
80
Junxiao Shi88884492014-02-15 15:57:43 -070081PUBLIC_WITH_TESTS_ELSE_PRIVATE: // pipelines
Junxiao Shid3c792f2014-01-30 00:46:13 -070082 /** \brief incoming Interest pipeline
83 */
Junxiao Shi88884492014-02-15 15:57:43 -070084 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -070085 onIncomingInterest(Face& inFace, const Interest& interest);
86
87 /** \brief Interest loop pipeline
88 */
Junxiao Shi88884492014-02-15 15:57:43 -070089 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -070090 onInterestLoop(Face& inFace, const Interest& interest,
91 shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -070092
Junxiao Shid3c792f2014-01-30 00:46:13 -070093 /** \brief outgoing Interest pipeline
94 */
Junxiao Shi88884492014-02-15 15:57:43 -070095 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -070096 onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace);
Junxiao Shic041ca32014-02-25 20:01:15 -070097
Junxiao Shi09498f02014-02-26 19:41:08 -070098 /** \brief Interest reject pipeline
Junxiao Shid3c792f2014-01-30 00:46:13 -070099 */
Junxiao Shi88884492014-02-15 15:57:43 -0700100 VIRTUAL_WITH_TESTS void
Junxiao Shi09498f02014-02-26 19:41:08 -0700101 onInterestReject(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700102
Junxiao Shid3c792f2014-01-30 00:46:13 -0700103 /** \brief Interest unsatisfied pipeline
104 */
Junxiao Shi88884492014-02-15 15:57:43 -0700105 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700106 onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700107
Junxiao Shid3c792f2014-01-30 00:46:13 -0700108 /** \brief incoming Data pipeline
109 */
Junxiao Shi88884492014-02-15 15:57:43 -0700110 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700111 onIncomingData(Face& inFace, const Data& data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700112
Junxiao Shid3c792f2014-01-30 00:46:13 -0700113 /** \brief Data unsolicited pipeline
114 */
Junxiao Shi88884492014-02-15 15:57:43 -0700115 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700116 onDataUnsolicited(Face& inFace, const Data& data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700117
Junxiao Shid3c792f2014-01-30 00:46:13 -0700118 /** \brief outgoing Data pipeline
119 */
Junxiao Shi88884492014-02-15 15:57:43 -0700120 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700121 onOutgoingData(const Data& data, Face& outFace);
122
Junxiao Shi88884492014-02-15 15:57:43 -0700123PROTECTED_WITH_TESTS_ELSE_PRIVATE:
124 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700125 setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700126
Junxiao Shi88884492014-02-15 15:57:43 -0700127 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700128 setStragglerTimer(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 cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700132
Junxiao Shif3c07812014-03-11 21:48:49 -0700133 /// call trigger (method) on the effective strategy of pitEntry
134#ifdef WITH_TESTS
135 virtual void
136 dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, function<void(fw::Strategy*)> trigger);
137#else
138 template<class Function>
139 void
140 dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, Function trigger);
141#endif
Junxiao Shid3c792f2014-01-30 00:46:13 -0700142
143private:
Junxiao Shib289cc12014-03-15 12:19:05 -0700144 ForwarderCounters m_counters;
145
Junxiao Shia4f2be82014-03-02 22:56:41 -0700146 FaceTable m_faceTable;
HangZhangad4afd12014-03-01 11:03:08 +0800147
Junxiao Shibb5105f2014-03-03 12:06:45 -0700148 // tables
149 NameTree m_nameTree;
150 Fib m_fib;
151 Pit m_pit;
152 Cs m_cs;
153 Measurements m_measurements;
154 StrategyChoice m_strategyChoice;
155
Junxiao Shif3c07812014-03-11 21:48:49 -0700156 static const Name LOCALHOST_NAME;
Junxiao Shic041ca32014-02-25 20:01:15 -0700157
Junxiao Shid3c792f2014-01-30 00:46:13 -0700158 // allow Strategy (base class) to enter pipelines
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700159 friend class fw::Strategy;
Alexander Afanasyev33b72772014-01-26 23:22:58 -0800160};
161
Junxiao Shib289cc12014-03-15 12:19:05 -0700162inline const ForwarderCounters&
163Forwarder::getCounters() const
164{
165 return m_counters;
166}
167
Junxiao Shia4f2be82014-03-02 22:56:41 -0700168inline FaceTable&
169Forwarder::getFaceTable()
170{
171 return m_faceTable;
172}
173
174inline shared_ptr<Face>
175Forwarder::getFace(FaceId id) const
176{
177 return m_faceTable.get(id);
178}
179
180inline void
181Forwarder::addFace(shared_ptr<Face> face)
182{
183 m_faceTable.add(face);
184}
185
186inline void
Junxiao Shia4f2be82014-03-02 22:56:41 -0700187Forwarder::onInterest(Face& face, const Interest& interest)
188{
189 this->onIncomingInterest(face, interest);
190}
191
192inline void
193Forwarder::onData(Face& face, const Data& data)
194{
195 this->onIncomingData(face, data);
196}
197
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700198inline NameTree&
199Forwarder::getNameTree()
200{
201 return m_nameTree;
202}
203
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700204inline Fib&
205Forwarder::getFib()
206{
207 return m_fib;
208}
209
210inline Pit&
211Forwarder::getPit()
212{
213 return m_pit;
214}
215
216inline Cs&
217Forwarder::getCs()
218{
219 return m_cs;
220}
221
Junxiao Shidbe71732014-02-21 22:23:28 -0700222inline Measurements&
223Forwarder::getMeasurements()
224{
225 return m_measurements;
226}
227
Junxiao Shibb5105f2014-03-03 12:06:45 -0700228inline StrategyChoice&
229Forwarder::getStrategyChoice()
230{
231 return m_strategyChoice;
232}
Junxiao Shia4f2be82014-03-02 22:56:41 -0700233
Junxiao Shif3c07812014-03-11 21:48:49 -0700234#ifdef WITH_TESTS
235inline void
236Forwarder::dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, function<void(fw::Strategy*)> trigger)
237#else
238template<class Function>
239inline void
240Forwarder::dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, Function trigger)
241#endif
242{
243 fw::Strategy& strategy = m_strategyChoice.findEffectiveStrategy(*pitEntry);
244 trigger(&strategy);
245}
246
Junxiao Shid3c792f2014-01-30 00:46:13 -0700247} // namespace nfd
Alexander Afanasyev33b72772014-01-26 23:22:58 -0800248
249#endif // NFD_FW_FORWARDER_HPP