Commit | Line | Data |
---|---|---|
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 | ||
8bca3c5d | 12 | using namespace std; |
527536b3 | 13 | |
b1be615b BS |
14 | /** |
15 | * Default constructor which initializes the member Conf. | |
16 | */ | |
4545a371 BS |
17 | Updater::Updater() |
18 | { | |
254bbf53 BS |
19 | // initialize Logger |
20 | LoggerPtr _log(new Logger); | |
21 | Log = _log; | |
4545a371 | 22 | |
254bbf53 BS |
23 | // initialize Config |
24 | ConfigPtr _config(new Config(Log)); | |
25 | Conf = _config; | |
527536b3 | 26 | |
254bbf53 | 27 | Log->print_constructor_call("Updater"); |
4545a371 BS |
28 | } |
29 | ||
527536b3 | 30 | |
b1be615b BS |
31 | /** |
32 | * Default destructor. | |
33 | */ | |
4545a371 BS |
34 | Updater::~Updater() |
35 | { | |
254bbf53 | 36 | Log->print_destructor_call("Updater"); |
4545a371 BS |
37 | } |
38 | ||
527536b3 | 39 | |
b1be615b | 40 | /** |
254bbf53 BS |
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. | |
38060291 | 45 | */ |
254bbf53 | 46 | int Updater::init_config_from_cmd(int argc, char *argv[]) |
38060291 BS |
47 | { |
48 | // Load the command line parameters | |
254bbf53 BS |
49 | if( Conf->parse_cmd_line( argc, argv ) != 0) |
50 | return 1; | |
38060291 | 51 | |
59c8d63c BS |
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 | ||
254bbf53 BS |
55 | // successful parsed |
56 | Log->print_cmd_parsed(); | |
38060291 BS |
57 | return 0; |
58 | } | |
59 | ||
60 | ||
61 | /** | |
254bbf53 BS |
62 | * Load the main config and the service definition files in config path. |
63 | * @return 0 if all is fine, | |
b1be615b | 64 | */ |
254bbf53 | 65 | int Updater::init_config_from_files() |
4545a371 | 66 | { |
254bbf53 | 67 | // Load the main and service config files in config path |
27baf279 | 68 | if ( Conf->load_config_from_files() != 0 ) |
254bbf53 | 69 | return 1; |
4545a371 | 70 | |
59c8d63c BS |
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 | ||
27baf279 BS |
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 | ||
254bbf53 BS |
79 | // successful loaded |
80 | return 0; | |
4545a371 BS |
81 | } |
82 | ||
527536b3 | 83 | |
b1be615b | 84 | /** |
3434b35f BS |
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 | /** | |
c5675c01 BS |
105 | * Reloading the config. Delete all Service objects and then init new Service objects from config files. |
106 | */ | |
107 | void Updater::reload_config() | |
108 | { | |
27baf279 BS |
109 | // serialize all service objects |
110 | Conf->serialize_services(); | |
111 | ||
8bca3c5d | 112 | // delete all service objects |
c5675c01 | 113 | Conf->delete_services(); |
8bca3c5d BS |
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 | |
c5675c01 | 119 | init_config_from_files(); |
8bca3c5d BS |
120 | } |
121 | ||
122 | ||
2bc1878a BS |
123 | /** |
124 | * Initialize the logging facility with loglevel and syslog. | |
125 | */ | |
8bca3c5d BS |
126 | void Updater::init_log_facility() |
127 | { | |
128 | Log->set_log_facility(Conf->get_loglevel(),Conf->get_syslog()); | |
129 | Log->print_init_log_facility(); | |
c5675c01 BS |
130 | } |
131 | ||
132 | ||
133 | /** | |
b1be615b BS |
134 | * Update all configured services. |
135 | */ | |
4545a371 BS |
136 | void Updater::update_services() |
137 | { | |
85a0abf9 | 138 | list<ServicePtr> services = this->Conf->get_services(); |
4545a371 BS |
139 | |
140 | string ip = "192.168.1.1"; | |
141 | ||
85a0abf9 | 142 | BOOST_FOREACH( ServicePtr service, services ) |
4545a371 BS |
143 | { |
144 | service->update(ip); | |
145 | } | |
b1be615b | 146 | } |