sample/destroyable_traits/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_traits_t
: public so_4::rt::agent_traits_t
{
private :
std::string m_agent;
public :
my_traits_t( const std::string & agent )
: m_agent( agent )
{}
~my_traits_t()
{
std::cout << "~my_traits_t: " << m_agent << std::endl;
}
virtual void
init( so_4::rt::agent_t & agent )
{
std::cout << "my_traits_t::init(): " << m_agent << std::endl;
}
virtual void
deinit( so_4::rt::agent_t & agent )
{
std::cout << "my_traits_t::deinit(): " << m_agent << std::endl;
}
};
class a_my_t
: public so_4::rt::agent_t
{
typedef so_4::rt::agent_t base_type_t;
public :
a_my_t(
const std::string & self_name )
:
base_type_t( self_name )
{
so_add_destroyable_traits( new my_traits_t( self_name ) );
}
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() << 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 ** )
{
a_my_t * a_1 = new a_my_t( "a_1" );
a_my_t * a_2 = new a_my_t( "a_2" );
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::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;
}
else
std::cout << "successful finish" << std::endl;
return int( rc );
}