From 3a89ac3162a659f0bc595f5e75b6ab103afb2163 Mon Sep 17 00:00:00 2001 From: Bjoern Sikora Date: Thu, 6 Aug 2009 11:59:48 +0200 Subject: [PATCH] Each Service has Protocol, Hostname, Login, Password, Shared_Ptr to Logging facility and Lastupdated member. A Service is identified through Protocol and Hostname, if they are equal, it is the same service. --- src/config.cpp | 6 +- src/dhs.cpp | 74 +++++++++++++++++++++++++------- src/dhs.h | 17 +++++--- src/main.cpp | 2 - src/ods.cpp | 74 +++++++++++++++++++++++++------- src/ods.h | 20 +++++---- src/service.cpp | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++----- src/service.h | 33 +++++++++++++-- 8 files changed, 288 insertions(+), 65 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index d76cc46..4297049 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -195,16 +195,16 @@ int Config::parse_cmd_line(int argc, char *argv[]) * @param password * @return A pointer to the created Service object. */ -ServicePtr Config::create_service(const string &protocol,const string &host, const string &login, const string &password) +ServicePtr Config::create_service(const string &protocol,const string &hostname, const string &login, const string &password) { if(protocol == "dhs") { - ServicePtr service_dhs(new DHS(Log,host,login,password)); + ServicePtr service_dhs(new DHS(protocol,hostname,login,password,Log,0,0,1)); return service_dhs; } else if(protocol == "ods") { - ServicePtr service_ods(new ODS(Log,host,login,password)); + ServicePtr service_ods(new ODS(protocol,hostname,login,password,Log,0,0,1)); return service_ods; } else diff --git a/src/dhs.cpp b/src/dhs.cpp index 46e1ea3..4c80a12 100644 --- a/src/dhs.cpp +++ b/src/dhs.cpp @@ -17,6 +17,7 @@ using namespace std; */ DHS::DHS() { + get_logger()->print_constructor_call("DHS default!!!"); } @@ -26,19 +27,19 @@ DHS::DHS() * @param _login The login name. * @param _password The corresponding password. */ -DHS::DHS(const LoggerPtr& _log ,const string& _hostname, const string& _login, const string& _password) +DHS::DHS(const string& _protocol, const string& _hostname, const string& _login, const string& _password, const LoggerPtr& _logger, const int _lastupdated, const int _timeout, const int _max_updates_per_timeout) { - Log = _log; + Timeout = _timeout; + Max_updates_per_timeout = _max_updates_per_timeout; - Hostname = _hostname; - Login = _login; - Password = _password; + set_protocol(_protocol); + set_hostname(_hostname); + set_login(_login); + set_password(_password); + set_logger(_logger); + set_lastupdated(_lastupdated); - // TODO: setting members from base class correctly. - this->set_timeout(100); - this->set_lastupdated(100); - - Log->print_constructor_call("DHS"); + get_logger()->print_constructor_call("DHS"); } @@ -47,7 +48,7 @@ DHS::DHS(const LoggerPtr& _log ,const string& _hostname, const string& _login, c */ DHS::~DHS() { - Log->print_destructor_call("DHS"); + get_logger()->print_destructor_call("DHS"); } @@ -57,7 +58,11 @@ DHS::~DHS() */ void DHS::update(const string& ip) { - Log->print_update_service("DHS"); + // if update was successful, we need to set the lastupdated base member. + time_t actual_time = time(NULL); + set_lastupdated(actual_time); + + get_logger()->print_update_service("DHS"); } @@ -70,7 +75,46 @@ template void DHS::serialize(Archive & ar, const unsigned int version) { ar & boost::serialization::base_object(*this); - ar & Hostname; - ar & Login; - ar & Password; + ar & Timeout; + ar & Max_updates_per_timeout; +} + + +/** + * Setter for member Timeout. + * @param _timeout Value to set Timeout to. + */ +void DHS::set_timeout(const int _timeout) +{ + Timeout = _timeout; +} + + +/** + * Getter for member Timeout. + * @return Value of Timeout. + */ +int DHS::get_timeout() +{ + return Timeout; +} + + +/** + * Setter for member Max_updates_per_timeout. + * @param _max_updates_per_timeout Value to set Max_updates_per_timeout to. + */ +void DHS::set_max_updates_per_timeout(const int _max_updates_per_timeout) +{ + Max_updates_per_timeout = _max_updates_per_timeout; +} + + +/** + * Getter for member Max_updates_per_timeout. + * @return Value of Max_updates_per_timeout. + */ +int DHS::get_max_updates_per_timeout() +{ + return Max_updates_per_timeout; } diff --git a/src/dhs.h b/src/dhs.h index e863468..911dc29 100644 --- a/src/dhs.h +++ b/src/dhs.h @@ -11,6 +11,8 @@ #define DHS_H #include +#include + #include "service.h" #include "logger.h" @@ -21,11 +23,8 @@ typedef boost::shared_ptr LoggerPtr; class DHS : public Service { private: - std::string Hostname; - std::string Login; - std::string Password; - - LoggerPtr Log; + int Timeout; + int Max_updates_per_timeout; friend class boost::serialization::access; template @@ -35,10 +34,16 @@ public: DHS(); - DHS(const LoggerPtr&, const std::string&, const std::string&, const std::string&); + DHS(const std::string&, const std::string&, const std::string&, const std::string&, const LoggerPtr&, const int, const int, const int); ~DHS(); + void set_timeout(const int); + int get_timeout(); + + void set_max_updates_per_timeout(const int); + int get_max_updates_per_timeout(); + void update(const std::string&); }; diff --git a/src/main.cpp b/src/main.cpp index 9f10759..4c68013 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,7 +38,6 @@ using namespace std; typedef boost::shared_ptr UpdaterPtr; -typedef boost::shared_ptr ServicePtr; UpdaterPtr updater; bool online_mode = 1; @@ -201,7 +200,6 @@ int main(int argc, char *argv[]) // child starts here } - // service processing starts here do { diff --git a/src/ods.cpp b/src/ods.cpp index afcd6aa..8ef8ca9 100644 --- a/src/ods.cpp +++ b/src/ods.cpp @@ -17,6 +17,7 @@ using namespace std; */ ODS::ODS() { + get_logger()->print_constructor_call("ODS default!!!"); } @@ -26,19 +27,19 @@ ODS::ODS() * @param _login The login name. * @param _password The corresponding password. */ -ODS::ODS(const LoggerPtr& _log, const string& _hostname, const string& _login, const string& _password) +ODS::ODS(const string& _protocol, const string& _hostname, const string& _login, const string& _password, const LoggerPtr& _logger, const int _lastupdated, const int _timeout, const int _max_updates_per_timeout) { - Log = _log; + Timeout = _timeout; + Max_updates_per_timeout = _max_updates_per_timeout; - Hostname = _hostname; - Login = _login; - Password = _password; + set_protocol(_protocol); + set_hostname(_hostname); + set_login(_login); + set_password(_password); + set_logger(_logger); + set_lastupdated(_lastupdated); - // TODO: setting members from base class correctly. - this->set_timeout(100); - this->set_lastupdated(100); - - Log->print_constructor_call("ODS"); + get_logger()->print_constructor_call("ODS"); } @@ -47,7 +48,7 @@ ODS::ODS(const LoggerPtr& _log, const string& _hostname, const string& _login, c */ ODS::~ODS() { - Log->print_destructor_call("ODS"); + get_logger()->print_destructor_call("ODS"); } @@ -57,7 +58,11 @@ ODS::~ODS() */ void ODS::update(const string& ip) { - Log->print_update_service("ODS"); + // if update was successful, we need to set the lastupdated base member. + time_t actual_time = time(NULL); + set_lastupdated(actual_time); + + get_logger()->print_update_service("ODS"); } @@ -70,7 +75,46 @@ template void ODS::serialize(Archive & ar, const unsigned int version) { ar & boost::serialization::base_object(*this); - ar & Hostname; - ar & Login; - ar & Password; + ar & Timeout; + ar & Max_updates_per_timeout; +} + + +/** + * Setter for member Timeout. + * @param _timeout Value to set Timeout to. + */ +void ODS::set_timeout(const int _timeout) +{ + Timeout = _timeout; +} + + +/** + * Getter for member Timeout. + * @return Value of Timeout. + */ +int ODS::get_timeout() +{ + return Timeout; +} + + +/** + * Setter for member Max_updates_per_timeout. + * @param _max_updates_per_timeout Value to set Max_updates_per_timeout to. + */ +void ODS::set_max_updates_per_timeout(const int _max_updates_per_timeout) +{ + Max_updates_per_timeout = _max_updates_per_timeout; +} + + +/** + * Getter for member Max_updates_per_timeout. + * @return Value of Max_updates_per_timeout. + */ +int ODS::get_max_updates_per_timeout() +{ + return Max_updates_per_timeout; } diff --git a/src/ods.h b/src/ods.h index caafceb..ed91c31 100644 --- a/src/ods.h +++ b/src/ods.h @@ -11,6 +11,8 @@ #define ODS_H #include +#include + #include "service.h" #include "logger.h" @@ -20,14 +22,9 @@ typedef boost::shared_ptr LoggerPtr; class ODS : public Service { - private: - - std::string Hostname; - std::string Login; - std::string Password; - - LoggerPtr Log; + int Timeout; + int Max_updates_per_timeout; friend class boost::serialization::access; template @@ -37,12 +34,17 @@ public: ODS(); - ODS(const LoggerPtr&, const std::string&, const std::string&, const std::string&); + ODS(const std::string&, const std::string&, const std::string&, const std::string&, const LoggerPtr&, const int, const int, const int); ~ODS(); - void update(const std::string&); + void set_timeout(const int); + int get_timeout(); + + void set_max_updates_per_timeout(const int); + int get_max_updates_per_timeout(); + void update(const std::string&); }; #endif diff --git a/src/service.cpp b/src/service.cpp index 19d5851..b74bcd8 100644 --- a/src/service.cpp +++ b/src/service.cpp @@ -15,7 +15,6 @@ */ Service::Service() { - } @@ -24,7 +23,6 @@ Service::Service() */ Service::~Service() { - } @@ -36,8 +34,111 @@ Service::~Service() template 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. ar & Lastupdated; - ar & Timeout; +} + + +/** + * Setter for member Protocol. + * @param _protocol Value to set Protocol to. + */ +void Service::set_protocol(const string& _protocol) +{ + Protocol = _protocol; +} + + +/** + * Getter for memeber Protocol. + * @return Value of member Protocol. + */ +string Service::get_protocol() +{ + return Protocol; +} + + +/** + * Setter for member Hostname. + * @param _hostname Value to set Hostname to. + */ +void Service::set_hostname(const string& _hostname) +{ + Hostname = _hostname; +} + + +/** + * Getter for member Hostname. + * @return Value of member Hostname. + */ +string Service::get_hostname() +{ + return Hostname; +} + + +/** + * Setter for member Login. + * @param _login Value to set Login to. + */ +void Service::set_login(const string& _login) +{ + Login = _login; +} + + +/** + * Getter for member Login. + * @return Value of member Login. + */ +string Service::get_login() +{ + return Login; +} + + +/** + * Setter for member Password. + * @param _password Value to set Password to. + */ +void Service::set_password(const string& _password) +{ + Password = _password; +} + + +/** + * Getter for member Password. + * @return Value of member Password. + */ +string Service::get_password() +{ + return Password; +} + + +/** + * Setter for member Log. + * @param _log Shared pointer to Logger object. + */ +void Service::set_logger(const LoggerPtr& _log) +{ + Log = _log; +} + + +/** + * Getter for member Log. + * @return Shared pointer to Logger object. + */ +LoggerPtr Service::get_logger() +{ + return Log; } @@ -62,20 +163,24 @@ int Service::get_lastupdated() /** - * Setter for member Timeout. - * @param _timeout Value to set Timeout to. + * Overloading of comparison operator. + * @param other Reference to other Service object. + * @return True if they equal, false if not. */ -void Service::set_timeout(const int _timeout) +bool Service::operator== (const Service& other) const { - Timeout = _timeout; + if ( ( this->Protocol == other.Protocol ) && ( this->Hostname == other.Hostname ) ) + return true; + return false; } /** - * Getter for member Timeout. - * @return Value of member Timeout. + * Overloading of disparate operator. + * @param other Reference to other Service object. + * @return True if they differ, false if they are equal. */ -int Service::get_timeout() +bool Service::operator!= (const Service& other) const { - return Timeout; + return !(*this == other); } diff --git a/src/service.h b/src/service.h index 13a2f45..1fd2809 100644 --- a/src/service.h +++ b/src/service.h @@ -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. * * * @@ -14,11 +14,21 @@ #include +#include "logger.h" + +typedef boost::shared_ptr LoggerPtr; + class Service { private: + std::string Protocol; + std::string Hostname; + std::string Login; + std::string Password; + + LoggerPtr Log; + int Lastupdated; - int Timeout; friend class boost::serialization::access; template @@ -31,11 +41,26 @@ public: virtual void update(const std::string&)=0; + void set_protocol(const std::string&); + std::string get_protocol(); + + void set_hostname(const std::string&); + std::string get_hostname(); + + void set_login(const std::string&); + std::string get_login(); + + 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_timeout(const int); - int get_timeout(); + bool operator== (const Service&) const; + bool operator!= (const Service&) const; }; #endif -- 1.7.1