00001
00002
00003
00004
00005
00006
00007
00008 #include <iostream>
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
00020 #include <so_4/rt/comm/h/a_raw_srv_channel.hpp>
00021 #include <so_4/socket/channels/h/channels.hpp>
00022
00023
00024
00025
00026 class a_main_t :
00027 public so_4::rt::agent_t
00028 {
00029 typedef so_4::rt::agent_t base_type_t;
00030
00031 public :
00032 a_main_t();
00033 virtual ~a_main_t();
00034
00035 virtual const char *
00036 so_query_type() const;
00037
00038 virtual void
00039 so_on_subscription();
00040
00041
00042
00043 static std::string &
00044 agent_name();
00045
00046
00047
00048 static std::string &
00049 tcp_agent_name();
00050
00051
00052 void
00053 evt_success(
00054 const so_4::rt::comm::a_srv_channel_base_t::msg_success & );
00055
00056
00057 void
00058 evt_fail(
00059 const so_4::rt::comm::a_srv_channel_base_t::msg_fail & );
00060
00061
00062 void
00063 evt_client_connected(
00064 const so_4::rt::comm::msg_client_connected & cmd );
00065
00066
00067 void
00068 evt_client_disconnected(
00069 const so_4::rt::comm::msg_client_disconnected & cmd );
00070
00071
00072 void
00073 evt_incoming_data(
00074 const so_4::rt::comm::msg_raw_package & cmd );
00075
00076 };
00077
00078 SOL4_CLASS_START( a_main_t )
00079
00080 SOL4_EVENT_STC(
00081 evt_success,
00082 so_4::rt::comm::a_srv_channel_base_t::msg_success )
00083 SOL4_EVENT_STC(
00084 evt_fail,
00085 so_4::rt::comm::a_srv_channel_base_t::msg_fail )
00086 SOL4_EVENT_STC(
00087 evt_client_connected,
00088 so_4::rt::comm::msg_client_connected )
00089 SOL4_EVENT_STC(
00090 evt_client_disconnected,
00091 so_4::rt::comm::msg_client_disconnected )
00092 SOL4_EVENT_STC(
00093 evt_incoming_data,
00094 so_4::rt::comm::msg_raw_package )
00095
00096 SOL4_STATE_START( st_normal )
00097 SOL4_STATE_EVENT( evt_success )
00098 SOL4_STATE_EVENT( evt_fail )
00099 SOL4_STATE_EVENT( evt_client_connected )
00100 SOL4_STATE_EVENT( evt_client_disconnected )
00101 SOL4_STATE_EVENT( evt_incoming_data )
00102 SOL4_STATE_FINISH()
00103
00104 SOL4_CLASS_FINISH()
00105
00106 a_main_t::a_main_t()
00107 :
00108 base_type_t( agent_name().c_str() )
00109 {
00110 }
00111
00112 a_main_t::~a_main_t()
00113 {
00114 }
00115
00116 void
00117 a_main_t::so_on_subscription()
00118 {
00119 so_subscribe( "evt_success", tcp_agent_name(), "msg_success" );
00120
00121 so_subscribe( "evt_fail", tcp_agent_name(), "msg_fail" );
00122
00123 so_subscribe( "evt_client_connected", tcp_agent_name(),
00124 "msg_client_connected" );
00125
00126 so_subscribe( "evt_client_disconnected", tcp_agent_name(),
00127 "msg_client_disconnected" );
00128
00129 so_subscribe( "evt_incoming_data", tcp_agent_name(),
00130 "msg_raw_package" );
00131 }
00132
00133 std::string &
00134 a_main_t::agent_name()
00135 {
00136 static std::string name( "a_main" );
00137
00138 return name;
00139 }
00140
00141 std::string &
00142 a_main_t::tcp_agent_name()
00143 {
00144 static std::string name( "a_tcp_srvsock" );
00145
00146 return name;
00147 }
00148
00149 void
00150 a_main_t::evt_success(
00151 const so_4::rt::comm::a_srv_channel_base_t::msg_success & )
00152 {
00153 std::cout << so_query_name() << ".evt_success" << std::endl;
00154 }
00155
00156 void
00157 a_main_t::evt_fail(
00158 const so_4::rt::comm::a_srv_channel_base_t::msg_fail & cmd )
00159 {
00160 std::cout << so_query_name() << ".evt_fail: "
00161 << cmd.m_ret_code << std::endl;
00162 }
00163
00164 void
00165 a_main_t::evt_client_connected(
00166 const so_4::rt::comm::msg_client_connected & cmd )
00167 {
00168 std::cout << so_query_name() << ".evt_client_connected: "
00169 << cmd.m_channel.comm_agent() << ", "
00170 << cmd.m_channel.client()
00171 << std::endl;
00172 }
00173
00174 void
00175 a_main_t::evt_client_disconnected(
00176 const so_4::rt::comm::msg_client_disconnected & cmd )
00177 {
00178 std::cout << so_query_name() << ".evt_client_disconnected: "
00179 << cmd.m_channel.comm_agent() << ", "
00180 << cmd.m_channel.client()
00181 << std::endl;
00182 }
00183
00184 void
00185 a_main_t::evt_incoming_data(
00186 const so_4::rt::comm::msg_raw_package & cmd )
00187 {
00188 std::cout << so_query_name() << ".evt_incoming_data: "
00189 << cmd.m_channel.comm_agent() << ", "
00190 << cmd.m_channel.client()
00191 << "\n\tdata size: " << cmd.m_package.size() << std::endl;
00192
00193 std::string v(
00194 (const char *)cmd.m_package.ptr(),
00195 cmd.m_package.size() );
00196 std::cout << v << std::endl;
00197
00198
00199 cmd.unblock_channel();
00200 }
00201
00202
00203 so_4::rt::agent_coop_t *
00204 create_coop( const char * ip_address )
00205 {
00206 a_main_t * a_main = new a_main_t();
00207 so_4::rt::comm::a_raw_srv_channel_t * a_tcp_srvsock =
00208 new so_4::rt::comm::a_raw_srv_channel_t(
00209 a_main_t::tcp_agent_name(),
00210 so_4::socket::channels::
00211 create_server_channel( ip_address ) );
00212
00213 so_4::rt::agent_t * agents[] = {
00214 a_main, a_tcp_srvsock
00215 };
00216
00217 return new so_4::rt::dyn_agent_coop_t( "srvsock1",
00218 agents, sizeof( agents ) / sizeof( agents[ 0 ] ) );
00219 }
00220
00221 int
00222 main( int argc, char ** argv )
00223 {
00224 if( 2 == argc ) {
00225
00226
00227 so_4::ret_code_t rc = so_4::api::start(
00228
00229 so_4::disp::one_thread::create_disp(
00230
00231 so_4::timer_thread::simple::create_timer_thread(),
00232 so_4::auto_destroy_timer ),
00233 so_4::auto_destroy_disp,
00234 create_coop( argv[ 1 ] ) );
00235 if( rc )
00236 {
00237
00238 std::cerr << "start: " << rc << std::endl;
00239 }
00240
00241 return rc;
00242 }
00243 else
00244 std::cerr << "sample_raw_channel_tcp_srv <ip-address>" << std::endl;
00245
00246 return 0;
00247 }