sample/hello_delay/main.cpp

00001 /*
00002   Пример простейшего агента, который использует собственное
00003   отложенное сообщение.
00004 */
00005 
00006 #include <iostream>
00007 
00008 #include <time.h>
00009 
00010 // Загружаем основные заголовочные файлы SObjectizer.
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 // C++ описание класса агента.
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 // Описание класса агента для SObjectizer-а.
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   // Запускаем SObjectizer Run-Time.
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     // Запустить SObjectizer Run-Time не удалось.
00135     std::cerr << "start: " << rc << std::endl;
00136   }
00137 
00138   return int( rc );
00139 }

Документация по SObjectizer. Последние изменения: Thu Jan 12 10:52:50 2006. Создано системой  doxygen 1.4.6-NO
Hosted by uCoz