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
virtual const PacketTypePtr peek(const QueueEnd &from=QueueEnd::FRONT) const
Gets the packet currently at one end of the queue.
virtual data::ElementPtr getInfo() const
Fetches operational information about the current state of the queue.
Definition: packet_queue.h:102
Interface for managing a queue of inbound DHCP packets.
Definition: packet_queue.h:50
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
virtual void setCapacity(size_t capacity)
Sets the maximum number of packets allowed in the buffer.
virtual size_t getCapacity() const
Returns the maximum number of packets allowed in the buffer.
QueueEnd
Enumerates choices between the two ends of the queue.
Definition: packet_queue.h:34
This holds a lock on a Mutex.
Definition: sync.h:72
Provides a ring-buffer implementation of the PacketQueue interface.
virtual PacketTypePtr popPacket(const QueueEnd &from=QueueEnd::FRONT)
Pops a packet from the queue.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
virtual bool shouldDropPacket(PacketTypePtr, const SocketInfo &)
Determines if a packet should be discarded.
virtual int eatPackets(const QueueEnd &)
Discards packets from one end of the queue.
DHCPv4 packet queue buffer implementation.
virtual void clear()
Discards all packets currently in the buffer.
virtual PacketTypePtr dequeuePacket()
Dequeues the next packet from the queue.
Defines the logger used by the top-level component of kea-dhcp-ddns.
virtual data::ElementPtr getInfo() const
Fetches pertinent information.
Mutex with very simple interface.
Definition: sync.h:39
virtual void pushPacket(PacketTypePtr &packet, const QueueEnd &to=QueueEnd::BACK)
Pushes a packet onto the queue.
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition: data.cc:223
PacketQueueRing(const std::string &queue_type, size_t capacity)
Constructor.
PacketQueueRing6(const std::string &queue_type, size_t capacity)
Constructor.
virtual void enqueuePacket(PacketTypePtr packet, const SocketInfo &source)
Adds a packet to the queue.
virtual ~PacketQueueRing()
virtual Destructor
static const size_t MIN_RING_CAPACITY
Minimum queue capacity permitted.
DHCPv6 packet queue buffer implementation.
virtual ~PacketQueueRing4()
virtual Destructor
virtual ~PacketQueueRing6()
virtual Destructor
Holds information about socket.
Definition: socket_info.h:19
virtual size_t getSize() const
Returns the current number of packets in the buffer.
virtual bool empty() const
Returns True if the queue is empty.
PacketQueueRing4(const std::string &queue_type, size_t capacity)
Constructor.