Splitted method serialize() into load() and save() as this is needed to introduce...
[bpdyndnsd] / src / service.hpp
1 /** @file
2  * @brief The abstract service class. This class represents all services.
3  *
4  *
5  *
6  * @copyright Intra2net AG
7  * @license GPLv2
8 */
9
10 #ifndef SERVICE_H
11 #define SERVICE_H
12
13 #include "logger.hpp"
14
15 #include <boost/archive/text_oarchive.hpp>
16 #include <boost/archive/text_iarchive.hpp>
17 #include <boost/serialization/version.hpp>
18 #include <boost/serialization/split_member.hpp>
19
20 #include <boost/shared_ptr.hpp>
21 #include <string>
22 #include <list>
23
24
25 class Service
26 {
27
28 private:
29
30     std::string Protocol;
31     std::string Hostname;
32
33     std::string Login;
34     std::string Password;
35
36     std::string ActualIP;
37     bool ActualIPIsBurnt;
38     bool Activated;
39
40     int UpdateInterval;
41     int MaxUpdatesWithinInterval;
42     int MaxEqualUpdatesInSuccession;
43     int DNSCacheTTL;
44
45     std::map<time_t,std::string> LastUpdates;
46
47     int ErrorCount;
48     time_t ErrorServiceBlockedUntil;
49
50     friend class boost::serialization::access;
51     template<class Archive>
52     void save(Archive & ar, const unsigned int version) const
53     {
54         // protocol and hostname are the unique identifier for each service.
55         ar & Protocol;
56         ar & Hostname;
57         // lastupdated and actual_ip must also be serialized, cause these are essential infos of each service.
58         ar & LastUpdates;
59         ar & ActualIP;
60         ar & UpdateInterval;
61         ar & MaxUpdatesWithinInterval;
62         ar & MaxEqualUpdatesInSuccession;
63         ar & DNSCacheTTL;
64
65         // Activated was introduced with version 1.
66         ar & Activated;
67     }
68     template<class Archive>
69     void load(Archive & ar, const unsigned int version)
70     {
71         // protocol and hostname are the unique identifier for each service.
72         ar & Protocol;
73         ar & Hostname;
74         // lastupdated and actual_ip must also be serialized, cause these are essential infos of each service.
75         ar & LastUpdates;
76         ar & ActualIP;
77         ar & UpdateInterval;
78         ar & MaxUpdatesWithinInterval;
79         ar & MaxEqualUpdatesInSuccession;
80         ar & DNSCacheTTL;
81
82         // Activated was introduced with version 1.
83         if ( version >= 1 )
84             ar & Activated;
85     }
86     BOOST_SERIALIZATION_SPLIT_MEMBER()
87
88     Logger::Ptr Log;
89
90 protected:
91
92     enum UpdateErrorCode { UpdateOk=0, UpdateError=1, NoChange=2, Blocked=3, NotAuth=4, GenericError=4 };
93     virtual UpdateErrorCode perform_update(const std::string& ip) = 0;
94
95     // UpdateOk:        Update was successful
96     // UpdateError:     Unspecified error code returned from update server
97     // NoChange:        IP was not changed_to_online
98     // Blocked:         Client blocked due to abusive updates
99     // NotAuth:         Authentication was not successful
100     // GenericError:    Generic error. For example update server not reached, wrong hostname, not authenticated or network error.
101     //                  Generic error indicates that the update server couldn't associate the update request to any hostname.
102
103 public:
104
105     typedef boost::shared_ptr<Service> Ptr;
106
107     Service();
108
109     virtual ~Service();
110
111     void update(const std::string& ip, const time_t current_time, bool changed_to_online);
112
113     bool update_allowed(const time_t current_time, bool changed_to_online, const std::string& ip_host);
114
115     void set_last_update(const time_t current_time, const std::string& ip);
116
117     time_t get_last_update_time();
118
119     int get_update_interval()const;
120     void set_update_interval(const int _update_interval);
121
122     int get_max_updates_within_interval() const;
123     void set_max_updates_within_interval(const int _max_updates_within_interval);
124
125     int get_max_equal_updates_in_succession() const;
126     void set_max_equal_updates_in_succession(const int _max_equal_updates_in_succession);
127
128     int get_dns_cache_ttl() const;
129     void set_dns_cache_ttl(const int _dns_cache_ttl);
130
131     void set_protocol(const std::string& _protocol);
132     std::string get_protocol() const;
133
134     void set_hostname(const std::string& _hostname);
135     std::string get_hostname() const;
136
137     void set_login(const std::string& _login);
138     std::string get_login() const;
139
140     void set_password(const std::string& _password);
141     std::string get_password() const;
142
143     void set_last_updates(std::map<time_t,std::string> _last_updates);
144     const std::map<time_t,std::string> get_last_updates() const;
145
146     void set_actual_ip(const std::string& _actual_ip);
147     std::string get_actual_ip() const;
148
149     void set_logger(const Logger::Ptr& _log);
150     Logger::Ptr get_logger() const;
151
152     std::string get_service_name() const;
153
154     void set_activated();
155     bool get_activated() const;
156
157     bool operator== (const Service& other) const;
158     bool operator!= (const Service& other) const;
159 };
160
161 BOOST_CLASS_VERSION(Service,1)
162
163 #endif