Implemented object serialization of Service objects (first steps).
authorBjoern Sikora <bjoern.sikora@intra2net.com>
Wed, 5 Aug 2009 15:05:44 +0000 (17:05 +0200)
committerBjoern Sikora <bjoern.sikora@intra2net.com>
Wed, 5 Aug 2009 15:05:44 +0000 (17:05 +0200)
src/config.cpp
src/config.h
src/dhs.cpp
src/dhs.h
src/main.cpp
src/ods.cpp
src/ods.h
src/service.cpp
src/service.h
src/updater.cpp
src/updater.h

index ca5bf3e..d76cc46 100644 (file)
@@ -9,6 +9,10 @@
 
 #include "config.h"
 
+// Following boost macros are needed for serialization of derived classes through a base class pointer (Service *).
+BOOST_CLASS_EXPORT_GUID(ODS, "ODS")
+BOOST_CLASS_EXPORT_GUID(DHS, "DHS")
+
 namespace po = boost::program_options;
 namespace fs = boost::filesystem;
 
@@ -83,6 +87,30 @@ Config::~Config()
 
 
 /**
+ * This function serializes all Service objects in Services into a specified file.
+ * @return 0 if all is fine, -1 otherwise.
+ */
+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);
+
+    BOOST_FOREACH(ServicePtr service, Services)
+    {
+        Service* ptr = service.get();
+        oa << BOOST_SERIALIZATION_NVP(ptr);
+    }
+    ofs.close();
+
+    return 0;
+}
+
+
+/**
  * Parses the command line arguments and does the needed actions.
  * @param argc Command line argument number given to main.
  * @param argv[] Pointer to command line argument array given to main.
index 439fb84..d13c077 100644 (file)
@@ -15,6 +15,9 @@
 #include <boost/filesystem.hpp>
 #include <boost/regex.hpp>
 #include <boost/shared_ptr.hpp>
+#include <boost/archive/text_oarchive.hpp>
+#include <boost/archive/text_iarchive.hpp>
+#include <boost/serialization/export.hpp>
 
 #include <string>
 #include <iostream>
@@ -58,6 +61,8 @@ public:
 
     ~Config();
 
+    int serialize_services();
+
     int parse_cmd_line(int, char **);
 
     int load_config_from_files();
index b4e966f..46e1ea3 100644 (file)
 
 using namespace std;
 
+
+/**
+ * Default Constructor, needed for object serialization.
+ */
+DHS::DHS()
+{
+}
+
+
 /**
  * Constructor.
  * @param _hostname The hostname to update
@@ -25,6 +34,10 @@ DHS::DHS(const LoggerPtr& _log ,const string& _hostname, const string& _login, c
     Login = _login;
     Password = _password;
 
+    // TODO: setting members from base class correctly.
+    this->set_timeout(100);
+    this->set_lastupdated(100);
+
     Log->print_constructor_call("DHS");
 }
 
@@ -46,3 +59,18 @@ void DHS::update(const string& ip)
 {
     Log->print_update_service("DHS");
 }
+
+
+/**
+ * Serialize function needed by boost/serialization to define which members should be stored as the object state.
+ * @param ar Archive
+ * @param version Version
+ */
+template<class Archive>
+void DHS::serialize(Archive & ar, const unsigned int version)
+{
+    ar & boost::serialization::base_object<Service>(*this);
+    ar & Hostname;
+    ar & Login;
+    ar & Password;
+}
index a3d3392..e863468 100644 (file)
--- a/src/dhs.h
+++ b/src/dhs.h
 #include "service.h"
 #include "logger.h"
 
+#include <boost/serialization/array.hpp>
+
 typedef boost::shared_ptr<Logger> LoggerPtr;
 
 class DHS : public Service
 {
-
 private:
-
     std::string Hostname;
     std::string Login;
     std::string Password;
 
     LoggerPtr Log;
 
+    friend class boost::serialization::access;
+    template<class Archive>
+    void serialize(Archive &, const unsigned int);
+
 public:
 
+    DHS();
+
     DHS(const LoggerPtr&, const std::string&, const std::string&, const std::string&);
 
     ~DHS();
 
     void update(const std::string&);
-
 };
 
 #endif
index 514ebfe..9f10759 100644 (file)
 #include <sys/types.h>
 #include <signal.h>
 
-
 using namespace std;
 
 typedef boost::shared_ptr<Updater> UpdaterPtr;
+typedef boost::shared_ptr<Service> ServicePtr;
 
 UpdaterPtr updater;
 bool online_mode = 1;
@@ -95,11 +95,16 @@ void write_pidfile(int pid)
 void terminate(int param)
 {
     updater->get_logger()->print_caught_sigterm();
+
+    // unfortunately we can't call serialize_services in any destructor
+    // cause some singleton are already destroyed !?!
+    updater->get_config()->serialize_services();
     updater.reset();
     exit(0);
 }
 
 
+
 /**
  * Signal SIGUSR1 caught, switching to offline mode.
  * @param param Parameter from the signal interface.
@@ -215,5 +220,9 @@ int main(int argc, char *argv[])
 
     }while ( updater->get_config()->get_daemon_mode() == 1 );
 
+
+    // serialize services
+    updater->get_config()->serialize_services();
+
     return 0;
 }
index 81d3c96..afcd6aa 100644 (file)
 
 using namespace std;
 
+
+/**
+ * Default Constructor, needed for object serialization.
+ */
+ODS::ODS()
+{
+}
+
+
 /**
  * Constructor.
  * @param _hostname The hostname to update
@@ -25,6 +34,10 @@ ODS::ODS(const LoggerPtr& _log, const string& _hostname, const string& _login, c
     Login = _login;
     Password = _password;
 
+    // TODO: setting members from base class correctly.
+    this->set_timeout(100);
+    this->set_lastupdated(100);
+
     Log->print_constructor_call("ODS");
 }
 
@@ -46,3 +59,18 @@ void ODS::update(const string& ip)
 {
     Log->print_update_service("ODS");
 }
+
+
+/**
+ * Serialize function needed by boost/serialization to define which members should be stored as the object state.
+ * @param ar Archive
+ * @param version Version
+ */
+template<class Archive>
+void ODS::serialize(Archive & ar, const unsigned int version)
+{
+    ar & boost::serialization::base_object<Service>(*this);
+    ar & Hostname;
+    ar & Login;
+    ar & Password;
+}
index 869098c..caafceb 100644 (file)
--- a/src/ods.h
+++ b/src/ods.h
@@ -14,6 +14,8 @@
 #include "service.h"
 #include "logger.h"
 
+#include <boost/serialization/array.hpp>
+
 typedef boost::shared_ptr<Logger> LoggerPtr;
 
 class ODS : public Service
@@ -27,8 +29,14 @@ private:
 
     LoggerPtr Log;
 
+    friend class boost::serialization::access;
+    template<class Archive>
+    void serialize(Archive &, const unsigned int);
+
 public:
 
+    ODS();
+
     ODS(const LoggerPtr&, const std::string&, const std::string&, const std::string&);
 
     ~ODS();
index bce79be..19d5851 100644 (file)
@@ -1,5 +1,5 @@
 /** @file
- * @brief The abstract service interface. This class represents all services.
+ * @brief The abstract service class. This class represents all services.
  *
  *
  *
@@ -9,6 +9,7 @@
 
 #include "service.h"
 
+
 /**
  * Default Constructor
  */
@@ -17,6 +18,7 @@ Service::Service()
 
 }
 
+
 /**
  * Default Destructor
  */
@@ -26,3 +28,54 @@ Service::~Service()
 }
 
 
+/**
+ * Although this is an abstract class, we need the serialize function that we can serialize derived classes through a Service *.
+ * @param ar Archive.
+ * @param version Version.
+ */
+template<class Archive>
+void Service::serialize(Archive & ar, const unsigned int version)
+{
+    ar & Lastupdated;
+    ar & Timeout;
+}
+
+
+/**
+ * Setter for member Lastupdated.
+ * @param _lastupdated Value to set Lastupdated to.
+ */
+void Service::set_lastupdated(const int _lastupdated)
+{
+    Lastupdated = _lastupdated;
+}
+
+
+/**
+ * Getter for member Lastupdated.
+ * @return Value of member Lastupdated.
+ */
+int Service::get_lastupdated()
+{
+    return Lastupdated;
+}
+
+
+/**
+ * Setter for member Timeout.
+ * @param _timeout Value to set Timeout to.
+ */
+void Service::set_timeout(const int _timeout)
+{
+    Timeout = _timeout;
+}
+
+
+/**
+ * Getter for member Timeout.
+ * @return Value of member Timeout.
+ */
+int Service::get_timeout()
+{
+    return Timeout;
+}
index 591252f..13a2f45 100644 (file)
 
 #include <string>
 
+#include <boost/serialization/array.hpp>
+
 class Service
 {
+private:
+    int Lastupdated;
+    int Timeout;
+
+    friend class boost::serialization::access;
+    template<class Archive>
+    void serialize(Archive &, const unsigned int);
+
 public:
     Service();
 
@@ -21,6 +31,11 @@ public:
 
     virtual void update(const std::string&)=0;
 
+    void set_lastupdated(const int);
+    int get_lastupdated();
+
+    void set_timeout(const int);
+    int get_timeout();
 };
 
 #endif
index 8b38d29..8bcf06a 100644 (file)
@@ -9,8 +9,6 @@
 
 #include "updater.h"
 
-#include <boost/foreach.hpp>
-
 using namespace std;
 
 /**
@@ -115,6 +113,9 @@ void Updater::reload_config()
 }
 
 
+/**
+ * Initialize the logging facility with loglevel and syslog.
+ */
 void Updater::init_log_facility()
 {
     Log->set_log_facility(Conf->get_loglevel(),Conf->get_syslog());
index b148e8a..66bd6a9 100644 (file)
@@ -10,6 +10,8 @@
 #ifndef UPDATER_H
 #define UPDATER_H
 
+#include <boost/foreach.hpp>
+
 #include "config.h"
 #include "logger.h"