orotool/src/timer_base.cpp
2020-08-11 02:10:43 +01:00

84 lines
2 KiB
C++

/* Copyright 2020, Michele Santullo
* This file is part of orotool.
*
* Orotool is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Orotool is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Orotool. If not, see <http://www.gnu.org/licenses/>.
*/
#include "timer_base.hpp"
#include "roar11/ThreadPool.hpp"
#include "oro/api.hpp"
#include "oro/originsdb.hpp"
#include <cassert>
#include <iostream>
namespace duck {
TimerBase::TimerBase (
const eve::Context& ctx,
double timeout,
roar11::ThreadPool* pool,
oro::Api* oro_api,
oro::OriginsDB* db
) :
eve::Timer(timeout, ctx),
m_pool(pool),
m_oro_api(oro_api),
m_db(db)
{
assert(m_pool);
assert(m_oro_api);
}
void TimerBase::on_timer() {
m_pool->submit(&TimerBase::fetch_data, this);
}
void TimerBase::set_next_timer (const oro::Header& header) {
const int next_timer = header.retry_after / header.rate_limit;
std::cout << "Next timer in " << next_timer << " secs\n";
this->set_timer(static_cast<double>(next_timer));
}
roar11::ThreadPool& TimerBase::pool() {
assert(m_pool);
return *m_pool;
}
oro::Api& TimerBase::oro_api() {
assert(m_oro_api);
return *m_oro_api;
}
oro::OriginsDB& TimerBase::db() {
assert(m_db);
return *m_db;
}
void TimerBase::update_db (const oro::Shops& shops) {
db().update(shops);
}
void TimerBase::update_db (const oro::Items& items) {
db().update(items);
}
void TimerBase::update_db (const oro::Icons& icons) {
db().update(icons);
}
void TimerBase::update_db (const oro::Creators& creators) {
db().update(creators);
}
} //namespace duck