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:
parent
53ba7febf4
commit
5bc151a3ec
7 changed files with 98 additions and 68 deletions
|
@ -50,6 +50,7 @@ executable(meson.project_name(),
|
||||||
'eventia/timer.cpp',
|
'eventia/timer.cpp',
|
||||||
'timer_items.cpp',
|
'timer_items.cpp',
|
||||||
'timer_icons.cpp',
|
'timer_icons.cpp',
|
||||||
|
'timer_base.cpp',
|
||||||
'oro/originsdb.cpp',
|
'oro/originsdb.cpp',
|
||||||
'gnulib/lib/base64.c',
|
'gnulib/lib/base64.c',
|
||||||
'base64.cpp',
|
'base64.cpp',
|
||||||
|
|
42
src/timer_base.cpp
Normal file
42
src/timer_base.cpp
Normal 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
43
src/timer_base.hpp
Normal 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
|
|
@ -1,9 +1,6 @@
|
||||||
#include "timer_icons.hpp"
|
#include "timer_icons.hpp"
|
||||||
#include "oro/originsdb.hpp"
|
#include "oro/originsdb.hpp"
|
||||||
#include "oro/api.hpp"
|
#include "oro/api.hpp"
|
||||||
#include "eventia/private/context.hpp"
|
|
||||||
#include "roar11/ThreadPool.hpp"
|
|
||||||
#include <cassert>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
namespace duck {
|
namespace duck {
|
||||||
|
@ -14,21 +11,12 @@ namespace duck {
|
||||||
oro::Api* oro_api,
|
oro::Api* oro_api,
|
||||||
oro::OriginsDB* db
|
oro::OriginsDB* db
|
||||||
) :
|
) :
|
||||||
eve::Timer(timeout, ctx),
|
TimerBase(ctx, timeout, pool, oro_api, db)
|
||||||
m_pool(pool),
|
|
||||||
m_oro_api(oro_api),
|
|
||||||
m_db(db)
|
|
||||||
{
|
{
|
||||||
assert(m_pool);
|
|
||||||
assert(m_oro_api);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TimerIcons::on_timer() {
|
|
||||||
m_pool->submit(&TimerIcons::fetch_data, this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TimerIcons::fetch_data() {
|
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;
|
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));
|
set_timer(static_cast<double>(next_timer));
|
||||||
}
|
}
|
||||||
|
|
||||||
m_db->update(icons.second);
|
db().update(icons.second);
|
||||||
}
|
}
|
||||||
|
|
||||||
} //namespace duck
|
} //namespace duck
|
||||||
|
|
|
@ -1,20 +1,10 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "eventia/private/context.hpp"
|
#include "timer_base.hpp"
|
||||||
#include "eventia/timer.hpp"
|
|
||||||
|
|
||||||
namespace roar11 {
|
|
||||||
class ThreadPool;
|
|
||||||
} //namespace roar11
|
|
||||||
|
|
||||||
namespace oro {
|
|
||||||
class Api;
|
|
||||||
class OriginsDB;
|
|
||||||
} //namespace oro
|
|
||||||
|
|
||||||
namespace duck {
|
namespace duck {
|
||||||
|
|
||||||
class TimerIcons : eve::Timer {
|
class TimerIcons : TimerBase {
|
||||||
public:
|
public:
|
||||||
TimerIcons (
|
TimerIcons (
|
||||||
const eve::Context& ctx,
|
const eve::Context& ctx,
|
||||||
|
@ -24,14 +14,8 @@ public:
|
||||||
oro::OriginsDB* db
|
oro::OriginsDB* db
|
||||||
);
|
);
|
||||||
|
|
||||||
virtual void on_timer() override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void fetch_data();
|
virtual void fetch_data() override;
|
||||||
|
|
||||||
roar11::ThreadPool* m_pool;
|
|
||||||
oro::Api* m_oro_api;
|
|
||||||
oro::OriginsDB* m_db;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} //namespace duck
|
} //namespace duck
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
#include "timer_items.hpp"
|
#include "timer_items.hpp"
|
||||||
#include "oro/originsdb.hpp"
|
#include "oro/originsdb.hpp"
|
||||||
#include "oro/api.hpp"
|
#include "oro/api.hpp"
|
||||||
#include "eventia/private/context.hpp"
|
|
||||||
#include "roar11/ThreadPool.hpp"
|
|
||||||
#include <cassert>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
namespace duck {
|
namespace duck {
|
||||||
|
@ -14,21 +11,12 @@ namespace duck {
|
||||||
oro::Api* oro_api,
|
oro::Api* oro_api,
|
||||||
oro::OriginsDB* db
|
oro::OriginsDB* db
|
||||||
) :
|
) :
|
||||||
eve::Timer(timeout, ctx),
|
TimerBase(ctx, timeout, pool, oro_api, db)
|
||||||
m_pool(pool),
|
|
||||||
m_oro_api(oro_api),
|
|
||||||
m_db(db)
|
|
||||||
{
|
{
|
||||||
assert(m_pool);
|
|
||||||
assert(m_oro_api);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TimerItems::on_timer() {
|
|
||||||
m_pool->submit(&TimerItems::fetch_data, this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TimerItems::fetch_data() {
|
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;
|
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));
|
set_timer(static_cast<double>(next_timer));
|
||||||
}
|
}
|
||||||
|
|
||||||
m_db->overwrite(items.second);
|
db().overwrite(items.second);
|
||||||
}
|
}
|
||||||
|
|
||||||
} //namespace duck
|
} //namespace duck
|
||||||
|
|
|
@ -1,20 +1,10 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "eventia/private/context.hpp"
|
#include "timer_base.hpp"
|
||||||
#include "eventia/timer.hpp"
|
|
||||||
|
|
||||||
namespace roar11 {
|
|
||||||
class ThreadPool;
|
|
||||||
} //namespace roar11
|
|
||||||
|
|
||||||
namespace oro {
|
|
||||||
class Api;
|
|
||||||
class OriginsDB;
|
|
||||||
} //namespace oro
|
|
||||||
|
|
||||||
namespace duck {
|
namespace duck {
|
||||||
|
|
||||||
class TimerItems : eve::Timer {
|
class TimerItems : TimerBase {
|
||||||
public:
|
public:
|
||||||
TimerItems (
|
TimerItems (
|
||||||
const eve::Context& ctx,
|
const eve::Context& ctx,
|
||||||
|
@ -24,14 +14,8 @@ public:
|
||||||
oro::OriginsDB* db
|
oro::OriginsDB* db
|
||||||
);
|
);
|
||||||
|
|
||||||
virtual void on_timer() override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void fetch_data();
|
virtual void fetch_data() override;
|
||||||
|
|
||||||
roar11::ThreadPool* m_pool;
|
|
||||||
oro::Api* m_oro_api;
|
|
||||||
oro::OriginsDB* m_db;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} //namespace duck
|
} //namespace duck
|
||||||
|
|
Loading…
Add table
Reference in a new issue