Kea: resource_handler.cc Source File

| Kea 3.2.0-git

resource_handler.cc
Go to the documentation of this file.
1// Copyright (C) 2020-2024 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#include <config.h>
8
9#include <dhcpsrv/resource_handler.h>
10#include <exceptions/exceptions.h>
11
12using namespace std;
13using namespace isc::asiolink;
14using namespace isc::util;
15
16namespace isc {
17namespace dhcp {
18
19mutex ResourceHandler::mutex_;
20
21ResourceHandler::ResourceContainer ResourceHandler::resources_;
22
23ResourceHandler::ResourceHandler() : owned_() {
24}
25
26ResourceHandler::~ResourceHandler() {
27 lock_guard<mutex> lock_(mutex_);
28 for (auto const& res : owned_) {
29 unLockInternal(res->type_, res->addr_);
30 }
31 owned_.clear();
32}
33
34ResourceHandler::ResourcePtr
35ResourceHandler::lookup(Lease::Type type, const asiolink::IOAddress& addr) {
36 auto key = boost::make_tuple(type, addr.toBytes());
37 auto it = resources_.find(key);
38 if (it == resources_.end()) {
39 return (ResourcePtr());
40 }
41 return (*it);
42}
43
44void
45ResourceHandler::lock(Lease::Type type, const asiolink::IOAddress& addr) {
46 ResourcePtr res(new Resource(type, addr));
47 // Assume insert will never fail so not checking its result.
48 resources_.insert(res);
49 owned_.insert(res);
50}
51
52void
53ResourceHandler::unLockInternal(Lease::Type type,
54 const asiolink::IOAddress& addr) {
55 auto key = boost::make_tuple(type, addr.toBytes());
56 auto it = resources_.find(key);
57 if (it == resources_.end()) {
58 return;
59 }
60 resources_.erase(it);
61}
62
63bool
64ResourceHandler::tryLock(Lease::Type type, const asiolink::IOAddress& addr) {
65 ResourcePtr holder;
66 // Try to acquire the lock and return the holder when it failed.
67 lock_guard<mutex> lock_(mutex_);
68 holder = lookup(type, addr);
69 if (holder) {
70 return (false);
71 }
72 lock(type, addr);
73 return (true);
74}
75
76bool
77ResourceHandler::isLocked(Lease::Type type, const asiolink::IOAddress& addr) {
78 auto key = boost::make_tuple(type, addr.toBytes());
79 lock_guard<mutex> lock_(mutex_);
80 auto it = owned_.find(key);
81 return (it != owned_.end());
82}
83
84void
85ResourceHandler::unLock(Lease::Type type, const asiolink::IOAddress& addr) {
86 auto key = boost::make_tuple(type, addr.toBytes());
87 lock_guard<mutex> lock_(mutex_);
88 auto it = owned_.find(key);
89 if (it == owned_.end()) {
90 isc_throw(NotFound, "does not own " << Lease::typeToText(type)
91 << " " << addr.toText());
92 }
93 unLockInternal(type, addr);
94 owned_.erase(it);
95}
96
97} // namespace dhcp
98} // namespace isc
isc::NotFound
A generic exception that is thrown when an object can not be found.
Definition exceptions/exceptions.h:175
isc::asiolink::IOAddress
The IOAddress class represents an IP addresses (version agnostic).
Definition io_address.h:53
isc::asiolink::IOAddress::toText
std::string toText() const
Convert the address to a string.
Definition io_address.cc:60
isc::asiolink::IOAddress::toBytes
std::vector< uint8_t > toBytes() const
Return address as set of bytes.
Definition io_address.cc:81
isc::dhcp::ResourceHandler::~ResourceHandler
virtual ~ResourceHandler()
Destructor.
Definition resource_handler.cc:26
isc::dhcp::ResourceHandler::tryLock
bool tryLock(Lease::Type type, const asiolink::IOAddress &addr)
Tries to acquires a resource.
Definition resource_handler.cc:64
isc::dhcp::ResourceHandler::unLock
void unLock(Lease::Type type, const asiolink::IOAddress &addr)
Releases a resource.
Definition resource_handler.cc:85
isc::dhcp::ResourceHandler::ResourceHandler
ResourceHandler()
Constructor.
Definition resource_handler.cc:23
isc::dhcp::ResourceHandler::isLocked
bool isLocked(Lease::Type type, const asiolink::IOAddress &addr)
Checks if a resource is owned by this handler.
Definition resource_handler.cc:77
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::asiolink
Definition addr_utilities.cc:170
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::util
Definition time_utils.cc:43
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
resource_handler.h
isc::dhcp::Lease::Type
Type
Type of lease or pool.
Definition lease.h:46
isc::dhcp::Lease::typeToText
static std::string typeToText(Type type)
returns text representation of a lease type
Definition lease.cc:56