From 8fa434e76e13bfb9e360d76afe1a24ba5826c422 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sun, 9 Aug 2020 21:07:54 +0100 Subject: [PATCH] Timer queries the remote api --- src/evloop.cpp | 5 +++-- src/evloop.hpp | 6 +++++- src/main.cpp | 8 ++++---- src/timer_items.cpp | 25 +++++++++++++++++-------- src/timer_items.hpp | 12 +++++++++++- 5 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/evloop.cpp b/src/evloop.cpp index 449d190..7259f22 100644 --- a/src/evloop.cpp +++ b/src/evloop.cpp @@ -3,6 +3,7 @@ #include "eventia/eventia.hpp" #include "eventia/timer.hpp" #include "roar11/ThreadPool.hpp" +#include "oro/api.hpp" #include namespace duck { @@ -26,14 +27,14 @@ void join(roar11::ThreadPool& pool) { } //unnamed namespace -void test() { +void test(oro::Api* api) { 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()); - auto fetcher = worker.make_timer(3.0, &pool); + auto fetcher = worker.make_timer(3.0, &pool, api); join(pool); } diff --git a/src/evloop.hpp b/src/evloop.hpp index 12628cf..5a86842 100644 --- a/src/evloop.hpp +++ b/src/evloop.hpp @@ -1,7 +1,11 @@ #pragma once +namespace oro { + class Api; +} //namespace oro + namespace duck { -void test(); +void test(oro::Api* api); } //namespace duck diff --git a/src/main.cpp b/src/main.cpp index f8b6bc1..8215de0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,9 +5,6 @@ #include int main(int argc, char* argv[]) { - duck::test(); - return 0; -/* if (2 != argc) { std::cerr << "Please provide your API key\n"; return 2; @@ -27,6 +24,9 @@ int main(int argc, char* argv[]) { std::cout << "answer: " << ping.second.message << '\n'; std::cout << "version: " << ping.second.version << '\n'; + duck::test(&oro_api); + +/* { auto whoami = oro_api.who_am_i(); std::cout << "master id: " << whoami.second.master_id << '\n'; @@ -69,7 +69,7 @@ int main(int argc, char* argv[]) { } std::cout << '\n'; } +*/ return 0; - */ } diff --git a/src/timer_items.cpp b/src/timer_items.cpp index bf4db5b..cd27b79 100644 --- a/src/timer_items.cpp +++ b/src/timer_items.cpp @@ -1,14 +1,23 @@ #include "timer_items.hpp" +#include "oro/api.hpp" #include "eventia/private/context.hpp" #include "roar11/ThreadPool.hpp" #include +#include namespace duck { - TimerItems::TimerItems (const eve::Context& ctx, double timeout, roar11::ThreadPool* pool) : + TimerItems::TimerItems ( + const eve::Context& ctx, + double timeout, + roar11::ThreadPool* pool, + oro::Api* oro_api + ) : eve::Timer(timeout, ctx), - m_pool(pool) + m_pool(pool), + m_oro_api(oro_api) { assert(m_pool); + assert(m_oro_api); } void TimerItems::on_timer() { @@ -16,13 +25,13 @@ namespace duck { } void TimerItems::fetch_data() { - using namespace std::chrono_literals; - std::cout << "Timer elapsed! Doing fake work...\n"; - std::this_thread::sleep_for(5s); + auto items = m_oro_api->items_list(); - const double new_delay = 5.0; - std::cout << "Now starting next timer for " << new_delay << " seconds\n"; - set_timer(new_delay); + { + const int next_timer = items.first.retry_after / items.first.rate_limit; + std::cout << "Next timer in " << next_timer << " secs\n"; + set_timer(static_cast(next_timer)); + } } } //namespace duck diff --git a/src/timer_items.hpp b/src/timer_items.hpp index 7bd9991..65f70e1 100644 --- a/src/timer_items.hpp +++ b/src/timer_items.hpp @@ -7,11 +7,20 @@ namespace roar11 { class ThreadPool; } //namespace roar11 +namespace oro { + class Api; +} //namespace oro + namespace duck { class TimerItems : eve::Timer { public: - TimerItems (const eve::Context& ctx, double timeout, roar11::ThreadPool* pool); + TimerItems ( + const eve::Context& ctx, + double timeout, + roar11::ThreadPool* pool, + oro::Api* oro_api + ); virtual void on_timer() override; @@ -19,6 +28,7 @@ private: void fetch_data(); roar11::ThreadPool* m_pool; + oro::Api* m_oro_api; }; } //namespace duck