Kea  1.5.0
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 
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 
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
isc::perfdhcp::PacketStorage::getNext
PacketPtr getNext()
Returns next packet from the storage.
Definition: packet_storage.h:102
isc::perfdhcp::PacketStorage::size
uint64_t size() const
Returns number of packets in the storage.
Definition: packet_storage.h:138
isc::perfdhcp::PacketStorage::PacketPtr
boost::shared_ptr< T > PacketPtr
A type which represents the pointer to a packet.
Definition: packet_storage.h:45
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::perfdhcp::PacketStorage
Represents a list of packets with a sequential and random access to list elements.
Definition: packet_storage.h:42
perfdhcp
isc::perfdhcp::PacketStorage::empty
bool empty() const
Checks if the storage has no packets.
Definition: packet_storage.h:91
isc::perfdhcp::PacketStorage::append
void append(const PacketPtr &packet)
Appends the new packet object to the collection.
Definition: packet_storage.h:61
isc::perfdhcp::PacketStorage::getRandom
PacketPtr getRandom()
Returns random packet from the storage.
Definition: packet_storage.h:122
isc::perfdhcp::PacketStorage::clear
void clear(const uint64_t num=0)
Removes packets from the storage.
Definition: packet_storage.h:77
isc::perfdhcp::PacketStorage::PacketStorage
PacketStorage()
Constructor.
Definition: packet_storage.h:56