From: Tomek Mrugalski Date: Wed, 17 Oct 2012 16:03:31 +0000 (+0200) Subject: [2324] LeaseMgr is now a meta-singleton X-Git-Tag: trac2487_base~21^2~9^2~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8422f4dd15cbdd9bd33f3a570bdf40b991665ab9;p=thirdparty%2Fkea.git [2324] LeaseMgr is now a meta-singleton - only one of its derived classes can be instantiated --- diff --git a/src/lib/dhcp/lease_mgr.cc b/src/lib/dhcp/lease_mgr.cc index 8291df3e61..338a5cd03e 100644 --- a/src/lib/dhcp/lease_mgr.cc +++ b/src/lib/dhcp/lease_mgr.cc @@ -12,6 +12,10 @@ // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. +#include "lease_mgr.h" +#include +#include +#include #include #include #include @@ -20,16 +24,38 @@ #include #include #include -#include -#include -#include -#include "lease_mgr.h" +#include using namespace std; 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) { + if (instance_) { + isc_throw(InvalidOperation, "LeaseManager already instantiated"); + } + + // remember the pointer to the singleton instance + instance_ = this; if (dbconfig.length() == 0) { return; @@ -65,4 +91,5 @@ std::string LeaseMgr::getParameter(const std::string& name) const { } LeaseMgr::~LeaseMgr() { + instance_ = NULL; } diff --git a/src/lib/dhcp/lease_mgr.h b/src/lib/dhcp/lease_mgr.h index 4b7a1afa25..de141712d5 100644 --- a/src/lib/dhcp/lease_mgr.h +++ b/src/lib/dhcp/lease_mgr.h @@ -17,9 +17,11 @@ #include #include #include +#include #include #include #include +#include /// @file dhcp/lease_mgr.h /// @brief An abstract API for lease database @@ -55,10 +57,6 @@ namespace isc { 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 /// /// For performance reasons it is a simple structure, not a class. If we chose @@ -161,6 +159,10 @@ struct Lease6 { LEASE_IA_PD /// the lease contains IPv6 prefix (for prefix delegation) } 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) LeaseType type_; @@ -208,6 +210,8 @@ struct Lease6 { /// 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 /// seconds since cltt. + /// This value will also be useful for failover to calculate the next expected + /// client transmission time. uint32_t t1_; /// @brief T2 timer @@ -268,22 +272,23 @@ typedef std::vector< boost::shared_ptr > Lease6Collection; /// interface to all backends. As this is an abstract class, it should not /// be used directly, but rather specialized derived class should be used /// instead. -class LeaseMgr { +class LeaseMgr : public boost::noncopyable { public: - /// Client Hardware address typedef std::vector HWAddr; - /// @brief The sole lease manager constructor + /// @brief returns a single instance of LeaseMgr /// - /// 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); + /// LeaseMgr is a singleton and this method is the only way of + /// accessing it. LeaseMgr must be create first using + /// instantiate() method, otherwise instance() will throw + /// InvalidOperation exception. + /// @throw InvalidOperation if LeaseMgr not instantiated + static LeaseMgr& instance(); + + void instantiate(const std::string& config); - /// @brief Destructor (closes file) + /// @brief Destructor virtual ~LeaseMgr(); /// @brief Adds an IPv4 lease. @@ -464,6 +469,15 @@ public: /// is currently postponed. 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 std::string getParameter(const std::string& name) const; @@ -473,6 +487,8 @@ protected: /// password and other parameters required for DB access. It is not /// intended to keep any DHCP-related parameters. std::map parameters_; + + static LeaseMgr* instance_; }; }; // end of isc::dhcp namespace