From 930167da8d6a1e8c3c492267d1d51c94e4cd476c Mon Sep 17 00:00:00 2001 From: Viktor UZLOV Date: Tue, 2 Nov 2021 15:09:53 +0300 Subject: [PATCH] Add GetIpByHostname() for DataZMQ --- src/Basics/Basics_Utils.cxx | 10 ++++++++++ src/Basics/Basics_Utils.hxx | 1 + src/SalomeZeroMQ/CMakeLists.txt | 1 + src/SalomeZeroMQ/SalomeZeroMQ.cxx | 11 ++++++----- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Basics/Basics_Utils.cxx b/src/Basics/Basics_Utils.cxx index e421e6062..dd6fc7add 100644 --- a/src/Basics/Basics_Utils.cxx +++ b/src/Basics/Basics_Utils.cxx @@ -32,6 +32,8 @@ #include #endif +#include +#include #include #include @@ -83,6 +85,14 @@ namespace Kernel_Utils return p; } + std::string GetIpByHostname() { + std::string host = GetHostname(); + hostent* hostname = gethostbyname(host.c_str()); + if(hostname) + return std::string(inet_ntoa(**(in_addr**)hostname->h_addr_list)); + return {}; + } + Localizer::Localizer() { init(LC_NUMERIC, "C"); diff --git a/src/Basics/Basics_Utils.hxx b/src/Basics/Basics_Utils.hxx index 1f3e45125..6caa60974 100644 --- a/src/Basics/Basics_Utils.hxx +++ b/src/Basics/Basics_Utils.hxx @@ -44,6 +44,7 @@ namespace Kernel_Utils { BASICS_EXPORT std::string GetHostname(); + BASICS_EXPORT std::string GetIpByHostname(); class BASICS_EXPORT Localizer { diff --git a/src/SalomeZeroMQ/CMakeLists.txt b/src/SalomeZeroMQ/CMakeLists.txt index ee41f00d3..602ef261a 100644 --- a/src/SalomeZeroMQ/CMakeLists.txt +++ b/src/SalomeZeroMQ/CMakeLists.txt @@ -22,6 +22,7 @@ INCLUDE_DIRECTORIES( ${ZeroMQ_INCLUDE_DIR} ${PYTHON_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/../Basics ) # libraries to link to diff --git a/src/SalomeZeroMQ/SalomeZeroMQ.cxx b/src/SalomeZeroMQ/SalomeZeroMQ.cxx index efc0c6f3a..b7b5313bf 100644 --- a/src/SalomeZeroMQ/SalomeZeroMQ.cxx +++ b/src/SalomeZeroMQ/SalomeZeroMQ.cxx @@ -21,11 +21,12 @@ #include #include "SalomeZeroMQ.hxx" +#include "Basics_Utils.hxx" DataZMQ::DataZMQ() { this->cont = zmq::context_t(1); - this->addr = "inproc://hello"; + this->addr = "tcp://" + Kernel_Utils::GetIpByHostname() + ":*"; this->sock = zmq::socket_t(this->cont, zmq::socket_type::pair); } @@ -34,14 +35,14 @@ void DataZMQ::connectDataZMQ() char port[1024]; size_t size = sizeof(port); try{ - this->sock.bind("tcp://*:*"); + this->sock.bind(this->addr); + sock.getsockopt( ZMQ_LAST_ENDPOINT, &port, &size ); + std::cout << "socket is bound at port " << port << std::endl; + this->addr = std::string(port); } catch (zmq::error_t&e ){ std::cerr << "couldn't bind to socket: " << e.what(); } - sock.getsockopt( ZMQ_LAST_ENDPOINT, &port, &size ); - std::cout << "socket is bound at port " << port << std::endl; - this->addr = std::string(port); } void DataZMQ::sendDataZMQ(const char* data, int len) -- 2.39.2