refactor namespaces

This commit is contained in:
Florian Lüke 2024-12-07 16:48:24 +01:00
parent ce338ce680
commit d3a5f2c75c
12 changed files with 105 additions and 54 deletions

View file

@ -1,10 +1,10 @@
cmake_minimum_required(VERSION 3.15) cmake_minimum_required(VERSION 3.15)
project(mesytec-node LANGUAGES CXX) project(mesytec-mnode LANGUAGES CXX)
set(MESYTEC_NODE_MAIN_PROJECT OFF) set(MESYTEC_MNODE_MAIN_PROJECT OFF)
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(MESYTEC_NODE_MAIN_PROJECT ON) set(MESYTEC_MNODE_MAIN_PROJECT ON)
endif() endif()
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
@ -16,12 +16,11 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Protobuf REQUIRED) find_package(Protobuf REQUIRED)
#protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS proto/service.proto)
add_library(mnode-proto proto/service.proto) add_library(mnode-proto proto/service.proto)
target_link_libraries(mnode-proto PUBLIC protobuf::libprotobuf) target_link_libraries(mnode-proto PUBLIC protobuf::libprotobuf)
target_include_directories(mnode-proto PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) target_include_directories(mnode-proto PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
protobuf_generate(TARGET mnode-proto) protobuf_generate(TARGET mnode-proto)
#protobuf_generate(TARGET mnode-proto-py LANGUAGE python PROTOS proto/service.proto)
protobuf_generate_python(PROTO_PY proto/service.proto) protobuf_generate_python(PROTO_PY proto/service.proto)
add_custom_target(mnode-proto-py ALL DEPENDS ${PROTO_PY}) add_custom_target(mnode-proto-py ALL DEPENDS ${PROTO_PY})

View file

@ -14,7 +14,7 @@
#include <optional> #include <optional>
namespace mesytec::nng namespace mesytec::mnode::nng
{ {
inline void mesy_nng_fatal(const char *const msg, int rv) inline void mesy_nng_fatal(const char *const msg, int rv)
@ -424,6 +424,23 @@ inline unique_msg make_message(const std::string &data)
return make_unique_msg(msg); return make_unique_msg(msg);
} }
inline unique_msg clone_message(const nng_msg *msg)
{
nng_msg *newMsg = nullptr;
if (int res = nng_msg_dup(&newMsg, msg))
{
mesy_nng_error("nng_msg_dup", res);
return make_unique_msg();
}
return make_unique_msg(newMsg);
}
inline unique_msg clone_message(const unique_msg &msg)
{
return clone_message(msg.get());
}
} }
#endif /* B18E3651_CA9A_43BC_AA25_810EA16533CD */ #endif /* B18E3651_CA9A_43BC_AA25_810EA16533CD */

View file

@ -0,0 +1,25 @@
#ifndef C7F35237_1097_46F2_9573_490355317C24
#define C7F35237_1097_46F2_9573_490355317C24
#include <mesytec-node/mesytec_node_nng.h>
namespace mesytec::mnode::nng
{
class IWork
{
public:
virtual ~IWork() = default;
virtual void work() = 0;
};
class IAsyncReqRepWork: public IWork
{
public:
virtual nng::unique_msg make_request() = 0;
virtual void handle_reply(nng::unique_msg &&request, nng::unique_msg &&reply) = 0;
};
}
#endif /* C7F35237_1097_46F2_9573_490355317C24 */

View file

@ -1,4 +1,4 @@
set(MVLC_NNG_NODE_WARN_FLAGS -Wall -Wextra -Wpedantic) set(MVLC_NNG_MNODE_WARN_FLAGS -Wall -Wextra -Wpedantic)
# Bit of a hack to set the variables here. If set earlier clang-tidy will for # Bit of a hack to set the variables here. If set earlier clang-tidy will for
# some reason pickup log.c and warn about some va_list stuff. Might be because # some reason pickup log.c and warn about some va_list stuff. Might be because
@ -20,37 +20,37 @@ set(MVLC_NNG_NODE_WARN_FLAGS -Wall -Wextra -Wpedantic)
#) #)
#target_include_directories(dp_common INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) #target_include_directories(dp_common INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
add_library(mesytec-node-nng mesytec_node_nng.cc) add_library(mesytec-mnode-nng mnode_nng.cc)
target_include_directories(mesytec-node-nng PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>) target_include_directories(mesytec-mnode-nng PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>)
target_link_libraries(mesytec-node-nng PUBLIC nng PUBLIC spdlog) target_link_libraries(mesytec-mnode-nng PUBLIC nng PUBLIC spdlog)
target_compile_features(mesytec-node-nng PUBLIC cxx_std_17) target_compile_features(mesytec-mnode-nng PUBLIC cxx_std_17)
add_library(mesytec-node-dev INTERFACE) add_library(mesytec-mnode-dev INTERFACE)
target_link_libraries(mesytec-node-dev INTERFACE mesytec-node-nng mesytec-mvlc) target_link_libraries(mesytec-mnode-dev INTERFACE mesytec-mnode-nng mesytec-mvlc)
function(add_node_dev_executable name) function(add_mnode_dev_executable name)
add_executable(${name} ${name}.cc) add_executable(${name} ${name}.cc)
target_link_libraries(${name} PRIVATE mesytec-node-dev) target_link_libraries(${name} PRIVATE mesytec-mnode-dev)
target_compile_options(${name} PRIVATE ${MVLC_NNG_NODE_WARN_FLAGS}) target_compile_options(${name} PRIVATE ${MVLC_NNG_MNODE_WARN_FLAGS})
endfunction() endfunction()
function(add_node_proto_dev_executable name) function(add_mnode_proto_dev_executable name)
add_node_dev_executable(${name}) add_mnode_dev_executable(${name})
target_link_libraries(${name} PRIVATE mnode-proto mesytec-mvlc) target_link_libraries(${name} PRIVATE mnode-proto mesytec-mvlc)
endfunction() endfunction()
add_node_dev_executable(pair_producer) add_mnode_dev_executable(pair_producer)
add_node_dev_executable(pair_consumer) add_mnode_dev_executable(pair_consumer)
add_node_dev_executable(pair_inproc) add_mnode_dev_executable(pair_inproc)
add_node_dev_executable(mvlc_nng_replay) add_mnode_dev_executable(mvlc_nng_replay)
add_node_dev_executable(mesy_nng_pipeline_main) add_mnode_dev_executable(mesy_nng_pipeline_main)
add_node_dev_executable(mesy_nng_push_pull_main) add_mnode_dev_executable(mesy_nng_push_pull_main)
add_node_dev_executable(mesy_nng_pub_producer) add_mnode_dev_executable(mesy_nng_pub_producer)
add_node_dev_executable(mesy_nng_sub_consumer) add_mnode_dev_executable(mesy_nng_sub_consumer)
add_node_proto_dev_executable(mnode_proto_test1) add_mnode_proto_dev_executable(mnode_proto_test1)
add_node_proto_dev_executable(mnode_proto_ping_client) add_mnode_proto_dev_executable(mnode_proto_ping_client)
add_node_proto_dev_executable(mnode_proto_ping_server) add_mnode_proto_dev_executable(mnode_proto_ping_server)
#add_subdirectory(qt) #add_subdirectory(qt)

View file

@ -1,7 +1,7 @@
#include <future> #include <future>
#include <mesytec-node/mesytec_node_nng.h> #include <mesytec-mnode/mnode_nng.h>
using namespace mesytec::nng; using namespace mesytec::mnode::nng;
struct PipelineElement struct PipelineElement
{ {

View file

@ -1,12 +1,12 @@
#include <mesytec-node/mesytec_node_nng.h> #include <mesytec-mnode/mnode_nng.h>
#include <nng/protocol/pipeline0/push.h> #include <nng/protocol/pipeline0/push.h>
#include <nng/protocol/pipeline0/pull.h> #include <nng/protocol/pipeline0/pull.h>
#include <thread> #include <thread>
#include "test_producer_consumer.h" #include "test_producer_consumer.h"
#include <mesytec-node/mesytec_node_nng.h> #include <mesytec-mnode/mnode_nng.h>
using namespace mesytec::nng; using namespace mesytec::mnode::nng;
struct Header struct Header
{ {

View file

@ -1,6 +1,6 @@
#include "mesytec-node/mesytec_node_nng.h" #include <mesytec-mnode/mnode_nng.h>
namespace mesytec::nng namespace mesytec::mnode::nng
{ {
nng_socket make_pair_socket(nng_duration timeout) nng_socket make_pair_socket(nng_duration timeout)

View file

@ -3,9 +3,10 @@
#include <mesytec-mvlc/mesytec-mvlc.h> #include <mesytec-mvlc/mesytec-mvlc.h>
#include <mesytec-mvlc/util/signal_handling.h> #include <mesytec-mvlc/util/signal_handling.h>
#include "proto/service.pb.h" #include "proto/service.pb.h"
#include <mesytec-node/mesytec_node_nng.h> #include <mesytec-mnode/mnode_nng.h>
using namespace mesytec; using namespace mesytec;
using namespace mesytec::mnode;
using namespace std::literals; using namespace std::literals;
void client_cb(void *arg); void client_cb(void *arg);
@ -15,6 +16,7 @@ class Work
public: public:
explicit Work(nng_socket socket) explicit Work(nng_socket socket)
: socket(socket) : socket(socket)
, request_(nng::make_unique_msg())
{ {
} }
@ -33,7 +35,8 @@ class Work
nng_ctx_open(&ctx, socket); nng_ctx_open(&ctx, socket);
state = SEND; state = SEND;
nng_aio_set_msg(aio, make_request().release()); request_ = make_request();
nng_aio_set_msg(aio, nng::clone_message(request_.get()).release());
nng_ctx_send(ctx, aio); nng_ctx_send(ctx, aio);
break; break;
@ -43,7 +46,8 @@ class Work
nng::mesy_nng_error("nng_ctx_send", rv); nng::mesy_nng_error("nng_ctx_send", rv);
nng::make_unique_msg(nng_aio_get_msg(aio)).reset(); nng::make_unique_msg(nng_aio_get_msg(aio)).reset();
state = SEND; state = SEND;
nng_aio_set_msg(aio, make_request().release()); request_ = make_request();
nng_aio_set_msg(aio, nng::clone_message(request_.get()).release());
nng_ctx_send(ctx, aio); nng_ctx_send(ctx, aio);
} }
else else
@ -58,15 +62,17 @@ class Work
{ {
nng::mesy_nng_error("nng_ctx_recv", rv); nng::mesy_nng_error("nng_ctx_recv", rv);
state = SEND; state = SEND;
nng_aio_set_msg(aio, make_request().release()); request_ = make_request();
nng_aio_set_msg(aio, nng::clone_message(request_.get()).release());
nng_ctx_send(ctx, aio); nng_ctx_send(ctx, aio);
} }
else else
{ {
auto reply = nng::make_unique_msg(nng_aio_get_msg(aio)); auto reply = nng::make_unique_msg(nng_aio_get_msg(aio));
handle_reply(std::move(reply)); handle_reply(std::move(request_), std::move(reply));
state = SEND; state = SEND;
nng_aio_set_msg(aio, make_request().release()); request_ = make_request();
nng_aio_set_msg(aio, nng::clone_message(request_.get()).release());
nng_ctx_send(ctx, aio); nng_ctx_send(ctx, aio);
} }
@ -77,16 +83,16 @@ class Work
nng::unique_msg make_request() nng::unique_msg make_request()
{ {
mnode::Ping ping; Ping ping;
ping.set_peer_id(42); ping.set_peer_id(42);
ping.set_sequence_number(++sequence_number); ping.set_sequence_number(++sequence_number);
return nng::make_message(ping.SerializeAsString()); return nng::make_message(ping.SerializeAsString());
} }
void handle_reply(nng::unique_msg &&msg) void handle_reply(nng::unique_msg &&request, nng::unique_msg &&reply)
{ {
mnode::Pong pong; Pong pong;
pong.ParseFromArray(nng_msg_body(msg.get()), nng_msg_len(msg.get())); pong.ParseFromArray(nng_msg_body(reply.get()), nng_msg_len(reply.get()));
if (pong.peer_id() != 42 || pong.sequence_number() != sequence_number) if (pong.peer_id() != 42 || pong.sequence_number() != sequence_number)
{ {
spdlog::error("received pong with unexpected values: {}", pong.ShortDebugString()); spdlog::error("received pong with unexpected values: {}", pong.ShortDebugString());
@ -106,13 +112,15 @@ class Work
RECEIVE, // receive response RECEIVE, // receive response
}; };
mnode::Ping ping;
mnode::Pong pong;
size_t sequence_number = 0; size_t sequence_number = 0;
State state = INIT; State state = INIT;
nng_socket socket; nng_socket socket;
nng_aio *aio = nullptr; nng_aio *aio = nullptr;
nng_ctx ctx; nng_ctx ctx;
nng::unique_msg request_;
Ping ping;
Pong pong;
}; };
void client_cb(void *arg) void client_cb(void *arg)

View file

@ -3,9 +3,10 @@
#include <mesytec-mvlc/mesytec-mvlc.h> #include <mesytec-mvlc/mesytec-mvlc.h>
#include <mesytec-mvlc/util/signal_handling.h> #include <mesytec-mvlc/util/signal_handling.h>
#include "proto/service.pb.h" #include "proto/service.pb.h"
#include <mesytec-node/mesytec_node_nng.h> #include <mesytec-mnode/mnode_nng.h>
using namespace mesytec; using namespace mesytec;
using namespace mesytec::mnode;
using namespace std::literals; using namespace std::literals;
void server_cb(void *arg); void server_cb(void *arg);

View file

@ -1,8 +1,9 @@
#include <mesytec-mvlc/mesytec-mvlc.h> #include <mesytec-mvlc/mesytec-mvlc.h>
#include "proto/service.pb.h" #include "proto/service.pb.h"
#include <mesytec-node/mesytec_node_nng.h> #include <mesytec-mnode/mnode_nng.h>
using namespace mesytec; using namespace mesytec;
using namespace mesytec::mnode;
using namespace std::literals; using namespace std::literals;
void requester(nng_socket socket, unsigned id) void requester(nng_socket socket, unsigned id)

View file

@ -3,11 +3,11 @@
#include <mesytec-mvlc/mesytec-mvlc.h> #include <mesytec-mvlc/mesytec-mvlc.h>
#include <nng/nng.h> #include <nng/nng.h>
#include <sys/prctl.h> #include <sys/prctl.h>
#include <mesytec-node/mesytec_node_nng.h> #include <mesytec-mnode/mnode_nng.h>
using namespace mesytec; using namespace mesytec;
using namespace mesytec::mvlc; using namespace mesytec::mvlc;
using namespace mesytec::nng; using namespace mesytec::mnode::nng;
static const size_t DefaultOutputMessageReserve = mvlc::util::Megabytes(1); static const size_t DefaultOutputMessageReserve = mvlc::util::Megabytes(1);

View file

@ -1,9 +1,9 @@
#ifndef E6EFFE63_EB2C_4573_B723_61840850BBF6 #ifndef E6EFFE63_EB2C_4573_B723_61840850BBF6
#define E6EFFE63_EB2C_4573_B723_61840850BBF6 #define E6EFFE63_EB2C_4573_B723_61840850BBF6
#include <mesytec-node/mesytec_node_nng.h> #include <mesytec-mnode/mnode_nng.h>
using namespace mesytec::nng; // to make the old code compile. for test code only. using namespace mesytec::mnode::nng; // to make the old code compile. for test code only.
static const size_t BufferSize = 1024u * 1024u; static const size_t BufferSize = 1024u * 1024u;
static const size_t BuffersToSend = 100000u; static const size_t BuffersToSend = 100000u;