Vanetza
 
Loading...
Searching...
No Matches
packet_buffer.hpp
1#ifndef PACKET_BUFFER_HPP_U97KIBQC
2#define PACKET_BUFFER_HPP_U97KIBQC
3
4#include <vanetza/common/clock.hpp>
5#include <vanetza/geonet/packet.hpp>
6#include <vanetza/geonet/pdu.hpp>
7#include <vanetza/geonet/timestamp.hpp>
8#include <vanetza/net/mac_address.hpp>
9#include <functional>
10#include <list>
11#include <memory>
12
13namespace vanetza
14{
15namespace geonet
16{
17namespace packet_buffer
18{
19
20class Expiry
21{
22public:
23 Expiry(Clock::time_point now, Clock::duration lifetime);
24 bool is_expired(Clock::time_point now) const;
25 Clock::time_point buffered_since() const { return m_buffered_since; }
26 Clock::time_point expires_at() const { return m_expires_at; }
27
28private:
29 Clock::time_point m_buffered_since;
30 Clock::time_point m_expires_at;
31};
32
33class Data
34{
35public:
36 /**
37 * Length of packet data
38 * \return length in bytes
39 */
40 virtual std::size_t length() const = 0;
41
42 /**
43 * Reduce lifetime associated with data
44 * \param d reduce lifetime by given duration
45 * \return remaining lifetime (never negative)
46 */
47 virtual Clock::duration reduce_lifetime(Clock::duration d) = 0;
48
49 /**
50 * Flush data
51 * \note length() and lifetime() should not be called afterwards!
52 */
53 virtual void flush() = 0;
54
55 virtual ~Data() {}
56};
57
58} // namespace packet_buffer
59
60
61/**
62 * PacketBuffer with bounded capacity, packet expiry and head-drop
63 */
65{
66public:
67 typedef std::unique_ptr<packet_buffer::Data> data_ptr;
68
69 /**
70 * Create PacketBuffer with given capacity
71 * \param capacity Buffer can store this many bytes
72 */
73 PacketBuffer(std::size_t capacity);
74
75 /**
76 * Push one packet into buffer
77 * \param packet Packet data
78 * \param t Current time
79 * \return true if packet has been pushed successfully
80 */
81 bool push(data_ptr packet, Clock::time_point t);
82
83 /**
84 * Flush packets from buffer. Expired packets are dropped.
85 * \note Some packets might remain in buffer (re-added during flushing)
86 * \param t Current time
87 */
88 void flush(Clock::time_point t);
89
90private:
92 typedef std::tuple<expiry_type, data_ptr> node_type;
93
94 std::size_t free() const { return m_capacity - m_stored; }
95 std::size_t capacity() const { return m_capacity; }
96
97 /**
98 * Push one new element into buffer list
99 * \note capacity is not checked by this method, has to be done manually
100 * \param expiry Expiry data
101 * \param packet Packet data
102 */
103 void push(expiry_type&& expiry, data_ptr packet);
104
105 /**
106 * Drop current head element
107 * \return true if head element was dropped
108 */
109 bool drop_head();
110
111 /**
112 * Drop all packets with expired timestamp
113 * \param t current time
114 */
115 void drop_expired(Clock::time_point t);
116
117 /**
118 * Drop as many packets as required to store given number of bytes.
119 * Packets at the head of the list are dropped first.
120 * \param bytes require #bytes free capacity
121 * \return true if there is enough capacity left for #bytes
122 */
123 bool drop(std::size_t bytes);
124
125 std::list<node_type> m_nodes;
126 std::size_t m_capacity;
127 std::size_t m_stored;
128};
129
130} // namespace geonet
131} // namespace vanetza
132
133#endif /* PACKET_BUFFER_HPP_U97KIBQC */
134
bool drop(std::size_t bytes)
void push(expiry_type &&expiry, data_ptr packet)
void flush(Clock::time_point t)
void drop_expired(Clock::time_point t)
bool push(data_ptr packet, Clock::time_point t)
virtual std::size_t length() const =0
virtual Clock::duration reduce_lifetime(Clock::duration d)=0