sample/inheritance/main.cpp
#include <iostream>
#include <memory>
#include <ace/OS_main.h>
#include <so_4/rt/h/rt.hpp>
#include <so_4/api/h/api.hpp>
#include <so_4/timer_thread/simple/h/pub.hpp>
#include <so_4/disp/one_thread/h/pub.hpp>
class a_trx_t :
public virtual so_4::rt::agent_t
{
typedef so_4::rt::agent_t agent_type_t;
public :
a_trx_t();
virtual ~a_trx_t();
virtual const char *
so_query_type() const;
virtual void
so_on_subscription();
struct msg_start {};
struct msg_commit {};
struct msg_rollback {};
protected :
void
do_subscription();
};
SOL4_CLASS_START( a_trx_t )
SOL4_MSG_START( msg_start, a_trx_t::msg_start )
SOL4_MSG_FINISH()
SOL4_MSG_START( msg_commit, a_trx_t::msg_commit )
SOL4_MSG_FINISH()
SOL4_MSG_START( msg_rollback, a_trx_t::msg_rollback )
SOL4_MSG_FINISH()
SOL4_CLASS_FINISH()
a_trx_t::a_trx_t()
:
agent_type_t( "a_trx" )
{
}
a_trx_t::~a_trx_t()
{
}
void
a_trx_t::so_on_subscription()
{
do_subscription();
}
void
a_trx_t::do_subscription()
{
}
class a_concrete_trx_t :
public virtual a_trx_t
{
typedef so_4::rt::agent_t agent_type_t;
public :
a_concrete_trx_t(
unsigned int commit_timeout,
unsigned int rollback_timeout );
virtual ~a_concrete_trx_t();
virtual const char *
so_query_type() const;
virtual void
so_on_subscription();
void
evt_start();
void
evt_trx_start();
void
evt_trx_commit();
void
evt_trx_rollback();
protected :
void
do_subscription();
private :
unsigned int m_commit_timeout;
unsigned int m_rollback_timeout;
};
SOL4_CLASS_START( a_concrete_trx_t )
SOL4_SUPER_CLASS( a_trx_t )
SOL4_INITIAL_STATE( st_not_started )
SOL4_EVENT( evt_start )
SOL4_EVENT( evt_trx_start )
SOL4_EVENT( evt_trx_commit )
SOL4_EVENT( evt_trx_rollback )
SOL4_STATE_START( st_not_started )
SOL4_STATE_EVENT( evt_start )
SOL4_STATE_EVENT( evt_trx_start )
SOL4_STATE_FINISH()
SOL4_STATE_START( st_started )
SOL4_STATE_EVENT( evt_trx_commit )
SOL4_STATE_EVENT( evt_trx_rollback )
SOL4_STATE_FINISH()
SOL4_STATE_START( st_commited )
SOL4_STATE_FINISH()
SOL4_CLASS_FINISH()
a_concrete_trx_t::a_concrete_trx_t(
unsigned int commit_timeout,
unsigned int rollback_timeout )
:
agent_type_t( "a_concrete_trx" ),
m_commit_timeout( commit_timeout ),
m_rollback_timeout( rollback_timeout )
{
std::cout << "commit_timeout: " << commit_timeout
<< "\nrollback_timeout: " << rollback_timeout
<< std::endl;
}
a_concrete_trx_t::~a_concrete_trx_t()
{
}
void
a_concrete_trx_t::so_on_subscription()
{
a_trx_t::do_subscription();
do_subscription();
}
void
a_concrete_trx_t::evt_start()
{
so_4::api::send_msg( so_query_name(), "msg_start" );
so_4::api::send_msg( so_query_name(), "msg_commit", 0,
so_query_name(), m_commit_timeout );
so_4::api::send_msg( so_query_name(), "msg_rollback", 0,
so_query_name(), m_rollback_timeout );
}
void
a_concrete_trx_t::evt_trx_start()
{
so_change_state( "st_started" );
std::cout << "trx started" << std::endl;
}
void
a_concrete_trx_t::evt_trx_commit()
{
so_change_state( "st_commited" );
std::cout << "trx commited" << std::endl;
so_4::api::send_msg(
so_4::rt::sobjectizer_agent_name(),
"msg_normal_shutdown" );
}
void
a_concrete_trx_t::evt_trx_rollback()
{
so_change_state( "st_not_started" );
std::cout << "trx rolled back" << std::endl;
so_4::api::send_msg(
so_4::rt::sobjectizer_agent_name(),
"msg_normal_shutdown" );
}
void
a_concrete_trx_t::do_subscription()
{
so_subscribe( "evt_start",
so_4::rt::sobjectizer_agent_name(), "msg_start" );
so_subscribe( "evt_trx_start", "msg_start" );
so_subscribe( "evt_trx_commit", "msg_commit" );
so_subscribe( "evt_trx_rollback", "msg_rollback" );
}
class a_timed_trx_t :
public virtual a_trx_t
{
typedef so_4::rt::agent_t agent_type_t;
public :
a_timed_trx_t(
unsigned int lifetime );
virtual ~a_timed_trx_t();
virtual const char *
so_query_type() const;
virtual void
so_on_subscription();
struct msg_lifetime_left {};
void
evt_lifetime_left(
const msg_lifetime_left * );
void
on_enter_appropriate_state(
const std::string & );
protected :
void
do_subscription();
private :
unsigned int m_lifetime;
};
SOL4_CLASS_START( a_timed_trx_t )
SOL4_SUPER_CLASS( a_trx_t )
SOL4_MSG_START( msg_lifetime_left,
a_timed_trx_t::msg_lifetime_left )
SOL4_MSG_FINISH()
SOL4_EVENT_STC( evt_lifetime_left,
a_timed_trx_t::msg_lifetime_left )
SOL4_CLASS_FINISH()
a_timed_trx_t::a_timed_trx_t(
unsigned int lifetime )
:
agent_type_t( "a_timed_trx" ),
m_lifetime( lifetime )
{
std::cout << "lifetime: " << lifetime
<< std::endl;
}
a_timed_trx_t::~a_timed_trx_t()
{
}
void
a_timed_trx_t::so_on_subscription()
{
a_trx_t::do_subscription();
do_subscription();
}
void
a_timed_trx_t::evt_lifetime_left(
const msg_lifetime_left * )
{
so_4::api::send_msg( so_query_name(), "msg_rollback" );
std::cout << "no time left" << std::endl;
}
void
a_timed_trx_t::on_enter_appropriate_state(
const std::string & )
{
so_4::api::send_msg( so_query_name(), "msg_lifetime_left", 0,
so_query_name(), m_lifetime );
}
void
a_timed_trx_t::do_subscription()
{
so_subscribe( "evt_lifetime_left", "msg_lifetime_left" );
}
class a_concrete_timed_trx_t :
public virtual a_concrete_trx_t,
public virtual a_timed_trx_t
{
typedef so_4::rt::agent_t agent_type_t;
public :
a_concrete_timed_trx_t(
unsigned int commit_timeout,
unsigned int rollback_timeout,
unsigned int lifetime );
virtual ~a_concrete_timed_trx_t();
virtual const char *
so_query_type() const;
virtual void
so_on_subscription();
protected :
void
do_subscription();
};
SOL4_CLASS_START( a_concrete_timed_trx_t )
SOL4_SUPER_CLASS( a_concrete_trx_t )
SOL4_SUPER_CLASS( a_timed_trx_t )
SOL4_INITIAL_STATE( st_not_started )
SOL4_STATE_START( st_started )
SOL4_STATE_MERGE( a_concrete_trx_t, st_started )
SOL4_STATE_EVENT( evt_lifetime_left )
SOL4_STATE_ON_ENTER( on_enter_appropriate_state )
SOL4_STATE_FINISH()
SOL4_CLASS_FINISH()
a_concrete_timed_trx_t::a_concrete_timed_trx_t(
unsigned int commit_timeout,
unsigned int rollback_timeout,
unsigned int lifetime )
:
agent_type_t( "a_concrete_timed_trx" ),
a_concrete_trx_t( commit_timeout, rollback_timeout ),
a_timed_trx_t( lifetime )
{
}
a_concrete_timed_trx_t::~a_concrete_timed_trx_t()
{
}
void
a_concrete_timed_trx_t::so_on_subscription()
{
a_trx_t::do_subscription();
a_concrete_trx_t::do_subscription();
a_timed_trx_t::do_subscription();
do_subscription();
}
void
a_concrete_timed_trx_t::do_subscription()
{
}
int
main( int, char ** )
{
std::auto_ptr< so_4::timer_thread::timer_thread_t >
timer_ptr( so_4::timer_thread::simple::create_timer_thread() );
std::auto_ptr< so_4::rt::dispatcher_t >
disp_ptr( so_4::disp::one_thread::create_disp( *timer_ptr ) );
a_concrete_timed_trx_t agent( 2500, 2000, 1500 );
so_4::rt::agent_coop_t coop( agent );
so_4::ret_code_t rc = so_4::api::start( *disp_ptr, &coop );
if( rc ) {
std::cerr << "start: " << rc << std::endl;
}
return int( rc );
}