More refactoring

This commit is contained in:
King_DuckZ 2020-08-09 20:47:04 +01:00
parent 73ec4446ea
commit c0461f6b09
6 changed files with 61 additions and 47 deletions

View file

@ -8,6 +8,9 @@ class Context;
class Timer {
public:
Timer() = delete;
Timer(Timer&&) = default;
Timer(const Timer&) = delete;
Timer (double delay, const Context& ctx);
virtual ~Timer() noexcept;

View file

@ -1,5 +1,5 @@
#include "evloop.hpp"
#include "html_fetch_task.hpp"
#include "timer_items.hpp"
#include "eventia/eventia.hpp"
#include "eventia/timer.hpp"
#include "roar11/ThreadPool.hpp"
@ -27,12 +27,13 @@ void join(roar11::ThreadPool& pool) {
} //unnamed namespace
void test() {
roar11::ThreadPool pool(std::max(2U, std::thread::hardware_concurrency()) - 1);
const std::size_t thread_count = 2U; //std::min(std::max(3U, std::thread::hardware_concurrency()) - 1, 4U);
std::cout << "Running with " << thread_count << " worker threads\n";
roar11::ThreadPool pool(thread_count);
eve::Eventia worker;
pool.submit(worker.event_functor());
std::cout << "Instantiating html timer\n";
auto fetcher = worker.make_timer<HtmlFetchTimer>(&pool, "test_url_lol");
auto fetcher = worker.make_timer<TimerItems>(3.0, &pool);
join(pool);
}

View file

@ -1,43 +0,0 @@
#pragma once
#include "eventia/timer.hpp"
#include "roar11/ThreadPool.hpp"
#include <thread>
#include <chrono>
#include <random>
#include <functional>
#include <iostream>
namespace duck {
auto time_rand = std::bind(std::uniform_int_distribution<int>(2, 8), std::mt19937(std::time(0)));
class HtmlFetchTimer : public eve::Timer {
public:
HtmlFetchTimer (HtmlFetchTimer&&) = default;
HtmlFetchTimer (const HtmlFetchTimer&) = delete;
HtmlFetchTimer (const eve::Context& ctx, roar11::ThreadPool* pool, std::string&& url) :
eve::Timer(3.0, ctx),
m_url(std::move(url)),
m_pool(pool)
{
}
virtual void on_timer() override {
auto work_chunk = [this]() {
using namespace std::chrono_literals;
std::cout << "Timer elapsed for " << m_url << "! Doing fake work...\n";
std::this_thread::sleep_for(5s);
const double new_delay = 10.0 + static_cast<double>(time_rand());
std::cout << "Now starting next timer for " << new_delay << " seconds\n";
set_timer(new_delay);
};
m_pool->submit(work_chunk);
}
private:
std::string m_url;
roar11::ThreadPool* m_pool;
};
} //namespace duck

View file

@ -42,6 +42,7 @@ executable(meson.project_name(),
'evloop.cpp',
'eventia/eventia.cpp',
'eventia/timer.cpp',
'timer_items.cpp',
project_config_file,
install: true,
dependencies: lib_deps,

28
src/timer_items.cpp Normal file
View file

@ -0,0 +1,28 @@
#include "timer_items.hpp"
#include "eventia/private/context.hpp"
#include "roar11/ThreadPool.hpp"
#include <cassert>
namespace duck {
TimerItems::TimerItems (const eve::Context& ctx, double timeout, roar11::ThreadPool* pool) :
eve::Timer(timeout, ctx),
m_pool(pool)
{
assert(m_pool);
}
void TimerItems::on_timer() {
m_pool->submit(&TimerItems::fetch_data, this);
}
void TimerItems::fetch_data() {
using namespace std::chrono_literals;
std::cout << "Timer elapsed! Doing fake work...\n";
std::this_thread::sleep_for(5s);
const double new_delay = 5.0;
std::cout << "Now starting next timer for " << new_delay << " seconds\n";
set_timer(new_delay);
}
} //namespace duck

24
src/timer_items.hpp Normal file
View file

@ -0,0 +1,24 @@
#pragma once
#include "eventia/private/context.hpp"
#include "eventia/timer.hpp"
namespace roar11 {
class ThreadPool;
} //namespace roar11
namespace duck {
class TimerItems : eve::Timer {
public:
TimerItems (const eve::Context& ctx, double timeout, roar11::ThreadPool* pool);
virtual void on_timer() override;
private:
void fetch_data();
roar11::ThreadPool* m_pool;
};
} //namespace duck