From 7329cb44285c125964be30b93ed4bc1420854fb4 Mon Sep 17 00:00:00 2001 From: Dennis Klein Date: Tue, 12 Oct 2021 18:12:28 +0200 Subject: [PATCH] refactor: Deduplicate GetConstant() * Deprecate its old name in the Socket classes --- fairmq/CMakeLists.txt | 1 + fairmq/shmem/Socket.h | 42 +++++------------------------- fairmq/zeromq/Common.h | 58 ++++++++++++++++++++++++++++++++++++++++++ fairmq/zeromq/Socket.h | 42 +++++------------------------- 4 files changed, 72 insertions(+), 71 deletions(-) create mode 100644 fairmq/zeromq/Common.h diff --git a/fairmq/CMakeLists.txt b/fairmq/CMakeLists.txt index cfdb788f..3cf5847a 100644 --- a/fairmq/CMakeLists.txt +++ b/fairmq/CMakeLists.txt @@ -211,6 +211,7 @@ if(BUILD_FAIRMQ) shmem/Common.h shmem/Manager.h shmem/Region.h + zeromq/Common.h zeromq/Context.h zeromq/Message.h zeromq/Poller.h diff --git a/fairmq/shmem/Socket.h b/fairmq/shmem/Socket.h index d917a751..ced656e1 100644 --- a/fairmq/shmem/Socket.h +++ b/fairmq/shmem/Socket.h @@ -16,6 +16,7 @@ #include #include #include +#include #include @@ -68,7 +69,7 @@ class Socket final : public fair::mq::Socket throw SocketError("PUB/SUB socket type is not supported for shared memory transport"); } - fSocket = zmq_socket(context, GetConstant(type)); + fSocket = zmq_socket(context, zmq::getConstant(type)); if (fSocket == nullptr) { LOG(error) << "Failed creating socket " << fId << ", reason: " << zmq_strerror(errno); @@ -361,14 +362,14 @@ class Socket final : public fair::mq::Socket void SetOption(const std::string& option, const void* value, size_t valueSize) override { - if (zmq_setsockopt(fSocket, GetConstant(option), value, valueSize) < 0) { + if (zmq_setsockopt(fSocket, zmq::getConstant(option), value, valueSize) < 0) { LOG(error) << "Failed setting socket option, reason: " << zmq_strerror(errno); } } void GetOption(const std::string& option, void* value, size_t* valueSize) override { - if (zmq_getsockopt(fSocket, GetConstant(option), value, valueSize) < 0) { + if (zmq_getsockopt(fSocket, zmq::getConstant(option), value, valueSize) < 0) { LOG(error) << "Failed getting socket option, reason: " << zmq_strerror(errno); } } @@ -469,39 +470,8 @@ class Socket final : public fair::mq::Socket unsigned long GetMessagesTx() const override { return fMessagesTx; } unsigned long GetMessagesRx() const override { return fMessagesRx; } - static int GetConstant(const std::string& constant) - { - if (constant.empty()) { return 0; } - if (constant == "sub") { return ZMQ_SUB; } - if (constant == "pub") { return ZMQ_PUB; } - if (constant == "xsub") { return ZMQ_XSUB; } - if (constant == "xpub") { return ZMQ_XPUB; } - if (constant == "push") { return ZMQ_PUSH; } - if (constant == "pull") { return ZMQ_PULL; } - if (constant == "req") { return ZMQ_REQ; } - if (constant == "rep") { return ZMQ_REP; } - if (constant == "dealer") { return ZMQ_DEALER; } - if (constant == "router") { return ZMQ_ROUTER; } - if (constant == "pair") { return ZMQ_PAIR; } - - if (constant == "snd-hwm") { return ZMQ_SNDHWM; } - if (constant == "rcv-hwm") { return ZMQ_RCVHWM; } - if (constant == "snd-size") { return ZMQ_SNDBUF; } - if (constant == "rcv-size") { return ZMQ_RCVBUF; } - if (constant == "snd-more") { return ZMQ_SNDMORE; } - if (constant == "rcv-more") { return ZMQ_RCVMORE; } - - if (constant == "linger") { return ZMQ_LINGER; } - if (constant == "no-block") { return ZMQ_DONTWAIT; } - if (constant == "snd-more no-block") { return ZMQ_DONTWAIT|ZMQ_SNDMORE; } - - if (constant == "fd") { return ZMQ_FD; } - if (constant == "events") { return ZMQ_EVENTS; } - if (constant == "pollin") { return ZMQ_POLLIN; } - if (constant == "pollout") { return ZMQ_POLLOUT; } - - throw SocketError(tools::ToString("GetConstant called with an invalid argument: ", constant)); - } + [[deprecated("Use fair::mq::zmq::getConstant() from instead.")]] + static int GetConstant(const std::string& constant) { return zmq::getConstant(constant); } ~Socket() override { Close(); } diff --git a/fairmq/zeromq/Common.h b/fairmq/zeromq/Common.h new file mode 100644 index 00000000..912a716b --- /dev/null +++ b/fairmq/zeromq/Common.h @@ -0,0 +1,58 @@ +/******************************************************************************** + * Copyright (C) 2021 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * * + * This software is distributed under the terms of the * + * GNU Lesser General Public Licence (LGPL) version 3, * + * copied verbatim in the file "LICENSE" * + ********************************************************************************/ +#ifndef FAIR_MQ_ZMQ_COMMON_H +#define FAIR_MQ_ZMQ_COMMON_H + +#include +#include +#include +#include + +namespace fair::mq::zmq +{ + +struct Error : std::runtime_error { using std::runtime_error::runtime_error; }; + +/// Lookup table for various zmq constants +inline auto getConstant(std::string_view constant) -> int +{ + if (constant.empty()) { return 0; } + if (constant == "sub") { return ZMQ_SUB; } + if (constant == "pub") { return ZMQ_PUB; } + if (constant == "xsub") { return ZMQ_XSUB; } + if (constant == "xpub") { return ZMQ_XPUB; } + if (constant == "push") { return ZMQ_PUSH; } + if (constant == "pull") { return ZMQ_PULL; } + if (constant == "req") { return ZMQ_REQ; } + if (constant == "rep") { return ZMQ_REP; } + if (constant == "dealer") { return ZMQ_DEALER; } + if (constant == "router") { return ZMQ_ROUTER; } + if (constant == "pair") { return ZMQ_PAIR; } + + if (constant == "snd-hwm") { return ZMQ_SNDHWM; } + if (constant == "rcv-hwm") { return ZMQ_RCVHWM; } + if (constant == "snd-size") { return ZMQ_SNDBUF; } + if (constant == "rcv-size") { return ZMQ_RCVBUF; } + if (constant == "snd-more") { return ZMQ_SNDMORE; } + if (constant == "rcv-more") { return ZMQ_RCVMORE; } + + if (constant == "linger") { return ZMQ_LINGER; } + if (constant == "no-block") { return ZMQ_DONTWAIT; } + if (constant == "snd-more no-block") { return ZMQ_DONTWAIT|ZMQ_SNDMORE; } + + if (constant == "fd") { return ZMQ_FD; } + if (constant == "events") { return ZMQ_EVENTS; } + if (constant == "pollin") { return ZMQ_POLLIN; } + if (constant == "pollout") { return ZMQ_POLLOUT; } + + throw Error(tools::ToString("getConstant called with an invalid argument: ", constant)); +} + +} // namespace fair::mq::zmq + +#endif /* FAIR_MQ_ZMQ_COMMON_H */ diff --git a/fairmq/zeromq/Socket.h b/fairmq/zeromq/Socket.h index 0081e459..c5a13943 100644 --- a/fairmq/zeromq/Socket.h +++ b/fairmq/zeromq/Socket.h @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (C) 2014-2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2014-2021 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -30,7 +31,7 @@ class Socket final : public fair::mq::Socket Socket(Context& ctx, const std::string& type, const std::string& name, const std::string& id, FairMQTransportFactory* factory = nullptr) : fair::mq::Socket(factory) , fCtx(ctx) - , fSocket(zmq_socket(fCtx.GetZmqCtx(), GetConstant(type))) + , fSocket(zmq_socket(fCtx.GetZmqCtx(), getConstant(type))) , fId(id + "." + name + "." + type) , fBytesTx(0) , fBytesRx(0) @@ -313,14 +314,14 @@ class Socket final : public fair::mq::Socket void SetOption(const std::string& option, const void* value, size_t valueSize) override { - if (zmq_setsockopt(fSocket, GetConstant(option), value, valueSize) < 0) { + if (zmq_setsockopt(fSocket, getConstant(option), value, valueSize) < 0) { LOG(error) << "Failed setting socket option, reason: " << zmq_strerror(errno); } } void GetOption(const std::string& option, void* value, size_t* valueSize) override { - if (zmq_getsockopt(fSocket, GetConstant(option), value, valueSize) < 0) { + if (zmq_getsockopt(fSocket, getConstant(option), value, valueSize) < 0) { LOG(error) << "Failed getting socket option, reason: " << zmq_strerror(errno); } } @@ -421,37 +422,8 @@ class Socket final : public fair::mq::Socket unsigned long GetMessagesTx() const override { return fMessagesTx; } unsigned long GetMessagesRx() const override { return fMessagesRx; } - static int GetConstant(const std::string& constant) - { - if (constant.empty()) { return 0; } - if (constant == "sub") { return ZMQ_SUB; } - if (constant == "pub") { return ZMQ_PUB; } - if (constant == "xsub") { return ZMQ_XSUB; } - if (constant == "xpub") { return ZMQ_XPUB; } - if (constant == "push") { return ZMQ_PUSH; } - if (constant == "pull") { return ZMQ_PULL; } - if (constant == "req") { return ZMQ_REQ; } - if (constant == "rep") { return ZMQ_REP; } - if (constant == "dealer") { return ZMQ_DEALER; } - if (constant == "router") { return ZMQ_ROUTER; } - if (constant == "pair") { return ZMQ_PAIR; } - - if (constant == "snd-hwm") { return ZMQ_SNDHWM; } - if (constant == "rcv-hwm") { return ZMQ_RCVHWM; } - if (constant == "snd-size") { return ZMQ_SNDBUF; } - if (constant == "rcv-size") { return ZMQ_RCVBUF; } - if (constant == "snd-more") { return ZMQ_SNDMORE; } - if (constant == "rcv-more") { return ZMQ_RCVMORE; } - - if (constant == "linger") { return ZMQ_LINGER; } - - if (constant == "fd") { return ZMQ_FD; } - if (constant == "events") { return ZMQ_EVENTS; } - if (constant == "pollin") { return ZMQ_POLLIN; } - if (constant == "pollout") { return ZMQ_POLLOUT; } - - throw SocketError(tools::ToString("GetConstant called with an invalid argument: ", constant)); - } + [[deprecated("Use fair::mq::zmq::getConstant() from instead.")]] + static int GetConstant(const std::string& constant) { return getConstant(constant); } ~Socket() override { Close(); }