Vanetza
 
Loading...
Searching...
No Matches
cbf_packet_buffer.hpp
1#ifndef CBF_PACKET_BUFFER_HPP_MU3RK5V1
2#define CBF_PACKET_BUFFER_HPP_MU3RK5V1
3
4#include <vanetza/common/clock.hpp>
5#include <vanetza/geonet/cbf_packet_identifier.hpp>
6#include <vanetza/geonet/packet.hpp>
7#include <vanetza/geonet/pending_packet.hpp>
8#include <vanetza/geonet/pdu_variant.hpp>
9#include <boost/bimap/bimap.hpp>
10#include <boost/bimap/multiset_of.hpp>
11#include <boost/bimap/unordered_set_of.hpp>
12#include <boost/optional/optional.hpp>
13#include <cstddef>
14#include <list>
15#include <memory>
16
17// forward declarations
18namespace vanetza
19{
20class Runtime;
21
22namespace geonet
23{
24class Address;
25class CbfCounter;
26class CbfPacket;
27class Lifetime;
28
29/**
30 * CbfPacket enables handling of conventional packets in a CBF packet buffer.
31 * It contains a GeoBroadcast PDU and the network layer payload.
32 */
34{
35public:
38
39 CbfPacket(CbfPacket&&) = default;
40 CbfPacket& operator=(CbfPacket&&) = default;
41
42 /**
43 * Get sender of buffered packet
44 * \return sender's link-layer address
45 */
46 const MacAddress& sender() const;
47
48 /**
49 * Get source address of buffered packet
50 * \return source address
51 */
52 const Address& source() const;
53
54 /**
55 * Get sequence number of buffered packet
56 * \return sequence number
57 */
59
60 /**
61 * Reduce lifetime of buffered packet
62 * \param d reduce lifetime by this duration
63 * \return remaining lifetime
64 */
65 Clock::duration reduce_lifetime(Clock::duration d);
66
67 /**
68 * Length of packet data in bytes (PDU including payload)
69 * \return size of packet on wire
70 */
71 std::size_t length() const;
72
73 PendingPacket<GbcPdu> packet() && { return std::move(m_packet); }
74
75private:
76 PendingPacket<GbcPdu> m_packet;
77 MacAddress m_sender;
78};
79
80
81/**
82 * CbfPacketBuffer facilitates implementation of contention based forwarding
83 */
85{
86public:
87 using TimerCallback = std::function<void(PendingPacket<GbcPdu>&&)>;
88 using Identifier = CbfPacketIdentifier;
89
90 /**
91 * Create CBF packet buffer with bounded capacity
92 * \param rt Runtime instance used for internal timers
93 * \param cb Callback invoked for each packet on expiry
94 * \param cnt CBF counter implementation
95 * \param bytes Buffer can hold at most this number of bytes
96 */
97 CbfPacketBuffer(Runtime& rt, TimerCallback cb, std::unique_ptr<CbfCounter> cnt, std::size_t bytes);
99
100 /**
101 * Enqueue a packet and start an associated timer expiring after timeout
102 * \param packet Buffer this packet
103 * \param timeout CBF timer expiration for this packet
104 */
105 void add(CbfPacket&& packet, Clock::duration timeout);
106
107 /**
108 * Try to remove a packet from buffer.
109 * \param id packet identification
110 * \return true if packet existed before removal
111 */
112 bool remove(const Identifier& id);
113
114 /**
115 * Update associated packet timer
116 * \param id packet identification
117 * \param timeout CBF timer expiration
118 */
119 void update(const Identifier& id, Clock::duration timeout);
120
121 /**
122 * Fetch a packet from buffer.
123 *
124 * Associated timer is automatically stopped and packet removed from buffer.
125 * Packet lifetime is reduced by queueing time at return.
126 *
127 * \param id packet identification
128 * \return packet if found in buffer
129 */
130 boost::optional<CbfPacket> fetch(const Identifier& id);
131
132 /**
133 * Find packet in buffer.
134 * \param id packet identification
135 * \return read-only pointer to packet, nullptr if not found
136 */
137 const CbfPacket* find(const Identifier& id) const;
138
139 /**
140 * Get counter associated with given packet
141 * \note packet counter is incremented at each timer update
142 * \param id packet identification
143 * \return 0 if packet has never been buffered before
144 */
145 std::size_t counter(const Identifier& packet) const;
146
147private:
148
149 struct Timer
150 {
151 Timer(const Runtime&, Clock::duration timeout);
152 Timer(const Timer&) = default;
153 Timer& operator=(const Timer&) = default;
154 bool operator<(const Timer&) const;
155
156 Clock::time_point expiry;
157 Clock::time_point start;
158 };
159
160 using timer_bimap = boost::bimaps::bimap<
161 boost::bimaps::multiset_of<Timer>,
162 boost::bimaps::unordered_set_of<Identifier, std::hash<Identifier>>,
163 boost::bimaps::with_info<std::list<CbfPacket>::iterator>
164 >;
165
166 /**
167 * Flush all expired packets
168 */
169 void flush();
170
171 /**
172 * Remove timer from map and reschedule timer event if necessary
173 */
174 void remove_timer(typename timer_bimap::left_map::iterator);
175
176 /**
177 * Schedule next timer event at runtime
178 */
179 void schedule_timer();
180
181 /**
182 * Reduce packet lifetime by queueing time
183 * \param timer contains queueing start time
184 * \param packet associated packet
185 * \return true if packet remains valid, false if end of lifetime is reached
186 */
187 bool reduce_lifetime(const Timer&, CbfPacket&) const;
188
189 std::list<CbfPacket> m_packets;
190 timer_bimap m_timers;
191 Runtime& m_runtime;
192 std::unique_ptr<CbfCounter> m_counter;
193 const std::size_t m_capacity_bytes;
194 std::size_t m_stored_bytes;
195 TimerCallback m_timer_callback;
196};
197
198} // namespace geonet
199} // namespace vanetza
200
201#endif /* CBF_PACKET_BUFFER_HPP_MU3RK5V1 */
202
std::size_t counter(const Identifier &packet) const
boost::optional< CbfPacket > fetch(const Identifier &id)
void remove_timer(typename timer_bimap::left_map::iterator)
bool remove(const Identifier &id)
void update(const Identifier &id, Clock::duration timeout)
void add(CbfPacket &&packet, Clock::duration timeout)
const CbfPacket * find(const Identifier &id) const
bool reduce_lifetime(const Timer &, CbfPacket &) const
Clock::duration reduce_lifetime(Clock::duration d)
const MacAddress & sender() const
SequenceNumber sequence_number() const
const Address & source() const