From: Bjoern Sikora Date: Wed, 5 Aug 2009 15:05:44 +0000 (+0200) Subject: Implemented object serialization of Service objects (first steps). X-Git-Tag: v1.1~248 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=2bc1878a9b51db93ab2c60bf3aaf8e601ae2480e;p=bpdyndnsd Implemented object serialization of Service objects (first steps). --- diff --git a/src/config.cpp b/src/config.cpp index ca5bf3e..d76cc46 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -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. diff --git a/src/config.h b/src/config.h index 439fb84..d13c077 100644 --- a/src/config.h +++ b/src/config.h @@ -15,6 +15,9 @@ #include #include #include +#include +#include +#include #include #include @@ -58,6 +61,8 @@ public: ~Config(); + int serialize_services(); + int parse_cmd_line(int, char **); int load_config_from_files(); diff --git a/src/dhs.cpp b/src/dhs.cpp index b4e966f..46e1ea3 100644 --- a/src/dhs.cpp +++ b/src/dhs.cpp @@ -11,6 +11,15 @@ 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 +void DHS::serialize(Archive & ar, const unsigned int version) +{ + ar & boost::serialization::base_object(*this); + ar & Hostname; + ar & Login; + ar & Password; +} diff --git a/src/dhs.h b/src/dhs.h index a3d3392..e863468 100644 --- a/src/dhs.h +++ b/src/dhs.h @@ -14,27 +14,32 @@ #include "service.h" #include "logger.h" +#include + typedef boost::shared_ptr LoggerPtr; class DHS : public Service { - private: - std::string Hostname; std::string Login; std::string Password; LoggerPtr Log; + friend class boost::serialization::access; + template + 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 diff --git a/src/main.cpp b/src/main.cpp index 514ebfe..9f10759 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,10 +35,10 @@ #include #include - using namespace std; typedef boost::shared_ptr UpdaterPtr; +typedef boost::shared_ptr 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; } diff --git a/src/ods.cpp b/src/ods.cpp index 81d3c96..afcd6aa 100644 --- a/src/ods.cpp +++ b/src/ods.cpp @@ -11,6 +11,15 @@ 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 +void ODS::serialize(Archive & ar, const unsigned int version) +{ + ar & boost::serialization::base_object(*this); + ar & Hostname; + ar & Login; + ar & Password; +} diff --git a/src/ods.h b/src/ods.h index 869098c..caafceb 100644 --- a/src/ods.h +++ b/src/ods.h @@ -14,6 +14,8 @@ #include "service.h" #include "logger.h" +#include + typedef boost::shared_ptr LoggerPtr; class ODS : public Service @@ -27,8 +29,14 @@ private: LoggerPtr Log; + friend class boost::serialization::access; + template + void serialize(Archive &, const unsigned int); + public: + ODS(); + ODS(const LoggerPtr&, const std::string&, const std::string&, const std::string&); ~ODS(); diff --git a/src/service.cpp b/src/service.cpp index bce79be..19d5851 100644 --- a/src/service.cpp +++ b/src/service.cpp @@ -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 +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; +} diff --git a/src/service.h b/src/service.h index 591252f..13a2f45 100644 --- a/src/service.h +++ b/src/service.h @@ -12,8 +12,18 @@ #include +#include + class Service { +private: + int Lastupdated; + int Timeout; + + friend class boost::serialization::access; + template + 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 diff --git a/src/updater.cpp b/src/updater.cpp index 8b38d29..8bcf06a 100644 --- a/src/updater.cpp +++ b/src/updater.cpp @@ -9,8 +9,6 @@ #include "updater.h" -#include - 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()); diff --git a/src/updater.h b/src/updater.h index b148e8a..66bd6a9 100644 --- a/src/updater.h +++ b/src/updater.h @@ -10,6 +10,8 @@ #ifndef UPDATER_H #define UPDATER_H +#include + #include "config.h" #include "logger.h"