#include <iostream>
#include <set>
#include <ace/OS.h>
#include <ace/Time_Value.h>
class msg_shutdown
:
{
public:
msg_shutdown()
{}
virtual ~msg_shutdown()
{}
};
class event_subscriber_t
{
event_subscriber_t(
const event_subscriber_t & );
void
operator = (
const event_subscriber_t & );
public:
event_subscriber_t(
:
m_state( state )
{
}
virtual ~event_subscriber_t(){}
subscribe(
state() const
{
return m_state;
}
protected:
};
template< class AGENT >
class real_event_subscriber_t
:
public event_subscriber_t
{
public:
typedef void (AGENT::*FN_PTR_T)(
real_event_subscriber_t(
FN_PTR_T pfn,
:
event_subscriber_t( state ),
m_pfn( pfn )
{}
virtual ~real_event_subscriber_t()
{}
subscribe(
{
return subscription_bind
.in( m_state )
.event(
m_pfn,
}
private:
FN_PTR_T m_pfn;
};
class msg_hello_to_all
:
{
public:
msg_hello_to_all(
const std::string & sender,
:
m_sender( sender ),
m_mbox( mbox )
{}
virtual ~msg_hello_to_all()
{}
std::string m_sender;
};
class msg_hello_to_you
:
{
public:
msg_hello_to_you(
const std::string & sender )
:
m_sender( sender )
{}
virtual ~msg_hello_to_you()
{}
std::string m_sender;
};
class shutdowner_layer_t
:
{
public:
virtual ~shutdowner_layer_t()
{
}
{
std::unique_ptr< msg_shutdown > msg( new msg_shutdown );
msg,
m_shutdown_mbox,
3*1000 );
return 0;
}
virtual void
{
std::cout << "shutdowner_layer shutdown()" << std::endl;
}
virtual void
{
std::cout << "shutdowner_layer wait()" << std::endl;
}
template< class AGENT >
void
subscribe(
void (AGENT::*FN_PTR_T)
{
real_event_subscriber_t< AGENT > real_event_subscriber(
FN_PTR_T,
agent_state );
agent_ptr , real_event_subscriber );
if( !rc )
m_subscribers.insert(agent_ptr);
}
void
{
std::set<so_5::rt::agent_t*>::iterator it =
m_subscribers.find(agent_ptr);
if( it != m_subscribers.end() )
m_subscribers.erase(it);
if( m_subscribers.empty() )
{
std::cout << "all agents are unsubscribed\n";
}
}
private:
real_subscribe(
event_subscriber_t & event_subscriber )
{
return event_subscriber.subscribe(
*agent_ptr , m_shutdown_mbox );
}
std::set< so_5::rt::agent_t* > m_subscribers;
};
class a_hello_t
:
{
public:
a_hello_t(
const std::string & agent_name )
:
base_type_t( env ),
m_agent_name( agent_name ),
m_self_mbox( so_environment().create_local_mbox() ),
m_common_mbox( so_environment().create_local_mbox(
so_5::rt::nonempty_name_t( "common_mbox" ) ) )
{}
virtual ~a_hello_t()
{}
virtual void
virtual void
void
evt_hello_to_all(
void
evt_hello_to_you(
void
evt_shutdown(
private:
const std::string m_agent_name;
};
void
a_hello_t::so_define_agent()
{
so_subscribe( m_common_mbox )
.event( &a_hello_t::evt_hello_to_all );
so_subscribe( m_self_mbox )
.event( &a_hello_t::evt_hello_to_you );
so_environment().query_layer< shutdowner_layer_t >()->subscribe(
this ,
so_default_state() ,
&a_hello_t::evt_shutdown );
}
void
a_hello_t::so_evt_start()
{
std::cout << m_agent_name << ".so_evt_start" << std::endl;
new msg_hello_to_all(
m_agent_name,
m_self_mbox ) ) );
}
void
a_hello_t::evt_hello_to_all(
{
std::cout << m_agent_name << ".evt_hello_to_all: "
<< evt_data->m_sender << std::endl;
if( m_agent_name != evt_data->m_sender )
{
new msg_hello_to_you( m_agent_name ) ) );
}
}
void
a_hello_t::evt_hello_to_you(
{
std::cout << m_agent_name << ".evt_hello_to_you: "
<< evt_data->m_sender << std::endl;
}
void
a_hello_t::evt_shutdown(
{
std::cout << m_agent_name << " : preparing to shutdown\n";
so_environment().query_layer< shutdowner_layer_t >()->unsubscribe(
this );
}
void
{
new a_hello_t( env, "alpha" ) ) );
new a_hello_t( env, "beta" ) ) );
new a_hello_t( env, "gamma" ) ) );
ACE_OS::sleep( ACE_Time_Value( 0, 200*1000 ) );
}
int
main( int, char ** )
{
try
{
.add_layer( std::unique_ptr< shutdowner_layer_t > ( new shutdowner_layer_t() ) ) ) ;
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << std::endl;
return 1;
}
return 0;
}