47 lines
1.1 KiB
C++
47 lines
1.1 KiB
C++
#include "time_trigger.hpp"
|
|||
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
||
|
|
#include <iostream>
|
||
|
|
#include <functional>
|
||
|
|
|
||
|
|
namespace asio = boost::asio;
|
||
|
|
namespace posix_time = boost::posix_time;
|
||
|
|
using namespace vanetza;
|
||
|
|
|
||
|
|
TimeTrigger::TimeTrigger(asio::io_context& io_context) :
|
||
|
|
io_context_(io_context), timer_(io_context), runtime_(Clock::at(now()))
|
||
|
|
{
|
||
|
|
std::cout << "Starting runtime at " << now() <<"\n";
|
||
|
|
schedule();
|
||
|
|
}
|
||
|
|
|
||
|
|
posix_time::ptime TimeTrigger::now() const
|
||
|
|
{
|
||
|
|
return posix_time::microsec_clock::universal_time();
|
||
|
|
}
|
||
|
|
|
||
|
|
void TimeTrigger::schedule()
|
||
|
|
{
|
||
|
|
update_runtime();
|
||
|
|
auto next = runtime_.next();
|
||
|
|
if (next < Clock::time_point::max()) {
|
||
|
|
auto timeout = next - runtime_.now();
|
||
|
|
timer_.expires_after(timeout);
|
||
|
|
timer_.async_wait(std::bind(&TimeTrigger::on_timeout, this, std::placeholders::_1));
|
||
|
|
} else {
|
||
|
|
timer_.cancel();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void TimeTrigger::on_timeout(const boost::system::error_code& ec)
|
||
|
|
{
|
||
|
|
if (asio::error::operation_aborted != ec) {
|
||
|
|
schedule();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void TimeTrigger::update_runtime()
|
||
|
|
{
|
||
|
|
auto current_time = now();
|
||
|
|
runtime_.trigger(Clock::at(current_time));
|
||
|
|
}
|