blob: 62612f2fdaf1e9260cfed5421e510e12ae76316b [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 Shi8c8d2182014-01-30 22:33:00 -070063 Fib&
64 getFib();
Junxiao Shic041ca32014-02-25 20:01:15 -070065
Junxiao Shi8c8d2182014-01-30 22:33:00 -070066 Pit&
67 getPit();
Junxiao Shic041ca32014-02-25 20:01:15 -070068
Junxiao Shi8c8d2182014-01-30 22:33:00 -070069 Cs&
70 getCs();
Junxiao Shic041ca32014-02-25 20:01:15 -070071
Junxiao Shidbe71732014-02-21 22:23:28 -070072 Measurements&
73 getMeasurements();
Junxiao Shi8c8d2182014-01-30 22:33:00 -070074
Junxiao Shibb5105f2014-03-03 12:06:45 -070075 StrategyChoice&
76 getStrategyChoice();
77
Junxiao Shi88884492014-02-15 15:57:43 -070078PUBLIC_WITH_TESTS_ELSE_PRIVATE: // pipelines
Junxiao Shid3c792f2014-01-30 00:46:13 -070079 /** \brief incoming Interest pipeline
80 */
Junxiao Shi88884492014-02-15 15:57:43 -070081 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -070082 onIncomingInterest(Face& inFace, const Interest& interest);
83
84 /** \brief Interest loop pipeline
85 */
Junxiao Shi88884492014-02-15 15:57:43 -070086 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -070087 onInterestLoop(Face& inFace, const Interest& interest,
88 shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -070089
Junxiao Shid3c792f2014-01-30 00:46:13 -070090 /** \brief outgoing Interest pipeline
91 */
Junxiao Shi88884492014-02-15 15:57:43 -070092 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -070093 onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace);
Junxiao Shic041ca32014-02-25 20:01:15 -070094
Junxiao Shi09498f02014-02-26 19:41:08 -070095 /** \brief Interest reject pipeline
Junxiao Shid3c792f2014-01-30 00:46:13 -070096 */
Junxiao Shi88884492014-02-15 15:57:43 -070097 VIRTUAL_WITH_TESTS void
Junxiao Shi09498f02014-02-26 19:41:08 -070098 onInterestReject(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -070099
Junxiao Shid3c792f2014-01-30 00:46:13 -0700100 /** \brief Interest unsatisfied pipeline
101 */
Junxiao Shi88884492014-02-15 15:57:43 -0700102 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700103 onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700104
Junxiao Shid3c792f2014-01-30 00:46:13 -0700105 /** \brief incoming Data pipeline
106 */
Junxiao Shi88884492014-02-15 15:57:43 -0700107 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700108 onIncomingData(Face& inFace, const Data& data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700109
Junxiao Shid3c792f2014-01-30 00:46:13 -0700110 /** \brief Data unsolicited pipeline
111 */
Junxiao Shi88884492014-02-15 15:57:43 -0700112 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700113 onDataUnsolicited(Face& inFace, const Data& data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700114
Junxiao Shid3c792f2014-01-30 00:46:13 -0700115 /** \brief outgoing Data pipeline
116 */
Junxiao Shi88884492014-02-15 15:57:43 -0700117 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700118 onOutgoingData(const Data& data, Face& outFace);
119
Junxiao Shi88884492014-02-15 15:57:43 -0700120PROTECTED_WITH_TESTS_ELSE_PRIVATE:
121 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700122 setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700123
Junxiao Shi88884492014-02-15 15:57:43 -0700124 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700125 setStragglerTimer(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 cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700129
Junxiao Shif3c07812014-03-11 21:48:49 -0700130 /// call trigger (method) on the effective strategy of pitEntry
131#ifdef WITH_TESTS
132 virtual void
133 dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, function<void(fw::Strategy*)> trigger);
134#else
135 template<class Function>
136 void
137 dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, Function trigger);
138#endif
Junxiao Shid3c792f2014-01-30 00:46:13 -0700139
140private:
Junxiao Shib289cc12014-03-15 12:19:05 -0700141 ForwarderCounters m_counters;
142
Junxiao Shia4f2be82014-03-02 22:56:41 -0700143 FaceTable m_faceTable;
HangZhangad4afd12014-03-01 11:03:08 +0800144
Junxiao Shibb5105f2014-03-03 12:06:45 -0700145 // tables
146 NameTree m_nameTree;
147 Fib m_fib;
148 Pit m_pit;
149 Cs m_cs;
150 Measurements m_measurements;
151 StrategyChoice m_strategyChoice;
152
Junxiao Shif3c07812014-03-11 21:48:49 -0700153 static const Name LOCALHOST_NAME;
Junxiao Shic041ca32014-02-25 20:01:15 -0700154
Junxiao Shid3c792f2014-01-30 00:46:13 -0700155 // allow Strategy (base class) to enter pipelines
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700156 friend class fw::Strategy;
Alexander Afanasyev33b72772014-01-26 23:22:58 -0800157};
158
Junxiao Shib289cc12014-03-15 12:19:05 -0700159inline const ForwarderCounters&
160Forwarder::getCounters() const
161{
162 return m_counters;
163}
164
Junxiao Shia4f2be82014-03-02 22:56:41 -0700165inline FaceTable&
166Forwarder::getFaceTable()
167{
168 return m_faceTable;
169}
170
171inline shared_ptr<Face>
172Forwarder::getFace(FaceId id) const
173{
174 return m_faceTable.get(id);
175}
176
177inline void
178Forwarder::addFace(shared_ptr<Face> face)
179{
180 m_faceTable.add(face);
181}
182
183inline void
Junxiao Shia4f2be82014-03-02 22:56:41 -0700184Forwarder::onInterest(Face& face, const Interest& interest)
185{
186 this->onIncomingInterest(face, interest);
187}
188
189inline void
190Forwarder::onData(Face& face, const Data& data)
191{
192 this->onIncomingData(face, data);
193}
194
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700195inline Fib&
196Forwarder::getFib()
197{
198 return m_fib;
199}
200
201inline Pit&
202Forwarder::getPit()
203{
204 return m_pit;
205}
206
207inline Cs&
208Forwarder::getCs()
209{
210 return m_cs;
211}
212
Junxiao Shidbe71732014-02-21 22:23:28 -0700213inline Measurements&
214Forwarder::getMeasurements()
215{
216 return m_measurements;
217}
218
Junxiao Shibb5105f2014-03-03 12:06:45 -0700219inline StrategyChoice&
220Forwarder::getStrategyChoice()
221{
222 return m_strategyChoice;
223}
Junxiao Shia4f2be82014-03-02 22:56:41 -0700224
Junxiao Shif3c07812014-03-11 21:48:49 -0700225#ifdef WITH_TESTS
226inline void
227Forwarder::dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, function<void(fw::Strategy*)> trigger)
228#else
229template<class Function>
230inline void
231Forwarder::dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, Function trigger)
232#endif
233{
234 fw::Strategy& strategy = m_strategyChoice.findEffectiveStrategy(*pitEntry);
235 trigger(&strategy);
236}
237
Junxiao Shid3c792f2014-01-30 00:46:13 -0700238} // namespace nfd
Alexander Afanasyev33b72772014-01-26 23:22:58 -0800239
240#endif // NFD_FW_FORWARDER_HPP