Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Silence zmq bullseye #126

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 20 additions & 27 deletions src/binpickingtask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1817,14 +1817,12 @@ void BinPickingTaskResource::_HeartbeatMonitorThread(const double reinitializeti
socket.reset();
}
socket.reset(new zmq::socket_t((*_zmqcontext.get()),ZMQ_SUB));
socket->setsockopt(ZMQ_TCP_KEEPALIVE, 1); // turn on tcp keepalive, do these configuration before connect
socket->setsockopt(ZMQ_TCP_KEEPALIVE_IDLE, 2); // the interval between the last data packet sent (simple ACKs are not considered data) and the first keepalive probe; after the connection is marked to need keepalive, this counter is not used any further
socket->setsockopt(ZMQ_TCP_KEEPALIVE_INTVL, 2); // the interval between subsequential keepalive probes, regardless of what the connection has exchanged in the meantime
socket->setsockopt(ZMQ_TCP_KEEPALIVE_CNT, 2); // the number of unacknowledged probes to send before considering the connection dead and notifying the application layer
std::stringstream ss; ss << std::setprecision(std::numeric_limits<double>::digits10+1);
ss << _heartbeatPort;
socket->connect(("tcp://"+ _mujinControllerIp+":"+ss.str()).c_str());
socket->setsockopt(ZMQ_SUBSCRIBE, "", 0);
socket->set(zmq::sockopt::tcp_keepalive, 1); // turn on tcp keepalive, do these configuration before connect
socket->set(zmq::sockopt::tcp_keepalive_idle, 2); // the interval between the last data packet sent (simple ACKs are not considered data) and the first keepalive probe; after the connection is marked to need keepalive, this counter is not used any further
socket->set(zmq::sockopt::tcp_keepalive_intvl, 2); // the interval between subsequential keepalive probes, regardless of what the connection has exchanged in the meantime
socket->set(zmq::sockopt::tcp_keepalive_cnt, 2); // the number of unacknowledged probes to send before considering the connection dead and notifying the application layer
socket->connect("tcp://" + _mujinControllerIp + ":" + std::to_string(_heartbeatPort));
socket->set(zmq::sockopt::subscribe, "");

zmq::pollitem_t pollitem;
memset(&pollitem, 0, sizeof(zmq::pollitem_t));
Expand All @@ -1833,13 +1831,12 @@ void BinPickingTaskResource::_HeartbeatMonitorThread(const double reinitializeti

unsigned long long lastheartbeat = GetMilliTime();
while (!_bShutdownHeartbeatMonitor && (GetMilliTime() - lastheartbeat) / 1000.0f < reinitializetimeout) {
zmq::poll(&pollitem,1, 50); // wait 50 ms for message
zmq::poll(&pollitem, 1, std::chrono::milliseconds{50}); // wait 50 ms for message
if (pollitem.revents & ZMQ_POLLIN) {
zmq::message_t reply;
socket->recv(&reply);
std::string replystring((char *)reply.data (), (size_t)reply.size());
socket->recv(reply);
//if ((size_t)reply.size() == 1 && ((char *)reply.data())[0]==255) {
if (replystring == "255") {
if (reply.to_string() == "255") {
lastheartbeat = GetMilliTime();
}
}
Expand All @@ -1859,37 +1856,36 @@ void BinPickingTaskResource::_HeartbeatMonitorThread(const double reinitializeti
std::string utils::GetHeartbeat(const std::string& endpoint) {
zmq::context_t zmqcontext(1);
zmq::socket_t socket(zmqcontext, ZMQ_SUB);
socket.connect(endpoint.c_str());
socket.setsockopt(ZMQ_SUBSCRIBE, "", 0);
socket.connect(endpoint);
socket.set(zmq::sockopt::subscribe, "");

zmq::pollitem_t pollitem;
memset(&pollitem, 0, sizeof(zmq::pollitem_t));
pollitem.socket = socket;
pollitem.events = ZMQ_POLLIN;

zmq::poll(&pollitem,1, 50); // wait 50 ms for message
zmq::poll(&pollitem, 1, std::chrono::milliseconds{50}); // wait 50 ms for message
if (!(pollitem.revents & ZMQ_POLLIN)) {
return "";
}

zmq::message_t reply;
socket.recv(&reply);
const std::string received((char *)reply.data (), (size_t)reply.size());
socket.recv(reply);
#ifndef _WIN32
return received;
return reply.to_string();
#else
// sometimes buffer can container \n or \\, which windows does not like
std::string newbuffer;
std::vector< std::pair<std::string, std::string> > serachpairs(2);
serachpairs[0].first = "\n"; serachpairs[0].second = "";
serachpairs[1].first = "\\"; serachpairs[1].second = "";
SearchAndReplace(newbuffer, received, serachpairs);
SearchAndReplace(newbuffer, reply.to_string(), serachpairs);
return newbuffer;
#endif
}


namespace {

std::string FindSmallestSlaveRequestId(const rapidjson::Value& pt) {
// get all slave request ids
std::vector<std::string> slavereqids;
Expand Down Expand Up @@ -1936,13 +1932,11 @@ std::string FindSmallestSlaveRequestId(const rapidjson::Value& pt) {
return slavereqids[smallest_suffix_index];
}

std::string GetValueForSmallestSlaveRequestId(const std::string& heartbeat,
const std::string& key)
std::string GetValueForSmallestSlaveRequestId(const std::string& heartbeat, const std::string& key)
{

rapidjson::Document pt(rapidjson::kObjectType);
std::stringstream ss(heartbeat);
ParseJson(pt, ss.str());
ParseJson(pt, heartbeat);
try {
const std::string slavereqid = FindSmallestSlaveRequestId(pt);
std::string result;
Expand All @@ -1954,8 +1948,8 @@ std::string GetValueForSmallestSlaveRequestId(const std::string& heartbeat,
}

}
}

} // anonymous namespace

std::string mujinclient::utils::GetScenePkFromHeartbeat(const std::string& heartbeat) {
static const std::string prefix("mujin:/");
Expand All @@ -1964,8 +1958,7 @@ std::string mujinclient::utils::GetScenePkFromHeartbeat(const std::string& heart

std::string utils::GetSlaveRequestIdFromHeartbeat(const std::string& heartbeat) {
rapidjson::Document pt;
std::stringstream ss(heartbeat);
ParseJson(pt, ss.str());
ParseJson(pt, heartbeat);
try {
static const std::string prefix("slaverequestid-");
return FindSmallestSlaveRequestId(pt).substr(prefix.length());
Expand Down
26 changes: 10 additions & 16 deletions src/binpickingtaskzmq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,30 +220,26 @@ void BinPickingTaskZmqResource::_HeartbeatMonitorThread(const double reinitializ
socket.reset();
}
socket.reset(new zmq::socket_t((*_zmqcontext.get()),ZMQ_SUB));
socket->setsockopt(ZMQ_TCP_KEEPALIVE, 1); // turn on tcp keepalive, do these configuration before connect
socket->setsockopt(ZMQ_TCP_KEEPALIVE_IDLE, 2); // the interval between the last data packet sent (simple ACKs are not considered data) and the first keepalive probe; after the connection is marked to need keepalive, this counter is not used any further
socket->setsockopt(ZMQ_TCP_KEEPALIVE_INTVL, 2); // the interval between subsequential keepalive probes, regardless of what the connection has exchanged in the meantime
socket->setsockopt(ZMQ_TCP_KEEPALIVE_CNT, 2); // the number of unacknowledged probes to send before considering the connection dead and notifying the application layer
std::stringstream ss; ss << std::setprecision(std::numeric_limits<double>::digits10+1);
ss << _heartbeatPort;
socket->connect (("tcp://"+ _mujinControllerIp+":"+ss.str()).c_str());
socket->setsockopt(ZMQ_SUBSCRIBE, "", 0);
socket->set(zmq::sockopt::tcp_keepalive, 1); // turn on tcp keepalive, do these configuration before connect
socket->set(zmq::sockopt::tcp_keepalive_idle, 2); // the interval between the last data packet sent (simple ACKs are not considered data) and the first keepalive probe; after the connection is marked to need keepalive, this counter is not used any further
socket->set(zmq::sockopt::tcp_keepalive_intvl, 2); // the interval between subsequential keepalive probes, regardless of what the connection has exchanged in the meantime
socket->set(zmq::sockopt::tcp_keepalive_cnt, 2); // the number of unacknowledged probes to send before considering the connection dead and notifying the application layer
socket->connect("tcp://" + _mujinControllerIp + ":" + std::to_string(_heartbeatPort));
socket->set(zmq::sockopt::subscribe, "");

zmq::pollitem_t pollitem;
memset(&pollitem, 0, sizeof(zmq::pollitem_t));
pollitem.socket = socket->operator void*();
pollitem.events = ZMQ_POLLIN;
unsigned long long lastheartbeat = GetMilliTime();
while (!_bShutdownHeartbeatMonitor && (GetMilliTime() - lastheartbeat) / 1000.0f < reinitializetimeout) {
zmq::poll(&pollitem,1, 50); // wait 50 ms for message
zmq::poll(&pollitem, 1, std::chrono::milliseconds{50}); // wait 50 ms for message
if (pollitem.revents & ZMQ_POLLIN) {
zmq::message_t reply;
socket->recv(&reply);
std::string replystring((char *)reply.data (), (size_t)reply.size());
socket->recv(reply);
rapidjson::Document pt(rapidjson::kObjectType);
try{
std::stringstream replystring_ss(replystring);
ParseJson(pt, replystring_ss.str());
ParseJson(pt, reply.to_string());
heartbeat.Parse(pt);
{
boost::mutex::scoped_lock lock(_mutexTaskState);
Expand All @@ -257,7 +253,7 @@ void BinPickingTaskZmqResource::_HeartbeatMonitorThread(const double reinitializ
}
catch (std::exception const &e) {
MUJIN_LOG_ERROR("HeartBeat reply is not JSON");
MUJIN_LOG_ERROR(replystring);
MUJIN_LOG_ERROR(reply.to_string());
MUJIN_LOG_ERROR(e.what());
continue;
}
Expand All @@ -272,6 +268,4 @@ void BinPickingTaskZmqResource::_HeartbeatMonitorThread(const double reinitializ
MUJIN_LOG_DEBUG(str(boost::format("Stopped controller %s monitoring thread on port %d for slaverequestid=%s.")%_mujinControllerIp%_heartbeatPort%_slaverequestid));
}



} // end namespace mujinclient
4 changes: 2 additions & 2 deletions src/controllerclientimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <boost/algorithm/string.hpp>
#include <boost/scope_exit.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <strstream>
#include <sstream>

#define SKIP_PEER_VERIFICATION // temporary
//#define SKIP_HOSTNAME_VERIFICATION
Expand Down Expand Up @@ -126,7 +126,7 @@ ControllerClientImpl::ControllerClientImpl(const std::string& usernamepassword,
_mujinControllerIp = _baseuri.substr(_baseuri.find("//")+2, _baseuri.size());
_mujinControllerIp.erase(remove(_mujinControllerIp.begin(), _mujinControllerIp.end(), '/'), _mujinControllerIp.end()); // Remove / from string
// Remove port
int idx = _mujinControllerIp.find(':');
size_t idx = _mujinControllerIp.find(':');
if (idx != std::string::npos) {
_mujinControllerIp = _mujinControllerIp.substr(0, idx);
}
Expand Down
Loading