#include <iostream>
#include <ace/OS.h>
#include <so_4/rt/h/rt.hpp>
#include <so_4/api/h/api.hpp>
#include <so_4/disp/active_obj/h/pub.hpp>
#include <so_4/timer_thread/simple/h/pub.hpp>
#include <so_4/rt/comm/h/a_sop_outgoing_channel.hpp>
#include <so_4/transport_layer/socket/h/pub.hpp>
#include <so_alt_channel_2/h/pub.hpp>
class a_handler_t
: public so_4::rt::agent_t
{
typedef so_4::rt::agent_t base_type_t;
private :
const std::string m_comm_agent_name;
so_4::rt::comm_channel_t m_channel;
public :
a_handler_t(
const std::string & self_name,
const std::string & comm_agent_name )
: base_type_t( self_name )
, m_comm_agent_name( comm_agent_name )
{}
virtual ~a_handler_t()
{}
virtual const char *
so_query_type() const;
virtual void
so_on_subscription()
{
so_subscribe( "evt_success",
m_comm_agent_name, "msg_success" );
so_subscribe( "evt_fail",
m_comm_agent_name, "msg_fail" );
so_subscribe( "evt_client_connected",
so_4::rt::comm::communicator_agent_name(),
"msg_client_connected" );
so_subscribe( "evt_client_disconnected",
so_4::rt::comm::communicator_agent_name(),
"msg_client_disconnected" );
}
void
evt_success(
const so_4::rt::comm::msg_success & cmd )
{
std::cout << "physical connection established: "
<< cmd.m_channel << std::endl;
}
void
evt_fail(
const so_4::rt::comm::msg_fail & cmd )
{
std::cout << "physical connection failed: "
<< cmd.m_reason << std::endl;
}
void
evt_client_connected(
const so_4::rt::comm::msg_client_connected & cmd )
{
std::cout << "sop connection established: "
<< cmd.m_channel << std::endl;
m_channel = cmd.m_channel;
}
void
evt_client_disconnected(
const so_4::rt::comm::msg_client_disconnected & cmd )
{
std::cout << "sop connection lost: "
<< cmd.m_channel << std::endl;
}
};
SOL4_CLASS_START( a_handler_t )
SOL4_EVENT_STC( evt_success,
so_4::rt::comm::msg_success )
SOL4_EVENT_STC( evt_fail,
so_4::rt::comm::msg_fail )
SOL4_EVENT_STC( evt_client_connected,
so_4::rt::comm::msg_client_connected )
SOL4_EVENT_STC( evt_client_disconnected,
so_4::rt::comm::msg_client_disconnected )
SOL4_STATE_START( st_normal )
SOL4_STATE_EVENT( evt_success )
SOL4_STATE_EVENT( evt_fail )
SOL4_STATE_EVENT( evt_client_connected )
SOL4_STATE_EVENT( evt_client_disconnected )
SOL4_STATE_FINISH()
SOL4_CLASS_FINISH()
so_4::transport_layer::connector_controller_auto_ptr_t
make_connector( int argc, char ** argv )
{
so_alt_channel_2::cyclic_connector_controller_auto_ptr_t connector =
so_alt_channel_2::create_cyclic_connector_controller( 15 );
for( int i = 1; i < argc; ++i )
connector->add_connector_controller(
so_4::transport_layer::socket::create_connector_controller(
argv[ i ] ) );
return so_4::transport_layer::connector_controller_auto_ptr_t(
connector );
}
int
main( int argc, char ** argv )
{
if( 2 <= argc )
{
a_handler_t a_handler( "a_handler", "a_channel" );
so_4::disp::active_obj::make_active( a_handler );
so_4::rt::comm::a_sop_outgoing_channel_t a_channel(
"a_channel",
make_connector( argc, argv ),
so_4::sop::create_all_enable_filter(),
so_4::rt::comm::create_def_disconnect_handler( 5000, 1000 ) );
so_4::disp::active_obj::make_active( a_channel );
so_4::rt::agent_t * agents[] =
{
&a_handler
, &a_channel
};
so_4::rt::agent_coop_t coop( "sample_sop",
agents, sizeof( agents ) / sizeof( agents[ 0 ] ) );
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 );
}
else
std::cerr << "sample_sop <ip1> [<ip2> [...]]" << std::endl;
return 0;
}