mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-13 08:41:16 +00:00
Add resolver for hostname -> ip, use it before bind/connect
This commit is contained in:
parent
166e537d9f
commit
86cced792b
|
@ -12,12 +12,12 @@
|
|||
* @author A. Rybalchenko
|
||||
*/
|
||||
|
||||
#include <set>
|
||||
#include <utility> // std::move
|
||||
#include "FairMQChannel.h"
|
||||
|
||||
#include <boost/algorithm/string.hpp> // join/split
|
||||
|
||||
#include "FairMQChannel.h"
|
||||
#include <set>
|
||||
#include <utility> // std::move
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
|
|
@ -302,15 +302,32 @@ bool FairMQDevice::AttachChannel(FairMQChannel& ch)
|
|||
address = endpoint.substr(1);
|
||||
}
|
||||
|
||||
bool rc = true;
|
||||
if (address.compare(0, 6, "tcp://") == 0)
|
||||
{
|
||||
string addressString = address.substr(6);
|
||||
auto pos = addressString.find(":");
|
||||
string hostPart = addressString.substr(0, pos);
|
||||
if (!(bind && hostPart == "*"))
|
||||
{
|
||||
string portPart = addressString.substr(pos + 1);
|
||||
string resolvedHost = fair::mq::tools::getIpFromHostname(hostPart);
|
||||
if (resolvedHost == "")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
address.assign("tcp://" + resolvedHost + ":" + portPart);
|
||||
}
|
||||
}
|
||||
|
||||
bool success = true;
|
||||
// make the connection
|
||||
if (bind)
|
||||
{
|
||||
rc = BindEndpoint(*ch.fSocket, address);
|
||||
success = BindEndpoint(*ch.fSocket, address);
|
||||
}
|
||||
else
|
||||
{
|
||||
rc = ConnectEndpoint(*ch.fSocket, address);
|
||||
success = ConnectEndpoint(*ch.fSocket, address);
|
||||
}
|
||||
|
||||
// bind might bind to an address different than requested,
|
||||
|
@ -325,9 +342,9 @@ bool FairMQDevice::AttachChannel(FairMQChannel& ch)
|
|||
LOG(debug) << "Attached channel " << ch.fName << " to " << endpoint << (bind ? " (bind) " : " (connect) ");
|
||||
|
||||
// after the book keeping is done, exit in case of errors
|
||||
if (!rc)
|
||||
if (!success)
|
||||
{
|
||||
return rc;
|
||||
return success;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -530,8 +530,8 @@ class FairMQDevice : public FairMQStateMachine
|
|||
bool fExternalConfig;
|
||||
|
||||
const fair::mq::tools::Version fVersion;
|
||||
float fRate;
|
||||
size_t fLastTime;
|
||||
float fRate; ///< Rate limiting for ConditionalRun
|
||||
size_t fLastTime; ///< Rate limiting for ConditionalRun
|
||||
};
|
||||
|
||||
#endif /* FAIRMQDEVICE_H_ */
|
||||
|
|
|
@ -22,11 +22,13 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#include <boost/algorithm/string.hpp> // trim
|
||||
#include <boost/asio.hpp>
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
#include <exception>
|
||||
|
||||
namespace fair
|
||||
{
|
||||
|
@ -128,6 +130,59 @@ inline std::string getDefaultRouteNetworkInterface()
|
|||
return interfaceName;
|
||||
}
|
||||
|
||||
inline std::string getIpFromHostname(const std::string& hostname)
|
||||
{
|
||||
try {
|
||||
boost::asio::io_service ios;
|
||||
boost::asio::ip::tcp::resolver resolver(ios);
|
||||
boost::asio::ip::tcp::resolver::query query(hostname, "");
|
||||
boost::asio::ip::tcp::resolver::iterator end;
|
||||
|
||||
auto it = std::find_if(resolver.resolve(query), end, [](const boost::asio::ip::tcp::endpoint& ep) {
|
||||
return ep.address().is_v4();
|
||||
});
|
||||
|
||||
if (it != end) {
|
||||
std::stringstream ss;
|
||||
ss << static_cast<boost::asio::ip::tcp::endpoint>(*it).address();
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
LOG(warn) << "could not find ipv4 address for hostname '" << hostname << "'";
|
||||
|
||||
return "";
|
||||
} catch (std::exception& e) {
|
||||
LOG(error) << "could not resolve hostname '" << hostname << "', reason: " << e.what();
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
inline std::string getIpFromHostname(const std::string& hostname, boost::asio::io_service& ios)
|
||||
{
|
||||
try {
|
||||
boost::asio::ip::tcp::resolver resolver(ios);
|
||||
boost::asio::ip::tcp::resolver::query query(hostname, "");
|
||||
boost::asio::ip::tcp::resolver::iterator end;
|
||||
|
||||
auto it = std::find_if(resolver.resolve(query), end, [](const boost::asio::ip::tcp::endpoint& ep) {
|
||||
return ep.address().is_v4();
|
||||
});
|
||||
|
||||
if (it != end) {
|
||||
std::stringstream ss;
|
||||
ss << static_cast<boost::asio::ip::tcp::endpoint>(*it).address();
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
LOG(warn) << "could not find ipv4 address for hostname '" << hostname << "'";
|
||||
|
||||
return "";
|
||||
} catch (std::exception& e) {
|
||||
LOG(error) << "could not resolve hostname '" << hostname << "', reason: " << e.what();
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
} /* namespace tools */
|
||||
} /* namespace mq */
|
||||
} /* namespace fair */
|
||||
|
|
Loading…
Reference in New Issue
Block a user