Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 1 | /* -*- 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 Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 11 | #include "core/scheduler.hpp" |
Junxiao Shi | b289cc1 | 2014-03-15 12:19:05 -0700 | [diff] [blame] | 12 | #include "forwarder-counter.hpp" |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 13 | #include "face-table.hpp" |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 14 | #include "table/fib.hpp" |
| 15 | #include "table/pit.hpp" |
| 16 | #include "table/cs.hpp" |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 17 | #include "table/measurements.hpp" |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 18 | #include "table/strategy-choice.hpp" |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 19 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 20 | namespace nfd { |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 21 | |
Junxiao Shi | 2d9bdc8 | 2014-03-02 20:55:42 -0700 | [diff] [blame] | 22 | namespace fw { |
| 23 | class Strategy; |
| 24 | } // namespace fw |
| 25 | |
| 26 | /** \brief main class of NFD |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 27 | * |
Junxiao Shi | 2d9bdc8 | 2014-03-02 20:55:42 -0700 | [diff] [blame] | 28 | * Forwarder owns all faces and tables, and implements forwarding pipelines. |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 29 | */ |
| 30 | class Forwarder |
| 31 | { |
| 32 | public: |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 33 | Forwarder(); |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 34 | |
Junxiao Shi | b289cc1 | 2014-03-15 12:19:05 -0700 | [diff] [blame] | 35 | const ForwarderCounters& |
| 36 | getCounters() const; |
| 37 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 38 | public: // 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 Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 53 | void |
| 54 | addFace(shared_ptr<Face> face); |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 55 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 56 | /** \brief remove existing Face |
| 57 | * |
| 58 | * shortcut to .getFaceTable().remove(face) |
| 59 | */ |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 60 | void |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 61 | removeFace(shared_ptr<Face> face); |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 62 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 63 | public: // forwarding entrypoints and tables |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 64 | void |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 65 | onInterest(Face& face, const Interest& interest); |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 66 | |
| 67 | void |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 68 | onData(Face& face, const Data& data); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 69 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 70 | Fib& |
| 71 | getFib(); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 72 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 73 | Pit& |
| 74 | getPit(); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 75 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 76 | Cs& |
| 77 | getCs(); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 78 | |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 79 | Measurements& |
| 80 | getMeasurements(); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 81 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 82 | StrategyChoice& |
| 83 | getStrategyChoice(); |
| 84 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 85 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: // pipelines |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 86 | /** \brief incoming Interest pipeline |
| 87 | */ |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 88 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 89 | onIncomingInterest(Face& inFace, const Interest& interest); |
| 90 | |
| 91 | /** \brief Interest loop pipeline |
| 92 | */ |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 93 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 94 | onInterestLoop(Face& inFace, const Interest& interest, |
| 95 | shared_ptr<pit::Entry> pitEntry); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 96 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 97 | /** \brief outgoing Interest pipeline |
| 98 | */ |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 99 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 100 | onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 101 | |
Junxiao Shi | 09498f0 | 2014-02-26 19:41:08 -0700 | [diff] [blame] | 102 | /** \brief Interest reject pipeline |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 103 | */ |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 104 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | 09498f0 | 2014-02-26 19:41:08 -0700 | [diff] [blame] | 105 | onInterestReject(shared_ptr<pit::Entry> pitEntry); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 106 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 107 | /** \brief Interest unsatisfied pipeline |
| 108 | */ |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 109 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 110 | onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 111 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 112 | /** \brief incoming Data pipeline |
| 113 | */ |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 114 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 115 | onIncomingData(Face& inFace, const Data& data); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 116 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 117 | /** \brief Data unsolicited pipeline |
| 118 | */ |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 119 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 120 | onDataUnsolicited(Face& inFace, const Data& data); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 121 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 122 | /** \brief outgoing Data pipeline |
| 123 | */ |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 124 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 125 | onOutgoingData(const Data& data, Face& outFace); |
| 126 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 127 | PROTECTED_WITH_TESTS_ELSE_PRIVATE: |
| 128 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 129 | setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 130 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 131 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 132 | setStragglerTimer(shared_ptr<pit::Entry> pitEntry); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 133 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 134 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 135 | cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 136 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 137 | /// 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 Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 146 | |
| 147 | private: |
Junxiao Shi | b289cc1 | 2014-03-15 12:19:05 -0700 | [diff] [blame] | 148 | ForwarderCounters m_counters; |
| 149 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 150 | FaceTable m_faceTable; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 151 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 152 | // 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 Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 160 | static const ndn::Milliseconds DEFAULT_INTEREST_LIFETIME; |
| 161 | static const Name LOCALHOST_NAME; |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame^] | 162 | static const Name LOCALHOP_NAME; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 163 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 164 | // allow Strategy (base class) to enter pipelines |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 165 | friend class fw::Strategy; |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 166 | }; |
| 167 | |
Junxiao Shi | b289cc1 | 2014-03-15 12:19:05 -0700 | [diff] [blame] | 168 | inline const ForwarderCounters& |
| 169 | Forwarder::getCounters() const |
| 170 | { |
| 171 | return m_counters; |
| 172 | } |
| 173 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 174 | inline FaceTable& |
| 175 | Forwarder::getFaceTable() |
| 176 | { |
| 177 | return m_faceTable; |
| 178 | } |
| 179 | |
| 180 | inline shared_ptr<Face> |
| 181 | Forwarder::getFace(FaceId id) const |
| 182 | { |
| 183 | return m_faceTable.get(id); |
| 184 | } |
| 185 | |
| 186 | inline void |
| 187 | Forwarder::addFace(shared_ptr<Face> face) |
| 188 | { |
| 189 | m_faceTable.add(face); |
| 190 | } |
| 191 | |
| 192 | inline void |
| 193 | Forwarder::removeFace(shared_ptr<Face> face) |
| 194 | { |
| 195 | m_faceTable.remove(face); |
| 196 | } |
| 197 | |
| 198 | inline void |
| 199 | Forwarder::onInterest(Face& face, const Interest& interest) |
| 200 | { |
| 201 | this->onIncomingInterest(face, interest); |
| 202 | } |
| 203 | |
| 204 | inline void |
| 205 | Forwarder::onData(Face& face, const Data& data) |
| 206 | { |
| 207 | this->onIncomingData(face, data); |
| 208 | } |
| 209 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 210 | inline Fib& |
| 211 | Forwarder::getFib() |
| 212 | { |
| 213 | return m_fib; |
| 214 | } |
| 215 | |
| 216 | inline Pit& |
| 217 | Forwarder::getPit() |
| 218 | { |
| 219 | return m_pit; |
| 220 | } |
| 221 | |
| 222 | inline Cs& |
| 223 | Forwarder::getCs() |
| 224 | { |
| 225 | return m_cs; |
| 226 | } |
| 227 | |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 228 | inline Measurements& |
| 229 | Forwarder::getMeasurements() |
| 230 | { |
| 231 | return m_measurements; |
| 232 | } |
| 233 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 234 | inline StrategyChoice& |
| 235 | Forwarder::getStrategyChoice() |
| 236 | { |
| 237 | return m_strategyChoice; |
| 238 | } |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 239 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 240 | #ifdef WITH_TESTS |
| 241 | inline void |
| 242 | Forwarder::dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, function<void(fw::Strategy*)> trigger) |
| 243 | #else |
| 244 | template<class Function> |
| 245 | inline void |
| 246 | Forwarder::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 Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 253 | } // namespace nfd |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 254 | |
| 255 | #endif // NFD_FW_FORWARDER_HPP |