Vanetza
 
Loading...
Searching...
No Matches
packet_buffer.cpp
1#include "packet_buffer.hpp"
2#include "basic_header.hpp"
3
4namespace vanetza
5{
6namespace geonet
7{
8namespace packet_buffer
9{
10
11Expiry::Expiry(Clock::time_point now, Clock::duration lifetime) :
12 m_buffered_since(now), m_expires_at(now)
13{
14 m_expires_at += lifetime;
15}
16
17bool Expiry::is_expired(Clock::time_point now) const
18{
19 return (m_expires_at < now);
20}
21
22} // namespace packet_buffer
23
24
25PacketBuffer::PacketBuffer(std::size_t capacity) :
26 m_capacity(capacity), m_stored(0)
27{
28}
29
30bool PacketBuffer::push(data_ptr data, Clock::time_point now)
31{
32 assert(data);
33 bool pushed = false;
34 drop_expired(now);
35 if (drop(data->length())) {
36 m_stored += data->length();
37 m_nodes.push_back(std::make_tuple(
38 expiry_type(now, data->reduce_lifetime(Clock::duration::zero())),
39 std::move(data)
40 ));
41 pushed = true;
42 }
43 return pushed;
44}
45
46void PacketBuffer::flush(Clock::time_point now)
47{
48 decltype(m_nodes) nodes;
49 std::swap(m_nodes, nodes);
50 m_stored = 0;
51
52 for (auto& node : nodes) {
53 const auto& expiry = std::get<0>(node);
54 if (!expiry.is_expired(now)) {
55 auto& data = std::get<1>(node);
56 const auto queuing_time = now - expiry.buffered_since();
57 data->reduce_lifetime(queuing_time);
58 data->flush();
59 }
60 }
61}
62
64{
65 bool dropped = !m_nodes.empty();
66 if (dropped) {
67 m_stored -= std::get<1>(m_nodes.front())->length();
68 m_nodes.pop_front();
69 }
70 return dropped;
71}
72
73void PacketBuffer::drop_expired(Clock::time_point now)
74{
75 std::list<decltype(m_nodes)::iterator> expired;
76 for (auto it = m_nodes.begin(), end = m_nodes.end(); it != end;) {
77 if (std::get<0>(*it).is_expired(now)) {
78 m_stored -= std::get<1>(*it)->length();
79 it = m_nodes.erase(it);
80 } else {
81 ++it;
82 }
83 }
84}
85
86bool PacketBuffer::drop(std::size_t bytes)
87{
88 while (free() < bytes && drop_head()) {}
89 return free() >= bytes;
90}
91
92} // namespace geonet
93} // namespace vanetza
94
bool drop(std::size_t bytes)
void flush(Clock::time_point t)
void drop_expired(Clock::time_point t)
bool push(data_ptr packet, Clock::time_point t)