mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-05-06 21:00:02 +02:00
cmake 3.9 and removed zmq
This commit is contained in:
parent
274ec27934
commit
9ca831d954
@ -1,6 +1,6 @@
|
|||||||
# SPDX-License-Identifier: LGPL-3.0-or-other
|
# SPDX-License-Identifier: LGPL-3.0-or-other
|
||||||
# Copyright (C) 2021 Contributors to the SLS Detector Package
|
# Copyright (C) 2021 Contributors to the SLS Detector Package
|
||||||
cmake_minimum_required(VERSION 3.12)
|
cmake_minimum_required(VERSION 3.9)
|
||||||
project(slsDetectorPackage)
|
project(slsDetectorPackage)
|
||||||
set(PROJECT_VERSION 6.0.0)
|
set(PROJECT_VERSION 6.0.0)
|
||||||
|
|
||||||
@ -172,35 +172,35 @@ set(CMAKE_INSTALL_RPATH $ORIGIN)
|
|||||||
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
|
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
|
||||||
|
|
||||||
|
|
||||||
set(ZeroMQ_HINT "" CACHE STRING "Hint where ZeroMQ could be found")
|
# set(ZeroMQ_HINT "" CACHE STRING "Hint where ZeroMQ could be found")
|
||||||
#Adapted from: https://github.com/zeromq/cppzmq/
|
# #Adapted from: https://github.com/zeromq/cppzmq/
|
||||||
if (NOT TARGET libzmq)
|
# if (NOT TARGET libzmq)
|
||||||
if(ZeroMQ_HINT)
|
# if(ZeroMQ_HINT)
|
||||||
message(STATUS "Looking for ZeroMQ in: ${ZeroMQ_HINT}")
|
# message(STATUS "Looking for ZeroMQ in: ${ZeroMQ_HINT}")
|
||||||
find_package(ZeroMQ 4
|
# find_package(ZeroMQ 4
|
||||||
NO_DEFAULT_PATH
|
# NO_DEFAULT_PATH
|
||||||
HINTS ${ZeroMQ_DIR}
|
# HINTS ${ZeroMQ_DIR}
|
||||||
)
|
# )
|
||||||
else()
|
# else()
|
||||||
find_package(ZeroMQ 4 QUIET)
|
# find_package(ZeroMQ 4 QUIET)
|
||||||
endif()
|
# endif()
|
||||||
|
|
||||||
# libzmq autotools install: fallback to pkg-config
|
# # libzmq autotools install: fallback to pkg-config
|
||||||
if(NOT ZeroMQ_FOUND)
|
# if(NOT ZeroMQ_FOUND)
|
||||||
message(STATUS "CMake libzmq package not found, trying again with pkg-config (normal install of zeromq)")
|
# message(STATUS "CMake libzmq package not found, trying again with pkg-config (normal install of zeromq)")
|
||||||
list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/libzmq-pkg-config)
|
# list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/libzmq-pkg-config)
|
||||||
find_package(ZeroMQ 4 REQUIRED)
|
# find_package(ZeroMQ 4 REQUIRED)
|
||||||
endif()
|
# endif()
|
||||||
|
|
||||||
# TODO "REQUIRED" above should already cause a fatal failure if not found, but this doesn't seem to work
|
# # TODO "REQUIRED" above should already cause a fatal failure if not found, but this doesn't seem to work
|
||||||
if(NOT ZeroMQ_FOUND)
|
# if(NOT ZeroMQ_FOUND)
|
||||||
message(FATAL_ERROR "ZeroMQ was not found, neither as a CMake package nor via pkg-config")
|
# message(FATAL_ERROR "ZeroMQ was not found, neither as a CMake package nor via pkg-config")
|
||||||
endif()
|
# endif()
|
||||||
|
|
||||||
if (ZeroMQ_FOUND AND NOT TARGET libzmq)
|
# if (ZeroMQ_FOUND AND NOT TARGET libzmq)
|
||||||
message(FATAL_ERROR "ZeroMQ version not supported!")
|
# message(FATAL_ERROR "ZeroMQ version not supported!")
|
||||||
endif()
|
# endif()
|
||||||
endif()
|
# endif()
|
||||||
|
|
||||||
if (SLS_USE_TESTS)
|
if (SLS_USE_TESTS)
|
||||||
enable_testing()
|
enable_testing()
|
||||||
|
@ -80,7 +80,7 @@ target_include_directories(slsSupportObject
|
|||||||
target_link_libraries(slsSupportObject
|
target_link_libraries(slsSupportObject
|
||||||
PUBLIC
|
PUBLIC
|
||||||
slsProjectOptions
|
slsProjectOptions
|
||||||
libzmq
|
# libzmq
|
||||||
rapidjson
|
rapidjson
|
||||||
PRIVATE
|
PRIVATE
|
||||||
slsProjectWarnings
|
slsProjectWarnings
|
||||||
|
@ -10,249 +10,249 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <zmq.h>
|
// #include <zmq.h>
|
||||||
|
|
||||||
using namespace rapidjson;
|
using namespace rapidjson;
|
||||||
ZmqSocket::ZmqSocket(const char *const hostname_or_ip,
|
ZmqSocket::ZmqSocket(const char *const hostname_or_ip,
|
||||||
const uint32_t portnumber)
|
const uint32_t portnumber)
|
||||||
: portno(portnumber), sockfd(false) {
|
: portno(portnumber), sockfd(false) {
|
||||||
// Extra check that throws if conversion fails, could be removed
|
// Extra check that throws if conversion fails, could be removed
|
||||||
auto ipstr = sls::HostnameToIp(hostname_or_ip).str();
|
// auto ipstr = sls::HostnameToIp(hostname_or_ip).str();
|
||||||
std::ostringstream oss;
|
// std::ostringstream oss;
|
||||||
oss << "tcp://" << ipstr << ":" << portno;
|
// oss << "tcp://" << ipstr << ":" << portno;
|
||||||
sockfd.serverAddress = oss.str();
|
// sockfd.serverAddress = oss.str();
|
||||||
LOG(logDEBUG) << "zmq address: " << sockfd.serverAddress;
|
// LOG(logDEBUG) << "zmq address: " << sockfd.serverAddress;
|
||||||
|
|
||||||
// create context
|
// // create context
|
||||||
sockfd.contextDescriptor = zmq_ctx_new();
|
// sockfd.contextDescriptor = zmq_ctx_new();
|
||||||
if (sockfd.contextDescriptor == nullptr)
|
// if (sockfd.contextDescriptor == nullptr)
|
||||||
throw sls::ZmqSocketError("Could not create contextDescriptor");
|
// throw sls::ZmqSocketError("Could not create contextDescriptor");
|
||||||
|
|
||||||
// create subscriber
|
// // create subscriber
|
||||||
sockfd.socketDescriptor = zmq_socket(sockfd.contextDescriptor, ZMQ_SUB);
|
// sockfd.socketDescriptor = zmq_socket(sockfd.contextDescriptor, ZMQ_SUB);
|
||||||
if (sockfd.socketDescriptor == nullptr) {
|
// if (sockfd.socketDescriptor == nullptr) {
|
||||||
PrintError();
|
// PrintError();
|
||||||
throw sls::ZmqSocketError("Could not create socket");
|
// throw sls::ZmqSocketError("Could not create socket");
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Socket Options provided above
|
// // Socket Options provided above
|
||||||
// an empty string implies receiving any messages
|
// // an empty string implies receiving any messages
|
||||||
if (zmq_setsockopt(sockfd.socketDescriptor, ZMQ_SUBSCRIBE, "", 0)) {
|
// if (zmq_setsockopt(sockfd.socketDescriptor, ZMQ_SUBSCRIBE, "", 0)) {
|
||||||
PrintError();
|
// PrintError();
|
||||||
throw sls::ZmqSocketError("Could set socket opt");
|
// throw sls::ZmqSocketError("Could set socket opt");
|
||||||
}
|
// }
|
||||||
// ZMQ_LINGER default is already -1 means no messages discarded. use this
|
// // ZMQ_LINGER default is already -1 means no messages discarded. use this
|
||||||
// options if optimizing required ZMQ_SNDHWM default is 0 means no limit.
|
// // options if optimizing required ZMQ_SNDHWM default is 0 means no limit.
|
||||||
// use this to optimize if optimizing required eg. int value = -1;
|
// // use this to optimize if optimizing required eg. int value = -1;
|
||||||
const int value = 0;
|
// const int value = 0;
|
||||||
if (zmq_setsockopt(sockfd.socketDescriptor, ZMQ_LINGER, &value,
|
// if (zmq_setsockopt(sockfd.socketDescriptor, ZMQ_LINGER, &value,
|
||||||
sizeof(value))) {
|
// sizeof(value))) {
|
||||||
PrintError();
|
// PrintError();
|
||||||
throw sls::ZmqSocketError("Could not set ZMQ_LINGER");
|
// throw sls::ZmqSocketError("Could not set ZMQ_LINGER");
|
||||||
}
|
// }
|
||||||
LOG(logDEBUG) << "Default receive high water mark:"
|
// LOG(logDEBUG) << "Default receive high water mark:"
|
||||||
<< GetReceiveHighWaterMark();
|
// << GetReceiveHighWaterMark();
|
||||||
}
|
}
|
||||||
|
|
||||||
ZmqSocket::ZmqSocket(const uint32_t portnumber, const char *ethip)
|
ZmqSocket::ZmqSocket(const uint32_t portnumber, const char *ethip)
|
||||||
: portno(portnumber), sockfd(true) {
|
: portno(portnumber), sockfd(true) {
|
||||||
// create context
|
// create context
|
||||||
sockfd.contextDescriptor = zmq_ctx_new();
|
// sockfd.contextDescriptor = zmq_ctx_new();
|
||||||
if (sockfd.contextDescriptor == nullptr)
|
// if (sockfd.contextDescriptor == nullptr)
|
||||||
throw sls::ZmqSocketError("Could not create contextDescriptor");
|
// throw sls::ZmqSocketError("Could not create contextDescriptor");
|
||||||
|
|
||||||
// create publisher
|
// // create publisher
|
||||||
sockfd.socketDescriptor = zmq_socket(sockfd.contextDescriptor, ZMQ_PUB);
|
// sockfd.socketDescriptor = zmq_socket(sockfd.contextDescriptor, ZMQ_PUB);
|
||||||
if (sockfd.socketDescriptor == nullptr) {
|
// if (sockfd.socketDescriptor == nullptr) {
|
||||||
PrintError();
|
// PrintError();
|
||||||
throw sls::ZmqSocketError("Could not create socket");
|
// throw sls::ZmqSocketError("Could not create socket");
|
||||||
}
|
// }
|
||||||
LOG(logDEBUG) << "Default send high water mark:" << GetSendHighWaterMark();
|
// LOG(logDEBUG) << "Default send high water mark:" << GetSendHighWaterMark();
|
||||||
|
|
||||||
// construct address, can be refactored with libfmt
|
// // construct address, can be refactored with libfmt
|
||||||
std::ostringstream oss;
|
// std::ostringstream oss;
|
||||||
oss << "tcp://" << ethip << ":" << portno;
|
// oss << "tcp://" << ethip << ":" << portno;
|
||||||
sockfd.serverAddress = oss.str();
|
// sockfd.serverAddress = oss.str();
|
||||||
LOG(logDEBUG) << "zmq address: " << sockfd.serverAddress;
|
// LOG(logDEBUG) << "zmq address: " << sockfd.serverAddress;
|
||||||
|
|
||||||
// bind address
|
// // bind address
|
||||||
if (zmq_bind(sockfd.socketDescriptor, sockfd.serverAddress.c_str())) {
|
// if (zmq_bind(sockfd.socketDescriptor, sockfd.serverAddress.c_str())) {
|
||||||
PrintError();
|
// PrintError();
|
||||||
throw sls::ZmqSocketError("Could not bind socket");
|
// throw sls::ZmqSocketError("Could not bind socket");
|
||||||
}
|
// }
|
||||||
// sleep to allow a slow-joiner
|
// // sleep to allow a slow-joiner
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
// std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||||
};
|
};
|
||||||
|
|
||||||
int ZmqSocket::GetSendHighWaterMark() {
|
int ZmqSocket::GetSendHighWaterMark() {
|
||||||
int value = 0;
|
int value = 0;
|
||||||
size_t value_size = sizeof(value);
|
// size_t value_size = sizeof(value);
|
||||||
if (zmq_getsockopt(sockfd.socketDescriptor, ZMQ_SNDHWM, &value,
|
// if (zmq_getsockopt(sockfd.socketDescriptor, ZMQ_SNDHWM, &value,
|
||||||
&value_size)) {
|
// &value_size)) {
|
||||||
PrintError();
|
// PrintError();
|
||||||
throw sls::ZmqSocketError("Could not get ZMQ_SNDHWM");
|
// throw sls::ZmqSocketError("Could not get ZMQ_SNDHWM");
|
||||||
}
|
// }
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ZmqSocket::SetSendHighWaterMark(int limit) {
|
void ZmqSocket::SetSendHighWaterMark(int limit) {
|
||||||
if (zmq_setsockopt(sockfd.socketDescriptor, ZMQ_SNDHWM, &limit,
|
// if (zmq_setsockopt(sockfd.socketDescriptor, ZMQ_SNDHWM, &limit,
|
||||||
sizeof(limit))) {
|
// sizeof(limit))) {
|
||||||
PrintError();
|
// PrintError();
|
||||||
throw sls::ZmqSocketError("Could not set ZMQ_SNDHWM");
|
// throw sls::ZmqSocketError("Could not set ZMQ_SNDHWM");
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
int ZmqSocket::GetReceiveHighWaterMark() {
|
int ZmqSocket::GetReceiveHighWaterMark() {
|
||||||
int value = 0;
|
int value = 0;
|
||||||
size_t value_size = sizeof(value);
|
// size_t value_size = sizeof(value);
|
||||||
if (zmq_getsockopt(sockfd.socketDescriptor, ZMQ_RCVHWM, &value,
|
// if (zmq_getsockopt(sockfd.socketDescriptor, ZMQ_RCVHWM, &value,
|
||||||
&value_size)) {
|
// &value_size)) {
|
||||||
PrintError();
|
// PrintError();
|
||||||
throw sls::ZmqSocketError("Could not get ZMQ_SNDHWM");
|
// throw sls::ZmqSocketError("Could not get ZMQ_SNDHWM");
|
||||||
}
|
// }
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ZmqSocket::SetReceiveHighWaterMark(int limit) {
|
void ZmqSocket::SetReceiveHighWaterMark(int limit) {
|
||||||
if (zmq_setsockopt(sockfd.socketDescriptor, ZMQ_RCVHWM, &limit,
|
// if (zmq_setsockopt(sockfd.socketDescriptor, ZMQ_RCVHWM, &limit,
|
||||||
sizeof(limit))) {
|
// sizeof(limit))) {
|
||||||
PrintError();
|
// PrintError();
|
||||||
throw sls::ZmqSocketError("Could not set ZMQ_SNDHWM");
|
// throw sls::ZmqSocketError("Could not set ZMQ_SNDHWM");
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
int ZmqSocket::Connect() {
|
int ZmqSocket::Connect() {
|
||||||
if (zmq_connect(sockfd.socketDescriptor, sockfd.serverAddress.c_str())) {
|
// if (zmq_connect(sockfd.socketDescriptor, sockfd.serverAddress.c_str())) {
|
||||||
PrintError();
|
// PrintError();
|
||||||
return 1;
|
// return 1;
|
||||||
}
|
// }
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ZmqSocket::SendHeader(int index, zmqHeader header) {
|
int ZmqSocket::SendHeader(int index, zmqHeader header) {
|
||||||
|
|
||||||
/** Json Header Format */
|
// /** Json Header Format */
|
||||||
const char jsonHeaderFormat[] = "{"
|
// const char jsonHeaderFormat[] = "{"
|
||||||
"\"jsonversion\":%u, "
|
// "\"jsonversion\":%u, "
|
||||||
"\"bitmode\":%u, "
|
// "\"bitmode\":%u, "
|
||||||
"\"fileIndex\":%lu, "
|
// "\"fileIndex\":%lu, "
|
||||||
"\"detshape\":[%u, %u], "
|
// "\"detshape\":[%u, %u], "
|
||||||
"\"shape\":[%u, %u], "
|
// "\"shape\":[%u, %u], "
|
||||||
"\"size\":%u, "
|
// "\"size\":%u, "
|
||||||
"\"acqIndex\":%lu, "
|
// "\"acqIndex\":%lu, "
|
||||||
"\"frameIndex\":%lu, "
|
// "\"frameIndex\":%lu, "
|
||||||
"\"progress\":%lf, "
|
// "\"progress\":%lf, "
|
||||||
"\"fname\":\"%s\", "
|
// "\"fname\":\"%s\", "
|
||||||
"\"data\": %d, "
|
// "\"data\": %d, "
|
||||||
"\"completeImage\": %d, "
|
// "\"completeImage\": %d, "
|
||||||
|
|
||||||
"\"frameNumber\":%lu, "
|
// "\"frameNumber\":%lu, "
|
||||||
"\"expLength\":%u, "
|
// "\"expLength\":%u, "
|
||||||
"\"packetNumber\":%u, "
|
// "\"packetNumber\":%u, "
|
||||||
"\"bunchId\":%lu, "
|
// "\"bunchId\":%lu, "
|
||||||
"\"timestamp\":%lu, "
|
// "\"timestamp\":%lu, "
|
||||||
"\"modId\":%u, "
|
// "\"modId\":%u, "
|
||||||
"\"row\":%u, "
|
// "\"row\":%u, "
|
||||||
"\"column\":%u, "
|
// "\"column\":%u, "
|
||||||
"\"reserved\":%u, "
|
// "\"reserved\":%u, "
|
||||||
"\"debug\":%u, "
|
// "\"debug\":%u, "
|
||||||
"\"roundRNumber\":%u, "
|
// "\"roundRNumber\":%u, "
|
||||||
"\"detType\":%u, "
|
// "\"detType\":%u, "
|
||||||
"\"version\":%u, "
|
// "\"version\":%u, "
|
||||||
|
|
||||||
// additional stuff
|
// // additional stuff
|
||||||
"\"flipRows\":%u, "
|
// "\"flipRows\":%u, "
|
||||||
"\"quad\":%u"
|
// "\"quad\":%u"
|
||||||
|
|
||||||
; //"}\n";
|
// ; //"}\n";
|
||||||
memset(header_buffer.get(), '\0', MAX_STR_LENGTH); // TODO! Do we need this
|
// memset(header_buffer.get(), '\0', MAX_STR_LENGTH); // TODO! Do we need this
|
||||||
sprintf(header_buffer.get(), jsonHeaderFormat, header.jsonversion,
|
// sprintf(header_buffer.get(), jsonHeaderFormat, header.jsonversion,
|
||||||
header.dynamicRange, header.fileIndex, header.ndetx, header.ndety,
|
// header.dynamicRange, header.fileIndex, header.ndetx, header.ndety,
|
||||||
header.npixelsx, header.npixelsy, header.imageSize, header.acqIndex,
|
// header.npixelsx, header.npixelsy, header.imageSize, header.acqIndex,
|
||||||
header.frameIndex, header.progress, header.fname.c_str(),
|
// header.frameIndex, header.progress, header.fname.c_str(),
|
||||||
header.data ? 1 : 0, header.completeImage ? 1 : 0,
|
// header.data ? 1 : 0, header.completeImage ? 1 : 0,
|
||||||
|
|
||||||
header.frameNumber, header.expLength, header.packetNumber,
|
// header.frameNumber, header.expLength, header.packetNumber,
|
||||||
header.bunchId, header.timestamp, header.modId, header.row,
|
// header.bunchId, header.timestamp, header.modId, header.row,
|
||||||
header.column, header.reserved, header.debug, header.roundRNumber,
|
// header.column, header.reserved, header.debug, header.roundRNumber,
|
||||||
header.detType, header.version,
|
// header.detType, header.version,
|
||||||
|
|
||||||
// additional stuff
|
// // additional stuff
|
||||||
header.flipRows, header.quad);
|
// header.flipRows, header.quad);
|
||||||
|
|
||||||
if (!header.addJsonHeader.empty()) {
|
// if (!header.addJsonHeader.empty()) {
|
||||||
strcat(header_buffer.get(), ", ");
|
// strcat(header_buffer.get(), ", ");
|
||||||
strcat(header_buffer.get(), "\"addJsonHeader\": {");
|
// strcat(header_buffer.get(), "\"addJsonHeader\": {");
|
||||||
for (auto it = header.addJsonHeader.begin();
|
// for (auto it = header.addJsonHeader.begin();
|
||||||
it != header.addJsonHeader.end(); ++it) {
|
// it != header.addJsonHeader.end(); ++it) {
|
||||||
if (it != header.addJsonHeader.begin()) {
|
// if (it != header.addJsonHeader.begin()) {
|
||||||
strcat(header_buffer.get(), ", ");
|
// strcat(header_buffer.get(), ", ");
|
||||||
}
|
// }
|
||||||
strcat(header_buffer.get(), "\"");
|
// strcat(header_buffer.get(), "\"");
|
||||||
strcat(header_buffer.get(), it->first.c_str());
|
// strcat(header_buffer.get(), it->first.c_str());
|
||||||
strcat(header_buffer.get(), "\":\"");
|
// strcat(header_buffer.get(), "\":\"");
|
||||||
strcat(header_buffer.get(), it->second.c_str());
|
// strcat(header_buffer.get(), it->second.c_str());
|
||||||
strcat(header_buffer.get(), "\"");
|
// strcat(header_buffer.get(), "\"");
|
||||||
}
|
// }
|
||||||
strcat(header_buffer.get(), " } ");
|
// strcat(header_buffer.get(), " } ");
|
||||||
}
|
// }
|
||||||
|
|
||||||
strcat(header_buffer.get(), "}\n");
|
// strcat(header_buffer.get(), "}\n");
|
||||||
int length = strlen(header_buffer.get());
|
// int length = strlen(header_buffer.get());
|
||||||
|
|
||||||
#ifdef VERBOSE
|
// #ifdef VERBOSE
|
||||||
// if(!index)
|
// // if(!index)
|
||||||
cprintf(BLUE, "%d : Streamer: buf: %s\n", index, buf);
|
// cprintf(BLUE, "%d : Streamer: buf: %s\n", index, buf);
|
||||||
#endif
|
// #endif
|
||||||
|
|
||||||
if (zmq_send(sockfd.socketDescriptor, header_buffer.get(), length,
|
// if (zmq_send(sockfd.socketDescriptor, header_buffer.get(), length,
|
||||||
header.data ? ZMQ_SNDMORE : 0) < 0) {
|
// header.data ? ZMQ_SNDMORE : 0) < 0) {
|
||||||
PrintError();
|
// PrintError();
|
||||||
return 0;
|
// return 0;
|
||||||
}
|
// }
|
||||||
#ifdef VERBOSE
|
// #ifdef VERBOSE
|
||||||
cprintf(GREEN, "[%u] send header data\n", portno);
|
// cprintf(GREEN, "[%u] send header data\n", portno);
|
||||||
#endif
|
// #endif
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ZmqSocket::SendData(char *buf, int length) {
|
int ZmqSocket::SendData(char *buf, int length) {
|
||||||
if (zmq_send(sockfd.socketDescriptor, buf, length, 0) < 0) {
|
// if (zmq_send(sockfd.socketDescriptor, buf, length, 0) < 0) {
|
||||||
PrintError();
|
// PrintError();
|
||||||
return 0;
|
// return 0;
|
||||||
}
|
// }
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ZmqSocket::ReceiveHeader(const int index, zmqHeader &zHeader,
|
int ZmqSocket::ReceiveHeader(const int index, zmqHeader &zHeader,
|
||||||
uint32_t version) {
|
uint32_t version) {
|
||||||
const int bytes_received = zmq_recv(sockfd.socketDescriptor,
|
// const int bytes_received = zmq_recv(sockfd.socketDescriptor,
|
||||||
header_buffer.get(), MAX_STR_LENGTH, 0);
|
// header_buffer.get(), MAX_STR_LENGTH, 0);
|
||||||
if (bytes_received > 0) {
|
// if (bytes_received > 0) {
|
||||||
#ifdef ZMQ_DETAIL
|
// #ifdef ZMQ_DETAIL
|
||||||
cprintf(BLUE, "Header %d [%d] Length: %d Header:%s \n", index, portno,
|
// cprintf(BLUE, "Header %d [%d] Length: %d Header:%s \n", index, portno,
|
||||||
bytes_received, buffer.data());
|
// bytes_received, buffer.data());
|
||||||
#endif
|
// #endif
|
||||||
if (ParseHeader(index, bytes_received, header_buffer.get(), zHeader,
|
// if (ParseHeader(index, bytes_received, header_buffer.get(), zHeader,
|
||||||
version)) {
|
// version)) {
|
||||||
#ifdef ZMQ_DETAIL
|
// #ifdef ZMQ_DETAIL
|
||||||
cprintf(RED, "Parsed Header %d [%d] Length: %d Header:%s \n", index,
|
// cprintf(RED, "Parsed Header %d [%d] Length: %d Header:%s \n", index,
|
||||||
portno, bytes_received, buffer.data());
|
// portno, bytes_received, buffer.data());
|
||||||
#endif
|
// #endif
|
||||||
if (!zHeader.data) {
|
// if (!zHeader.data) {
|
||||||
#ifdef ZMQ_DETAIL
|
// #ifdef ZMQ_DETAIL
|
||||||
cprintf(RED, "%d [%d] Received end of acquisition\n", index,
|
// cprintf(RED, "%d [%d] Received end of acquisition\n", index,
|
||||||
portno);
|
// portno);
|
||||||
#endif
|
// #endif
|
||||||
return 0;
|
// return 0;
|
||||||
}
|
// }
|
||||||
#ifdef ZMQ_DETAIL
|
// #ifdef ZMQ_DETAIL
|
||||||
cprintf(GREEN, "%d [%d] data\n", index, portno);
|
// cprintf(GREEN, "%d [%d] data\n", index, portno);
|
||||||
#endif
|
// #endif
|
||||||
return 1;
|
// return 1;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -323,96 +323,96 @@ int ZmqSocket::ParseHeader(const int index, int length, char *buff,
|
|||||||
}
|
}
|
||||||
|
|
||||||
int ZmqSocket::ReceiveData(const int index, char *buf, const int size) {
|
int ZmqSocket::ReceiveData(const int index, char *buf, const int size) {
|
||||||
zmq_msg_t message;
|
// zmq_msg_t message;
|
||||||
zmq_msg_init(&message);
|
// zmq_msg_init(&message);
|
||||||
int length = ReceiveMessage(index, message);
|
// int length = ReceiveMessage(index, message);
|
||||||
if (length == size) {
|
// if (length == size) {
|
||||||
memcpy(buf, (char *)zmq_msg_data(&message), size);
|
// memcpy(buf, (char *)zmq_msg_data(&message), size);
|
||||||
} else if (length < size) {
|
// } else if (length < size) {
|
||||||
memcpy(buf, (char *)zmq_msg_data(&message), length);
|
// memcpy(buf, (char *)zmq_msg_data(&message), length);
|
||||||
memset(buf + length, 0xFF, size - length);
|
// memset(buf + length, 0xFF, size - length);
|
||||||
} else {
|
// } else {
|
||||||
LOG(logERROR) << "Received weird packet size " << length
|
// LOG(logERROR) << "Received weird packet size " << length
|
||||||
<< " for socket " << index;
|
// << " for socket " << index;
|
||||||
memset(buf, 0xFF, size);
|
// memset(buf, 0xFF, size);
|
||||||
}
|
// }
|
||||||
|
|
||||||
zmq_msg_close(&message);
|
// zmq_msg_close(&message);
|
||||||
return length;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ZmqSocket::ReceiveMessage(const int index, zmq_msg_t &message) {
|
int ZmqSocket::ReceiveMessage(const int index, zmq_msg_t &message) {
|
||||||
int length = zmq_msg_recv(&message, sockfd.socketDescriptor, 0);
|
// int length = zmq_msg_recv(&message, sockfd.socketDescriptor, 0);
|
||||||
if (length == -1) {
|
// if (length == -1) {
|
||||||
PrintError();
|
// PrintError();
|
||||||
LOG(logERROR) << "Could not read header for socket " << index;
|
// LOG(logERROR) << "Could not read header for socket " << index;
|
||||||
}
|
// }
|
||||||
return length;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ZmqSocket::PrintError() {
|
void ZmqSocket::PrintError() {
|
||||||
switch (errno) {
|
// switch (errno) {
|
||||||
case EINVAL:
|
// case EINVAL:
|
||||||
LOG(logERROR) << "The socket type/option or value/endpoint supplied is "
|
// LOG(logERROR) << "The socket type/option or value/endpoint supplied is "
|
||||||
"invalid (zmq)";
|
// "invalid (zmq)";
|
||||||
break;
|
// break;
|
||||||
case EAGAIN:
|
// case EAGAIN:
|
||||||
LOG(logERROR) << "Non-blocking mode was requested and the message "
|
// LOG(logERROR) << "Non-blocking mode was requested and the message "
|
||||||
"cannot be sent/available at the moment (zmq)";
|
// "cannot be sent/available at the moment (zmq)";
|
||||||
break;
|
// break;
|
||||||
case ENOTSUP:
|
// case ENOTSUP:
|
||||||
LOG(logERROR) << "The zmq_send()/zmq_msg_recv() operation is not "
|
// LOG(logERROR) << "The zmq_send()/zmq_msg_recv() operation is not "
|
||||||
"supported by this socket type (zmq)";
|
// "supported by this socket type (zmq)";
|
||||||
break;
|
// break;
|
||||||
case EFSM:
|
// case EFSM:
|
||||||
LOG(logERROR) << "The zmq_send()/zmq_msg_recv() unavailable now as "
|
// LOG(logERROR) << "The zmq_send()/zmq_msg_recv() unavailable now as "
|
||||||
"socket in inappropriate state (eg. ZMQ_REP). Look up "
|
// "socket in inappropriate state (eg. ZMQ_REP). Look up "
|
||||||
"messaging patterns (zmq)";
|
// "messaging patterns (zmq)";
|
||||||
break;
|
// break;
|
||||||
case EFAULT:
|
// case EFAULT:
|
||||||
LOG(logERROR) << "The provided context/message is invalid (zmq)";
|
// LOG(logERROR) << "The provided context/message is invalid (zmq)";
|
||||||
break;
|
// break;
|
||||||
case EMFILE:
|
// case EMFILE:
|
||||||
LOG(logERROR) << "The limit on the total number of open ØMQ sockets "
|
// LOG(logERROR) << "The limit on the total number of open ØMQ sockets "
|
||||||
"has been reached (zmq)";
|
// "has been reached (zmq)";
|
||||||
break;
|
// break;
|
||||||
case EPROTONOSUPPORT:
|
// case EPROTONOSUPPORT:
|
||||||
LOG(logERROR)
|
// LOG(logERROR)
|
||||||
<< "The requested transport protocol is not supported (zmq)";
|
// << "The requested transport protocol is not supported (zmq)";
|
||||||
break;
|
// break;
|
||||||
case ENOCOMPATPROTO:
|
// case ENOCOMPATPROTO:
|
||||||
LOG(logERROR) << "The requested transport protocol is not compatible "
|
// LOG(logERROR) << "The requested transport protocol is not compatible "
|
||||||
"with the socket type (zmq)";
|
// "with the socket type (zmq)";
|
||||||
break;
|
// break;
|
||||||
case EADDRINUSE:
|
// case EADDRINUSE:
|
||||||
LOG(logERROR) << "The requested address is already in use (zmq)";
|
// LOG(logERROR) << "The requested address is already in use (zmq)";
|
||||||
break;
|
// break;
|
||||||
case EADDRNOTAVAIL:
|
// case EADDRNOTAVAIL:
|
||||||
LOG(logERROR) << "The requested address was not local (zmq)";
|
// LOG(logERROR) << "The requested address was not local (zmq)";
|
||||||
break;
|
// break;
|
||||||
case ENODEV:
|
// case ENODEV:
|
||||||
LOG(logERROR)
|
// LOG(logERROR)
|
||||||
<< "The requested address specifies a nonexistent interface (zmq)";
|
// << "The requested address specifies a nonexistent interface (zmq)";
|
||||||
break;
|
// break;
|
||||||
case ETERM:
|
// case ETERM:
|
||||||
LOG(logERROR) << "The ØMQ context associated with the specified socket "
|
// LOG(logERROR) << "The ØMQ context associated with the specified socket "
|
||||||
"was terminated (zmq)";
|
// "was terminated (zmq)";
|
||||||
break;
|
// break;
|
||||||
case ENOTSOCK:
|
// case ENOTSOCK:
|
||||||
LOG(logERROR) << "The provided socket was invalid (zmq)";
|
// LOG(logERROR) << "The provided socket was invalid (zmq)";
|
||||||
break;
|
// break;
|
||||||
case EINTR:
|
// case EINTR:
|
||||||
LOG(logERROR)
|
// LOG(logERROR)
|
||||||
<< "The operation was interrupted by delivery of a signal (zmq)";
|
// << "The operation was interrupted by delivery of a signal (zmq)";
|
||||||
break;
|
// break;
|
||||||
case EMTHREAD:
|
// case EMTHREAD:
|
||||||
LOG(logERROR)
|
// LOG(logERROR)
|
||||||
<< "No I/O thread is available to accomplish the task (zmq)";
|
// << "No I/O thread is available to accomplish the task (zmq)";
|
||||||
break;
|
// break;
|
||||||
default:
|
// default:
|
||||||
LOG(logERROR) << "Unknown socket error (zmq)";
|
// LOG(logERROR) << "Unknown socket error (zmq)";
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nested class to do RAII handling of socket descriptors
|
// Nested class to do RAII handling of socket descriptors
|
||||||
@ -423,19 +423,19 @@ ZmqSocket::mySocketDescriptors::~mySocketDescriptors() {
|
|||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
void ZmqSocket::mySocketDescriptors::Disconnect() {
|
void ZmqSocket::mySocketDescriptors::Disconnect() {
|
||||||
if (server)
|
// if (server)
|
||||||
zmq_unbind(socketDescriptor, serverAddress.c_str());
|
// zmq_unbind(socketDescriptor, serverAddress.c_str());
|
||||||
else
|
// else
|
||||||
zmq_disconnect(socketDescriptor, serverAddress.c_str());
|
// zmq_disconnect(socketDescriptor, serverAddress.c_str());
|
||||||
};
|
};
|
||||||
void ZmqSocket::mySocketDescriptors::Close() {
|
void ZmqSocket::mySocketDescriptors::Close() {
|
||||||
if (socketDescriptor != nullptr) {
|
// if (socketDescriptor != nullptr) {
|
||||||
zmq_close(socketDescriptor);
|
// zmq_close(socketDescriptor);
|
||||||
socketDescriptor = nullptr;
|
// socketDescriptor = nullptr;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (contextDescriptor != nullptr) {
|
// if (contextDescriptor != nullptr) {
|
||||||
zmq_ctx_destroy(contextDescriptor);
|
// zmq_ctx_destroy(contextDescriptor);
|
||||||
contextDescriptor = nullptr;
|
// contextDescriptor = nullptr;
|
||||||
}
|
// }
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user