Commit 8422f4dd authored by Tomek Mrugalski's avatar Tomek Mrugalski 🛰
Browse files

[2324] LeaseMgr is now a meta-singleton

- only one of its derived classes can be instantiated
parent 8d321e3e
...@@ -12,6 +12,10 @@ ...@@ -12,6 +12,10 @@
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE. // PERFORMANCE OF THIS SOFTWARE.
#include "lease_mgr.h"
#include <exceptions/exceptions.h>
#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>
#include <sstream> #include <sstream>
#include <iostream> #include <iostream>
#include <map> #include <map>
...@@ -20,16 +24,38 @@ ...@@ -20,16 +24,38 @@
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
#include <iterator> #include <iterator>
#include <exceptions/exceptions.h> #include <time.h>
#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>
#include "lease_mgr.h"
using namespace std; using namespace std;
using namespace isc::dhcp; using namespace isc::dhcp;
LeaseMgr* LeaseMgr::instance_ = NULL;
Lease6::Lease6(LeaseType type, const isc::asiolink::IOAddress& addr, DuidPtr duid,
uint32_t iaid, uint32_t preferred, uint32_t valid, uint32_t t1,
uint32_t t2, SubnetID subnet_id)
:type_(type), addr_(addr), iaid_(iaid), duid_(duid),
preferred_lft_(preferred), valid_lft_(valid), t1_(t1), t2_(t2),
subnet_id_(subnet_id), fixed_(false), fqdn_fwd_(false),
fqdn_rev_(false) {
cltt_ = time(NULL);
}
LeaseMgr& LeaseMgr::instance() {
if (!instance_) {
isc_throw(InvalidOperation, "LeaseManager not instantiated yet");
}
return (*instance_);
}
LeaseMgr::LeaseMgr(const std::string& dbconfig) { LeaseMgr::LeaseMgr(const std::string& dbconfig) {
if (instance_) {
isc_throw(InvalidOperation, "LeaseManager already instantiated");
}
// remember the pointer to the singleton instance
instance_ = this;
if (dbconfig.length() == 0) { if (dbconfig.length() == 0) {
return; return;
...@@ -65,4 +91,5 @@ std::string LeaseMgr::getParameter(const std::string& name) const { ...@@ -65,4 +91,5 @@ std::string LeaseMgr::getParameter(const std::string& name) const {
} }
LeaseMgr::~LeaseMgr() { LeaseMgr::~LeaseMgr() {
instance_ = NULL;
} }
...@@ -17,9 +17,11 @@ ...@@ -17,9 +17,11 @@
#include <vector> #include <vector>
#include <map> #include <map>
#include <asiolink/io_address.h> #include <asiolink/io_address.h>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <dhcp/option.h> #include <dhcp/option.h>
#include <dhcp/duid.h> #include <dhcp/duid.h>
#include <dhcp/subnet.h>
/// @file dhcp/lease_mgr.h /// @file dhcp/lease_mgr.h
/// @brief An abstract API for lease database /// @brief An abstract API for lease database
...@@ -55,10 +57,6 @@ ...@@ -55,10 +57,6 @@
namespace isc { namespace isc {
namespace dhcp { namespace dhcp {
/// @brief specifies unique subnet identifier
/// @todo: Move this to subnet.h once ticket #2237 is merged
typedef uint32_t SubnetID;
/// @brief Structure that holds a lease for IPv4 address /// @brief Structure that holds a lease for IPv4 address
/// ///
/// For performance reasons it is a simple structure, not a class. If we chose /// For performance reasons it is a simple structure, not a class. If we chose
...@@ -161,6 +159,10 @@ struct Lease6 { ...@@ -161,6 +159,10 @@ struct Lease6 {
LEASE_IA_PD /// the lease contains IPv6 prefix (for prefix delegation) LEASE_IA_PD /// the lease contains IPv6 prefix (for prefix delegation)
} LeaseType; } LeaseType;
Lease6(LeaseType type, const isc::asiolink::IOAddress& addr, DuidPtr duid,
uint32_t iaid, uint32_t preferred, uint32_t valid, uint32_t t1,
uint32_t t2, SubnetID subnet_id);
/// @brief specifies lease type (normal addr, temporary addr, prefix) /// @brief specifies lease type (normal addr, temporary addr, prefix)
LeaseType type_; LeaseType type_;
...@@ -208,6 +210,8 @@ struct Lease6 { ...@@ -208,6 +210,8 @@ struct Lease6 {
/// entity, we are keeping it in the lease. In case of multiple addresses/prefixes /// entity, we are keeping it in the lease. In case of multiple addresses/prefixes
/// for the same IA, each must have consistent T1 and T2 values. Specified in /// for the same IA, each must have consistent T1 and T2 values. Specified in
/// seconds since cltt. /// seconds since cltt.
/// This value will also be useful for failover to calculate the next expected
/// client transmission time.
uint32_t t1_; uint32_t t1_;
/// @brief T2 timer /// @brief T2 timer
...@@ -268,22 +272,23 @@ typedef std::vector< boost::shared_ptr<Lease6Ptr> > Lease6Collection; ...@@ -268,22 +272,23 @@ typedef std::vector< boost::shared_ptr<Lease6Ptr> > Lease6Collection;
/// interface to all backends. As this is an abstract class, it should not /// interface to all backends. As this is an abstract class, it should not
/// be used directly, but rather specialized derived class should be used /// be used directly, but rather specialized derived class should be used
/// instead. /// instead.
class LeaseMgr { class LeaseMgr : public boost::noncopyable {
public: public:
/// Client Hardware address /// Client Hardware address
typedef std::vector<uint8_t> HWAddr; typedef std::vector<uint8_t> HWAddr;
/// @brief The sole lease manager constructor /// @brief returns a single instance of LeaseMgr
/// ///
/// dbconfig is a generic way of passing parameters. Parameters /// LeaseMgr is a singleton and this method is the only way of
/// are passed in the "name=value" format, separated by spaces. /// accessing it. LeaseMgr must be create first using
/// Values may be enclosed in double quotes, if needed. /// instantiate() method, otherwise instance() will throw
/// /// InvalidOperation exception.
/// @param dbconfig database configuration /// @throw InvalidOperation if LeaseMgr not instantiated
LeaseMgr(const std::string& dbconfig); static LeaseMgr& instance();
void instantiate(const std::string& config);
/// @brief Destructor (closes file) /// @brief Destructor
virtual ~LeaseMgr(); virtual ~LeaseMgr();
/// @brief Adds an IPv4 lease. /// @brief Adds an IPv4 lease.
...@@ -464,6 +469,15 @@ public: ...@@ -464,6 +469,15 @@ public:
/// is currently postponed. /// is currently postponed.
protected: protected:
/// @brief The sole lease manager constructor
///
/// dbconfig is a generic way of passing parameters. Parameters
/// are passed in the "name=value" format, separated by spaces.
/// Values may be enclosed in double quotes, if needed.
///
/// @param dbconfig database configuration
LeaseMgr(const std::string& dbconfig);
/// @brief returns value of the parameter /// @brief returns value of the parameter
std::string getParameter(const std::string& name) const; std::string getParameter(const std::string& name) const;
...@@ -473,6 +487,8 @@ protected: ...@@ -473,6 +487,8 @@ protected:
/// password and other parameters required for DB access. It is not /// password and other parameters required for DB access. It is not
/// intended to keep any DHCP-related parameters. /// intended to keep any DHCP-related parameters.
std::map<std::string, std::string> parameters_; std::map<std::string, std::string> parameters_;
static LeaseMgr* instance_;
}; };
}; // end of isc::dhcp namespace }; // end of isc::dhcp namespace
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment