#include <iostream>
#include <time.h>
#include <ace/OS.h>
#include <ace/Time_Value.h>
class msg_periodic
:
{};
class state_monitor_t
:
{
const std::string m_type_hint;
public:
state_monitor_t( const std::string & type_hint )
:
m_type_hint( type_hint )
{}
virtual ~state_monitor_t()
{}
virtual void
{
std::cout << m_type_hint << " agent changed state to "
<< std::endl;
}
};
class a_state_swither_t
:
{
public:
:
base_type_t( env ),
m_state_1( self_ptr(), "state_1" ),
m_state_2( self_ptr(), "state_2" ),
m_state_3( self_ptr(), "state_3" ),
m_state_shutdown( self_ptr(), "state_shutdown" ),
m_self_mbox( so_environment().create_local_mbox() )
{}
virtual ~a_state_swither_t()
{}
virtual void
virtual void
void
evt_handler_default(
void
evt_handler_1(
void
evt_handler_2(
void
evt_handler_3(
void
evt_handler_shutdown(
private:
};
void
a_state_swither_t::so_define_agent()
{
so_subscribe( m_self_mbox )
.event( &a_state_swither_t::evt_handler_default );
so_subscribe( m_self_mbox )
.in( m_state_1 )
.event( &a_state_swither_t::evt_handler_1 );
so_subscribe( m_self_mbox )
.in( m_state_2 )
.event( &a_state_swither_t::evt_handler_2 );
so_subscribe( m_self_mbox )
.in( m_state_3 )
.event( &a_state_swither_t::evt_handler_3 );
so_subscribe( m_self_mbox )
.in( m_state_shutdown )
.event( &a_state_swither_t::evt_handler_shutdown );
}
void
a_state_swither_t::so_evt_start()
{
time_t t = time( 0 );
std::cout << asctime( localtime( &t ) )
<< "a_state_swither_t::so_evt_start()" << std::endl;
m_timer_id =
so_environment()
.schedule_timer< msg_periodic >(
m_self_mbox,
1 * 1000,
1 * 1000 );
}
void
a_state_swither_t::evt_handler_default(
{
time_t t = time( 0 );
std::cout << asctime( localtime( &t ) )
<< "evt_handler_default" << std::endl;
so_change_state( m_state_1 );
}
void
a_state_swither_t::evt_handler_1(
{
time_t t = time( 0 );
std::cout << asctime( localtime( &t ) )
<< "evt_handler_1, state: " << so_current_state().query_name()
<< std::endl;
so_change_state( m_state_2 );
}
void
a_state_swither_t::evt_handler_2(
{
time_t t = time( 0 );
std::cout << asctime( localtime( &t ) )
<< "evt_handler_2, state: " << so_current_state().query_name()
<< std::endl;
so_change_state( m_state_3 );
}
void
a_state_swither_t::evt_handler_3(
{
time_t t = time( 0 );
std::cout << asctime( localtime( &t ) )
<< "evt_handler_3, state: " << so_current_state().query_name()
<< std::endl;
so_change_state( m_state_shutdown );
}
void
a_state_swither_t::evt_handler_shutdown(
{
time_t t = time( 0 );
std::cout << asctime( localtime( &t ) )
<< "evt_handler_shutdown, state: "
<< so_current_state().query_name()
<< std::endl;
so_change_state( so_default_state() );
std::cout << "Stop sobjectizer..." << std::endl;
so_environment().stop();
}
state_monitor_t g_state_monitor( "nondestroyable_listener" );
void
{
ag->so_add_nondestroyable_listener( g_state_monitor );
ag->so_add_destroyable_listener(
new state_monitor_t( "destroyable_listener" ) ) );
coop->add_agent( ag );
}
int
main( int, char ** )
{
try
{
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << std::endl;
return 1;
}
return 0;
}