Implemented object serialization of Service objects (first steps).
[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 using namespace std;
13
14 /**
15  * Default constructor which initializes the member Conf.
16  */
17 Updater::Updater()
18 {
19     // initialize Logger
20     LoggerPtr _log(new Logger);
21     Log = _log;
22
23     // initialize Config
24     ConfigPtr _config(new Config(Log));
25     Conf = _config;
26
27     Log->print_constructor_call("Updater");
28 }
29
30
31 /**
32  * Default destructor.
33  */
34 Updater::~Updater()
35 {
36     Log->print_destructor_call("Updater");
37 }
38
39
40 /**
41  * Parse the command line arguments and initialize corresponding options.
42  * @param argc Number command line arguments.
43  * @param argv[] Array with arguments.
44  * @return 0 if cmd options successfully parsed, 1 if usage or version.
45  */
46 int Updater::init_config_from_cmd(int argc, char *argv[])
47 {
48     // Load the command line parameters
49     if( Conf->parse_cmd_line( argc, argv ) != 0)
50         return 1;
51
52     // If we have loaded the cmd options we need to init the log facility immediately in case debugging is enabled from cmd.
53     init_log_facility();
54
55     // successful parsed
56     Log->print_cmd_parsed();
57     return 0;
58 }
59
60
61 /**
62  * Load the main config and the service definition files in config path.
63  * @return 0 if all is fine, 
64  */
65 int Updater::init_config_from_files()
66 {
67     // Load the main and service config files in config path
68     if( Conf->load_config_from_files() != 0)
69         return 1;
70
71     // Re-init log facility, perhaps new config file options for logger are set.
72     // These config file options will only overwrite the cmd options if the SIGHUP (reload config) is caught.
73     init_log_facility();
74
75     // successful loaded
76     return 0;
77 }
78
79
80 /**
81  * Getter for member Config.
82  * @return Member Config.
83  */
84 ConfigPtr Updater::get_config()
85 {
86     return Conf;
87 }
88
89
90 /**
91  * Getter for member Logger.
92  * @return Member Logger.
93  */
94 LoggerPtr Updater::get_logger()
95 {
96     return Log;
97 }
98
99
100 /**
101  * Reloading the config. Delete all Service objects and then init new Service objects from config files.
102  */
103 void Updater::reload_config()
104 {
105     // delete all service objects
106     Conf->delete_services();
107
108     // delete the actual Variables_map, perhaps with old cmd options which would overwrite new config file options.
109     Conf->delete_variables_map();
110
111     // load only config files
112     init_config_from_files();
113 }
114
115
116 /**
117  * Initialize the logging facility with loglevel and syslog.
118  */
119 void Updater::init_log_facility()
120 {
121     Log->set_log_facility(Conf->get_loglevel(),Conf->get_syslog());
122     Log->print_init_log_facility();
123 }
124
125
126 /**
127  * Update all configured services.
128  */
129 void Updater::update_services()
130 {
131     list<ServicePtr> services = this->Conf->get_services();
132
133     string ip = "192.168.1.1";
134
135     BOOST_FOREACH( ServicePtr service, services )
136     {
137         service->update(ip);
138     }
139 }