ndn-embeds 0.1.0
Lightweight NDN protocol stack for embedded systems
Loading...
Searching...
No Matches
pit.cpp
1#include "ndn/pit.hpp"
2
3namespace ndn {
4
5// =============================================================================
6// PitEntry
7// =============================================================================
8
9FaceId PitEntry::face(size_t index) const {
10 if (index >= numFaces_) {
11 return FACE_ID_INVALID;
12 }
13 return incomingFaces_[index];
14}
15
16bool PitEntry::hasFace(FaceId faceId) const {
17 for (size_t i = 0; i < numFaces_; ++i) {
18 if (incomingFaces_[i] == faceId) {
19 return true;
20 }
21 }
22 return false;
23}
24
26 if (hasFace(faceId)) {
27 return true; // Already exists
28 }
29 if (numFaces_ >= PIT_MAX_FACES_PER_ENTRY) {
30 return false; // Full
31 }
32 incomingFaces_[numFaces_++] = faceId;
33 return true;
34}
35
36// =============================================================================
37// Pit
38// =============================================================================
39
40PitInsertResult Pit::insert(const Interest& interest, FaceId incomingFace, PitEntry** outEntry) {
41 const Name& name = interest.name();
42 const uint32_t nonce = interest.nonce().value_or(0);
43
44 // Search for existing entry
45 for (auto& entry : entries_) {
46 if (entry.inUse_ && entry.name_.equals(name)) {
47 // Check if same nonce (loop detection)
48 if (entry.nonce_ == nonce) {
49 stats_.duplicates++;
50 if (outEntry != nullptr) {
51 *outEntry = &entry;
52 }
53 return PitInsertResult::Duplicate;
54 }
55
56 // Different nonce: add Face (aggregation)
57 entry.addFace(incomingFace);
58 stats_.aggregations++;
59 if (outEntry != nullptr) {
60 *outEntry = &entry;
61 }
62 return PitInsertResult::Aggregated;
63 }
64 }
65
66 // Create new entry
67 for (auto& entry : entries_) {
68 if (!entry.inUse_) {
69 entry.name_ = name;
70 entry.nonce_ = nonce;
71 entry.expireTime_ = currentTimeMs() + interest.lifetime();
72 entry.numFaces_ = 0;
73 entry.addFace(incomingFace);
74 entry.inUse_ = true;
75 size_++;
76 stats_.insertions++;
77 if (outEntry != nullptr) {
78 *outEntry = &entry;
79 }
80 return PitInsertResult::New;
81 }
82 }
83
84 // Table full
85 return PitInsertResult::Full;
86}
87
88PitEntry* Pit::find(const Name& name) {
89 for (auto& entry : entries_) {
90 if (entry.inUse_ && entry.name_.equals(name)) {
91 return &entry;
92 }
93 }
94 return nullptr;
95}
96
97const PitEntry* Pit::find(const Name& name) const {
98 for (const auto& entry : entries_) {
99 if (entry.inUse_ && entry.name_.equals(name)) {
100 return &entry;
101 }
102 }
103 return nullptr;
104}
105
106void Pit::remove(PitEntry* entry) {
107 if (entry != nullptr && entry->inUse_) {
108 entry->inUse_ = false;
109 entry->numFaces_ = 0;
110 size_--;
111 }
112}
113
114void Pit::remove(const Name& name) {
115 PitEntry* entry = find(name);
116 if (entry != nullptr) {
117 remove(entry);
118 }
119}
120
122 for (auto& entry : entries_) {
123 if (entry.inUse_ && entry.expireTime_ <= now) {
124 if (callback) {
125 callback(entry);
126 }
127 entry.inUse_ = false;
128 entry.numFaces_ = 0;
129 size_--;
130 stats_.timeouts++;
131 }
132 }
133}
134
135} // namespace ndn
NDN Interest packet.
Definition interest.hpp:50
uint32_t lifetime() const
Get the InterestLifetime.
Definition interest.hpp:167
std::optional< uint32_t > nonce() const
Get the Nonce.
Definition interest.hpp:139
const Name & name() const
Get the Name (const reference)
Definition interest.hpp:105
NDN Name class.
Definition name.hpp:64
PIT entry.
Definition pit.hpp:46
bool hasFace(FaceId faceId) const
Check if a given Face is registered.
Definition pit.cpp:16
FaceId face(size_t index) const
Get the Face ID at a given index.
Definition pit.cpp:9
bool addFace(FaceId faceId)
Add a Face.
Definition pit.cpp:25
PitInsertResult insert(const Interest &interest, FaceId incomingFace, PitEntry **outEntry=nullptr)
Insert an Interest.
Definition pit.cpp:40
void processTimeouts(TimeMs now, TimeoutCallback callback=nullptr)
Process timed-out entries.
Definition pit.cpp:121
std::function< void(const PitEntry &)> TimeoutCallback
Timeout callback.
Definition pit.hpp:193
PitEntry * find(const Name &name)
Find an entry by Name.
Definition pit.cpp:88
void remove(PitEntry *entry)
Remove an entry.
Definition pit.cpp:106
TimeMs currentTimeMs()
Get current time (milliseconds)
Definition common.cpp:7
uint64_t TimeMs
Timestamp type (milliseconds)
Definition common.hpp:107
constexpr FaceId FACE_ID_INVALID
Invalid Face ID.
Definition common.hpp:99
uint16_t FaceId
Face identifier.
Definition common.hpp:96
Pending Interest Table (PIT)
PitInsertResult
PIT insertion result.
Definition pit.hpp:32
constexpr size_t PIT_MAX_FACES_PER_ENTRY
Maximum number of Faces per PIT entry.
Definition pit.hpp:25
uint32_t insertions
Number of new insertions.
Definition pit.hpp:226
uint32_t duplicates
Number of duplicate detections (loops)
Definition pit.hpp:228
uint32_t timeouts
Number of timeouts.
Definition pit.hpp:229
uint32_t aggregations
Number of aggregations.
Definition pit.hpp:227