#include <iostream>
class example_guard final
, public std::enable_shared_from_this< example_guard >
{
public :
example_guard(
{}
void stop() noexcept
override {
so_5::send< shutdown_started >( m_dest );
}
private :
};
{
state_t st_normal{ this }, st_shutdown{ this };
public:
worker( context_t ctx ) :
so_5::agent_t( std::move(ctx) )
{
this >>= st_normal;
st_normal
.event( &worker::on_timer_normal )
.event( &worker::on_shutdown_started );
st_shutdown
.event( &worker::on_timer_shutdown )
.event( &worker::on_terminate );
}
{
m_timer = so_5::send_periodic< timer >( *this,
std::chrono::milliseconds(125),
std::chrono::milliseconds(125) );
}
private :
void on_timer_normal( mhood_t<timer> )
{
std::cout << "working in normal mode..." << std::endl;
}
void on_timer_shutdown( mhood_t<timer> )
{
std::cout << "working in shutdown mode..." << std::endl;
}
void on_shutdown_started( mhood_t<example_guard::shutdown_started> )
{
std::cout << "shutdown is in progress!" << std::endl;
this >>= st_shutdown;
so_5::send_delayed< terminate_work >( *this,
std::chrono::milliseconds(300) );
}
void on_terminate( mhood_t<terminate_work> )
{
std::cout << "terminate work." << std::endl;
}
};
{
public :
work_stopper( context_t ctx ) :
so_5::agent_t( std::move(ctx) )
{
} );
}
{
so_5::send_delayed< stop_work >( *this,
std::chrono::milliseconds(400) );
}
};
int main()
{
try
{
{
} );
} );
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << std::endl;
return 1;
}
return 0;
}