Corrected include's in header files.
[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 {
21     // Initialize program wide Logger, at this point we don't know where to log and with which loglevel.
22     Logger::Ptr _log(new Logger);
23     Log = _log;
24
25     // initialize Config
26     Config::Ptr _config(new Config(Log));
27     Conf = _config;
28 }
29
30
31 /**
32  * Default destructor.
33  */
34 Updater::~Updater()
35 {
36 }
37
38
39 /**
40  * Parse the command line arguments and initialize corresponding options.
41  * @param argc Number command line arguments.
42  * @param argv[] Array with arguments.
43  * @return 0 if cmd options successfully parsed, 1 if usage or version.
44  */
45 int Updater::init_config_from_cmd(int argc, char *argv[])
46 {
47     // Load the command line parameters
48     if( Conf->parse_cmd_line( argc, argv ) != 0)
49         return 1;
50
51     // If we have loaded the cmd options we need to init the log facility immediately in case debugging is enabled from cmd.
52     init_log_facility();
53
54     // successful parsed
55     Log->print_cmd_parsed();
56     return 0;
57 }
58
59
60 /**
61  * Load the main config and the service definition files in config path.
62  * @return 0 if all is fine, 
63  */
64 int Updater::init_config_from_files()
65 {
66     // Load the main and service config files in config path
67     if ( Conf->load_config_from_files() != 0 )
68         return 1;
69
70     // Re-init log facility, perhaps new config file options for logger are set.
71     // These config file options will only overwrite the cmd options if the SIGHUP (reload config) is caught.
72     init_log_facility();
73
74     // Here we are. All Service Objects should be initialized, so it is time to deserialize the old service objects and compare them.
75     if ( Conf->deserialize_services() != 0 )
76         return 1;
77
78     // successful loaded
79     return 0;
80 }
81
82
83 /**
84  * Getter for member Config.
85  * @return Member Config.
86  */
87 Config::Ptr Updater::get_config()
88 {
89     return Conf;
90 }
91
92
93 /**
94  * Getter for member Logger.
95  * @return Member Logger.
96  */
97 Logger::Ptr Updater::get_logger()
98 {
99     return Log;
100 }
101
102
103 /**
104  * Reloading the config. Delete all Service objects and then init new Service objects from config files.
105  */
106 void Updater::reload_config()
107 {
108     // serialize all service objects
109     Conf->serialize_services();
110
111     // delete all service objects
112     Conf->delete_services();
113
114     // delete the actual Variables_map, perhaps with old cmd options which would overwrite new config file options.
115     Conf->delete_variables_map();
116
117     // load only config files
118     // TODO: Error handling?
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<Service::Ptr> services = this->Conf->get_services();
139
140     string ip = "192.168.1.1";
141
142     BOOST_FOREACH(Service::Ptr &service, services )
143     {
144         service->update(ip);
145     }
146 }