Browse Source

[2324] LeaseMgr is now a meta-singleton

- only one of its derived classes can be instantiated
Tomek Mrugalski 12 years ago
parent
commit
8422f4dd15
2 changed files with 61 additions and 18 deletions
  1. 31 4
      src/lib/dhcp/lease_mgr.cc
  2. 30 14
      src/lib/dhcp/lease_mgr.h

+ 31 - 4
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 <exceptions/exceptions.h>
+#include <boost/foreach.hpp>
+#include <boost/algorithm/string.hpp>
 #include <sstream>
 #include <iostream>
 #include <map>
@@ -20,16 +24,38 @@
 #include <sstream>
 #include <algorithm>
 #include <iterator>
-#include <exceptions/exceptions.h>
-#include <boost/foreach.hpp>
-#include <boost/algorithm/string.hpp>
-#include "lease_mgr.h"
+#include <time.h>
 
 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;
 }

+ 30 - 14
src/lib/dhcp/lease_mgr.h

@@ -17,9 +17,11 @@
 #include <vector>
 #include <map>
 #include <asiolink/io_address.h>
+#include <boost/noncopyable.hpp>
 #include <boost/shared_ptr.hpp>
 #include <dhcp/option.h>
 #include <dhcp/duid.h>
+#include <dhcp/subnet.h>
 
 /// @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<Lease6Ptr> > 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<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
-    /// 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<std::string, std::string> parameters_;
+
+    static LeaseMgr* instance_;
 };
 
 }; // end of isc::dhcp namespace