Browse Source

[1959] Use factory function to create option's instance.

Marcin Siodelski 12 years ago
parent
commit
1bb01f6f6c
3 changed files with 29 additions and 9 deletions
  1. 17 0
      src/lib/dhcp/option.h
  2. 11 8
      tests/tools/perfdhcp/test_control.cc
  3. 1 1
      tests/tools/perfdhcp/test_control.h

+ 17 - 0
src/lib/dhcp/option.h

@@ -82,6 +82,23 @@ public:
                              uint16_t type,
                              const OptionBuffer& buf);
 
+    /// @brief Factory function to create instance of option.
+    ///
+    /// Factory method creates instance of specified option. The option
+    /// to be created has to have corresponding factory function
+    /// registered with \ref LibDHCP::OptionFactoryRegister.
+    /// This method creates empty \ref OptionBuffer object. Use this
+    /// factory function if it is not needed to pass custom buffer.
+    ///
+    /// @param u universe of the option (V4 or V6)
+    /// @param type option-type
+    /// @throw isc::InvalidOperation if there is no factory function
+    /// registered for specified option type.
+    /// @return instance of option.
+    static OptionPtr factory(Option::Universe u, uint16_t type) {
+        return factory(u, type, OptionBuffer());
+    }
+
 
     /// @brief ctor, used for options constructed, usually during transmission
     ///

+ 11 - 8
tests/tools/perfdhcp/test_control.cc

@@ -58,14 +58,18 @@ TestControl::checkExitConditions() const {
     return(false);
 }
 
-Pkt4*
-TestControl::createDiscoverPkt4() {
+boost::shared_ptr<Pkt4>
+TestControl::createDiscoverPkt4() const {
     const uint32_t transid = static_cast<uint32_t>(random());
-    Pkt4* pkt4 = new Pkt4(DHCPDISCOVER, transid);
+    boost::shared_ptr<Pkt4> pkt4(new Pkt4(DHCPDISCOVER, transid));
+    if (!pkt4) {
+        isc_throw(isc::Unexpected, "failed to create DISCOVER packet");
+    }
 
-    OptionBuffer opt_request_list_buf();
-    // createRequestListBuffer4(opt_request_list_buf);
-    return NULL;
+    OptionPtr request_list_option =
+        Option::factory(Option::V4, DHO_DHCP_PARAMETER_REQUEST_LIST);
+    pkt4->addOption(request_list_option);
+    return pkt4;
 }
 
 OptionPtr
@@ -85,8 +89,7 @@ TestControl::factoryRequestList4(Option::Universe u,
 
     OptionBuffer buf_with_options(buf_array, buf_array + sizeof(buf_array));
     Option* opt = new Option(u, type, buf);
-    opt->setData(buf_with_options.begin(),
-                 buf_with_options.end());
+    opt->setData(buf_with_options.begin(), buf_with_options.end());
     return OptionPtr(opt);
 }
 

+ 1 - 1
tests/tools/perfdhcp/test_control.h

@@ -69,7 +69,7 @@ private:
     /// \return true if any of the exit conditions is fulfiled.
     bool checkExitConditions() const;
 
-    dhcp::Pkt4* createDiscoverPkt4();
+    boost::shared_ptr<dhcp::Pkt4> createDiscoverPkt4() const;
 
     static dhcp::OptionPtr factoryRequestList4(dhcp::Option::Universe u,
                                                uint16_t type,