SObjectizer 5.8
Loading...
Searching...
No Matches
so_5/dispatcher_for_children_2/main.cpp
/*
* Demonstration of usage of the same dispatcher for children
* cooperations. The so_5::agent_t::so_this_coop_disp_binder() is
* used for this purpose.
*/
#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,
// Current generation.
int generation,
// Max generation.
int max_generation )
: so_5::agent_t( ctx )
, m_coordinator( std::move( coordinator ) )
, 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.
[this]( so_5::coop_t & coop ) {
coop.make_agent< a_child_t >(
m_coordinator,
m_generation + 1,
m_max_generation );
} );
}
};
// Sample coordinator.
// 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()
{
// This agent will be parent for new cooperation.
*this,
// Use the dispatcher binder of this agent.
[this]( so_5::coop_t & coop ) {
coop.make_agent< a_child_t >( so_direct_mbox(), 1, 6 );
} );
}
};
int main()
{
try
{
[]( so_5::environment_t & env ) {
// The coop of the coordinator agent will use active_obj dispatcher.
[]( so_5::coop_t & coop ) {
coop.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
disp_binder_shptr_t so_this_agent_disp_binder() const
Returns the dispatcher binder that is used for binding this agent.
Definition agent.hpp:2671
virtual void so_define_agent()
Hook on define agent for SObjectizer.
Definition agent.cpp:841
void so_deregister_agent_coop_normally()
A helper method for deregistering agent's coop in case of normal deregistration.
Definition agent.cpp:982
disp_binder_shptr_t so_this_coop_disp_binder() const
Returns the dispatcher binder that is used as the default binder for the agent's coop.
Definition agent.cpp:988
const mbox_t & so_direct_mbox() const
Get the agent's direct mbox.
Definition agent.cpp:762
const state_t & so_default_state() const
Access to the agent's default state.
Definition agent.cpp:775
virtual void so_evt_start()
Hook on agent start inside SObjectizer.
Definition agent.cpp:701
Agent cooperation.
Definition coop.hpp:389
Agent * make_agent(Args &&... args)
Helper method for simplification of agents creation.
Definition coop.hpp:792
SObjectizer Environment.
decltype(auto) introduce_coop(Args &&... args)
Helper method for simplification of cooperation creation and registration.
const state_t & event(Args &&... args) const
Helper for subscription of event handler in this state.
Definition agent.hpp:3758
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.