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 
17 namespace isc {
18 
19 namespace dhcp {
20 
25 template<typename PacketTypePtr>
26 class PacketQueueRing : public PacketQueue<PacketTypePtr> {
27 public:
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 "
182  << MIN_RING_CAPACITY);
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 
207 private:
208 
210  boost::circular_buffer<PacketTypePtr> queue_;
211 
214 };
215 
216 
223 class PacketQueueRing4 : public PacketQueueRing<Pkt4Ptr> {
224 public:
229  PacketQueueRing4(const std::string& queue_type, size_t capacity)
230  : PacketQueueRing(queue_type, capacity) {
231  };
232 
234  virtual ~PacketQueueRing4(){}
235 };
236 
243 class PacketQueueRing6 : public PacketQueueRing<Pkt6Ptr> {
244 public:
249  PacketQueueRing6(const std::string& queue_type, size_t capacity)
250  : PacketQueueRing(queue_type, capacity) {
251  };
252 
254  virtual ~PacketQueueRing6(){}
255 };
256 
257 }; // namespace isc::dhcp
258 }; // namespace isc
259 
260 #endif // PACKET_QUEUE_RING_H
isc::dhcp::PacketQueueRing6
DHCPv6 packet queue buffer implementation.
Definition: packet_queue_ring.h:243
isc::dhcp::PacketQueueRing::enqueuePacket
virtual void enqueuePacket(PacketTypePtr packet, const SocketInfo &source)
Adds a packet to the queue.
Definition: packet_queue_ring.h:52
isc::dhcp::PacketQueue
Interface for managing a queue of inbound DHCP packets.
Definition: packet_queue.h:50
isc::dhcp::QueueEnd::FRONT
@ FRONT
isc::dhcp::PacketQueueRing::getInfo
virtual data::ElementPtr getInfo() const
Fetches pertinent information.
Definition: packet_queue_ring.h:200
isc::dhcp::PacketQueueRing::peek
virtual const PacketTypePtr peek(const QueueEnd &from=QueueEnd::FRONT) const
Gets the packet currently at one end of the queue.
Definition: packet_queue_ring.h:153
isc::dhcp::PacketQueueRing::eatPackets
virtual int eatPackets(const QueueEnd &)
Discards packets from one end of the queue.
Definition: packet_queue_ring.h:96
isc::util::thread::Mutex::Locker
This holds a lock on a Mutex.
Definition: sync.h:72
sync.h
isc::dhcp::PacketQueueRing::getCapacity
virtual size_t getCapacity() const
Returns the maximum number of packets allowed in the buffer.
Definition: packet_queue_ring.h:168
isc::util::thread::Mutex
Mutex with very simple interface.
Definition: sync.h:39
isc::dhcp::PacketQueueRing::~PacketQueueRing
virtual ~PacketQueueRing()
virtual Destructor
Definition: packet_queue_ring.h:42
isc::dhcp::PacketQueueRing::PacketQueueRing
PacketQueueRing(const std::string &queue_type, size_t capacity)
Constructor.
Definition: packet_queue_ring.h:36
isc::dhcp::PacketQueueRing::getSize
virtual size_t getSize() const
Returns the current number of packets in the buffer.
Definition: packet_queue_ring.h:190
isc::dhcp::PacketQueueRing::dequeuePacket
virtual PacketTypePtr dequeuePacket()
Dequeues the next packet from the queue.
Definition: packet_queue_ring.h:64
isc::dhcp::PacketQueueRing::setCapacity
virtual void setCapacity(size_t capacity)
Sets the maximum number of packets allowed in the buffer.
Definition: packet_queue_ring.h:178
isc::dhcp::PacketQueueRing
Provides a ring-buffer implementation of the PacketQueue interface.
Definition: packet_queue_ring.h:26
isc::dhcp::PacketQueueRing4::~PacketQueueRing4
virtual ~PacketQueueRing4()
virtual Destructor
Definition: packet_queue_ring.h:234
isc::dhcp::QueueEnd::BACK
@ BACK
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition: exceptions/exceptions.h:132
isc::dhcp::PacketQueueRing6::PacketQueueRing6
PacketQueueRing6(const std::string &queue_type, size_t capacity)
Constructor.
Definition: packet_queue_ring.h:249
isc::dhcp::PacketQueueRing4
DHCPv4 packet queue buffer implementation.
Definition: packet_queue_ring.h:223
isc::dhcp::PacketQueueRing6::~PacketQueueRing6
virtual ~PacketQueueRing6()
virtual Destructor
Definition: packet_queue_ring.h:254
isc::dhcp::PacketQueueRing::popPacket
virtual PacketTypePtr popPacket(const QueueEnd &from=QueueEnd::FRONT)
Pops a packet from the queue.
Definition: packet_queue_ring.h:125
isc::dhcp::PacketQueue::getInfo
virtual data::ElementPtr getInfo() const
Fetches operational information about the current state of the queue.
Definition: packet_queue.h:102
isc::dhcp::PacketQueueRing::shouldDropPacket
virtual bool shouldDropPacket(PacketTypePtr, const SocketInfo &)
Determines if a packet should be discarded.
Definition: packet_queue_ring.h:81
isc::dhcp::SocketInfo
Holds information about socket.
Definition: socket_info.h:19
isc::dhcp::PacketQueueRing::pushPacket
virtual void pushPacket(PacketTypePtr &packet, const QueueEnd &to=QueueEnd::BACK)
Pushes a packet onto the queue.
Definition: packet_queue_ring.h:107
isc::dhcp::PacketQueueRing::empty
virtual bool empty() const
Returns True if the queue is empty.
Definition: packet_queue_ring.h:163
isc::dhcp::PacketQueueRing::clear
virtual void clear()
Discards all packets currently in the buffer.
Definition: packet_queue_ring.h:195
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
isc::dhcp::QueueEnd
QueueEnd
Enumerates choices between the two ends of the queue.
Definition: packet_queue.h:34
isc::data::Element::create
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition: data.cc:223
isc::dhcp::PacketQueueRing::MIN_RING_CAPACITY
static const size_t MIN_RING_CAPACITY
Minimum queue capacity permitted.
Definition: packet_queue_ring.h:30
isc::dhcp::PacketQueueRing4::PacketQueueRing4
PacketQueueRing4(const std::string &queue_type, size_t capacity)
Constructor.
Definition: packet_queue_ring.h:229
packet_queue.h