Managed Service de/serialization through dedicated Serviceholder class.
[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     // Here we are. All Service Objects should be initialized, so it is time to deserialize the old service objects and compare them.
76     if ( Conf->deserialize_services() != 0 )
77         return 1;
78
79     // successful loaded
80     return 0;
81 }
82
83
84 /**
85  * Getter for member Config.
86  * @return Member Config.
87  */
88 ConfigPtr Updater::get_config()
89 {
90     return Conf;
91 }
92
93
94 /**
95  * Getter for member Logger.
96  * @return Member Logger.
97  */
98 LoggerPtr Updater::get_logger()
99 {
100     return Log;
101 }
102
103
104 /**
105  * Reloading the config. Delete all Service objects and then init new Service objects from config files.
106  */
107 void Updater::reload_config()
108 {
109     // serialize all service objects
110     Conf->serialize_services();
111
112     // delete all service objects
113     Conf->delete_services();
114
115     // delete the actual Variables_map, perhaps with old cmd options which would overwrite new config file options.
116     Conf->delete_variables_map();
117
118     // load only config files
119     init_config_from_files();
120 }
121
122
123 /**
124  * Initialize the logging facility with loglevel and syslog.
125  */
126 void Updater::init_log_facility()
127 {
128     Log->set_log_facility(Conf->get_loglevel(),Conf->get_syslog());
129     Log->print_init_log_facility();
130 }
131
132
133 /**
134  * Update all configured services.
135  */
136 void Updater::update_services()
137 {
138     list<ServicePtr> services = this->Conf->get_services();
139
140     string ip = "192.168.1.1";
141
142     BOOST_FOREACH( ServicePtr service, services )
143     {
144         service->update(ip);
145     }
146 }