Also (de-)serialize object state (LastWebcheck) of IPAddrHelper.
authorBjoern Sikora <bjoern.sikora@intra2net.com>
Mon, 21 Sep 2009 14:41:40 +0000 (16:41 +0200)
committerBjoern Sikora <bjoern.sikora@intra2net.com>
Mon, 21 Sep 2009 14:41:40 +0000 (16:41 +0200)
This is needed to prevent massiv use of webcheck IP url's.

src/ip_addr_helper.cpp
src/ip_addr_helper.h
src/serviceholder.cpp
src/serviceholder.h
src/updater.cpp

index b31df36..a55e0c3 100644 (file)
@@ -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;
+}
index 14b89ca..ebd855f 100644 (file)
 #include <boost/shared_ptr.hpp>
 #include <curl/curl.h>
 
+#include <boost/archive/text_oarchive.hpp>
+#include <boost/archive/text_iarchive.hpp>
+
 
 class IPAddrHelper
 {
 
 private:
 
+    friend class boost::serialization::access;
+    template<class Archive>
+    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
index 2e5c395..c1af7c8 100644 (file)
@@ -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<Service::Ptr> 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;
+}
index 2653783..348e90a 100644 (file)
@@ -13,6 +13,7 @@
 #include "service.h"
 #include "serializeservicecontainer.h"
 #include "logger.h"
+#include "ip_addr_helper.h"
 
 #include <boost/shared_ptr.hpp>
 #include <list>
@@ -28,6 +29,8 @@ private:
     std::list<Service::Ptr> OldServices;      // Represents all old Services where the timeout isn't expired (in case the same Service is redefined).
     std::list<Service::Ptr> Services;         // Represents all active Services.
 
+    IPAddrHelper::Ptr IPAddrHelp;
+
 public:
 
     typedef boost::shared_ptr<Serviceholder> 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<Service::Ptr> get_services() const;
 };
 
index 8872535..3f92a48 100644 (file)
@@ -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;
 }