| Kea 3.0.2-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>
39 class PacketQueueMgr {
40 public:
45 typedef std::function<PacketQueueTypePtr(data::ConstElementPtr)> Factory;
46
48 PacketQueueMgr()
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
133 void createPacketQueue(data::ConstElementPtr parameters) {
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
174 void destroyPacketQueue() {
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
isc::Exception::Exception
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
Definition exceptions/exceptions.cc:17
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::Unexpected
A generic exception that is thrown when an unexpected error condition occurs.
Definition exceptions/exceptions.h:153
isc::data::SimpleParser::getString
static std::string getString(isc::data::ConstElementPtr scope, const std::string &name)
Returns a string parameter from a scope.
Definition lib/cc/simple_parser.cc:60
isc::dhcp::InvalidQueueParameter
Invalid queue parameter exception.
Definition packet_queue.h:24
isc::dhcp::InvalidQueueType
Invalid Queue type exception.
Definition packet_queue_mgr.h:25
isc::dhcp::InvalidQueueType::InvalidQueueType
InvalidQueueType(const char *file, size_t line, const char *what)
Definition packet_queue_mgr.h:27
isc::dhcp::PacketQueueMgr::createPacketQueue
void createPacketQueue(data::ConstElementPtr parameters)
Create an instance of a packet queue.
Definition packet_queue_mgr.h:133
isc::dhcp::PacketQueueMgr::factories_
std::map< std::string, Factory > factories_
A map holding registered backend factory functions.
Definition packet_queue_mgr.h:180
isc::dhcp::PacketQueueMgr::registerPacketQueueFactory
bool registerPacketQueueFactory(const std::string &queue_type, Factory factory)
Registers new queue factory function for a given queue type.
Definition packet_queue_mgr.h:70
isc::dhcp::PacketQueueMgr::getPacketQueue
PacketQueueTypePtr getPacketQueue() const
Returns underlying packet queue.
Definition packet_queue_mgr.h:168
isc::dhcp::PacketQueueMgr::Factory
std::function< PacketQueueTypePtr(data::ConstElementPtr)> Factory
Defines the type of the packet queue factory function.
Definition packet_queue_mgr.h:45
isc::dhcp::PacketQueueMgr::PacketQueueMgr
PacketQueueMgr()
Constructor.
Definition packet_queue_mgr.h:48
isc::dhcp::PacketQueueMgr::unregisterPacketQueueFactory
bool unregisterPacketQueueFactory(const std::string &queue_type)
Unregisters the queue factory function for a given type.
Definition packet_queue_mgr.h:96
isc::dhcp::PacketQueueMgr::packet_queue_
PacketQueueTypePtr packet_queue_
the current queue_ ?
Definition packet_queue_mgr.h:183
isc::dhcp::PacketQueueMgr::destroyPacketQueue
void destroyPacketQueue()
Destroys the current packet queue.
Definition packet_queue_mgr.h:174
data.h
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
simple_parser.h
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::util::file
Definition filesystem.cc:28
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
packet_queue.h