#include <iostream>
#include <algorithm>
#include <string>
#include <oess_2/defs/h/types.hpp>
#include <oess_2/stdsn/h/serializable.hpp>
#include <oess_2/stdsn/h/ent_std.hpp>
#include <oess_2/stdsn/h/inout_templ.hpp>
#include <oess_2/stdsn/h/shptr.hpp>
namespace sample_mbapi_4
{
struct sample_message_t
:
public oess_2::stdsn::serializable_t
{
OESS_SERIALIZER( sample_message_t )
public:
std::string m_message;
oess_2::uint_t m_counter;
};
std::string
hex_digit( unsigned char c )
{
static const std::string hex_table = "0123456789abcdef";
return hex_table.substr( c >> 4, 1 ) + hex_table.substr( c & 0xF, 1 );
}
std::string
create_hex_dump(
const std::string & bin )
{
std::string res;
std::for_each(
bin.begin(),
bin.end(),
[&]( unsigned char c ){ res += hex_digit( c ); } );
return res;
}
class a_main_t
:
{
public:
:
base_type_t( env )
{}
virtual ~a_main_t()
{}
virtual void
so_define_agent();
virtual void
so_evt_start()
{
std::cout << "so_evt_start()\n\n";
std::unique_ptr< sample_message_t > new_msg(
new sample_message_t );
new_msg->m_counter = 5;
new_msg->m_message = "Hello!";
m_ep_b->send( m_ep_a->endpoint(), new_msg );
}
void
evt_sample_message(
{
std::cout << "\nevt_sample_message:\n"
" from: " << msg->from().name() << "\n"
" to: " << msg->to().name() << "\n"
" message: \"" << msg->msg().m_message << "\"\n"
" counter: " << msg->msg().m_counter << "\n";
if( 0 == msg->msg().m_counter )
so_environment().stop();
else
{
std::unique_ptr< sample_message_t > new_msg(
new sample_message_t );
new_msg->m_counter = msg->msg().m_counter - 1;
new_msg->m_message = msg->msg().m_message + " Hello!";
m_ep_a->send( m_ep_b->endpoint(), new_msg );
}
}
void
evt_sample_binary_message(
{
std::cout << "\nevt_sample_binary_message:\n"
" from: " << msg->from().name() << "\n"
" to: " << msg->to().name() << "\n"
" bin: " << create_hex_dump( msg->m_bin_message ) << "\n";
m_ep_b->send_binary_message(
m_ep_a->endpoint(),
msg->m_oess_id_wrapper,
msg->m_bin_message );
}
private:
};
void
a_main_t::so_define_agent()
{
m_ep_a = so_environment()
->create_endpoint_bind(
*this );
m_ep_b = so_environment()
->create_endpoint_bind(
*this );
m_ep_a->subscribe_event(
so_default_state(),
&a_main_t::evt_sample_message );
m_ep_b->subscribe_event(
so_default_state(),
&a_main_t::evt_sample_binary_message );
}
void
{
std::cout << "1 init()\n\n";
new a_main_t( env ) ) );
std::cout << "2 init()\n\n";
}
}
#include "messages.ddl.cpp"
int
main( int, char ** )
{
try
{
&sample_mbapi_4::init,
.add_layer(
std::unique_ptr< mbapi_4::mbapi_layer_t >(
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << std::endl;
return 1;
}
return 0;
}