Vanetza
 
Loading...
Searching...
No Matches
cbf_counter.hpp
1#ifndef CBF_COUNTER_HPP_QTMUOGJS
2#define CBF_COUNTER_HPP_QTMUOGJS
3
4#include <vanetza/common/clock.hpp>
5#include <vanetza/geonet/cbf_packet_identifier.hpp>
6#include <vanetza/geonet/soft_state_map.hpp>
7#include <cstddef>
8#include <unordered_map>
9
10namespace vanetza
11{
12
13// forward declaration
14class Runtime;
15
16namespace geonet
17{
18
19/**
20 * Interface for duplicate packet counters.
21 * This is used by the Contention Based Forwarding (CBF) packet buffer.
22 */
24{
25public:
26 using id_type = CbfPacketIdentifier;
27 using counter_type = std::size_t;
28
29 /**
30 * Packet has been added to buffer
31 * \param id packet identifier
32 */
33 virtual void add(const id_type& id) = 0;
34
35 /**
36 * Packet has been removed from buffer.
37 * \param id packet identifier
38 */
39 virtual void remove(const id_type& id) = 0;
40
41 /**
42 * Increment packet counter by one
43 * \param id packet identifier
44 */
45 virtual void increment(const id_type& id) = 0;
46
47 /**
48 * Retrieve counter value
49 * \param id packet identifier
50 */
51 virtual counter_type counter(const id_type& id) const = 0;
52
53 virtual ~CbfCounter() = default;
54};
55
56
57/**
58 * Immortal CBF counters, i.e. they never expire.
59 * \note Be aware, memory consumption is constantly growing of this implemenation!
60 */
61class CbfCounterImmortal : public virtual CbfCounter
62{
63public:
64 void add(const id_type&) override;
65 void remove(const id_type&) override {}
66 void increment(const id_type&) override;
67 counter_type counter(const id_type&) const override;
68
69protected:
70 std::unordered_map<id_type, counter_type> m_counters;
71};
72
73/**
74 * Remembers only counter values for packets currently contending, i.e. stored in CBF buffer.
75 * \note This is the ADVANCED routing behaviour of EN 302 636-4-1 v1.2.1
76 */
77class CbfCounterContending : public virtual CbfCounter, private CbfCounterImmortal
78{
79public:
80 void add(const id_type&) override;
81 void remove(const id_type&) override;
82};
83
84/**
85 * Fading CBF counters
86 *
87 * Counters are removed from the internal table only after expiry, i.e. they are soft-state.
88 * This fixes some serious GN flooding due to ADVANCED routing, see CbfCounterContending.
89 */
90class CbfCounterFading : public virtual CbfCounter
91{
92public:
93 /*
94 * Initialize fading counters
95 * \param rt runtime used for soft-state behaviour
96 * \param lifetime newly added counters are initialized with this lifetime
97 */
98 CbfCounterFading(Runtime&, Clock::duration lifetime);
99
100 void add(const id_type&) override;
101 void remove(const id_type&) override {}
102 void increment(const id_type&) override;
103 counter_type counter(const id_type&) const override;
104
105private:
107};
108
109} // namespace geonet
110} // namespace vanetza
111
112#endif /* CBF_COUNTER_HPP_QTMUOGJS */
113
void add(const id_type &) override
Definition: cbf_counter.cpp:29
void remove(const id_type &) override
Definition: cbf_counter.cpp:34
void increment(const id_type &) override
Definition: cbf_counter.cpp:51
void add(const id_type &) override
Definition: cbf_counter.cpp:45
void remove(const id_type &) override
counter_type counter(const id_type &) const override
Definition: cbf_counter.cpp:56
counter_type counter(const id_type &) const override
Definition: cbf_counter.cpp:19
void remove(const id_type &) override
Definition: cbf_counter.hpp:65
void add(const id_type &) override
Definition: cbf_counter.cpp:9
void increment(const id_type &) override
Definition: cbf_counter.cpp:14
virtual void increment(const id_type &id)=0
virtual void add(const id_type &id)=0
virtual void remove(const id_type &id)=0
virtual counter_type counter(const id_type &id) const =0