Kea  1.9.9-git
resource_handler.h
Go to the documentation of this file.
1 // Copyright (C) 2020 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 RESOURCE_HANDLER_H
8 #define RESOURCE_HANDLER_H
9 
10 #include <asiolink/io_address.h>
11 #include <dhcpsrv/lease.h>
12 
13 #include <boost/noncopyable.hpp>
14 #include <boost/multi_index_container.hpp>
15 #include <boost/multi_index/composite_key.hpp>
16 #include <boost/multi_index/hashed_index.hpp>
17 #include <boost/multi_index/member.hpp>
18 #include <boost/multi_index/mem_fun.hpp>
19 #include <boost/shared_ptr.hpp>
20 
21 #include <mutex>
22 
23 namespace isc {
24 namespace dhcp {
25 
27 class ResourceHandler : public boost::noncopyable {
28 public:
29 
32 
36  virtual ~ResourceHandler();
37 
47  bool tryLock(Lease::Type type, const asiolink::IOAddress& addr);
48 
54  bool isLocked(Lease::Type type, const asiolink::IOAddress& addr);
55 
63  void unLock(Lease::Type type, const asiolink::IOAddress& addr);
64 
65 private:
66 
68 
69 
71  struct Resource {
72 
76  Resource(Lease::Type type, const asiolink::IOAddress& addr)
77  : type_(type), addr_(addr) {
78  }
79 
81  Lease::Type type_;
82 
84  asiolink::IOAddress addr_;
85 
87  std::vector<uint8_t> toBytes() const {
88  return (addr_.toBytes());
89  }
90  };
91 
93  typedef boost::shared_ptr<Resource> ResourcePtr;
94 
96  typedef boost::multi_index_container<
97 
98  // This container stores pointers to resource objects.
99  ResourcePtr,
100 
101  // Start specification of indexes here.
102  boost::multi_index::indexed_by<
103 
104  // First index is used to search by type and address.
105  boost::multi_index::hashed_unique<
106  boost::multi_index::composite_key<
107  Resource,
108  // Lease type.
109  boost::multi_index::member<
110  Resource, Lease::Type, &Resource::type_
111  >,
112  // Address bytes.
113  boost::multi_index::const_mem_fun<
114  Resource, std::vector<uint8_t>, &Resource::toBytes
115  >
116  >
117  >
118  >
119  > ResourceContainer;
120 
122 
124 
125 
127  static ResourceContainer resources_;
128 
130  static std::mutex mutex_;
131 
139  static ResourcePtr
140  lookup(Lease::Type type, const asiolink::IOAddress& addr);
141 
143 
145 
146 
153  void lock(Lease::Type type, const asiolink::IOAddress& addr);
154 
163  void unLockInternal(Lease::Type type, const asiolink::IOAddress& addr);
164 
166  ResourceContainer owned_;
167 
169 };
170 
173 public:
174 
178  virtual ~ResourceHandler4() { }
179 
188  bool tryLock4(const asiolink::IOAddress& addr) {
189  return (tryLock(Lease::TYPE_V4, addr));
190  }
191 
196  bool isLocked4(const asiolink::IOAddress& addr) {
197  return (isLocked(Lease::TYPE_V4, addr));
198  }
199 
206  void unLock4(const asiolink::IOAddress& addr) {
207  unLock(Lease::TYPE_V4, addr);
208  }
209 };
210 
211 } // namespace isc
212 } // namespace dhcp
213 
214 #endif // RESOURCE_HANDLER_H
virtual ~ResourceHandler()
Destructor.
Resource race avoidance RAII handler for DHCPv4.
Resource race avoidance RAII handler.
bool tryLock(Lease::Type type, const asiolink::IOAddress &addr)
Tries to acquires a resource.
void unLock(Lease::Type type, const asiolink::IOAddress &addr)
Releases a resource.
virtual ~ResourceHandler4()
Destructor.
IPv4 lease.
Definition: lease.h:54
Defines the logger used by the top-level component of kea-dhcp-ddns.
Type
Type of lease or pool.
Definition: lease.h:50
bool isLocked4(const asiolink::IOAddress &addr)
Checks if a resource is owned by this handler.
void unLock4(const asiolink::IOAddress &addr)
Releases a resource.
bool isLocked(Lease::Type type, const asiolink::IOAddress &addr)
Checks if a resource is owned by this handler.
bool tryLock4(const asiolink::IOAddress &addr)
Tries to acquires a resource.