Refactor timer code

No need to copy-paste that much stuff, especially since
there are going to be more timer types in the future.
This commit is contained in:
King_DuckZ 2020-08-10 03:01:54 +01:00
parent 53ba7febf4
commit 5bc151a3ec
7 changed files with 98 additions and 68 deletions

View file

@ -50,6 +50,7 @@ executable(meson.project_name(),
'eventia/timer.cpp',
'timer_items.cpp',
'timer_icons.cpp',
'timer_base.cpp',
'oro/originsdb.cpp',
'gnulib/lib/base64.c',
'base64.cpp',

42
src/timer_base.cpp Normal file
View file

@ -0,0 +1,42 @@
#include "timer_base.hpp"
#include "roar11/ThreadPool.hpp"
#include <cassert>
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);
}
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;
}
} //namespace duck

43
src/timer_base.hpp Normal file
View file

@ -0,0 +1,43 @@
#pragma once
#include "eventia/private/context.hpp"
#include "eventia/timer.hpp"
namespace roar11 {
class ThreadPool;
} //namespace roar11
namespace oro {
class Api;
class OriginsDB;
} //namespace oro
namespace duck {
class TimerBase : public eve::Timer {
public:
TimerBase (
const eve::Context& ctx,
double timeout,
roar11::ThreadPool* pool,
oro::Api* oro_api,
oro::OriginsDB* db
);
virtual ~TimerBase() noexcept = default;
virtual void on_timer() override;
protected:
roar11::ThreadPool& pool();
oro::Api& oro_api();
oro::OriginsDB& db();
private:
virtual void fetch_data() = 0;
roar11::ThreadPool* m_pool;
oro::Api* m_oro_api;
oro::OriginsDB* m_db;
};
} //namespace duck

View file

@ -1,9 +1,6 @@
#include "timer_icons.hpp"
#include "oro/originsdb.hpp"
#include "oro/api.hpp"
#include "eventia/private/context.hpp"
#include "roar11/ThreadPool.hpp"
#include <cassert>
#include <iostream>
namespace duck {
@ -14,21 +11,12 @@ namespace duck {
oro::Api* oro_api,
oro::OriginsDB* db
) :
eve::Timer(timeout, ctx),
m_pool(pool),
m_oro_api(oro_api),
m_db(db)
TimerBase(ctx, timeout, pool, oro_api, db)
{
assert(m_pool);
assert(m_oro_api);
}
void TimerIcons::on_timer() {
m_pool->submit(&TimerIcons::fetch_data, this);
}
void TimerIcons::fetch_data() {
auto icons = m_oro_api->items_icons();
auto icons = oro_api().items_icons();
{
const int next_timer = icons.first.retry_after / icons.first.rate_limit;
@ -36,7 +24,7 @@ namespace duck {
set_timer(static_cast<double>(next_timer));
}
m_db->update(icons.second);
db().update(icons.second);
}
} //namespace duck

View file

@ -1,20 +1,10 @@
#pragma once
#include "eventia/private/context.hpp"
#include "eventia/timer.hpp"
namespace roar11 {
class ThreadPool;
} //namespace roar11
namespace oro {
class Api;
class OriginsDB;
} //namespace oro
#include "timer_base.hpp"
namespace duck {
class TimerIcons : eve::Timer {
class TimerIcons : TimerBase {
public:
TimerIcons (
const eve::Context& ctx,
@ -24,14 +14,8 @@ public:
oro::OriginsDB* db
);
virtual void on_timer() override;
private:
void fetch_data();
roar11::ThreadPool* m_pool;
oro::Api* m_oro_api;
oro::OriginsDB* m_db;
virtual void fetch_data() override;
};
} //namespace duck

View file

@ -1,9 +1,6 @@
#include "timer_items.hpp"
#include "oro/originsdb.hpp"
#include "oro/api.hpp"
#include "eventia/private/context.hpp"
#include "roar11/ThreadPool.hpp"
#include <cassert>
#include <iostream>
namespace duck {
@ -14,21 +11,12 @@ namespace duck {
oro::Api* oro_api,
oro::OriginsDB* db
) :
eve::Timer(timeout, ctx),
m_pool(pool),
m_oro_api(oro_api),
m_db(db)
TimerBase(ctx, timeout, pool, oro_api, db)
{
assert(m_pool);
assert(m_oro_api);
}
void TimerItems::on_timer() {
m_pool->submit(&TimerItems::fetch_data, this);
}
void TimerItems::fetch_data() {
auto items = m_oro_api->items_list();
auto items = oro_api().items_list();
{
const int next_timer = items.first.retry_after / items.first.rate_limit;
@ -36,7 +24,7 @@ namespace duck {
set_timer(static_cast<double>(next_timer));
}
m_db->overwrite(items.second);
db().overwrite(items.second);
}
} //namespace duck

View file

@ -1,20 +1,10 @@
#pragma once
#include "eventia/private/context.hpp"
#include "eventia/timer.hpp"
namespace roar11 {
class ThreadPool;
} //namespace roar11
namespace oro {
class Api;
class OriginsDB;
} //namespace oro
#include "timer_base.hpp"
namespace duck {
class TimerItems : eve::Timer {
class TimerItems : TimerBase {
public:
TimerItems (
const eve::Context& ctx,
@ -24,14 +14,8 @@ public:
oro::OriginsDB* db
);
virtual void on_timer() override;
private:
void fetch_data();
roar11::ThreadPool* m_pool;
oro::Api* m_oro_api;
oro::OriginsDB* m_db;
virtual void fetch_data() override;
};
} //namespace duck