blob: 8e7c0d2554cc8931eaefac5da9db8fc8f406e617 [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 Shia4f2be82014-03-02 22:56:41 -070012#include "face-table.hpp"
Junxiao Shid3c792f2014-01-30 00:46:13 -070013#include "table/fib.hpp"
14#include "table/pit.hpp"
15#include "table/cs.hpp"
Junxiao Shidbe71732014-02-21 22:23:28 -070016#include "table/measurements.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070017#include "table/strategy-choice.hpp"
Alexander Afanasyev33b72772014-01-26 23:22:58 -080018
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080019namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080020
Junxiao Shi2d9bdc82014-03-02 20:55:42 -070021namespace fw {
22class Strategy;
23} // namespace fw
24
25/** \brief main class of NFD
Junxiao Shic041ca32014-02-25 20:01:15 -070026 *
Junxiao Shi2d9bdc82014-03-02 20:55:42 -070027 * Forwarder owns all faces and tables, and implements forwarding pipelines.
Alexander Afanasyev33b72772014-01-26 23:22:58 -080028 */
29class Forwarder
30{
31public:
Junxiao Shic041ca32014-02-25 20:01:15 -070032 Forwarder();
Alexander Afanasyev33b72772014-01-26 23:22:58 -080033
Junxiao Shia4f2be82014-03-02 22:56:41 -070034public: // faces
35 FaceTable&
36 getFaceTable();
37
38 /** \brief get existing Face
39 *
40 * shortcut to .getFaceTable().get(face)
41 */
42 shared_ptr<Face>
43 getFace(FaceId id) const;
44
45 /** \brief add new Face
46 *
47 * shortcut to .getFaceTable().add(face)
48 */
Junxiao Shi8c8d2182014-01-30 22:33:00 -070049 void
50 addFace(shared_ptr<Face> face);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080051
Junxiao Shia4f2be82014-03-02 22:56:41 -070052 /** \brief remove existing Face
53 *
54 * shortcut to .getFaceTable().remove(face)
55 */
Alexander Afanasyev33b72772014-01-26 23:22:58 -080056 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070057 removeFace(shared_ptr<Face> face);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080058
Junxiao Shia4f2be82014-03-02 22:56:41 -070059public: // forwarding entrypoints and tables
Alexander Afanasyev33b72772014-01-26 23:22:58 -080060 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070061 onInterest(Face& face, const Interest& interest);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080062
63 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070064 onData(Face& face, const Data& data);
Junxiao Shic041ca32014-02-25 20:01:15 -070065
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 Shia4f2be82014-03-02 22:56:41 -0700144 FaceTable m_faceTable;
HangZhangad4afd12014-03-01 11:03:08 +0800145
Junxiao Shibb5105f2014-03-03 12:06:45 -0700146 // tables
147 NameTree m_nameTree;
148 Fib m_fib;
149 Pit m_pit;
150 Cs m_cs;
151 Measurements m_measurements;
152 StrategyChoice m_strategyChoice;
153
Junxiao Shif3c07812014-03-11 21:48:49 -0700154 static const ndn::Milliseconds DEFAULT_INTEREST_LIFETIME;
155 static const Name LOCALHOST_NAME;
Junxiao Shic041ca32014-02-25 20:01:15 -0700156
Junxiao Shid3c792f2014-01-30 00:46:13 -0700157 // allow Strategy (base class) to enter pipelines
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700158 friend class fw::Strategy;
Alexander Afanasyev33b72772014-01-26 23:22:58 -0800159};
160
Junxiao Shia4f2be82014-03-02 22:56:41 -0700161inline FaceTable&
162Forwarder::getFaceTable()
163{
164 return m_faceTable;
165}
166
167inline shared_ptr<Face>
168Forwarder::getFace(FaceId id) const
169{
170 return m_faceTable.get(id);
171}
172
173inline void
174Forwarder::addFace(shared_ptr<Face> face)
175{
176 m_faceTable.add(face);
177}
178
179inline void
180Forwarder::removeFace(shared_ptr<Face> face)
181{
182 m_faceTable.remove(face);
183}
184
185inline void
186Forwarder::onInterest(Face& face, const Interest& interest)
187{
188 this->onIncomingInterest(face, interest);
189}
190
191inline void
192Forwarder::onData(Face& face, const Data& data)
193{
194 this->onIncomingData(face, data);
195}
196
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700197inline Fib&
198Forwarder::getFib()
199{
200 return m_fib;
201}
202
203inline Pit&
204Forwarder::getPit()
205{
206 return m_pit;
207}
208
209inline Cs&
210Forwarder::getCs()
211{
212 return m_cs;
213}
214
Junxiao Shidbe71732014-02-21 22:23:28 -0700215inline Measurements&
216Forwarder::getMeasurements()
217{
218 return m_measurements;
219}
220
Junxiao Shibb5105f2014-03-03 12:06:45 -0700221inline StrategyChoice&
222Forwarder::getStrategyChoice()
223{
224 return m_strategyChoice;
225}
Junxiao Shia4f2be82014-03-02 22:56:41 -0700226
Junxiao Shif3c07812014-03-11 21:48:49 -0700227#ifdef WITH_TESTS
228inline void
229Forwarder::dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, function<void(fw::Strategy*)> trigger)
230#else
231template<class Function>
232inline void
233Forwarder::dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, Function trigger)
234#endif
235{
236 fw::Strategy& strategy = m_strategyChoice.findEffectiveStrategy(*pitEntry);
237 trigger(&strategy);
238}
239
Junxiao Shid3c792f2014-01-30 00:46:13 -0700240} // namespace nfd
Alexander Afanasyev33b72772014-01-26 23:22:58 -0800241
242#endif // NFD_FW_FORWARDER_HPP