00001
00002
00003
00004
00005
00006
00007
00008 #include <iostream>
00009
00010 #include <stdio.h>
00011
00012 #include <so_4/api/h/api.hpp>
00013 #include <so_4/rt/h/rt.hpp>
00014
00015 #include <so_4/rt/comm/h/a_srv_channel.hpp>
00016 #include <so_4/socket/channels/h/channels.hpp>
00017
00018 #include <so_4/timer_thread/simple/h/pub.hpp>
00019 #include <so_4/disp/one_thread/h/pub.hpp>
00020
00021 #include "common.cpp"
00022
00023
00024 class a_receiver_t
00025 : public so_4::rt::agent_t
00026 {
00027 typedef so_4::rt::agent_t base_type_t;
00028 public :
00029 a_receiver_t()
00030 :
00031 base_type_t( "a_receiver" )
00032 {}
00033 virtual ~a_receiver_t()
00034 {}
00035
00036 virtual const char *
00037 so_query_type() const;
00038
00039 virtual void
00040 so_on_subscription()
00041 {
00042
00043 so_4::api::make_global_agent(
00044 a_common_t::agent_name(),
00045 a_common_t::agent_type() );
00046
00047 so_subscribe( "evt_request",
00048 a_common_t::agent_name(),
00049 "msg_request" );
00050
00051 so_subscribe( "evt_client_connected",
00052 "a_channel", "msg_client_connected" );
00053
00054 so_subscribe( "evt_client_disconnected",
00055 "a_channel", "msg_client_disconnected" );
00056 }
00057
00058 void
00059 evt_request(
00060 const so_4::rt::event_data_t & data,
00061 const a_common_t::msg_request * cmd )
00062 {
00063 std::cout << cmd->m_uid << " ";
00064
00065 so_4::api::send_msg_safely(
00066 data.channel(),
00067 a_common_t::agent_name(),
00068 "msg_reply",
00069 new a_common_t::msg_reply( cmd->m_uid ) );
00070 }
00071
00072 void
00073 evt_client_connected(
00074 const so_4::rt::event_data_t & data,
00075 const so_4::rt::comm::msg_client_connected * cmd )
00076 {
00077 std::cout << "\nclient connected: " << cmd->m_channel
00078 << std::endl;
00079 }
00080
00081 void
00082 evt_client_disconnected(
00083 const so_4::rt::event_data_t & data,
00084 const so_4::rt::comm::msg_client_disconnected * cmd )
00085 {
00086 std::cout << "\nclient disconnected: " << cmd->m_channel
00087 << std::endl;
00088 }
00089 };
00090
00091 SOL4_CLASS_START( a_receiver_t )
00092
00093 SOL4_EVENT_WITH_INCIDENT_TYPE(
00094 evt_request,
00095 a_common_t::msg_request )
00096 SOL4_EVENT_WITH_INCIDENT_TYPE(
00097 evt_client_connected,
00098 so_4::rt::comm::msg_client_connected )
00099 SOL4_EVENT_WITH_INCIDENT_TYPE(
00100 evt_client_disconnected,
00101 so_4::rt::comm::msg_client_disconnected )
00102
00103 SOL4_STATE_START( st_normal )
00104 SOL4_STATE_EVENT( evt_request )
00105 SOL4_STATE_EVENT( evt_client_connected )
00106 SOL4_STATE_EVENT( evt_client_disconnected )
00107 SOL4_STATE_FINISH()
00108
00109 SOL4_CLASS_FINISH()
00110
00111 int
00112 main( int argc, char ** argv )
00113 {
00114 if( 2 == argc )
00115 {
00116 so_4::rt::comm::a_srv_channel_t a_channel(
00117 "a_channel",
00118 so_4::socket::channels::create_server_channel( argv[ 1 ] ) );
00119 std::cout << "Server thresholds: in " << a_channel.in_threshold()
00120 << ", out " << a_channel.out_threshold()
00121 << std::endl;
00122 a_receiver_t a_receiver;
00123
00124 so_4::rt::agent_t * agents[] =
00125 {
00126 &a_channel, &a_receiver
00127 };
00128 so_4::rt::agent_coop_t coop( "server",
00129 agents, sizeof( agents ) / sizeof( agents[ 0 ] ) );
00130
00131 so_4::ret_code_t rc = so_4::api::start(
00132
00133 so_4::disp::one_thread::create_disp(
00134
00135 so_4::timer_thread::simple::create_timer_thread(),
00136 so_4::auto_destroy_timer ),
00137 so_4::auto_destroy_disp,
00138 &coop );
00139
00140 if( rc )
00141 {
00142 std::cerr << "start: " << rc << std::endl;
00143 }
00144
00145 return int( rc );
00146 }
00147
00148 std::cerr << "sample_high_traffic_server <[ip]:port>" << std::endl;
00149 return 1;
00150 }