13 #include <nanomsg/pubsub.h> 15 #include <nanomsg/utils/err.h> 16 #include "../../include/mm_mux_demux/mux.hpp" 30 const std::string& url,
37 socket = nn_socket (AF_SP, NN_PUB);
40 std::cout <<
"Mux socket error: " << nn_strerror(errno) <<
" [" << nn_errno() <<
"][" << name <<
"][" << url <<
"]" << std::endl;
42 nn_setsockopt (
socket, NN_SOL_SOCKET, NN_SOCKET_NAME, name.c_str(), name.size());
50 std::cout <<
"Mux bind error: " << nn_strerror(errno) <<
" [" << nn_errno() <<
"][" << name <<
"][" << url <<
"]" << std::endl;
53 std::cout <<
"[" << ecl::TimeStamp() <<
"] MessageMux : [" << name <<
"][" << url <<
"][" <<
socket <<
"][" <<
endpoint_id <<
"]";
55 std::cout <<
"[bind]" << std::endl;
57 std::cout <<
"[connect]" << std::endl;
73 buffer.insert(buffer.end(), msg_buffer.begin(), msg_buffer.end());
76 std::cout <<
"[" << ecl::TimeStamp() <<
"] Mux: [" <<
id <<
"][" << buffer.size() <<
"][";
77 std::cout << std::hex;
78 for(
unsigned int i=0; i < buffer.size(); ++i ) {
79 std::cout << static_cast<unsigned int>(buffer[i]) <<
" ";
81 std::cout << std::dec;
82 std::cout <<
"]" << std::endl;
84 int result = ::nn_send(
socket, buffer.data(), buffer.size(), 0);
108 const std::string&
url,
112 if ( iter == multiplexers().end() ) {
116 std::pair<MuxMapIterator,bool> result;
117 result = multiplexers().insert(
118 MuxMapPair(name, std::make_shared<impl::MessageMux>(name, url, verbosity, bind)));
120 }
else if ( !url.empty() ) {
126 multiplexers().erase(name);
130 multiplexers().clear();
140 if ( iter != multiplexers().end() ) {
141 return (iter->second)->send(
id, msg_buffer);
144 std::cout <<
"Mux : no mux by that name found (while trying to send)"<< std::endl;
static MuxMap & multiplexers()
MessageMux(const std::string &name, const std::string &url, const mm_messages::Verbosity::Level &verbosity=mm_messages::Verbosity::QUIET, const bool bind=true)
std::pair< std::string, std::shared_ptr< impl::MessageMux > > MuxMapPair
int send(const unsigned int &id, const mm_messages::ByteArray &msg_buffer)
static void encode(const T &msg, ByteArray &buffer)
mm_messages::Verbosity::Level verbosity
std::map< std::string, std::shared_ptr< impl::MessageMux > > MuxMap
std::map< std::string, std::shared_ptr< impl::MessageMux > >::const_iterator MuxMapConstIterator
std::vector< unsigned char > ByteArray
std::map< std::string, std::shared_ptr< impl::MessageMux > >::iterator MuxMapIterator
static void start(const std::string &name, const std::string &url, const mm_messages::Verbosity::Level &verbosity=mm_messages::Verbosity::QUIET, const bool bind=true)
Pre-establish named connections.