{
//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;
}
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());
}
}
}
#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
{
int serialize_services();
+ int deserialize_services();
+
int parse_cmd_line(int, char **);
int load_config_from_files();
*/
DHS::DHS()
{
- get_logger()->print_constructor_call("DHS default!!!");
}
if ( Syslog )
syslog(LOG_NOTICE,msg.c_str());
else
- cout << msg;
+ cout << msg << endl;;
}
if ( Syslog )
syslog(LOG_WARNING,msg.c_str());
else
- cout << msg;
+ cout << msg << endl;
}
if ( Syslog )
syslog(LOG_ERR,msg.c_str());
else
- cerr << msg;
+ cerr << msg << endl;
}
}
}
+
/**
* Be verbose. Currently we are in 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());
+ }
+}
#include <syslog.h>
#include <sstream>
+
class Logger
{
private:
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
#define RELEASE 0
#define PIDFILE "/var/run/bpdyndnsd.pid"
+#define OBJECT_FILE "/home/bjoern/intranator/bpdyndnsd/objects.ser"
#include <iostream>
#include <list>
#include "logger.cpp"
#include "service.cpp"
+#include "serviceholder.cpp"
#include "dhs.cpp"
#include "ods.cpp"
*/
ODS::ODS()
{
- get_logger()->print_constructor_call("ODS default!!!");
}
* 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()
{
// 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;
}
/**
+ * 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.
private:
std::string Protocol;
std::string Hostname;
+
std::string Login;
std::string Password;
- LoggerPtr Log;
+ std::string Actual_IP;
int Lastupdated;
template<class Archive>
void serialize(Archive &, const unsigned int);
+ LoggerPtr Log;
+
public:
Service();
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;
};
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;
}
*/
void Updater::reload_config()
{
+ // serialize all service objects
+ Conf->serialize_services();
+
// delete all service objects
Conf->delete_services();