ndn-embeds 0.1.0
Lightweight NDN protocol stack for embedded systems
Loading...
Searching...
No Matches
forwarder.hpp
Go to the documentation of this file.
1
12#pragma once
13
14#include "ndn/common.hpp"
15#include "ndn/cs.hpp"
16#include "ndn/data.hpp"
17#include "ndn/face.hpp"
18#include "ndn/fib.hpp"
19#include "ndn/interest.hpp"
20#include "ndn/pit.hpp"
21
22namespace ndn {
23
32using InterestCallback = std::function<void(const Interest&, FaceId)>;
33
41using DataCallback = std::function<void(const Data&)>;
42
50using TimeoutCallback = std::function<void(const Interest&)>;
51
53constexpr size_t FORWARDER_MAX_FACES = 8;
54
56constexpr size_t FORWARDER_MAX_PREFIXES = 16;
57
99class Forwarder {
100public:
104 Forwarder();
105
114 Error init(size_t csMaxEntries = CS_DEFAULT_ENTRIES);
115
126 Error addFace(Face* face);
127
135 void removeFace(FaceId faceId);
154 Error expressInterest(const Interest& interest, DataCallback onData,
155 TimeoutCallback onTimeout = nullptr);
156
165 Error sendInterest(const Interest& interest);
182 Error registerPrefix(const Name& prefix, InterestCallback callback);
183
191 Error registerPrefix(std::string_view prefixUri, InterestCallback callback);
192
197 void unregisterPrefix(const Name& prefix);
198
208 Error putData(const Data& data);
223 Error addRoute(const Name& prefix, FaceId faceId, uint8_t cost = 0);
224
233 Error addRoute(std::string_view prefixUri, FaceId faceId, uint8_t cost = 0);
246 void processEvents();
256 struct Stats {
257 uint32_t interestsReceived = 0;
258 uint32_t interestsSent = 0;
259 uint32_t dataReceived = 0;
260 uint32_t dataSent = 0;
261 uint32_t cacheHits = 0;
262 uint32_t cacheMisses = 0;
263 };
264
269 const Stats& stats() const { return stats_; }
280 Pit& pit() { return pit_; }
281
286 ContentStore& cs() { return cs_; }
287
292 Fib& fib() { return fib_; }
295private:
302 void onPacketReceived(FaceId faceId, const uint8_t* data, size_t len);
303
309 void onInterestReceived(FaceId faceId, const Interest& interest);
310
316 void onDataReceived(FaceId faceId, const Data& data);
317
323 void forwardInterest(const Interest& interest, FaceId incomingFace);
324
330 void forwardData(const Data& data, PitEntry* pitEntry);
331
332 Pit pit_;
333 ContentStore cs_;
334 Fib fib_;
335
336 std::array<Face*, FORWARDER_MAX_FACES> faces_{};
337 size_t numFaces_ = 0;
338
342 struct PrefixRegistration {
343 Name prefix;
344 InterestCallback callback;
345 bool inUse = false;
346 };
347 std::array<PrefixRegistration, FORWARDER_MAX_PREFIXES> prefixRegs_{};
348
352 struct PendingInterest {
353 Interest interest;
354 DataCallback dataCallback;
355 TimeoutCallback timeoutCallback;
356 bool inUse = false;
357 };
358 std::array<PendingInterest, PIT_MAX_ENTRIES> pendingInterests_{};
359
360 Stats stats_{};
361 bool initialized_ = false;
362};
363
364} // namespace ndn
Content Store.
Definition cs.hpp:92
NDN Data packet.
Definition data.hpp:49
NDN Face abstract base class.
Definition face.hpp:48
Forwarding Information Base.
Definition fib.hpp:119
NDN Forwarder.
Definition forwarder.hpp:99
Error addRoute(const Name &prefix, FaceId faceId, uint8_t cost=0)
Add a route.
Fib & fib()
Get reference to FIB (for testing)
Error addFace(Face *face)
Add a Face.
Definition forwarder.cpp:37
Error registerPrefix(const Name &prefix, InterestCallback callback)
Register a prefix.
Forwarder()
Constructor.
Definition forwarder.cpp:12
ContentStore & cs()
Get reference to Content Store (for testing)
void unregisterPrefix(const Name &prefix)
Unregister a prefix.
Pit & pit()
Get reference to PIT (for testing)
void removeFace(FaceId faceId)
Remove a Face.
Definition forwarder.cpp:69
const Stats & stats() const
Get statistics.
Error expressInterest(const Interest &interest, DataCallback onData, TimeoutCallback onTimeout=nullptr)
Send an Interest and wait for Data.
Definition forwarder.cpp:81
Error putData(const Data &data)
Send Data.
Error sendInterest(const Interest &interest)
Send an Interest (without PIT registration)
Error init(size_t csMaxEntries=CS_DEFAULT_ENTRIES)
Initialize the Forwarder.
Definition forwarder.cpp:18
void processEvents()
Process events.
NDN Interest packet.
Definition interest.hpp:50
NDN Name class.
Definition name.hpp:64
PIT entry.
Definition pit.hpp:46
Pending Interest Table.
Definition pit.hpp:137
Common definitions for the NDN protocol stack.
uint16_t FaceId
Face identifier.
Definition common.hpp:96
Error
Error codes.
Definition common.hpp:24
Content Store (CS)
constexpr size_t CS_DEFAULT_ENTRIES
Default number of CS entries.
Definition cs.hpp:20
NDN Data packet.
NDN Face interface.
Forwarding Information Base (FIB)
std::function< void(const Interest &, FaceId)> InterestCallback
Interest receive callback.
Definition forwarder.hpp:32
std::function< void(const Interest &)> TimeoutCallback
Timeout callback.
Definition forwarder.hpp:50
constexpr size_t FORWARDER_MAX_PREFIXES
Maximum number of registerable prefixes.
Definition forwarder.hpp:56
std::function< void(const Data &)> DataCallback
Data receive callback.
Definition forwarder.hpp:41
constexpr size_t FORWARDER_MAX_FACES
Maximum number of Faces the Forwarder can manage.
Definition forwarder.hpp:53
NDN Interest packet.
constexpr uint32_t Interest
Interest packet.
Definition tlv.hpp:27
Pending Interest Table (PIT)
Forwarder statistics.
uint32_t interestsReceived
Number of Interests received.
uint32_t cacheHits
Number of cache hits.
uint32_t cacheMisses
Number of cache misses.
uint32_t dataReceived
Number of Data received.
uint32_t interestsSent
Number of Interests sent.
uint32_t dataSent
Number of Data sent.