/** @file * @brief The abstract service class. This class represents all services. * * * * @copyright Intra2net AG * @license GPLv2 */ #include "service.hpp" #include using namespace std; /** * Default Constructor */ Service::Service() : Login("NOT SERIALIZED") , Password("NOT SERIALIZED") , ActualIP("0.0.0.0") , UpdateInterval(0) , MaxUpdatesWithinInterval(0) , DNSCacheTTL(0) , Log(new Logger()) { } /** * Default Destructor needed for deserialization. */ Service::~Service() { } /** * 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() const { 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() const { 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() const { 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() const { return Password; } void Service::set_logger(const Logger::Ptr& _log) { Log = _log; } /** * Getter for member Log. * @return Shared pointer to Logger object. */ Logger::Ptr Service::get_logger() const { return Log; } void Service::set_last_updates(std::list _last_updates) { LastUpdates.clear(); BOOST_FOREACH( time_t update_time, _last_updates ) { LastUpdates.push_back(update_time); } } /** * Getter for member Lastupdated. * @return Value of member Lastupdated. */ const list Service::get_last_updates() const { return LastUpdates; } /** * Setter for member ActualIP. * @param _actual_ip Value to set ActualIP to. */ void Service::set_actual_ip(const std::string& _actual_ip) { ActualIP = _actual_ip; } /** * Getter for member ActualIP. * @return Value of member ActualIP. */ std::string Service::get_actual_ip() const { return ActualIP; } /** * Overloading of comparison operator. * @param other Reference to other Service object. * @return True if they equal, false if not. */ bool Service::operator== (const Service& other) const { if ( ( this->Protocol == other.Protocol ) && ( this->Hostname == other.Hostname ) ) return true; return false; } /** * Overloading of disparate operator. * @param other Reference to other Service object. * @return True if they differ, false if they are equal. */ bool Service::operator!= (const Service& other) const { return !(*this == other); } /** * Checks if update will exceed max update interval. * @param current_time Current time. * @return True if update is allowed, false if update would exceed max update interval. */ bool Service::update_allowed(const time_t current_time) { list::iterator iter; int i=0; for (iter = LastUpdates.begin(); (iter != LastUpdates.end()) && ( i < MaxUpdatesWithinInterval ); iter++) { if ( (i == (MaxUpdatesWithinInterval-1)) && ( (*iter + ((time_t)UpdateInterval*60)) >= current_time ) ) { Log->print_update_not_allowed(current_time,*iter,MaxUpdatesWithinInterval,get_service_name()); return false; } i++; } return true; } /** * Service update method, common to each service. * @param ip The new ip to set for the hostname. */ void Service::update(const string& ip, const time_t current_time) { Log->print_update_service(get_service_name()); // test if update is permitted by UpdateInterval Status if ( update_allowed(current_time) ) { if ( perform_update(ip) == 0 ) { // if update was successful, we need to set the Lastupdated and ActualIP base member. LastUpdates.push_front(current_time); ActualIP = ip; Log->print_update_service_successful(get_service_name()); } else { // problem while trying to update service Log->print_update_service_failure(get_service_name()); } } } /** * Setter for member Timeout. * @param _timeout Value to set Timeout to. */ void Service::set_update_interval(const int _update_interval) { UpdateInterval = _update_interval; } /** * Getter for member Timeout. * @return Value of Timeout. */ int Service::get_update_interval() const { return UpdateInterval; } /** * Setter for member Max_updates_per_timeout. * @param _max_updates_per_timeout Value to set Max_updates_per_timeout to. */ void Service::set_max_updates_within_interval(const int _max_updates_within_interval) { MaxUpdatesWithinInterval = _max_updates_within_interval; } /** * Getter for member Max_updates_per_timeout. * @return Value of Max_updates_per_timeout. */ int Service::get_max_updates_within_interval() const { return MaxUpdatesWithinInterval; } /** * Get a unique service identify string * @return A unique service identify string */ string Service::get_service_name() const { string service_name; service_name.append(Protocol); service_name.append(" "); service_name.append(Hostname); return service_name; } /** * Get member DNSCacheTTL * @return DNSCacheTTL */ int Service::get_dns_cache_ttl() const { return DNSCacheTTL; } /** * Set member DNSCacheTTL * @param _dns_cache_ttl DNSCacheTTL */ void Service::set_dns_cache_ttl(const int _dns_cache_ttl) { DNSCacheTTL = _dns_cache_ttl; }