00001
00002
00003
00004
00005
00006 #include <iostream>
00007
00008 #include <time.h>
00009
00010
00011 #include <so_4/rt/h/rt.hpp>
00012 #include <so_4/api/h/api.hpp>
00013
00014
00015 #include <so_4/timer_thread/simple/h/pub.hpp>
00016 #include <so_4/disp/one_thread/h/pub.hpp>
00017
00018
00019 class a_hello_t
00020 : public so_4::rt::agent_t
00021 {
00022
00023 typedef so_4::rt::agent_t base_type_t;
00024 public :
00025 a_hello_t();
00026 virtual ~a_hello_t();
00027
00028 virtual const char *
00029 so_query_type() const;
00030
00031 virtual void
00032 so_on_subscription();
00033
00034
00035 struct msg_hello_time {};
00036
00037
00038 struct msg_quit_time {};
00039
00040
00041 void
00042 evt_start();
00043
00044
00045 void
00046 evt_hello_time();
00047 };
00048
00049
00050 SOL4_CLASS_START( a_hello_t )
00051
00052
00053 SOL4_MSG_START( msg_hello_time, a_hello_t::msg_hello_time )
00054 SOL4_MSG_FINISH()
00055
00056
00057 SOL4_EVENT( evt_start )
00058 SOL4_EVENT( evt_hello_time )
00059
00060
00061 SOL4_STATE_START( st_initial )
00062 SOL4_STATE_EVENT( evt_start )
00063 SOL4_STATE_EVENT( evt_hello_time )
00064 SOL4_STATE_FINISH()
00065
00066 SOL4_CLASS_FINISH()
00067
00068 a_hello_t::a_hello_t()
00069 :
00070 base_type_t( "a_hello" )
00071 {}
00072
00073 a_hello_t::~a_hello_t()
00074 {}
00075
00076 void
00077 a_hello_t::so_on_subscription()
00078 {
00079 so_subscribe( "evt_start",
00080 so_4::rt::sobjectizer_agent_name(), "msg_start" );
00081
00082 so_subscribe( "evt_hello_time", "msg_hello_time" );
00083 }
00084
00085 void
00086 a_hello_t::evt_start()
00087 {
00088
00089 unsigned int sec = 2;
00090 time_t t = time( 0 );
00091 std::cout << so_query_name() << ": "
00092 << asctime( localtime( &t ) )
00093 << "hello after " << sec << " seconds" << std::endl;
00094
00095
00096 so_4::api::send_msg(
00097 so_query_name(),
00098 "msg_hello_time", 0, 0,
00099 sec * 1000 );
00100 }
00101
00102 void
00103 a_hello_t::evt_hello_time()
00104 {
00105 time_t t = time( 0 );
00106 std::cout << so_query_name() << ": "
00107 << asctime( localtime( &t ) ) << std::flush;
00108
00109 std::cout << "Hello, World!" << std::endl;
00110
00111
00112 so_4::api::send_msg(
00113 so_4::rt::sobjectizer_agent_name(),
00114 "msg_normal_shutdown", 0 );
00115 }
00116
00117 int
00118 main()
00119 {
00120
00121 a_hello_t a_hello;
00122
00123 so_4::rt::agent_coop_t a_hello_coop( a_hello );
00124
00125
00126 so_4::ret_code_t rc = so_4::api::start(
00127 so_4::disp::one_thread::create_disp(
00128 so_4::timer_thread::simple::create_timer_thread(),
00129 so_4::auto_destroy_timer ),
00130 so_4::auto_destroy_disp,
00131 &a_hello_coop );
00132 if( rc )
00133 {
00134
00135 std::cerr << "start: " << rc << std::endl;
00136 }
00137
00138 return int( rc );
00139 }