sample/hello_all/main.cpp

00001 /*
00002   Демострация целенаправленной и широковещательной рассылки сообщений.
00003 */
00004 
00005 #include <iostream>
00006 
00007 #include <so_4/rt/h/rt.hpp>
00008 #include <so_4/api/h/api.hpp>
00009 
00010 #include <so_4/timer_thread/simple/h/pub.hpp>
00011 #include <so_4/disp/one_thread/h/pub.hpp>
00012 
00013 //
00014 // a_msg_owner_t
00015 //
00016 
00017 // Класс агента, единственной задачей которого является
00018 // владение сообщениями.
00019 class a_msg_owner_t :
00020   public so_4::rt::agent_t
00021 {
00022   typedef so_4::rt::agent_t base_type_t;
00023 
00024   public :
00025     a_msg_owner_t()
00026     : base_type_t( agent_name() )
00027     {}
00028 
00029     virtual ~a_msg_owner_t()
00030     {}
00031 
00032     virtual const char *
00033     so_query_type() const;
00034 
00035     virtual void
00036     so_on_subscription()
00037     {}
00038 
00039     static std::string
00040     agent_name()
00041     {
00042       return "a_msg_owner";
00043     }
00044 
00045     // Сообщение, которым будут пользоваться агенты примера.
00046     struct  msg_hello
00047     {
00048       std::string m_sender;
00049 
00050       msg_hello() {}
00051       msg_hello(
00052         const std::string & sender )
00053       : m_sender( sender )
00054       {}
00055 
00056       static bool
00057       check( const msg_hello * msg )
00058       {
00059         return ( 0 != msg &&
00060           msg->m_sender.length() );
00061       }
00062     };
00063 };
00064 
00065 SOL4_CLASS_START( a_msg_owner_t )
00066 
00067   SOL4_MSG_START( msg_hello_to_all, a_msg_owner_t::msg_hello )
00068     SOL4_MSG_FIELD( m_sender )
00069 
00070     SOL4_MSG_CHECKER( a_msg_owner_t::msg_hello::check )
00071   SOL4_MSG_FINISH()
00072 
00073   SOL4_MSG_START( msg_hello_to_you, a_msg_owner_t::msg_hello )
00074     SOL4_MSG_FIELD( m_sender )
00075 
00076     SOL4_MSG_CHECKER( a_msg_owner_t::msg_hello::check )
00077   SOL4_MSG_FINISH()
00078 
00079 SOL4_CLASS_FINISH()
00080 
00081 //
00082 // a_msg_receiver_t
00083 //
00084 
00085 // Класс агента, который получает сообщения.
00086 class a_msg_receiver_t :
00087   public so_4::rt::agent_t
00088 {
00089   typedef so_4::rt::agent_t base_type_t;
00090 
00091   public :
00092     a_msg_receiver_t(
00093       const std::string & agent_name );
00094     virtual ~a_msg_receiver_t();
00095 
00096     virtual const char *
00097     so_query_type() const;
00098 
00099     virtual void
00100     so_on_subscription();
00101 
00102     // Отсылаем всем приветствие от своего имени.
00103     void
00104     evt_start();
00105 
00106     void
00107     evt_hello_to_all(
00108       const a_msg_owner_t::msg_hello & cmd );
00109 
00110     void
00111     evt_hello_to_you(
00112       const a_msg_owner_t::msg_hello & cmd );
00113 };
00114 
00115 SOL4_CLASS_START( a_msg_receiver_t )
00116 
00117   SOL4_EVENT( evt_start )
00118   SOL4_EVENT_STC(
00119     evt_hello_to_all,
00120     a_msg_owner_t::msg_hello )
00121   SOL4_EVENT_STC(
00122     evt_hello_to_you,
00123     a_msg_owner_t::msg_hello )
00124 
00125   SOL4_STATE_START( st_initial )
00126     SOL4_STATE_EVENT( evt_start )
00127     SOL4_STATE_EVENT( evt_hello_to_all )
00128     SOL4_STATE_EVENT( evt_hello_to_you )
00129   SOL4_STATE_FINISH()
00130 
00131 SOL4_CLASS_FINISH()
00132 
00133 //
00134 // a_msg_receiver_t
00135 //
00136 a_msg_receiver_t::a_msg_receiver_t(
00137   const std::string & agent_name )
00138 :
00139   base_type_t( agent_name )
00140 {}
00141 
00142 a_msg_receiver_t::~a_msg_receiver_t()
00143 {}
00144 
00145 void
00146 a_msg_receiver_t::so_on_subscription()
00147 {
00148   so_subscribe( "evt_start",
00149     so_4::rt::sobjectizer_agent_name(),
00150     "msg_start", 1 );
00151 
00152   so_subscribe( "evt_hello_to_all",
00153     a_msg_owner_t::agent_name(),
00154     "msg_hello_to_all", 1 );
00155 
00156   so_subscribe( "evt_hello_to_you",
00157     a_msg_owner_t::agent_name(),
00158     "msg_hello_to_you", 1 );
00159 }
00160 
00161 void
00162 a_msg_receiver_t::evt_start()
00163 {
00164   std::cout << so_query_name() << ".evt_start" << std::endl;
00165 
00166   so_4::api::send_msg_safely(
00167     a_msg_owner_t::agent_name(), "msg_hello_to_all",
00168     new a_msg_owner_t::msg_hello( so_query_name() ) );
00169 }
00170 
00171 void
00172 a_msg_receiver_t::evt_hello_to_all(
00173   const a_msg_owner_t::msg_hello & cmd )
00174 {
00175   std::cout << so_query_name() << ".evt_hello_to_all: "
00176     << cmd.m_sender << std::endl;
00177   // Если привествие слали не мы, то отошлем ответ.
00178   if( so_query_name() != cmd.m_sender )
00179   {
00180     so_4::api::send_msg_safely(
00181       a_msg_owner_t::agent_name(),
00182       "msg_hello_to_you",
00183       new a_msg_owner_t::msg_hello( so_query_name() ),
00184       cmd.m_sender );
00185   }
00186 }
00187 
00188 void
00189 a_msg_receiver_t::evt_hello_to_you(
00190   const a_msg_owner_t::msg_hello & cmd )
00191 {
00192   std::cout << so_query_name() << ".evt_hello_to_you: "
00193     << cmd.m_sender << std::endl;
00194 }
00195 
00196 //
00197 // a_shutdowner_t
00198 //
00199 
00200 // Агент для завершения работы примера.
00201 class a_shutdowner_t :
00202   public so_4::rt::agent_t
00203 {
00204   typedef so_4::rt::agent_t base_type_t;
00205 
00206   public :
00207     a_shutdowner_t();
00208     virtual ~a_shutdowner_t();
00209 
00210     virtual const char *
00211     so_query_type() const;
00212 
00213     virtual void
00214     so_on_subscription();
00215 
00216     static std::string
00217     agent_name();
00218 
00219     void
00220     evt_shutdown();
00221 };
00222 
00223 SOL4_CLASS_START( a_shutdowner_t )
00224 
00225   SOL4_EVENT( evt_shutdown )
00226 
00227   SOL4_STATE_START( st_initial )
00228     SOL4_STATE_EVENT( evt_shutdown )
00229   SOL4_STATE_FINISH()
00230 
00231 SOL4_CLASS_FINISH()
00232 
00233 //
00234 // a_shutdowner_t
00235 //
00236 a_shutdowner_t::a_shutdowner_t() :
00237   base_type_t( agent_name().c_str() )
00238 {
00239 }
00240 
00241 a_shutdowner_t::~a_shutdowner_t() {
00242 }
00243 
00244 void
00245 a_shutdowner_t::so_on_subscription()
00246 {
00247   so_subscribe( "evt_shutdown",
00248     so_4::rt::sobjectizer_agent_name(),
00249     "msg_start" );
00250 }
00251 
00252 std::string
00253 a_shutdowner_t::agent_name()
00254 {
00255   return "a_shutdowner";
00256 }
00257 
00258 void
00259 a_shutdowner_t::evt_shutdown()
00260 {
00261   so_4::api::send_msg( so_4::rt::sobjectizer_agent_name(),
00262     "msg_normal_shutdown" );
00263 }
00264 
00265 
00266 int
00267 main()
00268 {
00269   // Агенты примера.
00270   a_msg_owner_t a_msg_owner;
00271   a_shutdowner_t  a_shutdowner;
00272 
00273   a_msg_receiver_t  a_receiver_one( "a_receiver_one" );
00274   a_msg_receiver_t  a_receiver_two( "a_receiver_two" );
00275   a_msg_receiver_t  a_receiver_three( "a_receiver_three" );
00276 
00277   // Кооперация примера.
00278   so_4::rt::agent_t * g_coop_agents[] = {
00279     &a_msg_owner,
00280     &a_shutdowner,
00281     &a_receiver_one,
00282     &a_receiver_two,
00283     &a_receiver_three
00284   };
00285 
00286   so_4::rt::agent_coop_t  a_coop( "a_coop", g_coop_agents,
00287     sizeof( g_coop_agents ) / sizeof( g_coop_agents[ 0 ] ) );
00288 
00289   so_4::ret_code_t rc = so_4::api::start(
00290     // Диспетчер будет уничтожен при выходе из start().
00291     so_4::disp::one_thread::create_disp(
00292       // Таймер будет уничтожен диспетчером.
00293       so_4::timer_thread::simple::create_timer_thread(),
00294       so_4::auto_destroy_timer ),
00295     so_4::auto_destroy_disp,
00296     &a_coop );
00297   if( rc )
00298   {
00299     std::cerr << "start: " << rc << std::endl;
00300   }
00301 
00302   return int( rc );
00303 }

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