1 #include <sys/select.h>
10 #include <boost/bind.hpp>
12 #include "snmp_pp/snmp_pp.h"
17 #include "subscriber.h"
18 #include "datatypes.h"
24 using GTS::Subscriber;
27 Timer::Timer(boost::function<void ()> callback, time_t interval)
28 : _interval(interval),
38 int Timer::getTimeout() const
40 double delta = difftime(time(NULL), _lastFire);
41 return _interval - delta;
50 Syncer::Syncer(SettingsParser & sp,
52 : _settingsParser(sp),
56 _timers.push_back(Timer(boost::bind(&Syncer::syncInfo, this),
57 _settingsParser.settings().infoSyncInterval()));
64 void Syncer::run(const bool & running, bool & reload)
66 logger << "Syncer::run()" << std::endl;
69 logger << "Syncer::run() - wait stopped by signal" << std::endl;
73 logger << "Syncer::run() - reload" << std::endl;
75 _settingsParser.reloadConfig();
77 catch (std::exception & ex) {
78 logger << "Syncer::run() - exception: " << ex.what() << std::endl;
88 Timer & timer(getNextTimer());
90 if (timer.getTimeout() > 0) {
96 tv.tv_sec = timer.getTimeout();
99 int retval = select(1, &rfds, NULL, NULL, &tv);
111 Timer & Syncer::getNextTimer()
113 assert(_timers.size() && "Timer list must not be empty!");
114 int timeout = _timers.begin()->getTimeout();
115 std::list<Timer>::iterator it(_timers.begin());
116 std::list<Timer>::iterator pos(_timers.begin());
118 while (it != _timers.end()) {
119 if (it->getTimeout() < timeout) {
121 timeout = pos->getTimeout();
128 void Syncer::syncInfo()
130 std::map<std::string, Switch> switches;
131 if (!getSwitchesState(switches)) {
132 logger << "Syncer::syncInfo() - failed to get new switch states" << std::endl;
135 std::list<TimedSwitch>::iterator it(_switches.begin());
136 while (it != _switches.end()) {
137 _timers.erase(it->second);
138 _switches.erase(it++);
140 std::map<std::string, Switch>::const_iterator sit;
141 for (sit = switches.begin(); sit != switches.end(); ++sit) {
142 // Insert switch with no timer
143 _switches.push_back(std::make_pair(sit->second, _timers.end()));
144 // Insert timer for this switch
145 TimerIterator tit = _timers.insert(
147 Timer(boost::bind(&Switch::sync, &_switches.back().first),
148 _settingsParser.settings().switchSyncInterval()));
149 // Set timer iterator for this switch
150 _switches.back().second = tit;
152 logger << "Syncer::syncInfo() - data synchronization successfull, switches: " << _switches.size() << std::endl;
155 size_t curlWriteFunction(void * ptr, size_t size, size_t nmemb, void * userdata)
157 char * data = static_cast<char *>(ptr);
158 std::string * dest = static_cast<std::string *>(userdata);
159 dest->append(data, size * nmemb);
163 bool Syncer::getDBData(std::string & data) const
165 CURL * handle = curl_easy_init();
167 char errorBuffer[CURL_ERROR_SIZE];
168 curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0); // Accept self-signed certs
169 curl_easy_setopt(handle, CURLOPT_LOW_SPEED_LIMIT, 1); // Less than 1 bps
170 curl_easy_setopt(handle, CURLOPT_LOW_SPEED_TIME, 60); // During 60 secs
171 curl_easy_setopt(handle, CURLOPT_URL, _settingsParser.settings().dataURL().c_str()); // Our URL
172 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, curlWriteFunction); // Our write callback
173 curl_easy_setopt(handle, CURLOPT_WRITEDATA, &data); // Our callback data
174 curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, errorBuffer); // Buffer for an error messages
175 CURLcode res = curl_easy_perform(handle);
177 logger << "Syncer::getDBData() - DB communication error: '" << errorBuffer << "'" << std::endl;
178 curl_easy_cleanup(handle);
181 curl_easy_cleanup(handle);
184 logger << "Syncer::getDBData() - failed to init CURL library" << std::endl;
188 bool Syncer::getSwitchesState(std::map<std::string, Switch> & switches)
190 if (_settingsParser.settings().dataURL().empty()) {
191 logger << "Switch::getSwitchesState() - data URL is empty" << std::endl;
195 if (!getDBData(data)) {
196 logger << "Syncer::getSwitchesState() - failed to fetch data from the URL: '" << _settingsParser.settings().dataURL() << "'" << std::endl;
200 if (!parseData(data, lines)) {
201 logger << "Syncer::getSwitchesState() - failed to parse data:\n" << data << std::endl;
204 Lines::const_iterator it;
205 for (it = lines.begin(); it != lines.end(); ++it) {
206 std::pair<std::map<std::string, Switch>::iterator, bool> res(
211 _settingsParser.settings(),
221 res.first->second.addSubscriber(