Created
June 19, 2018 11:05
-
-
Save floli/d418405ca50d722f1abf8bf5971466e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void requestConnection(std::string const &nameAcceptor, | |
std::string const &nameRequester, | |
std::set<int> const &acceptorRanks, | |
int requesterRank) | |
{ | |
boost::asio::ip::tcp::resolver resolver(*_ioService); | |
for (auto const & acceptorRank : acceptorRanks) { | |
try { | |
address = readAddress(nameAcceptor, nameRequester, acceptorRank); | |
DEBUG("Requesting connection to " << address); | |
std::string ipAddress = address.substr(0, address.find(":")); | |
std::string portNumber = address.substr(ipAddress.length()+1, address.length() - ipAddress.length()-1); | |
_portNumber = static_cast<unsigned short>(std::stoi(portNumber)); | |
PtrSocket socket(new Socket(*_ioService)); | |
using asio::ip::tcp; | |
tcp::resolver::query query(tcp::v4(), ipAddress, portNumber); | |
while (not isConnected()) { | |
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); | |
boost::system::error_code error = asio::error::host_not_found; | |
boost::asio::connect(*socket, endpoint_iterator, error); | |
_isConnected = not error; | |
if (not isConnected()) { | |
// Wait a little, since after a couple of ten-thousand trials the system | |
// seems to get confused and the requester connects wrongly to itself. | |
boost::asio::deadline_timer timer(*_ioService, boost::posix_time::milliseconds(1)); | |
timer.wait(); | |
} | |
} | |
DEBUG("Requested connection to " << address << ", rank = " << acceptorRank); | |
_sockets[acceptorRank] = socket; | |
WARN("CHECKPOINT 1 FOR RANK " << acceptorRank); | |
asio::write(*_sockets.at(acceptorRank), asio::buffer(&requesterRank, sizeof(int))); | |
WARN("CHECKPOINT 2 FOR RANK " << acceptorRank); // reached only for acceptorRank=0 | |
} catch (std::exception &e) { | |
ERROR("Requesting connection to " << address << " failed: " << e.what()); | |
} | |
} | |
// NOTE: | |
// Keep IO service running so that it fires asynchronous handlers from another thread. | |
_work = PtrWork(new asio::io_service::work(*_ioService)); | |
_thread = std::thread([this]() { _ioService->run(); }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment