SObjectizer  5.8
Loading...
Searching...
No Matches
so_5/stop_guard/main.cpp
/*
* A sample of using stop_guard.
*/
#include <iostream>
// Main SObjectizer header files.
#include <so_5/all.hpp>
// Definition of stop_guard for the example.
// This guard sends a shutdown_started signal to the specified mbox.
class example_guard final
, public std::enable_shared_from_this< example_guard >
{
public :
// A signal to be sent when shutdown is started.
struct shutdown_started final : public so_5::signal_t {};
example_guard(
// Mbox to which shutdown_started must be sent.
so_5::mbox_t dest ) : m_dest( std::move(dest) )
{}
void stop() noexcept override
{
}
private :
const so_5::mbox_t m_dest;
};
// Definition of an agent for SObjectizer.
class worker final : public so_5::agent_t
{
// Worker will have two states:
state_t st_normal{ this }, st_shutdown{ this };
// These signals will be used by worker.
struct timer final : public so_5::signal_t {};
struct terminate_work final : public so_5::signal_t {};
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 );
}
// A reaction to start of work in SObjectizer.
void so_evt_start() override
{
// Stop_guard must be created and installed.
m_guard = std::make_shared< example_guard >( so_direct_mbox() );
// Periodic timer message must be sent.
m_timer = so_5::send_periodic< timer >( *this,
std::chrono::milliseconds(125),
std::chrono::milliseconds(125) );
}
private :
// Stop_guard to be used by this agent.
// Timer ID for periodic message.
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;
std::chrono::milliseconds(300) );
}
void on_terminate( mhood_t<terminate_work> )
{
std::cout << "terminate work." << std::endl;
}
};
// This agent will initiate the stop operation.
class work_stopper final : public so_5::agent_t
{
struct stop_work final : public so_5::signal_t {};
public :
work_stopper( context_t ctx ) : so_5::agent_t( std::move(ctx) )
{
so_subscribe_self().event( [this]( mhood_t<stop_work> ) {
// Initiate shutdown of SObjectizer Environment.
} );
}
void so_evt_start() override
{
std::chrono::milliseconds(400) );
}
};
int main()
{
try
{
// Starting SObjectizer.
// A function for SO Environment initialization.
[]( so_5::environment_t & env )
{
// Create coop with example's agents.
env.introduce_coop( []( so_5::coop_t & coop ) {
coop.make_agent< worker >();
coop.make_agent< work_stopper >();
} );
} );
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << std::endl;
return 1;
}
return 0;
}
A helper header file for including all public SObjectizer stuff.
A base class for agents.
Definition agent.hpp:673
subscription_bind_t so_subscribe_self()
Initiate subscription to agent's direct mbox.
Definition agent.hpp:1461
const mbox_t & so_direct_mbox() const
Get the agent's direct mbox.
Definition agent.cpp:887
environment_t & so_environment() const noexcept
Access to the SObjectizer Environment which this agent is belong.
Definition agent.cpp:987
virtual void so_evt_start()
Hook on agent start inside SObjectizer.
Definition agent.cpp:832
Agent cooperation.
Definition coop.hpp:389
Agent * make_agent(Args &&... args)
Helper method for simplification of agents creation.
Definition coop.hpp:792
SObjectizer Environment.
void stop() noexcept
Send a shutdown signal to the Run-Time.
decltype(auto) introduce_coop(Args &&... args)
Helper method for simplification of cooperation creation and registration.
void remove_stop_guard(stop_guard_shptr_t guard)
Remove stop_guard and complete the stop operation if necessary.
stop_guard_t::setup_result_t setup_stop_guard(stop_guard_shptr_t guard, stop_guard_t::what_if_stop_in_progress_t reaction_on_stop_in_progress=stop_guard_t::what_if_stop_in_progress_t::throw_exception)
Set up a new stop_guard.
A base class for agent signals.
Definition message.hpp:275
An interface of stop_guard entity.
virtual void stop() noexcept=0
Perform stop-related actions.
std::enable_if< details::is_agent_method_pointer< details::method_arity::unary, Method_Pointer >::value, subscription_bind_t & >::type event(Method_Pointer pfn, thread_safety_t thread_safety=not_thread_safe)
Make subscription to the message.
Definition agent.hpp:3657
An indentificator for the timer.
Definition timers.hpp:82
Private part of message limit implementation.
Definition agent.cpp:33
void launch(Init_Routine &&init_routine)
Launch a SObjectizer Environment with default parameters.
Definition api.hpp:142
std::shared_ptr< stop_guard_t > stop_guard_shptr_t
An alias of shared_ptr for stop_guard.
timer_id_t send_periodic(Target &&target, std::chrono::steady_clock::duration pause, std::chrono::steady_clock::duration period, Args &&... args)
A utility function for creating and delivering a periodic message to the specified destination.
void send(Target &&to, Args &&... args)
A utility function for creating and delivering a message or a signal.
void send_delayed(Target &&target, std::chrono::steady_clock::duration pause, Args &&... args)
A utility function for creating and delivering a delayed message to the specified destination.
STL namespace.