Small STL usage improvement
[bpdyndnsd] / src / updater.cpp
CommitLineData
b1be615b
BS
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
4545a371
BS
10#include "updater.h"
11
88a594e8
BS
12#include <boost/foreach.hpp>
13
8bca3c5d 14using namespace std;
527536b3 15
b1be615b
BS
16/**
17 * Default constructor which initializes the member Conf.
18 */
4545a371 19Updater::Updater()
019dc0d9 20 : IPHelp(new IPHelper)
4545a371 21{
2e956a36 22 // Initialize program wide Logger, at this point we don't know where to log and with which loglevel.
88a594e8 23 Logger::Ptr _log(new Logger);
254bbf53 24 Log = _log;
0665b239 25 _log.reset();
4545a371 26
254bbf53 27 // initialize Config
88a594e8 28 Config::Ptr _config(new Config(Log));
254bbf53 29 Conf = _config;
0665b239 30 _config.reset();
4545a371
BS
31}
32
527536b3 33
b1be615b
BS
34/**
35 * Default destructor.
36 */
4545a371
BS
37Updater::~Updater()
38{
39}
40
527536b3 41
b1be615b 42/**
254bbf53
BS
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.
38060291 47 */
254bbf53 48int Updater::init_config_from_cmd(int argc, char *argv[])
38060291
BS
49{
50 // Load the command line parameters
254bbf53 51 if( Conf->parse_cmd_line( argc, argv ) != 0)
667c672c 52 return -1;
38060291 53
59c8d63c
BS
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
254bbf53
BS
57 // successful parsed
58 Log->print_cmd_parsed();
38060291
BS
59 return 0;
60}
61
62
63/**
254bbf53
BS
64 * Load the main config and the service definition files in config path.
65 * @return 0 if all is fine,
b1be615b 66 */
254bbf53 67int Updater::init_config_from_files()
4545a371 68{
254bbf53 69 // Load the main and service config files in config path
27baf279 70 if ( Conf->load_config_from_files() != 0 )
667c672c 71 return -1;
4545a371 72
59c8d63c
BS
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
27baf279
BS
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 )
667c672c 79 return -1;
27baf279 80
254bbf53 81 // successful loaded
667c672c 82 Log->print_conf_files_parsed();
254bbf53 83 return 0;
4545a371
BS
84}
85
527536b3 86
b1be615b 87/**
3434b35f
BS
88 * Getter for member Config.
89 * @return Member Config.
90 */
b38684ce 91Config::Ptr Updater::get_config() const
3434b35f
BS
92{
93 return Conf;
94}
95
96
97/**
98 * Getter for member Logger.
99 * @return Member Logger.
100 */
b38684ce 101Logger::Ptr Updater::get_logger() const
3434b35f
BS
102{
103 return Log;
104}
105
106
107/**
0665b239
BS
108 * Init the IPHelp member with needed values.
109 * @return 0 if all is fine, -1 otherwise.
110 */
111int Updater::init_ip_helper()
112{
019dc0d9
BS
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();
0665b239
BS
117
118 return 0;
119}
120
121
122/**
c5675c01
BS
123 * Reloading the config. Delete all Service objects and then init new Service objects from config files.
124 */
667c672c 125int Updater::reload_config()
c5675c01 126{
27baf279 127 // serialize all service objects
667c672c
BS
128 if ( Conf->serialize_services() != 0 )
129 return -1;
27baf279 130
8bca3c5d 131 // delete all service objects
c5675c01 132 Conf->delete_services();
8bca3c5d
BS
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
667c672c
BS
138 if ( init_config_from_files() != 0 )
139 return -1;
140
141 return 0;
8bca3c5d
BS
142}
143
144
2bc1878a
BS
145/**
146 * Initialize the logging facility with loglevel and syslog.
147 */
8bca3c5d
BS
148void Updater::init_log_facility()
149{
150 Log->set_log_facility(Conf->get_loglevel(),Conf->get_syslog());
151 Log->print_init_log_facility();
c5675c01
BS
152}
153
154
155/**
b1be615b
BS
156 * Update all configured services.
157 */
4545a371
BS
158void Updater::update_services()
159{
88a594e8 160 list<Service::Ptr> services = this->Conf->get_services();
4545a371 161
0665b239 162 string ip = IPHelp->get_actual_ip();
4545a371 163
88a594e8 164 BOOST_FOREACH(Service::Ptr &service, services )
4545a371 165 {
1c0908b5 166 // TODO: only update if IP differs and is_valid and is_not_empty.
4545a371
BS
167 service->update(ip);
168 }
b1be615b 169}