Introduced new Service members: UpdateInterval and MaxUpdatesWithinInterval.
[bpdyndnsd] / src / updater.cpp
1 /** @file
2  * @brief The updater class implementation. This class implements the updater logic.
3  *
4  *
5  *
6  * @copyright Intra2net AG
7  * @license GPLv2
8 */
9
10 #include "updater.h"
11
12 #include <boost/foreach.hpp>
13
14 using namespace std;
15
16 /**
17  * Default constructor which initializes the member Conf.
18  */
19 Updater::Updater()
20     : IPHelp(new IPHelper)
21 {
22     // Initialize program wide Logger, at this point we don't know where to log and with which loglevel.
23     Logger::Ptr _log(new Logger);
24     Log = _log;
25     _log.reset();
26
27     // initialize Config
28     Config::Ptr _config(new Config(Log));
29     Conf = _config;
30     _config.reset();
31 }
32
33
34 /**
35  * Default destructor.
36  */
37 Updater::~Updater()
38 {
39 }
40
41
42 /**
43  * Parse the command line arguments and initialize corresponding options.
44  * @param argc Number command line arguments.
45  * @param argv[] Array with arguments.
46  * @return 0 if cmd options successfully parsed, 1 if usage or version.
47  */
48 int Updater::init_config_from_cmd(int argc, char *argv[])
49 {
50     // Load the command line parameters
51     if( Conf->parse_cmd_line( argc, argv ) != 0)
52         return -1;
53
54     // If we have loaded the cmd options we need to init the log facility immediately in case debugging is enabled from cmd.
55     init_log_facility();
56
57     // successful parsed
58     Log->print_cmd_parsed();
59     return 0;
60 }
61
62
63 /**
64  * Load the main config and the service definition files in config path.
65  * @return 0 if all is fine, 
66  */
67 int Updater::init_config_from_files()
68 {
69     // Load the main and service config files in config path
70     if ( Conf->load_config_from_files() != 0 )
71         return -1;
72
73     // Re-init log facility, perhaps new config file options for logger are set.
74     // These config file options will only overwrite the cmd options if the SIGHUP (reload config) is caught.
75     init_log_facility();
76
77     // Here we are. All Service Objects should be initialized, so it is time to deserialize the old service objects and compare them.
78     if ( Conf->deserialize_services() != 0 )
79         return -1;
80
81     // successful loaded
82     Log->print_conf_files_parsed();
83     return 0;
84 }
85
86
87 /**
88  * Getter for member Config.
89  * @return Member Config.
90  */
91 Config::Ptr Updater::get_config() const
92 {
93     return Conf;
94 }
95
96
97 /**
98  * Getter for member Logger.
99  * @return Member Logger.
100  */
101 Logger::Ptr Updater::get_logger() const
102 {
103     return Log;
104 }
105
106
107 /**
108  * Init the IPHelp member with needed values.
109  * @return 0 if all is fine, -1 otherwise.
110  */
111 int Updater::init_ip_helper()
112 {
113     // initialize IPHelper
114     IPHelper::Ptr _iphelp(new IPHelper(Log,Conf->get_webcheck_ip_url(),Conf->get_webcheck_ip_url_alt(),Conf->get_enable_ipv6()));
115     IPHelp = _iphelp;
116     _iphelp.reset();
117
118     return 0;
119 }
120
121
122 /**
123  * Reloading the config. Delete all Service objects and then init new Service objects from config files.
124  */
125 int Updater::reload_config()
126 {
127     // serialize all service objects
128     if ( Conf->serialize_services() != 0 )
129         return -1;
130
131     // delete all service objects
132     Conf->delete_services();
133
134     // delete the actual Variables_map, perhaps with old cmd options which would overwrite new config file options.
135     Conf->delete_variables_map();
136
137     // load only config files
138     if ( init_config_from_files() != 0 )
139         return -1;
140
141     return 0;
142 }
143
144
145 /**
146  * Initialize the logging facility with loglevel and syslog.
147  */
148 void Updater::init_log_facility()
149 {
150     Log->set_log_facility(Conf->get_loglevel(),Conf->get_syslog());
151     Log->print_init_log_facility();
152 }
153
154
155 /**
156  * Update all configured services.
157  */
158 void Updater::update_services()
159 {
160     list<Service::Ptr> services = this->Conf->get_services();
161
162     string ip = IPHelp->get_actual_ip();
163
164     if ( !ip.empty() )
165     {
166         BOOST_FOREACH(Service::Ptr &service, services )
167         {
168             service->update(ip);
169         }
170     }
171 }