Split html timed fetch into 2 classes

Signed-off-by: King_DuckZ <king_duckz@gmx.com>
This commit is contained in:
King_DuckZ 2020-08-01 07:24:45 +02:00
parent 3ab4f916d8
commit fb69fd2289
2 changed files with 37 additions and 13 deletions

View file

@ -6,33 +6,25 @@
#include <algorithm>
#include <random>
#include <functional>
#include <taskflow/taskflow.hpp>
#include <string>
namespace duck {
namespace {
auto time_rand = std::bind(std::uniform_int_distribution<int>(2, 8), std::mt19937(std::time(0)));
class HtmlFetchTimer : public ev::timer {
class HtmlFetchTimer : public EvTimerTask {
public:
HtmlFetchTimer (ev::loop_ref& loop, tf::Subflow* subflow, std::string&& url) :
m_subflow(subflow),
EvTimerTask(static_cast<double>(time_rand()), loop, subflow),
m_url(std::move(url))
{
this->set(loop);
this->set<HtmlFetchTimer, &HtmlFetchTimer::on_timer>(this);
}
void on_timer() {
m_subflow->emplace([this]() {std::cout << "Timer elapsed for " << m_url << "!\n";});
}
void start (double delay) {
ev::timer::start(delay, 0.0);
virtual void on_timer() override {
subflow().emplace([this]() {std::cout << "Timer elapsed for " << m_url << "!\n";});
}
private:
tf::Subflow* m_subflow;
std::string m_url;
};
@ -57,7 +49,6 @@ namespace {
private:
void main_loop (tf::Subflow& subflow) {
m_timer = std::make_unique<HtmlFetchTimer>(m_loop, &subflow, "lalalala");
m_timer->start(static_cast<double>(time_rand()));
this->m_loop.run(0);
}
@ -69,6 +60,22 @@ namespace {
};
} //unnamed namespace
EvTimerTask::EvTimerTask (double delay, ev::loop_ref& loop, tf::Subflow* subflow) :
m_subflow(subflow)
{
this->set(loop);
this->set<EvTimerTask, &EvTimerTask::on_timer_ev>(this);
ev::timer::start(delay, 0.0);
}
tf::Subflow& EvTimerTask::subflow() {
return *m_subflow;
}
void EvTimerTask::on_timer_ev() {
this->on_timer();
}
void test() {
//const auto processor_count = std::thread::hardware_concurrency();
EvThreadPool worker; //(std::max(2U, processor_count) - 1);

View file

@ -1,5 +1,22 @@
#pragma once
#include <ev++.h>
#include <taskflow/taskflow.hpp>
namespace duck {
class EvTimerTask : public ev::timer {
public:
EvTimerTask (double delay, ev::loop_ref&, tf::Subflow*);
virtual ~EvTimerTask() noexcept = default;
virtual void on_timer() = 0;
tf::Subflow& subflow();
private:
void on_timer_ev();
tf::Subflow* m_subflow;
};
void test();
} //namespace duck