#include <so_5/all.hpp>
class example_t final : public so_5::agent_t
{
struct first_delayed final : public so_5::signal_t {};
struct second_delayed final : public so_5::signal_t {};
struct last_delayed final : public so_5::signal_t {};
struct periodic final : public so_5::signal_t {};
timer_ns::timer_id_t m_first;
timer_ns::timer_id_t m_second;
timer_ns::timer_id_t m_last;
timer_ns::timer_id_t m_periodic;
public :
example_t( context_t ctx ) : so_5::agent_t{ std::move(ctx) }
{
so_subscribe_self()
.event( &example_t::on_first_delayed )
.event( &example_t::on_second_delayed )
.event( &example_t::on_last_delayed )
.event( &example_t::on_periodic );
}
void so_evt_start() override
{
using namespace std::chrono_literals;
m_first = timer_ns::send_delayed< first_delayed >( *this, 100ms );
m_second = timer_ns::send_delayed< second_delayed >( *this, 200ms );
m_last = timer_ns::send_delayed< last_delayed >( *this, 300ms );
m_periodic = timer_ns::send_periodic< periodic >( *this, 75ms, 75ms );
std::cout << "hang the agent..." << std::flush;
std::this_thread::sleep_for( 220ms );
std::cout << "done" << std::endl;
}
private :
void on_first_delayed( mhood_t<first_delayed> )
{
std::cout << "first_delayed received" << std::endl;
m_second.revoke();
m_periodic.revoke();
}
void on_second_delayed( mhood_t<second_delayed> )
{
std::cout << "second_delayed received" << std::endl;
}
void on_last_delayed( mhood_t<last_delayed> )
{
std::cout << "last_delayed received" << std::endl;
so_deregister_agent_coop_normally();
}
void on_periodic( mhood_t<periodic> )
{
std::cout << "periodic received" << std::endl;
}
};
int main()
{
so_5::launch( [](so_5::environment_t & env) {
env.register_agent_as_coop( env.make_agent< example_t >() );
} );
return 0;
}
Implementation of revocable timers.