sample/subscr_hook/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/active_obj/h/pub.hpp>
class a_child_t
: public so_4::rt::agent_t
{
typedef so_4::rt::agent_t base_type_t;
public :
a_child_t(
const std::string & self_name )
:
base_type_t( self_name )
{
so_add_traits(
so_4::disp::active_obj::query_active_obj_traits() );
}
virtual ~a_child_t()
{}
struct msg_hello {};
struct msg_bye {};
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_query_name(), "msg_hello", 0 );
so_4::api::send_msg( so_query_name(), "msg_bye", 0 );
}
};
SOL4_CLASS_START( a_child_t )
SOL4_MSG_START( msg_hello, a_child_t::msg_hello )
SOL4_MSG_FINISH()
SOL4_MSG_START( msg_bye, a_child_t::msg_bye )
SOL4_MSG_FINISH()
SOL4_EVENT( evt_start )
SOL4_STATE_START( st_normal )
SOL4_STATE_EVENT( evt_start )
SOL4_STATE_FINISH()
SOL4_CLASS_FINISH()
class a_owner_t
: public so_4::rt::agent_t
{
typedef so_4::rt::agent_t base_type_t;
public :
a_owner_t()
: base_type_t( "a_owner" )
{}
virtual ~a_owner_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()
{
a_child_t * child = new a_child_t( "a_child" );
so_4::rt::dyn_agent_coop_t * child_coop =
new so_4::rt::dyn_agent_coop_t( child );
so_4::rt::def_subscr_hook( *child_coop,
*this, "evt_hello",
*child, "msg_hello" );
so_4::rt::def_subscr_hook( *child_coop,
*this, "evt_bye",
"a_child", "msg_bye" );
so_4::rt::dyn_agent_coop_helper_t child_coop_helper(
child_coop );
}
void
evt_hello()
{
std::cout << "hello!" << std::endl;
}
void
evt_bye()
{
std::cout << "bye!" << std::endl;
so_4::api::send_msg( so_4::rt::sobjectizer_agent_name(),
"msg_normal_shutdown", 0 );
}
};
SOL4_CLASS_START( a_owner_t )
SOL4_EVENT( evt_start )
SOL4_EVENT( evt_hello )
SOL4_EVENT( evt_bye )
SOL4_STATE_START( st_normal )
SOL4_STATE_EVENT( evt_start )
SOL4_STATE_EVENT( evt_hello )
SOL4_STATE_EVENT( evt_bye )
SOL4_STATE_FINISH()
SOL4_CLASS_FINISH()
int
main( int, char ** )
{
a_owner_t a_owner;
so_4::rt::agent_coop_t coop( a_owner );
so_4::ret_code_t rc = so_4::api::start(
so_4::disp::active_obj::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::cerr << "successful finish" << std::endl;
return int( rc );
}