00001
00002
00003
00004
00005
00006 #include <iostream>
00007
00008 #include <so_4/api/h/api.hpp>
00009 #include <so_4/rt/h/rt.hpp>
00010
00011 #include <so_4/timer_thread/simple/h/pub.hpp>
00012 #include <so_4/disp/one_thread/h/pub.hpp>
00013
00014
00015 class my_traits_t
00016 : public so_4::rt::agent_traits_t
00017 {
00018 private :
00019
00020 std::string m_agent;
00021
00022 public :
00023 my_traits_t( const std::string & agent )
00024 : m_agent( agent )
00025 {}
00026 ~my_traits_t()
00027 {
00028
00029
00030 std::cout << "~my_traits_t: " << m_agent << std::endl;
00031 }
00032
00033
00034 virtual void
00035 init( so_4::rt::agent_t & agent )
00036 {
00037 std::cout << "my_traits_t::init(): " << m_agent << std::endl;
00038 }
00039
00040 virtual void
00041 deinit( so_4::rt::agent_t & agent )
00042 {
00043 std::cout << "my_traits_t::deinit(): " << m_agent << std::endl;
00044 }
00045 };
00046
00047
00048
00049 class a_my_t
00050 : public so_4::rt::agent_t
00051 {
00052 typedef so_4::rt::agent_t base_type_t;
00053 public :
00054 a_my_t(
00055 const std::string & self_name )
00056 :
00057 base_type_t( self_name )
00058 {
00059
00060
00061
00062 so_add_destroyable_traits( new my_traits_t( self_name ) );
00063 }
00064 virtual ~a_my_t()
00065 {
00066
00067
00068 std::cout << "~a_my_t: " << so_query_name() << std::endl;
00069 }
00070
00071 virtual const char *
00072 so_query_type() const;
00073
00074 virtual void
00075 so_on_subscription()
00076 {
00077 so_subscribe( "evt_start",
00078 so_4::rt::sobjectizer_agent_name(), "msg_start" );
00079 }
00080
00081
00082 void
00083 evt_start()
00084 {
00085 std::cout << so_query_name() << std::endl;
00086 }
00087 };
00088
00089 SOL4_CLASS_START( a_my_t )
00090
00091 SOL4_EVENT( evt_start )
00092
00093 SOL4_STATE_START( st_normal )
00094 SOL4_STATE_EVENT( evt_start )
00095 SOL4_STATE_FINISH()
00096
00097 SOL4_CLASS_FINISH()
00098
00099
00100 class a_shutdowner_t
00101 : public so_4::rt::agent_t
00102 {
00103 typedef so_4::rt::agent_t base_type_t;
00104 public :
00105 a_shutdowner_t()
00106 : base_type_t( "a_shutdowner" )
00107 {}
00108 virtual ~a_shutdowner_t()
00109 {}
00110
00111 virtual const char *
00112 so_query_type() const;
00113
00114 virtual void
00115 so_on_subscription()
00116 {
00117 so_subscribe( "evt_start",
00118 so_4::rt::sobjectizer_agent_name(), "msg_start" );
00119 }
00120
00121 void
00122 evt_start()
00123 {
00124 so_4::api::send_msg( so_4::rt::sobjectizer_agent_name(),
00125 "msg_normal_shutdown", 0 );
00126 }
00127 };
00128
00129 SOL4_CLASS_START( a_shutdowner_t )
00130
00131 SOL4_EVENT( evt_start )
00132
00133 SOL4_STATE_START( st_normal )
00134 SOL4_STATE_EVENT( evt_start )
00135 SOL4_STATE_FINISH()
00136
00137 SOL4_CLASS_FINISH()
00138
00139 int
00140 main()
00141 {
00142
00143 a_my_t * a_1 = new a_my_t( "a_1" );
00144 a_my_t * a_2 = new a_my_t( "a_2" );
00145 a_shutdowner_t * a_shutdowner = new a_shutdowner_t();
00146
00147 so_4::rt::agent_t * agents[] = { a_1, a_2, a_shutdowner };
00148 so_4::rt::dyn_agent_coop_t * coop = new so_4::rt::dyn_agent_coop_t(
00149 "sample", agents, sizeof( agents ) / sizeof( agents[ 0 ] ) );
00150
00151 so_4::ret_code_t rc = so_4::api::start(
00152
00153 so_4::disp::one_thread::create_disp(
00154
00155 so_4::timer_thread::simple::create_timer_thread(),
00156 so_4::auto_destroy_timer ),
00157 so_4::auto_destroy_disp,
00158 coop );
00159 if( rc )
00160 {
00161 std::cerr << "start: " << rc << std::endl;
00162 }
00163 else
00164 std::cerr << "successful finish" << std::endl;
00165
00166 return int( rc );
00167 }