From 203998470ad50542e2b10ce4cf1723f98b9e3f75 Mon Sep 17 00:00:00 2001 From: Bjoern Sikora Date: Mon, 21 Sep 2009 16:41:40 +0200 Subject: [PATCH] Also (de-)serialize object state (LastWebcheck) of IPAddrHelper. This is needed to prevent massiv use of webcheck IP url's. --- src/ip_addr_helper.cpp | 15 +++++++++++++-- src/ip_addr_helper.h | 14 +++++++++++++- src/serviceholder.cpp | 33 ++++++++++++++++++++++++++++++--- src/serviceholder.h | 7 +++++++ src/updater.cpp | 20 +++++++++++++++++--- 5 files changed, 80 insertions(+), 9 deletions(-) diff --git a/src/ip_addr_helper.cpp b/src/ip_addr_helper.cpp index b31df36..a55e0c3 100644 --- a/src/ip_addr_helper.cpp +++ b/src/ip_addr_helper.cpp @@ -21,6 +21,7 @@ namespace net = boost::asio; IPAddrHelper::IPAddrHelper() : Log(new Logger) , WebcheckInterval(0) + , LastWebcheck(0) , ProxyPort(0) , UseIPv6(false) { @@ -30,12 +31,12 @@ IPAddrHelper::IPAddrHelper() /** * Constructor. */ -IPAddrHelper::IPAddrHelper(const Logger::Ptr _log, const string& _webcheck_url, const string& _webcheck_url_alt, const int _webcheck_interval ,const bool _use_ipv6, const string& _proxy, const int _proxy_port) +IPAddrHelper::IPAddrHelper(const Logger::Ptr _log, const string& _webcheck_url, const string& _webcheck_url_alt, const int _webcheck_interval, const int _last_webcheck ,const bool _use_ipv6, const string& _proxy, const int _proxy_port) : Log(_log) , WebcheckIpUrl(_webcheck_url) , WebcheckIpUrlAlt(_webcheck_url_alt) , WebcheckInterval(_webcheck_interval) - , LastWebcheck(0) + , LastWebcheck(_last_webcheck) , Proxy(_proxy) , ProxyPort(_proxy_port) , UseIPv6(_use_ipv6) @@ -377,3 +378,13 @@ int IPAddrHelper::http_receive( char *inBuffer, size_t size, size_t nmemb, strin outBuffer->append(inBuffer); return (size*nmemb); } + + +/** + * Get member LastWebcheck + * @return LastWebcheck + */ +int IPAddrHelper::get_last_webcheck() const +{ + return LastWebcheck; +} diff --git a/src/ip_addr_helper.h b/src/ip_addr_helper.h index 14b89ca..ebd855f 100644 --- a/src/ip_addr_helper.h +++ b/src/ip_addr_helper.h @@ -15,12 +15,22 @@ #include #include +#include +#include + class IPAddrHelper { private: + friend class boost::serialization::access; + template + void serialize(Archive & ar, const unsigned int version) + { + ar & LastWebcheck; + } + Logger::Ptr Log; std::string WebcheckIpUrl; std::string WebcheckIpUrlAlt; @@ -43,7 +53,7 @@ public: IPAddrHelper(); - IPAddrHelper(const Logger::Ptr _log, const std::string& _webcheck_url, const std::string& _webcheck_url_alt, const int _webcheck_interval, const bool _use_ipv6, const std::string& _proxy, const int _proxy_port); + IPAddrHelper(const Logger::Ptr _log, const std::string& _webcheck_url, const std::string& _webcheck_url_alt, const int _webcheck_interval, const int _last_webcheck, const bool _use_ipv6, const std::string& _proxy, const int _proxy_port); ~IPAddrHelper(); @@ -55,6 +65,8 @@ public: static int http_receive(char *inBuffer, size_t size, size_t nmemb, std::string *outBuffer); void set_curl_url(CURL * curl_easy_handle, const std::string& url) const; + + int get_last_webcheck() const; }; #endif diff --git a/src/serviceholder.cpp b/src/serviceholder.cpp index 2e5c395..c1af7c8 100644 --- a/src/serviceholder.cpp +++ b/src/serviceholder.cpp @@ -26,6 +26,7 @@ namespace fs = boost::filesystem; */ Serviceholder::Serviceholder(Logger::Ptr _log) :Log(_log) + ,IPAddrHelp() { } @@ -60,15 +61,16 @@ int Serviceholder::serialize_services() service_container->add_service(service); } - // Serialize Serviceholder into file. + // Serialize SerializeServiceContainer and IPAddrHelper into file. ofstream ofs(OBJECT_FILE); if ( ofs.is_open() ) { SerializeServiceContainer* _service_container = service_container.get(); + IPAddrHelper* _ip_addr_helper = IPAddrHelp.get(); try { boost::archive::text_oarchive oa(ofs); - oa << _service_container; + oa << _service_container << _ip_addr_helper; } catch( boost::archive::archive_exception e ) { @@ -111,10 +113,11 @@ int Serviceholder::deserialize_services() { // deserialize SerializeServiceContainer SerializeServiceContainer* _service_container; + IPAddrHelper* _ip_addr_helper; try { boost::archive::text_iarchive ia(ifs); - ia >> _service_container; + ia >> _service_container >> _ip_addr_helper; } catch( boost::archive::archive_exception e ) { @@ -123,6 +126,8 @@ int Serviceholder::deserialize_services() return -1; } SerializeServiceContainer::Ptr service_container(_service_container); + IPAddrHelper::Ptr ip_addr_helper(_ip_addr_helper); + IPAddrHelp.swap(ip_addr_helper); ifs.close(); // Get the list of old Services from de-serialized SerializeServiceContainer object. @@ -185,6 +190,8 @@ void Serviceholder::delete_services() service.reset(); } OldServices.clear(); + + IPAddrHelp.reset(); } @@ -196,3 +203,23 @@ std::list Serviceholder::get_services() const { return Services; } + + +/** + * Set member IPAddrHelp + * @param _ip_addr_helper The IPAddrHelper. + */ +void Serviceholder::set_ip_addr_helper(IPAddrHelper::Ptr _ip_addr_helper) +{ + IPAddrHelp = _ip_addr_helper; +} + + +/** + * Get member IPAddrHelp + * @return IPAddrHelp + */ +IPAddrHelper::Ptr Serviceholder::get_ip_addr_helper() const +{ + return IPAddrHelp; +} diff --git a/src/serviceholder.h b/src/serviceholder.h index 2653783..348e90a 100644 --- a/src/serviceholder.h +++ b/src/serviceholder.h @@ -13,6 +13,7 @@ #include "service.h" #include "serializeservicecontainer.h" #include "logger.h" +#include "ip_addr_helper.h" #include #include @@ -28,6 +29,8 @@ private: std::list OldServices; // Represents all old Services where the timeout isn't expired (in case the same Service is redefined). std::list Services; // Represents all active Services. + IPAddrHelper::Ptr IPAddrHelp; + public: typedef boost::shared_ptr Ptr; @@ -44,6 +47,10 @@ public: void delete_services(); + void set_ip_addr_helper(IPAddrHelper::Ptr _ip_addr_helper); + + IPAddrHelper::Ptr get_ip_addr_helper() const; + std::list get_services() const; }; diff --git a/src/updater.cpp b/src/updater.cpp index 8872535..3f92a48 100644 --- a/src/updater.cpp +++ b/src/updater.cpp @@ -163,9 +163,23 @@ int Updater::init_helper_classes() */ int Updater::init_ip_helper() { - // initialize IPHelper - IPAddrHelper::Ptr _ipaddrhelp( new IPAddrHelper( Log, Conf->get_webcheck_ip_url(), Conf->get_webcheck_ip_url_alt(), Conf->get_webcheck_interval(), Conf->get_enable_ipv6(), Conf->get_proxy(), Conf->get_proxy_port() ) ); - IPAddrHelp.swap(_ipaddrhelp); + // Try to get deserialized IPAddrHelper from ServiceHolder + IPAddrHelper::Ptr _ip_addr_help = ServiceHolder->get_ip_addr_helper(); + if ( _ip_addr_help.use_count() != 0 ) + { + // Initialize IPHelper + IPAddrHelper::Ptr ip_addr_help( new IPAddrHelper( Log, Conf->get_webcheck_ip_url(), Conf->get_webcheck_ip_url_alt(), Conf->get_webcheck_interval(), _ip_addr_help->get_last_webcheck(), Conf->get_enable_ipv6(), Conf->get_proxy(), Conf->get_proxy_port() ) ); + IPAddrHelp.swap(ip_addr_help); + } + else + { + // IPAddrHelper from ServiceHolder was not declared, so init oen with LastWebcheck 0 + IPAddrHelper::Ptr ip_addr_help( new IPAddrHelper( Log, Conf->get_webcheck_ip_url(), Conf->get_webcheck_ip_url_alt(), Conf->get_webcheck_interval(), 0, Conf->get_enable_ipv6(), Conf->get_proxy(), Conf->get_proxy_port() ) ); + IPAddrHelp.swap(ip_addr_help); + } + + // Put the IPAddrHelper into ServiceHolder, so the LastWebcheck state will be serialized too. + ServiceHolder->set_ip_addr_helper(IPAddrHelp); return 0; } -- 1.7.1