40 #include <boost/python.hpp>
41namespace bp = boost::python;
45static constexpr double DefaultReadyTimeout = 10.0;
46static constexpr double DefaultReadyPeriod = 0.1;
56rim::TcpClient::TcpClient(std::string addr, uint16_t port,
bool waitReady) :
rim::
Slave(4, 0xFFFFFFFF) {
60 logstr =
"memory.TcpClient.";
63 logstr.append(std::to_string(port));
68 this->probeDone_ =
false;
69 this->probeResult_.clear();
73 this->respAddr_ =
"tcp://";
74 this->respAddr_.append(addr);
75 this->respAddr_.append(
":");
76 this->reqAddr_ = this->respAddr_;
78 this->zmqCtx_ = zmq_ctx_new();
79 this->zmqResp_ = zmq_socket(this->zmqCtx_, ZMQ_PULL);
80 this->zmqReq_ = zmq_socket(this->zmqCtx_, ZMQ_PUSH);
86 if (zmq_setsockopt(this->zmqReq_, ZMQ_IMMEDIATE, &opt,
sizeof(int32_t)) != 0)
87 throw(
rogue::GeneralError(
"memory::TcpClient::TcpClient",
"Failed to set socket immediate"));
89 this->respAddr_.append(std::to_string(
static_cast<int64_t
>(port + 1)));
90 this->reqAddr_.append(std::to_string(
static_cast<int64_t
>(port)));
92 this->bridgeLog_->debug(
"Creating response client port: %s", this->respAddr_.c_str());
95 if (zmq_setsockopt(this->zmqResp_, ZMQ_LINGER, &opt,
sizeof(int32_t)) != 0)
98 if (zmq_setsockopt(this->zmqReq_, ZMQ_LINGER, &opt,
sizeof(int32_t)) != 0)
102 if (zmq_setsockopt(this->zmqResp_, ZMQ_RCVTIMEO, &opt,
sizeof(int32_t)) != 0)
103 throw(
rogue::GeneralError(
"memory::TcpClient::TcpClient",
"Failed to set socket receive timeout"));
105 if (zmq_connect(this->zmqResp_, this->respAddr_.c_str()) < 0)
107 "Failed to connect to remote port %" PRIu16
" at address %s",
111 this->bridgeLog_->debug(
"Creating request client port: %s", this->reqAddr_.c_str());
113 if (zmq_connect(this->zmqReq_, this->reqAddr_.c_str()) < 0)
115 "Failed to connect to remote port %" PRIu16
" at address %s",
121 this->thread_ = std::make_unique<std::thread>(&rim::TcpClient::runThread,
this);
125 pthread_setname_np(thread_->native_handle(),
"TcpClient");
142 if (zmqResp_ !=
nullptr) {
146 if (zmqReq_ !=
nullptr) {
150 if (zmqCtx_ !=
nullptr) {
151 zmq_ctx_destroy(zmqCtx_);
159rim::TcpClient::~TcpClient() {
164void rim::TcpClient::close() {
168void rim::TcpClient::stop() {
176 std::lock_guard<std::mutex> lock(bridgeMtx_);
177 zmq_close(this->zmqResp_);
179 zmq_close(this->zmqReq_);
181 zmq_ctx_destroy(this->zmqCtx_);
186bool rim::TcpClient::waitReady(
double timeout,
double period) {
192 auto deadline = std::chrono::steady_clock::now() + std::chrono::duration<double>(timeout);
196 while (std::chrono::steady_clock::now() < deadline) {
198 std::lock_guard<std::mutex> probeLock(probeMtx_);
201 probeId_ = ++probeSeq_;
203 probeResult_.clear();
210 zmq_msg_init_size(&(msg[0]), 4);
211 std::memcpy(zmq_msg_data(&(msg[0])), &
id, 4);
212 zmq_msg_init_size(&(msg[1]), 8);
213 std::memcpy(zmq_msg_data(&(msg[1])), &addr, 8);
214 zmq_msg_init_size(&(msg[2]), 4);
215 std::memcpy(zmq_msg_data(&(msg[2])), &size, 4);
216 zmq_msg_init_size(&(msg[3]), 4);
217 std::memcpy(zmq_msg_data(&(msg[3])), &type, 4);
220 std::lock_guard<std::mutex> block(bridgeMtx_);
224 for (uint32_t x = 0; x < 4; ++x) {
225 if (zmq_sendmsg(this->zmqReq_, &(msg[x]), (x == 3 ? 0 : ZMQ_SNDMORE) | ZMQ_DONTWAIT) < 0) {
226 bridgeLog_->debug(
"Readiness probe send failed for port %s", this->reqAddr_.c_str());
232 for (uint32_t x = 0; x < 4; ++x) zmq_msg_close(&(msg[x]));
234 std::unique_lock<std::mutex> probeLock(probeMtx_);
238 probeCond_.wait_for(probeLock, std::chrono::duration<double>(period), [&]() {
return probeDone_ && probeId_ == id; });
240 if (probeDone_ && probeId_ ==
id && probeResult_ ==
"OK") {
241 bridgeLog_->debug(
"Readiness probe succeeded for port %s", this->reqAddr_.c_str());
246 bridgeLog_->warning(
"Timed out waiting for bridge readiness on port %s", this->reqAddr_.c_str());
250void rim::TcpClient::start() {
251 if (!waitReadyOnStart_)
return;
253 if (!waitReady(DefaultReadyTimeout, DefaultReadyPeriod)) {
255 "Timed out waiting for remote TcpServer readiness on %s",
256 this->reqAddr_.c_str()));
271 std::lock_guard<std::mutex> block(bridgeMtx_);
274 if (this->zmqReq_ ==
nullptr) {
275 tran->error(
"TcpClient stopped");
281 zmq_msg_init_size(&(msg[0]), 4);
282 std::memcpy(zmq_msg_data(&(msg[0])), &
id, 4);
285 addr = tran->address();
286 zmq_msg_init_size(&(msg[1]), 8);
287 std::memcpy(zmq_msg_data(&(msg[1])), &addr, 8);
291 zmq_msg_init_size(&(msg[2]), 4);
292 std::memcpy(zmq_msg_data(&(msg[2])), &size, 4);
296 zmq_msg_init_size(&(msg[3]), 4);
297 std::memcpy(zmq_msg_data(&(msg[3])), &type, 4);
302 zmq_msg_init_size(&(msg[4]), size);
303 std::memcpy(zmq_msg_data(&(msg[4])), tran->begin(), size);
310 bridgeLog_->debug(
"Requested transaction id=%" PRIu32
", addr=0x%" PRIx64
", size=%" PRIu32
", type=%" PRIu32
311 ", cnt=%" PRIu32
", port: %s",
317 this->reqAddr_.c_str());
323 addTransaction(tran);
326 for (x = 0; x < msgCnt; x++) {
327 if (zmq_sendmsg(this->zmqReq_, &(msg[x]), ((x == (msgCnt - 1) ? 0 : ZMQ_SNDMORE)) | ZMQ_DONTWAIT) < 0) {
328 bridgeLog_->warning(
"Failed to send transaction %" PRIu32
", msg %" PRIu32
" on %s: %s",
331 this->reqAddr_.c_str(),
332 zmq_strerror(zmq_errno()));
338void rim::TcpClient::runThread() {
352 bridgeLog_->logThreadId();
355 for (x = 0; x < 6; x++) zmq_msg_init(&(msg[x]));
362 if (zmq_recvmsg(this->zmqResp_, &(msg[x]), 0) >= 0) {
369 zmq_getsockopt(this->zmqResp_, ZMQ_RCVMORE, &more, &moreSize);
373 }
while (threadEn_ && more);
376 if (threadEn_ && (msgCnt == 6)) {
378 if ((zmq_msg_size(&(msg[0])) != 4) || (zmq_msg_size(&(msg[1])) != 8) || (zmq_msg_size(&(msg[2])) != 4) ||
379 (zmq_msg_size(&(msg[3])) != 4) || (zmq_msg_size(&(msg[5])) > 999)) {
381 "Bad message sizes. id=%zu addr=%zu size=%zu type=%zu result=%zu",
382 zmq_msg_size(&(msg[0])),
383 zmq_msg_size(&(msg[1])),
384 zmq_msg_size(&(msg[2])),
385 zmq_msg_size(&(msg[3])),
386 zmq_msg_size(&(msg[5])));
387 for (x = 0; x < msgCnt; x++) zmq_msg_close(&(msg[x]));
392 std::memcpy(&
id, zmq_msg_data(&(msg[0])), 4);
393 std::memcpy(&addr, zmq_msg_data(&(msg[1])), 8);
394 std::memcpy(&size, zmq_msg_data(&(msg[2])), 4);
395 std::memcpy(&type, zmq_msg_data(&(msg[3])), 4);
397 memset(result, 0, 1000);
398 std::strncpy(result,
reinterpret_cast<char*
>(zmq_msg_data(&(msg[5]))), zmq_msg_size(&(msg[5])));
401 std::lock_guard<std::mutex> probeLock(probeMtx_);
402 if (
id == probeId_) {
403 probeResult_ = result;
405 probeCond_.notify_all();
407 for (x = 0; x < msgCnt; x++) zmq_msg_close(&(msg[x]));
412 if ((tran = getTransaction(
id)) == NULL) {
413 bridgeLog_->warning(
"Failed to find transaction id=%" PRIu32,
id);
414 for (x = 0; x < msgCnt; x++) zmq_msg_close(&(msg[x]));
423 if (tran->expired()) {
424 bridgeLog_->warning(
"Dropping late response for expired transaction. Id=%" PRIu32,
id);
425 }
else if ((addr != tran->address()) || (size != tran->size()) || (type != tran->type())) {
426 bridgeLog_->warning(
"Transaction data mismatch. Id=%" PRIu32,
id);
427 tran->error(
"Transaction data mismatch in TcpClient");
428 }
else if ((type !=
rim::Write) && (zmq_msg_size(&(msg[4])) != size)) {
429 bridgeLog_->warning(
"Transaction size mismatch. Id=%" PRIu32,
id);
430 tran->error(
"Received transaction response did not match header size");
433 if (type !=
rim::Write) std::memcpy(tran->begin(), zmq_msg_data(&(msg[4])), size);
434 if (strcmp(result,
"OK") != 0)
435 tran->errorStr(std::string(result));
438 bridgeLog_->debug(
"Response for transaction id=%" PRIu32
", addr=0x%" PRIx64
", size=%" PRIu32
439 ", type=%" PRIu32
", cnt=%" PRIu32
", port: %s, Result: (%s)",
445 this->respAddr_.c_str(),
449 for (x = 0; x < msgCnt; x++) zmq_msg_close(&(msg[x]));
458 if (tran && Py_IsInitialized()) {
470void rim::TcpClient::setup_python() {
473#pragma GCC diagnostic push
474#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
475 bp::class_<rim::TcpClient, rim::TcpClientPtr, bp::bases<rim::Slave>, boost::noncopyable>(
477 bp::init<std::string, uint16_t, bp::optional<bool> >())
478 .def(
"close", &rim::TcpClient::close)
479 .def(
"waitReady", &rim::TcpClient::waitReady)
480 .def(
"_start", &rim::TcpClient::start)
481 .def(
"_stop", &rim::TcpClient::stop);
482#pragma GCC diagnostic pop
484 bp::implicitly_convertible<rim::TcpClientPtr, rim::SlavePtr>();
Generic Rogue exception type.
static GeneralError create(std::string src, const char *fmt,...)
Creates a formatted error instance.
RAII helper that releases the Python GIL for a scope.
static std::shared_ptr< rogue::Logging > create(const std::string &name, bool quiet=false)
Creates a logger instance.
RAII helper that acquires the Python GIL for a scope.
bool waitReady(double timeout, double period)
Wait for the remote TcpServer path to respond to a bridge probe.
std::shared_ptr< rogue::interfaces::memory::TransactionLock > TransactionLockPtr
Shared pointer alias for TransactionLock.
static const uint32_t TcpBridgeProbe
Internal TCP bridge readiness probe transaction type.
static const uint32_t Write
Memory write transaction type.
std::shared_ptr< rogue::interfaces::memory::Transaction > TransactionPtr
Shared pointer alias for Transaction.
static const uint32_t Post
Memory posted write transaction type.
std::shared_ptr< rogue::interfaces::memory::TcpClient > TcpClientPtr
Shared pointer alias for TcpClient.