From: Marcin Siodelski Date: Wed, 1 Aug 2012 16:08:05 +0000 (+0200) Subject: [1959] Use factory function to create option's instance. X-Git-Tag: trac2351_base~47^2~11^2~38 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1bb01f6f6ca354f10def4d35850a782d2ae348a0;p=thirdparty%2Fkea.git [1959] Use factory function to create option's instance. --- diff --git a/src/lib/dhcp/option.h b/src/lib/dhcp/option.h index 051b15dd4e..37069d6df5 100644 --- a/src/lib/dhcp/option.h +++ b/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 /// diff --git a/tests/tools/perfdhcp/test_control.cc b/tests/tools/perfdhcp/test_control.cc index 728d4d58ec..8072e75461 100644 --- a/tests/tools/perfdhcp/test_control.cc +++ b/tests/tools/perfdhcp/test_control.cc @@ -58,14 +58,18 @@ TestControl::checkExitConditions() const { return(false); } -Pkt4* -TestControl::createDiscoverPkt4() { +boost::shared_ptr +TestControl::createDiscoverPkt4() const { const uint32_t transid = static_cast(random()); - Pkt4* pkt4 = new Pkt4(DHCPDISCOVER, transid); + boost::shared_ptr 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); } diff --git a/tests/tools/perfdhcp/test_control.h b/tests/tools/perfdhcp/test_control.h index b1d7d27d32..ce5aa4c4b6 100644 --- a/tests/tools/perfdhcp/test_control.h +++ b/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 createDiscoverPkt4() const; static dhcp::OptionPtr factoryRequestList4(dhcp::Option::Universe u, uint16_t type,