diff --git a/src/evloop.cpp b/src/evloop.cpp index ba8f528..1707fbc 100644 --- a/src/evloop.cpp +++ b/src/evloop.cpp @@ -6,33 +6,25 @@ #include #include #include -#include #include namespace duck { namespace { auto time_rand = std::bind(std::uniform_int_distribution(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(time_rand()), loop, subflow), m_url(std::move(url)) { - this->set(loop); - this->set(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(m_loop, &subflow, "lalalala"); - m_timer->start(static_cast(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(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); diff --git a/src/evloop.hpp b/src/evloop.hpp index 7af6f1c..7a334fc 100644 --- a/src/evloop.hpp +++ b/src/evloop.hpp @@ -1,5 +1,22 @@ #pragma once +#include +#include + 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