Managed Service de/serialization through dedicated Serviceholder class.
authorBjoern Sikora <bjoern.sikora@intra2net.com>
Fri, 7 Aug 2009 10:58:58 +0000 (12:58 +0200)
committerBjoern Sikora <bjoern.sikora@intra2net.com>
Fri, 7 Aug 2009 10:58:58 +0000 (12:58 +0200)
src/config.cpp
src/config.h
src/dhs.cpp
src/logger.cpp
src/logger.h
src/main.cpp
src/ods.cpp
src/service.cpp
src/service.h
src/updater.cpp

index 4297049..bca9dc3 100644 (file)
@@ -94,17 +94,73 @@ int Config::serialize_services()
 {
     //TODO: error handling
 
-    cout << "starting serialize objects" << endl;
-    // serialize service objects
-    ofstream ofs("/home/bjoern/intranator/bpdyndnsd/objects.ser");
-    boost::archive::text_oarchive oa(ofs);
+    // First of all we have to put all service objects in a Serviceholder object.
+
+    ServiceholderPtr service_holder(new Serviceholder());
 
     BOOST_FOREACH(ServicePtr service, Services)
     {
-        Service* ptr = service.get();
-        oa << BOOST_SERIALIZATION_NVP(ptr);
+        service_holder->add_service(service.get());
+    }
+
+    // Then we can serialize this separate Object into one file.
+
+    ofstream ofs(OBJECT_FILE);
+    if ( ofs.is_open() )
+    {
+        Serviceholder* _service_holder = service_holder.get();
+        boost::archive::text_oarchive oa(ofs);
+        oa << _service_holder;
+
+        ofs.close();
+    }
+
+    Log->print_serialized_objects_success();
+
+    return 0;
+}
+
+
+int Config::deserialize_services()
+{
+    // TODO: error handling
+
+
+    ifstream ifs(OBJECT_FILE);
+    if ( ifs.is_open() )
+    {
+        Serviceholder* _service_holder;
+        boost::archive::text_iarchive ia(ifs);
+        ia >> _service_holder;       // ends up in default constructor call for Serviceholder
+        ServiceholderPtr service_holder(_service_holder);
+        ifs.close();
+
+        list<Service*> old_services = service_holder->get_hold_services();
+
+        BOOST_FOREACH(Service* old_service, old_services)
+        {
+            Log->print_service_object("Deserialized following Service object:", old_service->get_protocol(), old_service->get_hostname(), old_service->get_login() ,old_service->get_password() ,old_service->get_actual_ip(), old_service->get_lastupdated());
+        }
+
+        BOOST_FOREACH(ServicePtr service_ptr, Services)
+        {
+            Service * service = service_ptr.get();
+            BOOST_FOREACH(Service* old_service, old_services)
+            {
+                if ( *service == *old_service )
+                {
+                    service->set_lastupdated(old_service->get_lastupdated());
+                    service->set_actual_ip(old_service->get_actual_ip());
+                    Log->print_service_object("New Service object with adopted values:", service_ptr->get_protocol(), service_ptr->get_hostname(), service_ptr->get_login() ,service_ptr->get_password() ,service_ptr->get_actual_ip(), service_ptr->get_lastupdated());
+                }
+            }
+        }
+        Log->print_deserialized_objects_success();
+    }
+    else
+    {
+        Log->print_error_opening(OBJECT_FILE);
     }
-    ofs.close();
 
     return 0;
 }
@@ -250,6 +306,7 @@ int Config::load_service_config_file(const string& full_filename)
                 if ( service )
                 {
                     Services.push_back(service);
+                    Log->print_service_object("New Service object from config:", service->get_protocol(), service->get_hostname(), service->get_login() ,service->get_password() ,service->get_actual_ip(), service->get_lastupdated());
                 }
             }
         }
index d13c077..d2cbe55 100644 (file)
 
 #include "service.h"
 #include "logger.h"
+#include "serviceholder.h"
 
 #include "dhs.h"
 #include "ods.h"
 
 typedef boost::shared_ptr<Service> ServicePtr;
 typedef boost::shared_ptr<Logger> LoggerPtr;
+typedef boost::shared_ptr<Serviceholder> ServiceholderPtr;
 
 class Config
 {
@@ -63,6 +65,8 @@ public:
 
     int serialize_services();
 
+    int deserialize_services();
+
     int parse_cmd_line(int, char **);
 
     int load_config_from_files();
index 4c80a12..3159268 100644 (file)
@@ -17,7 +17,6 @@ using namespace std;
  */
 DHS::DHS()
 {
-    get_logger()->print_constructor_call("DHS default!!!");
 }
 
 
index 5bfa424..cb40e32 100644 (file)
@@ -44,7 +44,7 @@ void Logger::log_notice(const string& msg)
     if ( Syslog )
         syslog(LOG_NOTICE,msg.c_str());
     else
-        cout << msg;
+        cout << msg << endl;;
 }
 
 
@@ -57,7 +57,7 @@ void Logger::log_warning(const string& msg)
     if ( Syslog )
         syslog(LOG_WARNING,msg.c_str());
     else
-        cout << msg;
+        cout << msg << endl;
 }
 
 
@@ -70,7 +70,7 @@ void Logger::log_error(const string& msg)
     if ( Syslog )
         syslog(LOG_ERR,msg.c_str());
     else
-        cerr << msg;
+        cerr << msg << endl;
 }
 
 
@@ -526,6 +526,7 @@ void Logger::print_init_log_facility()
     }
 }
 
+
 /**
  * Be verbose. Currently we are in offline mode.
  */
@@ -538,3 +539,48 @@ void Logger::print_offline_mode()
         log_notice(msg.str());
     }
 }
+
+
+/**
+ * Objects successfully serialized.
+ */
+void Logger::print_serialized_objects_success()
+{
+    if ( 1 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Serialized objects successfully." << endl;
+        log_notice(msg.str());
+    }
+}
+
+
+/**
+ * Objects successfully de-serialized.
+ */
+void Logger::print_deserialized_objects_success()
+{
+    if ( 1 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "De-serialized objects successfully." << endl;
+        log_notice(msg.str());
+    }
+}
+
+
+void Logger::print_service_object(const string& message, const string& protocol, const string& hostname, const string& login, const string& password, const string& actual_ip, const int lastupdated)
+{
+    if ( 1 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << message << endl;
+        msg << "\t" << "Protocol:    " << protocol << endl;
+        msg << "\t" << "Hostname:    " << hostname << endl;
+        msg << "\t" << "Login:       " <<login << endl;
+        msg << "\t" << "Password:    " <<password << endl;
+        msg << "\t" << "Actual_IP:   " <<actual_ip << endl;
+        msg << "\t" << "Lastupdated: " <<lastupdated << endl;
+        log_notice(msg.str());
+    }
+}
index 78638da..6435a1d 100644 (file)
@@ -13,6 +13,7 @@
 #include <syslog.h>
 #include <sstream>
 
+
 class Logger
 {
 private:
@@ -96,6 +97,12 @@ public:
     void print_init_log_facility();
 
     void print_offline_mode();
+
+    void print_serialized_objects_success();
+
+    void print_deserialized_objects_success();
+
+    void print_service_object(const std::string&, const std::string&, const std::string&, const std::string&, const std::string&, const std::string&, const int);
 };
 
 #endif
index 4c68013..4dbc914 100644 (file)
@@ -16,6 +16,7 @@
 #define RELEASE     0
 
 #define PIDFILE "/var/run/bpdyndnsd.pid"
+#define OBJECT_FILE "/home/bjoern/intranator/bpdyndnsd/objects.ser"
 
 #include <iostream>
 #include <list>
@@ -28,6 +29,7 @@
 #include "logger.cpp"
 
 #include "service.cpp"
+#include "serviceholder.cpp"
 
 #include "dhs.cpp"
 #include "ods.cpp"
index 8ef8ca9..aa09996 100644 (file)
@@ -17,7 +17,6 @@ using namespace std;
  */
 ODS::ODS()
 {
-    get_logger()->print_constructor_call("ODS default!!!");
 }
 
 
index b74bcd8..95eb8dd 100644 (file)
  * Default Constructor
  */
 Service::Service()
+    : Protocol("")
+    , Hostname("")
+    , Login("NOT SERIALIZED")
+    , Password("NOT SERIALIZED")
+    , Lastupdated(0)
+    , Actual_IP("0.0.0.0")
 {
 }
 
 
 /**
- * Default Destructor
+ * Default Destructor needed for deserialization.
  */
 Service::~Service()
 {
@@ -37,8 +43,9 @@ void Service::serialize(Archive & ar, const unsigned int version)
     // protocol and hostname are the unique identifier for each service.
     ar & Protocol;
     ar & Hostname;
-    // lastupdated must also be serialized, cause this is essential info of each service.
+    // lastupdated and actual_ip must also be serialized, cause these are essential infos of each service.
     ar & Lastupdated;
+    ar & Actual_IP;
 }
 
 
@@ -163,6 +170,27 @@ int Service::get_lastupdated()
 
 
 /**
+ * Setter for member Actual_IP.
+ * @param _actual_ip Value to set Actual_IP to.
+ */
+void Service::set_actual_ip(const std::string& _actual_ip)
+{
+    Actual_IP = _actual_ip;
+}
+
+
+/**
+ * Getter for member Actual_IP.
+ * @return Value of member Actual_IP.
+ */
+std::string Service::get_actual_ip()
+{
+    return Actual_IP;
+}
+
+
+
+/**
  * Overloading of comparison operator.
  * @param other Reference to other Service object.
  * @return True if they equal, false if not.
index 1fd2809..b020173 100644 (file)
@@ -23,10 +23,11 @@ class Service
 private:
     std::string Protocol;
     std::string Hostname;
+
     std::string Login;
     std::string Password;
 
-    LoggerPtr Log;
+    std::string Actual_IP;
 
     int Lastupdated;
 
@@ -34,6 +35,8 @@ private:
     template<class Archive>
     void serialize(Archive &, const unsigned int);
 
+    LoggerPtr Log;
+
 public:
     Service();
 
@@ -53,12 +56,15 @@ public:
     void set_password(const std::string&);
     std::string get_password();
 
-    void set_logger(const LoggerPtr&);
-    LoggerPtr get_logger();
-
     void set_lastupdated(const int);
     int get_lastupdated();
 
+    void set_actual_ip(const std::string&);
+    std::string get_actual_ip();
+
+    void set_logger(const LoggerPtr&);
+    LoggerPtr get_logger();
+
     bool operator== (const Service&) const;
     bool operator!= (const Service&) const;
 };
index 8bcf06a..fac511d 100644 (file)
@@ -65,13 +65,17 @@ int Updater::init_config_from_cmd(int argc, char *argv[])
 int Updater::init_config_from_files()
 {
     // Load the main and service config files in config path
-    if( Conf->load_config_from_files() != 0)
+    if ( Conf->load_config_from_files() != 0 )
         return 1;
 
     // Re-init log facility, perhaps new config file options for logger are set.
     // These config file options will only overwrite the cmd options if the SIGHUP (reload config) is caught.
     init_log_facility();
 
+    // Here we are. All Service Objects should be initialized, so it is time to deserialize the old service objects and compare them.
+    if ( Conf->deserialize_services() != 0 )
+        return 1;
+
     // successful loaded
     return 0;
 }
@@ -102,6 +106,9 @@ LoggerPtr Updater::get_logger()
  */
 void Updater::reload_config()
 {
+    // serialize all service objects
+    Conf->serialize_services();
+
     // delete all service objects
     Conf->delete_services();