sample/dyn_coop_controlled/main.cpp
#include <iostream>
#include <ace/OS_main.h>
#include <so_4/api/h/api.hpp>
#include <so_4/rt/h/rt.hpp>
#include <so_4/timer_thread/simple/h/pub.hpp>
#include <so_4/disp/one_thread/h/pub.hpp>
class my_obj_t
: private cpp_util_2::nocopy_t
{
private :
std::string m_value;
public :
my_obj_t(
const std::string & value )
:
m_value( value )
{}
~my_obj_t()
{
std::cout << "~my_obj_t: " << m_value <<std::endl;
}
const std::string &
get() const
{
return m_value;
}
};
class a_my_t
: public so_4::rt::agent_t
{
typedef so_4::rt::agent_t base_type_t;
private :
const my_obj_t & m_a;
const my_obj_t & m_b;
public :
a_my_t(
const std::string & self_name,
const my_obj_t & a,
const my_obj_t & b )
:
base_type_t( self_name )
, m_a( a )
, m_b( b )
{}
virtual ~a_my_t()
{
std::cout << "~a_my_t: " << so_query_name() << std::endl;
}
virtual const char *
so_query_type() const;
virtual void
so_on_subscription()
{
so_subscribe( "evt_start",
so_4::rt::sobjectizer_agent_name(), "msg_start" );
}
void
evt_start()
{
std::cout << so_query_name() << ":\n\t"
<< m_a.get() << "\n\t"
<< m_b.get() << std::endl;
}
};
SOL4_CLASS_START( a_my_t )
SOL4_EVENT( evt_start )
SOL4_STATE_START( st_normal )
SOL4_STATE_EVENT( evt_start )
SOL4_STATE_FINISH()
SOL4_CLASS_FINISH()
class a_shutdowner_t
: public so_4::rt::agent_t
{
typedef so_4::rt::agent_t base_type_t;
public :
a_shutdowner_t()
: base_type_t( "a_shutdowner" )
{}
virtual ~a_shutdowner_t()
{}
virtual const char *
so_query_type() const;
virtual void
so_on_subscription()
{
so_subscribe( "evt_start",
so_4::rt::sobjectizer_agent_name(), "msg_start" );
}
void
evt_start()
{
so_4::api::send_msg( so_4::rt::sobjectizer_agent_name(),
"msg_normal_shutdown", 0 );
}
};
SOL4_CLASS_START( a_shutdowner_t )
SOL4_EVENT( evt_start )
SOL4_STATE_START( st_normal )
SOL4_STATE_EVENT( evt_start )
SOL4_STATE_FINISH()
SOL4_CLASS_FINISH()
int
main( int, char ** )
{
my_obj_t * a = new my_obj_t( "a" );
my_obj_t * b = new my_obj_t( "b" );
my_obj_t * c = new my_obj_t( "c" );
a_my_t * a_1 = new a_my_t( "a_1", *a, *b );
a_my_t * a_2 = new a_my_t( "a_2", *b, *c );
a_shutdowner_t * a_shutdowner = new a_shutdowner_t();
so_4::rt::agent_t * agents[] = { a_1, a_2, a_shutdowner };
so_4::rt::dyn_agent_coop_t * coop = new so_4::rt::dyn_agent_coop_t(
"sample", agents, sizeof( agents ) / sizeof( agents[ 0 ] ) );
so_4::rt::dyn_coop_controlled( *coop, a );
so_4::rt::dyn_coop_controlled( *coop, b );
so_4::rt::dyn_coop_controlled( *coop, c );
so_4::ret_code_t rc = so_4::api::start(
so_4::disp::one_thread::create_disp(
so_4::timer_thread::simple::create_timer_thread(),
so_4::auto_destroy_timer ),
so_4::auto_destroy_disp,
coop );
if( rc )
{
std::cerr << "start: " << rc << std::endl;
}
return int( rc );
}