Kea  2.3.4-git
packet_queue_mgr.h
Go to the documentation of this file.
1 // Copyright (C) 2018-2021 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_MGR_H
8 #define PACKET_QUEUE_MGR_H
9 
10 #include <cc/data.h>
11 #include <cc/simple_parser.h>
12 #include <dhcp/packet_queue.h>
13 #include <exceptions/exceptions.h>
14 #include <boost/shared_ptr.hpp>
15 #include <functional>
16 #include <map>
17 #include <string>
18 
19 namespace isc {
20 namespace dhcp {
21 
25 class InvalidQueueType : public Exception {
26 public:
27  InvalidQueueType(const char* file, size_t line, const char* what) :
28  isc::Exception(file, line, what) {}
29 };
30 
38 template<typename PacketQueueTypePtr>
40 public:
45  typedef std::function<PacketQueueTypePtr(data::ConstElementPtr)> Factory;
46 
49  : factories_(), packet_queue_() {
50  }
51 
70  bool registerPacketQueueFactory(const std::string& queue_type,
71  Factory factory) {
72  // Check if this backend has been already registered.
73  if (factories_.count(queue_type)) {
74  return (false);
75  }
76 
77  // Register the new backend.
78  factories_.insert(std::make_pair(queue_type, factory));
79  return (true);
80  }
81 
96  bool unregisterPacketQueueFactory(const std::string& queue_type) {
97  // Look for it.
98  auto index = factories_.find(queue_type);
99 
100  // Not there so nothing to do.
101  if (index == factories_.end()) {
102  return (false);
103  }
104 
105  // If the queue is of the type being unregistered, then remove it. We don't
106  // a queue instance outliving its library.
107  if ((packet_queue_) && (packet_queue_->getQueueType() == queue_type)) {
108  packet_queue_.reset();
109  }
110 
111  // Remove the factory.
112  factories_.erase(index);
113 
114  return (true);
115  }
116 
134  if (!parameters) {
135  isc_throw(Unexpected, "createPacketQueue - queue parameters is null");
136  }
137 
138  // Get the database type to locate a factory function.
139  std::string queue_type ;
140  try {
141  queue_type = data::SimpleParser::getString(parameters, "queue-type");
142  } catch (const std::exception& ex) {
143  isc_throw(InvalidQueueParameter, "queue-type missing or invalid: " << ex.what());
144  }
145 
146  // Look up the factory.
147  auto index = factories_.find(queue_type);
148 
149  // Punt if there is no matching factory.
150  if (index == factories_.end()) {
151  isc_throw(InvalidQueueType, "The type of the packet queue: '" <<
152  queue_type << "' is not supported"); }
153 
154  // Call the factory to create the new queue.
155  // Factories should throw InvalidQueueParameter if given
156  // bad values in the control.
157  auto new_queue = index->second(parameters);
158  if (!new_queue) {
159  isc_throw(Unexpected, "Packet queue " << queue_type <<
160  " factory returned NULL");
161  }
162 
163  // Replace the existing queue with the new one.
164  packet_queue_ = new_queue;
165  }
166 
168  PacketQueueTypePtr getPacketQueue() const {
169  return (packet_queue_);
170  }
171 
175  packet_queue_.reset();
176  }
177 
178 protected:
180  std::map<std::string, Factory> factories_;
181 
183  PacketQueueTypePtr packet_queue_;
184 };
185 
186 } // end of namespace isc::dhcp
187 } // end of namespace isc
188 
189 #endif // PACKET_QUEUE_MGR_H
std::function< PacketQueueTypePtr(data::ConstElementPtr)> Factory
Defines the type of the packet queue factory function.
PacketQueueTypePtr packet_queue_
the current queue_ ?
bool unregisterPacketQueueFactory(const std::string &queue_type)
Unregisters the queue factory function for a given type.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown when an unexpected error condition occurs.
void destroyPacketQueue()
Destroys the current packet queue.
InvalidQueueType(const char *file, size_t line, const char *what)
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
static std::string getString(isc::data::ConstElementPtr scope, const std::string &name)
Returns a string parameter from a scope.
Invalid Queue type exception.
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-lfc.
std::map< std::string, Factory > factories_
A map holding registered backend factory functions.
bool registerPacketQueueFactory(const std::string &queue_type, Factory factory)
Registers new queue factory function for a given queue type.
Invalid queue parameter exception.
Definition: packet_queue.h:24
void createPacketQueue(data::ConstElementPtr parameters)
Create an instance of a packet queue.
PacketQueueTypePtr getPacketQueue() const
Returns underlying packet queue.
Packet Queue Managers (PQM).