#include <iostream>
#include <set>
const std::string registry_mbox_name{ "agents_registry" };
{
public:
{
const std::string m_name;
agent_started( std::string name ) : m_name{ std::move(name) }
{}
};
{
const std::string m_name;
agent_finished( std::string name ) : m_name{ std::move(name) }
{}
};
agents_registry( context_t ctx )
:
so_5::agent_t{ std::move(ctx) }
{}
{
.
event( &agents_registry::evt_agent_started )
.event( &agents_registry::evt_agent_finished )
;
.
event( &agents_registry::evt_show_registry )
;
}
{
std::chrono::milliseconds{ 20 },
std::chrono::milliseconds{ 20 } );
}
private:
std::set< std::string > m_registry;
void evt_agent_started( mhood_t<agent_started> cmd )
{
m_registry.insert( cmd->m_name );
std::cout << " started: " << cmd->m_name << std::endl;
}
void evt_agent_finished( mhood_t<agent_finished> cmd )
{
m_registry.erase( cmd->m_name );
std::cout << "finished: " << cmd->m_name << std::endl;
if( m_registry.empty() )
}
void evt_show_registry( mhood_t<show_registry> )
{
std::cout << "--- registry begin ---" << std::endl;
for( const auto & name : m_registry )
std::cout << " " << name << std::endl;
std::cout << "--- registry end ---" << std::endl;
}
};
{
public:
simple_worker( context_t ctx, std::chrono::milliseconds work_time )
:
so_5::agent_t{ std::move(ctx) }
, m_work_time{ work_time }
{}
{
}
{
}
{
}
private:
const std::chrono::milliseconds m_work_time;
void evt_done( mhood_t<done> )
{
}
};
class named_worker : public simple_worker
{
public:
named_worker( context_t ctx, std::string_view name, std::chrono::milliseconds work_time )
: simple_worker{ ctx + name_for_agent(name), work_time }
{}
};
int main()
{
try
{
{
using namespace std::chrono_literals;
env.
make_agent< named_worker >(
"Alice", 50ms ) );
} );
}
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.
virtual void so_define_agent()
Hook on define agent for SObjectizer.
subscription_bind_t so_subscribe_self()
Initiate subscription to agent's direct mbox.
void so_deregister_agent_coop_normally()
A helper method for deregistering agent's coop in case of normal deregistration.
virtual void so_evt_finish()
Hook of agent finish in SObjectizer.
agent_identity_t so_agent_name() const noexcept
Get an optional name of the agent.
environment_t & so_environment() const noexcept
Access to the SObjectizer Environment which this agent is belong.
subscription_bind_t so_subscribe(const mbox_t &mbox_ref)
Initiate subscription.
virtual void so_evt_start()
Hook on agent start inside SObjectizer.
coop_handle_t register_agent_as_coop(std::unique_ptr< A > agent)
Register single agent as a cooperation.
std::unique_ptr< Agent > make_agent(Args &&... args)
Helper method for simplification of agents creation.
A base class for agent messages.
A base class for agent signals.
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.
An indentificator for the timer.
Private part of message limit implementation.
void launch(Init_Routine &&init_routine)
Launch a SObjectizer Environment with default parameters.
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.