Skip to content

Instantly share code, notes, and snippets.

@eao197
Created March 2, 2024 07:15
Show Gist options
  • Save eao197/413946f21d8a8f406d33cf06b8b9a3c9 to your computer and use it in GitHub Desktop.
Save eao197/413946f21d8a8f406d33cf06b8b9a3c9 to your computer and use it in GitHub Desktop.
A demo of using the same stop_guard several times
#include <so_5/all.hpp>
#include <iostream>
class shutdown_initiated final : public so_5::signal_t {};
class my_stop_guard final : public so_5::stop_guard_t
{
const so_5::mbox_t m_dest;
public:
my_stop_guard( so_5::mbox_t dest ) : m_dest{ std::move(dest) } {}
void stop() noexcept override
{
std::cout << "my_stop_guard: stop() called" << std::endl;
so_5::send< shutdown_initiated >( m_dest );
}
};
class demo final : public so_5::agent_t
{
state_t st_normal{ this, "normal" };
state_t st_dereg_initiated{ this, "dereg_initiated" };
const std::string m_name;
const so_5::mbox_t m_shutdown_notify_mbox;
const so_5::stop_guard_shptr_t m_stop_guard;
public:
demo(
context_t ctx,
std::string name,
so_5::mbox_t shutdown_notify_mbox,
so_5::stop_guard_shptr_t stop_guard )
: so_5::agent_t{ std::move(ctx) }
, m_name{ std::move(name) }
, m_shutdown_notify_mbox{ std::move(shutdown_notify_mbox) }
, m_stop_guard{ std::move(stop_guard) }
{}
void so_define_agent() override
{
st_normal.activate();
st_normal.event( m_shutdown_notify_mbox, &demo::evt_normal_shutdown );
st_dereg_initiated.event( m_shutdown_notify_mbox, &demo::evt_repeated_shutdown );
}
void so_evt_start() override
{
so_environment().setup_stop_guard( m_stop_guard );
}
void so_evt_finish() override
{
so_environment().remove_stop_guard( m_stop_guard );
}
private:
void evt_normal_shutdown( mhood_t<shutdown_initiated> )
{
std::cout << m_name << ": evt_normal_shutdown" << std::endl;
st_dereg_initiated.activate();
so_deregister_agent_coop_normally();
}
void evt_repeated_shutdown( mhood_t<shutdown_initiated> )
{
std::cout << m_name << ": evt_repeated_shutdown" << std::endl;
}
};
class shutdowner final : public so_5::agent_t
{
struct shutdown_time final : public so_5::signal_t {};
public:
shutdowner( context_t ctx ) : so_5::agent_t{ std::move(ctx) }
{
so_subscribe_self().event( [this]( mhood_t<shutdown_time> ) {
so_environment().stop();
} );
}
void so_evt_start() override
{
so_5::send_delayed< shutdown_time >( *this, std::chrono::milliseconds{ 250 } );
}
};
int main()
{
so_5::launch( []( so_5::environment_t & env ) {
const auto notify_mbox = env.create_mbox();
const so_5::stop_guard_shptr_t stop_guard = std::make_shared< my_stop_guard >(
notify_mbox );
env.register_agent_as_coop(
env.make_agent< demo >( "first", notify_mbox, stop_guard ) );
env.register_agent_as_coop(
env.make_agent< demo >( "second", notify_mbox, stop_guard ) );
env.register_agent_as_coop(
env.make_agent< demo >( "third", notify_mbox, stop_guard ) );
env.register_agent_as_coop(
env.make_agent< shutdowner >() );
} );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment