Kea  1.9.9-git
packet_storage.h
Go to the documentation of this file.
1 // Copyright (C) 2012-2015 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_STORAGE_H
8 #define PACKET_STORAGE_H
9 
10 #include <boost/noncopyable.hpp>
11 #include <boost/shared_ptr.hpp>
12 #include <list>
13 #include <stdint.h>
14 
15 namespace isc {
16 namespace perfdhcp {
17 
41 template<typename T>
42 class PacketStorage : public boost::noncopyable {
43 public:
45  typedef boost::shared_ptr<T> PacketPtr;
46 
47 private:
49  typedef typename std::list<PacketPtr> PacketContainer;
51  typedef typename PacketContainer::iterator PacketContainerIterator;
52 
53 public:
54 
57 
61  void append(const PacketPtr& packet) {
62  storage_.push_back(packet);
63  if (storage_.size() == 1) {
64  current_pointer_ = storage_.begin();
65  }
66  }
67 
77  void clear(const uint64_t num = 0) {
78  if (num != 0) {
79  PacketContainerIterator last = storage_.begin();
80  std::advance(last, num > size() ? size() : num);
81  current_pointer_ = storage_.erase(storage_.begin(), last);
82  } else {
83  storage_.clear();
84  current_pointer_ = storage_.begin();
85  }
86  }
87 
91  bool empty() const {
92  return (storage_.empty());
93  }
94 
102  PacketPtr getNext() {
103  if (storage_.empty()) {
104  return (PacketPtr());
105  } else if (current_pointer_ == storage_.end()) {
106  current_pointer_ = storage_.begin();
107  }
108  PacketPtr packet = *current_pointer_;
109  current_pointer_ = storage_.erase(current_pointer_);
110  return (packet);
111  }
112 
122  PacketPtr getRandom() {
123  if (empty()) {
124  return (PacketPtr());
125  }
126  current_pointer_ = storage_.begin();
127  if (size() > 1) {
128  std::advance(current_pointer_, rand() % (size() - 1));
129  }
130  PacketPtr packet = *current_pointer_;
131  current_pointer_ = storage_.erase(current_pointer_);
132  return (packet);
133  }
134 
138  uint64_t size() const {
139  return (storage_.size());
140  }
141 
142 private:
143 
144  std::list<PacketPtr> storage_;
145  PacketContainerIterator current_pointer_;
146 
148 };
149 
150 } // namespace perfdhcp
151 } // namespace isc
152 
153 #endif // PACKET_STORAGE_H
void append(const PacketPtr &packet)
Appends the new packet object to the collection.
void clear(const uint64_t num=0)
Removes packets from the storage.
boost::shared_ptr< T > PacketPtr
A type which represents the pointer to a packet.
bool empty() const
Checks if the storage has no packets.
PacketPtr getNext()
Returns next packet from the storage.
PacketPtr getRandom()
Returns random packet from the storage.
Defines the logger used by the top-level component of kea-dhcp-ddns.
uint64_t size() const
Returns number of packets in the storage.
Represents a list of packets with a sequential and random access to list elements.