SObjectizer  5.8
Loading...
Searching...
No Matches
so_5/dispatcher_for_children/main.cpp
/*
* Demonstration of usage of the same dispatcher for children
* cooperations.
*/
#include <iostream>
#include <sstream>
#include <so_5/all.hpp>
// Child agent.
// Sends a hello message to the coordinator at start.
// Then if creation of another child cooperation is enabled creates
// child cooperation.
class a_child_t final : public so_5::agent_t
{
public :
a_child_t(
context_t ctx,
// Mbox of the coordinator.
so_5::mbox_t coordinator,
// Dispatcher to be used for child.
// Current generation.
int generation,
// Max generation.
int max_generation )
: so_5::agent_t( ctx )
, m_coordinator( std::move( coordinator ) )
, m_dispatcher( std::move( dispatcher ) )
, m_generation( generation )
, m_max_generation( max_generation )
{}
void so_evt_start() override
{
send_hello_to_coordinator();
if( m_generation < m_max_generation )
}
private :
const so_5::mbox_t m_coordinator;
const int m_generation;
const int m_max_generation;
void send_hello_to_coordinator()
{
// Hello message must contain agent name and thread id.
std::ostringstream ss;
ss << "child at generation " << m_generation
<< " on thread: "
<< std::this_thread::get_id();
// Coordinator should receive a hello message.
so_5::send< std::string >( m_coordinator, ss.str() );
}
{
*this,
// The same dispatcher will be used for child cooperation.
m_dispatcher.binder(),
[this]( so_5::coop_t & coop ) {
coop.make_agent< a_child_t >(
m_coordinator,
m_dispatcher,
m_generation + 1,
m_max_generation );
} );
}
};
// Sample coordinator.
// Works on the default dispatcher.
// Receives messages from children agents.
// Finishes sample when all messages are received.
class a_coordinator_t final : public so_5::agent_t
{
public :
a_coordinator_t( context_t ctx ) : so_5::agent_t( ctx )
{}
void so_define_agent() override
{
// Just one message must be handled in the default agent state.
so_default_state().event( [this]( const std::string & msg ) {
std::cout << "hello: " << msg << std::endl;
// Work must be stopped if all messages are received.
if( 0 == (--m_remaining_messages) )
} );
}
void so_evt_start() override
{
// Cooperation must be created at the start of agent's work.
create_first_child_coop();
}
private :
unsigned int m_remaining_messages = 6;
void create_first_child_coop()
{
// The dispatcher for the family of child cooperations.
// This agent will be parent for new cooperation.
*this,
// The main dispatcher for the new cooperation.
disp.binder(),
[&]( so_5::coop_t & coop ) {
coop.make_agent< a_child_t >( so_direct_mbox(), disp, 1, 6 );
} );
}
};
int main()
{
try
{
[]( so_5::environment_t & env ) {
// Coordinator agent will work on the default dispatcher.
env.make_agent< a_coordinator_t >() );
} );
return 0;
}
catch( const std::exception & x )
{
std::cerr << "*** Exception caught: " << x.what() << std::endl;
}
return 2;
}
A helper header file for including all public SObjectizer stuff.
A base class for agents.
Definition agent.hpp:673
virtual void so_define_agent()
Hook on define agent for SObjectizer.
Definition agent.cpp:975
void so_deregister_agent_coop_normally()
A helper method for deregistering agent's coop in case of normal deregistration.
Definition agent.cpp:1116
environment_t & so_environment() const noexcept
Access to the SObjectizer Environment which this agent is belong.
Definition agent.cpp:987
const state_t & so_default_state() const
Access to the agent's default state.
Definition agent.cpp:900
virtual void so_evt_start()
Hook on agent start inside SObjectizer.
Definition agent.cpp:832
Agent cooperation.
Definition coop.hpp:389
A handle for active_obj dispatcher.
disp_binder_shptr_t binder() const noexcept
Get a binder for that dispatcher.
SObjectizer Environment.
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.
const state_t & event(Args &&... args) const
Helper for subscription of event handler in this state.
Definition agent.hpp:3925
SO_5_FUNC dispatcher_handle_t make_dispatcher(environment_t &env, const std::string_view data_sources_name_base, disp_params_t params)
Create an instance of active_obj dispatcher.
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
decltype(auto) introduce_child_coop(agent_t &owner, Args &&... args)
A simple way for creating and registering child cooperation.
void send(Target &&to, Args &&... args)
A utility function for creating and delivering a message or a signal.
coop_unique_holder_t create_child_coop(agent_t &owner, Args &&... args)
A simple way for creating child cooperation.