SObjectizer  5.8
Loading...
Searching...
No Matches
so_5/individual_msg_tracing/main.cpp
/*
* An example to show usage of so_5::msg_tracing::individual_trace function.
*
* A message delivery tracing is enabled. Trace is going to std::cout.
*/
// Main SObjectizer header file.
#include <so_5/all.hpp>
using namespace std::chrono_literals;
// Signals for ping-pong.
struct ping final : public so_5::signal_t {};
struct pong final : public so_5::signal_t {};
// A class for implementation of pinger and ponger agents.
template< typename Signal_To_Send, typename Signal_To_Receive, bool Use_Individual_Trace >
class a_pinger_ponger_t final : public so_5::agent_t
{
const so_5::mbox_t m_mbox;
public :
a_pinger_ponger_t( context_t ctx, so_5::mbox_t mbox )
: so_5::agent_t( std::move(ctx) )
, m_mbox( std::move(mbox) )
{}
void so_define_agent() override
{
so_subscribe( m_mbox ).event( [this](mhood_t<Signal_To_Receive>) {
if constexpr( Use_Individual_Trace )
// Use individual tracing for outgoing messages.
else
// Use the usual delivery.
} );
}
};
// Main example agent.
class a_example_t final : public so_5::agent_t
{
// Signal for finishing the example.
struct finish final : public so_5::signal_t {};
public :
a_example_t( context_t ctx )
: so_5::agent_t( std::move(ctx) )
{
so_subscribe_self().event( &a_example_t::on_finish );
}
virtual void so_evt_start() override
{
// Limit the work time.
so_5::send_delayed< finish >( *this, std::chrono::milliseconds(500) );
// Create a ping-pong pair which will work on separate thread.
make_ping_pong_pair(
so_environment() ).binder() );
}
private :
void on_finish( mhood_t<finish> )
{
}
void make_ping_pong_pair( so_5::disp_binder_shptr_t binder )
{
// A mbox to be used by pinger and ponger agents.
const auto mbox = so_environment().create_mbox();
// Create a new coop with two agents inside.
so_5::introduce_child_coop( *this, std::move(binder),
[mbox]( so_5::coop_t & coop ) {
// Only pinger will use individual_trace.
coop.make_agent< a_pinger_ponger_t<ping, pong, true> >( mbox );
coop.make_agent< a_pinger_ponger_t<pong, ping, false> >( mbox );
} );
// Initiate ping-pong exchange.
// Enable msg_tracing for this initial ping signal too.
}
};
int main()
{
try
{
env.introduce_coop( []( so_5::coop_t & coop ) {
coop.make_agent< a_example_t >();
} );
},
[]( so_5::environment_params_t & params ) {
// Turn message delivery tracing on.
params.message_delivery_tracer(
// Setup filter that allows only messages with individual trace.
params.message_delivery_tracer_filter(
} );
}
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
virtual void so_define_agent()
Hook on define agent for SObjectizer.
Definition agent.cpp:975
subscription_bind_t so_subscribe_self()
Initiate subscription to agent's direct mbox.
Definition agent.hpp:1461
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
subscription_bind_t so_subscribe(const mbox_t &mbox_ref)
Initiate subscription.
Definition agent.hpp:1404
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
Parameters for the SObjectizer Environment initialization.
SObjectizer Environment.
mbox_t create_mbox()
Create an anonymous MPMC mbox.
decltype(auto) introduce_coop(Args &&... args)
Helper method for simplification of cooperation creation and registration.
A base class for agent signals.
Definition message.hpp:275
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
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 one_thread dispatcher.
SO_5_FUNC tracer_unique_ptr_t std_cout_tracer()
Factory for tracer which uses std::cout stream.
so_5::mbox_t individual_trace(Dest &&dest)
Indicator that tells that delivery of the message/signal should be traced.
SO_5_FUNC filter_shptr_t make_individual_trace_filter()
Factory for special message tracing filter for individual tracing.
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.
std::shared_ptr< disp_binder_t > disp_binder_shptr_t
Typedef for the disp_binder smart pointer.
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.