sample/hello_periodic/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_no_data {};
00036 
00037     // Переодическое сообщение, которое содержит данные.
00038     struct  msg_hello_with_data
00039     {
00040       std::string m_msg;
00041 
00042       msg_hello_with_data() {}
00043       msg_hello_with_data(
00044         const std::string & msg )
00045       : m_msg( msg )
00046       {}
00047 
00048       // Не разрешает ходения сообщения без данных.
00049       static bool
00050       check( const msg_hello_with_data * msg )
00051       {
00052         return ( 0 != msg );
00053       }
00054     };
00055 
00056     // Начало работы агента в системе.
00057     void
00058     evt_start();
00059 
00060     // Реакция на сообщение без данных.
00061     void
00062     evt_hello_no_data();
00063 
00064     // Реакция на сообщение с данными.
00065     void
00066     evt_hello_with_data(
00067       const msg_hello_with_data & cmd );
00068 
00069   private :
00070     // Оставшеся количество циклов до завершения работы.
00071     unsigned int  m_remaining;
00072 };
00073 
00074 // Описание класса агента для SObjectizer-а.
00075 SOL4_CLASS_START( a_hello_t )
00076 
00077   // Описание сообщений.
00078   SOL4_MSG_START( msg_hello_no_data, a_hello_t::msg_hello_no_data )
00079   SOL4_MSG_FINISH()
00080 
00081   SOL4_MSG_START( msg_hello_with_data, a_hello_t::msg_hello_with_data )
00082     SOL4_MSG_CHECKER( a_hello_t::msg_hello_with_data::check )
00083   SOL4_MSG_FINISH()
00084 
00085   // Описание событий.
00086   SOL4_EVENT( evt_start )
00087   SOL4_EVENT( evt_hello_no_data )
00088   SOL4_EVENT_STC(
00089     evt_hello_with_data,
00090     a_hello_t::msg_hello_with_data )
00091 
00092   // Описание единственного состояния.
00093   SOL4_STATE_START( st_initial )
00094     SOL4_STATE_EVENT( evt_start )
00095     SOL4_STATE_EVENT( evt_hello_no_data )
00096     SOL4_STATE_EVENT( evt_hello_with_data )
00097   SOL4_STATE_FINISH()
00098 
00099 SOL4_CLASS_FINISH()
00100 
00101 a_hello_t::a_hello_t()
00102 :
00103   base_type_t( "a_hello" )
00104 , m_remaining( 5 )
00105 {}
00106 
00107 a_hello_t::~a_hello_t()
00108 {}
00109 
00110 void
00111 a_hello_t::so_on_subscription()
00112 {
00113   so_subscribe( "evt_start",
00114     so_4::rt::sobjectizer_agent_name(), "msg_start" );
00115 
00116   so_subscribe( "evt_hello_no_data", "msg_hello_no_data" );
00117   so_subscribe( "evt_hello_with_data", "msg_hello_with_data" );
00118 }
00119 
00120 void
00121 a_hello_t::evt_start()
00122 {
00123   unsigned int no_data_delay = 2;
00124   unsigned int no_data_period = 1;
00125   unsigned int with_data_delay = 0;
00126   unsigned int with_data_period = 2;
00127 
00128   time_t t = time( 0 );
00129 
00130   // Сообщаем, что и когда будет происходить.
00131   std::cout << so_query_name() << ".evt_start: "
00132     << asctime( localtime( &t ) )
00133     << "\thello_no_data delay: " << no_data_delay
00134     << "\n\thello_no_data period: " << no_data_period
00135     << "\n\thello_with_data delay: " << with_data_delay
00136     << "\n\thello_with_data period: " << with_data_period
00137     << std::endl;
00138 
00139   // Запускаем переодические сообщения.
00140   so_4::api::send_msg_safely(
00141     so_query_name(),
00142     "msg_hello_with_data",
00143     new msg_hello_with_data(
00144       std::string( "Sample started at " ) +
00145       asctime( localtime( &t ) ) ),
00146     "",
00147     with_data_delay * 1000, with_data_period * 1000 );
00148 
00149   so_4::api::send_msg(
00150     so_query_name(), "msg_hello_no_data", 0, 0,
00151     no_data_delay * 1000, no_data_period * 1000 );
00152 }
00153 
00154 void
00155 a_hello_t::evt_hello_no_data()
00156 {
00157   --m_remaining;
00158 
00159   time_t t = time( 0 );
00160   std::cout << so_query_name() << ".evt_hello_no_data: "
00161     << asctime( localtime( &t ) )
00162     << "\tremaining: " << m_remaining << std::endl;
00163 
00164   if( !m_remaining )
00165     // Отсылаем сообщение для завершения работы.
00166     so_4::api::send_msg(
00167       so_4::rt::sobjectizer_agent_name(),
00168       "msg_normal_shutdown", 0 );
00169 }
00170 
00171 void
00172 a_hello_t::evt_hello_with_data(
00173   const msg_hello_with_data & cmd )
00174 {
00175   time_t t = time( 0 );
00176   std::cout << so_query_name() << ".evt_hello_with_data: "
00177     << asctime( localtime( &t ) )
00178     << "\t" << cmd.m_msg << std::endl;
00179 }
00180 
00181 int
00182 main()
00183 {
00184   // Наш агент.
00185   a_hello_t a_hello;
00186   // И кооперация для него.
00187   so_4::rt::agent_coop_t  a_hello_coop( a_hello );
00188 
00189   // Запускаем SObjectizer Run-Time.
00190   so_4::ret_code_t rc = so_4::api::start(
00191     so_4::disp::one_thread::create_disp(
00192       so_4::timer_thread::simple::create_timer_thread(),
00193       so_4::auto_destroy_timer ),
00194     so_4::auto_destroy_disp,
00195     &a_hello_coop );
00196   if( rc )
00197   {
00198     // Запустить SObjectizer Run-Time не удалось.
00199     std::cerr << "start: " << rc << std::endl;
00200   }
00201 
00202   return int( rc );
00203 }

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