Kea 1.5.0
packet_queue_ring.h
Go to the documentation of this file.
1// Copyright (C) 2018 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef PACKET_QUEUE_RING_H
8#define PACKET_QUEUE_RING_H
9
10#include <dhcp/packet_queue.h>
11#include <util/threads/sync.h>
12
13#include <boost/function.hpp>
14#include <boost/circular_buffer.hpp>
15#include <sstream>
16
17namespace isc {
18
19namespace dhcp {
20
25template<typename PacketTypePtr>
26class PacketQueueRing : public PacketQueue<PacketTypePtr> {
27public:
30 static const size_t MIN_RING_CAPACITY = 5;
31
36 PacketQueueRing(const std::string& queue_type, size_t capacity)
37 : PacketQueue<PacketTypePtr>(queue_type) {
38 queue_.set_capacity(capacity);
39 }
40
42 virtual ~PacketQueueRing(){};
43
52 virtual void enqueuePacket(PacketTypePtr packet, const SocketInfo& source) {
53 if (!shouldDropPacket(packet, source)) {
54 pushPacket(packet);
55 }
56 }
57
64 virtual PacketTypePtr dequeuePacket() {
66 return(popPacket());
67 }
68
81 virtual bool shouldDropPacket(PacketTypePtr /* packet */,
82 const SocketInfo& /* source */) {
83 return (false);
84 }
85
96 virtual int eatPackets(const QueueEnd& /* from */) {
97 return (0);
98 }
99
107 virtual void pushPacket(PacketTypePtr& packet, const QueueEnd& to=QueueEnd::BACK) {
109 if (to == QueueEnd::BACK) {
110 queue_.push_back(packet);
111 } else {
112 queue_.push_front(packet);
113 }
114 }
115
125 virtual PacketTypePtr popPacket(const QueueEnd& from = QueueEnd::FRONT) {
127 PacketTypePtr packet;
128 if (queue_.empty()) {
129 return (packet);
130 }
131
132 if (from == QueueEnd::FRONT) {
133 packet = queue_.front();
134 queue_.pop_front();
135 } else {
136 packet = queue_.back();
137 queue_.pop_back();
138 }
139
140 return (packet);
141 }
142
143
153 virtual const PacketTypePtr peek(const QueueEnd& from=QueueEnd::FRONT) const {
154 PacketTypePtr packet;
155 if (!queue_.empty()) {
156 packet = (from == QueueEnd::FRONT ? queue_.front() : queue_.back());
157 }
158
159 return (packet);
160 }
161
163 virtual bool empty() const {
164 return(queue_.empty());
165 }
166
168 virtual size_t getCapacity() const {
169 return (queue_.capacity());
170 }
171
178 virtual void setCapacity(size_t capacity) {
179 if (capacity < MIN_RING_CAPACITY) {
180 isc_throw(BadValue, "Queue capacity of " << capacity
181 << " is invalid. It must be at least "
183 }
184
186 queue_.set_capacity(capacity);
187 }
188
190 virtual size_t getSize() const {
191 return (queue_.size());
192 }
193
195 virtual void clear() {
196 queue_.clear();
197 }
198
200 virtual data::ElementPtr getInfo() const {
202 info->set("capacity", data::Element::create(static_cast<int64_t>(getCapacity())));
203 info->set("size", data::Element::create(static_cast<int64_t>(getSize())));
204 return(info);
205 }
206
207private:
208
210 boost::circular_buffer<PacketTypePtr> queue_;
211
214};
215
216
223class PacketQueueRing4 : public PacketQueueRing<Pkt4Ptr> {
224public:
229 PacketQueueRing4(const std::string& queue_type, size_t capacity)
230 : PacketQueueRing(queue_type, capacity) {
231 };
232
235};
236
243class PacketQueueRing6 : public PacketQueueRing<Pkt6Ptr> {
244public:
249 PacketQueueRing6(const std::string& queue_type, size_t capacity)
250 : PacketQueueRing(queue_type, capacity) {
251 };
252
255};
256
257}; // namespace isc::dhcp
258}; // namespace isc
259
260#endif // PACKET_QUEUE_RING_H
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition: data.cc:223
DHCPv4 packet queue buffer implementation.
PacketQueueRing4(const std::string &queue_type, size_t capacity)
Constructor.
virtual ~PacketQueueRing4()
virtual Destructor
DHCPv6 packet queue buffer implementation.
PacketQueueRing6(const std::string &queue_type, size_t capacity)
Constructor.
virtual ~PacketQueueRing6()
virtual Destructor
Provides a ring-buffer implementation of the PacketQueue interface.
virtual void pushPacket(PacketTypePtr &packet, const QueueEnd &to=QueueEnd::BACK)
Pushes a packet onto the queue.
virtual void enqueuePacket(PacketTypePtr packet, const SocketInfo &source)
Adds a packet to the queue.
virtual size_t getSize() const
Returns the current number of packets in the buffer.
virtual void setCapacity(size_t capacity)
Sets the maximum number of packets allowed in the buffer.
virtual data::ElementPtr getInfo() const
Fetches pertinent information.
virtual size_t getCapacity() const
Returns the maximum number of packets allowed in the buffer.
virtual int eatPackets(const QueueEnd &)
Discards packets from one end of the queue.
virtual bool empty() const
Returns True if the queue is empty.
virtual bool shouldDropPacket(PacketTypePtr, const SocketInfo &)
Determines if a packet should be discarded.
virtual const PacketTypePtr peek(const QueueEnd &from=QueueEnd::FRONT) const
Gets the packet currently at one end of the queue.
virtual ~PacketQueueRing()
virtual Destructor
virtual PacketTypePtr dequeuePacket()
Dequeues the next packet from the queue.
virtual PacketTypePtr popPacket(const QueueEnd &from=QueueEnd::FRONT)
Pops a packet from the queue.
virtual void clear()
Discards all packets currently in the buffer.
PacketQueueRing(const std::string &queue_type, size_t capacity)
Constructor.
static const size_t MIN_RING_CAPACITY
Minimum queue capacity permitted.
Interface for managing a queue of inbound DHCP packets.
Definition: packet_queue.h:50
virtual data::ElementPtr getInfo() const
Fetches operational information about the current state of the queue.
Definition: packet_queue.h:102
This holds a lock on a Mutex.
Definition: sync.h:72
Mutex with very simple interface.
Definition: sync.h:39
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:22
QueueEnd
Enumerates choices between the two ends of the queue.
Definition: packet_queue.h:34
Defines the logger used by the top-level component of kea-dhcp-ddns.
Holds information about socket.
Definition: socket_info.h:19