diff --git a/CMakeLists.txt b/CMakeLists.txt index 453ccd2..4263f22 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,10 +1,10 @@ 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) - set(MESYTEC_NODE_MAIN_PROJECT ON) + set(MESYTEC_MNODE_MAIN_PROJECT ON) endif() set(CMAKE_EXPORT_COMPILE_COMMANDS ON) @@ -16,12 +16,11 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Protobuf REQUIRED) -#protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS proto/service.proto) add_library(mnode-proto proto/service.proto) target_link_libraries(mnode-proto PUBLIC protobuf::libprotobuf) target_include_directories(mnode-proto PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) + 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) add_custom_target(mnode-proto-py ALL DEPENDS ${PROTO_PY}) diff --git a/include/mesytec-node/mesytec_node_nng.h b/include/mesytec-mnode/mnode_nng.h similarity index 96% rename from include/mesytec-node/mesytec_node_nng.h rename to include/mesytec-mnode/mnode_nng.h index 02e40f5..c71fc25 100644 --- a/include/mesytec-node/mesytec_node_nng.h +++ b/include/mesytec-mnode/mnode_nng.h @@ -14,7 +14,7 @@ #include -namespace mesytec::nng +namespace mesytec::mnode::nng { 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); } +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 */ diff --git a/include/mesytec-mnode/mnode_nng_async.h b/include/mesytec-mnode/mnode_nng_async.h new file mode 100644 index 0000000..4079fc7 --- /dev/null +++ b/include/mesytec-mnode/mnode_nng_async.h @@ -0,0 +1,25 @@ +#ifndef C7F35237_1097_46F2_9573_490355317C24 +#define C7F35237_1097_46F2_9573_490355317C24 + +#include + +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 */ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0429290..dfb2e65 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 # 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}) -add_library(mesytec-node-nng mesytec_node_nng.cc) -target_include_directories(mesytec-node-nng PUBLIC $) -target_link_libraries(mesytec-node-nng PUBLIC nng PUBLIC spdlog) -target_compile_features(mesytec-node-nng PUBLIC cxx_std_17) +add_library(mesytec-mnode-nng mnode_nng.cc) +target_include_directories(mesytec-mnode-nng PUBLIC $) +target_link_libraries(mesytec-mnode-nng PUBLIC nng PUBLIC spdlog) +target_compile_features(mesytec-mnode-nng PUBLIC cxx_std_17) -add_library(mesytec-node-dev INTERFACE) -target_link_libraries(mesytec-node-dev INTERFACE mesytec-node-nng mesytec-mvlc) +add_library(mesytec-mnode-dev INTERFACE) +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) - target_link_libraries(${name} PRIVATE mesytec-node-dev) - target_compile_options(${name} PRIVATE ${MVLC_NNG_NODE_WARN_FLAGS}) + target_link_libraries(${name} PRIVATE mesytec-mnode-dev) + target_compile_options(${name} PRIVATE ${MVLC_NNG_MNODE_WARN_FLAGS}) endfunction() -function(add_node_proto_dev_executable name) - add_node_dev_executable(${name}) +function(add_mnode_proto_dev_executable name) + add_mnode_dev_executable(${name}) target_link_libraries(${name} PRIVATE mnode-proto mesytec-mvlc) endfunction() -add_node_dev_executable(pair_producer) -add_node_dev_executable(pair_consumer) -add_node_dev_executable(pair_inproc) -add_node_dev_executable(mvlc_nng_replay) -add_node_dev_executable(mesy_nng_pipeline_main) -add_node_dev_executable(mesy_nng_push_pull_main) -add_node_dev_executable(mesy_nng_pub_producer) -add_node_dev_executable(mesy_nng_sub_consumer) +add_mnode_dev_executable(pair_producer) +add_mnode_dev_executable(pair_consumer) +add_mnode_dev_executable(pair_inproc) +add_mnode_dev_executable(mvlc_nng_replay) +add_mnode_dev_executable(mesy_nng_pipeline_main) +add_mnode_dev_executable(mesy_nng_push_pull_main) +add_mnode_dev_executable(mesy_nng_pub_producer) +add_mnode_dev_executable(mesy_nng_sub_consumer) -add_node_proto_dev_executable(mnode_proto_test1) -add_node_proto_dev_executable(mnode_proto_ping_client) -add_node_proto_dev_executable(mnode_proto_ping_server) +add_mnode_proto_dev_executable(mnode_proto_test1) +add_mnode_proto_dev_executable(mnode_proto_ping_client) +add_mnode_proto_dev_executable(mnode_proto_ping_server) #add_subdirectory(qt) diff --git a/src/mesy_nng_pipeline_main.cc b/src/mesy_nng_pipeline_main.cc index 12ea751..f85835d 100644 --- a/src/mesy_nng_pipeline_main.cc +++ b/src/mesy_nng_pipeline_main.cc @@ -1,7 +1,7 @@ #include -#include +#include -using namespace mesytec::nng; +using namespace mesytec::mnode::nng; struct PipelineElement { diff --git a/src/mesy_nng_push_pull_main.cc b/src/mesy_nng_push_pull_main.cc index 55ee0fd..886b90a 100644 --- a/src/mesy_nng_push_pull_main.cc +++ b/src/mesy_nng_push_pull_main.cc @@ -1,12 +1,12 @@ -#include +#include #include #include #include #include "test_producer_consumer.h" -#include +#include -using namespace mesytec::nng; +using namespace mesytec::mnode::nng; struct Header { diff --git a/src/mesytec_node_nng.cc b/src/mnode_nng.cc similarity index 91% rename from src/mesytec_node_nng.cc rename to src/mnode_nng.cc index 8bedd41..d1c097e 100644 --- a/src/mesytec_node_nng.cc +++ b/src/mnode_nng.cc @@ -1,6 +1,6 @@ -#include "mesytec-node/mesytec_node_nng.h" +#include -namespace mesytec::nng +namespace mesytec::mnode::nng { nng_socket make_pair_socket(nng_duration timeout) diff --git a/src/mnode_proto_ping_client.cc b/src/mnode_proto_ping_client.cc index 16c52f3..84c8962 100644 --- a/src/mnode_proto_ping_client.cc +++ b/src/mnode_proto_ping_client.cc @@ -3,9 +3,10 @@ #include #include #include "proto/service.pb.h" -#include +#include using namespace mesytec; +using namespace mesytec::mnode; using namespace std::literals; void client_cb(void *arg); @@ -15,6 +16,7 @@ class Work public: explicit Work(nng_socket socket) : socket(socket) + , request_(nng::make_unique_msg()) { } @@ -33,7 +35,8 @@ class Work nng_ctx_open(&ctx, socket); 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); break; @@ -43,7 +46,8 @@ class Work nng::mesy_nng_error("nng_ctx_send", rv); nng::make_unique_msg(nng_aio_get_msg(aio)).reset(); 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); } else @@ -58,15 +62,17 @@ class Work { nng::mesy_nng_error("nng_ctx_recv", rv); 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); } else { 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; - 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); } @@ -77,16 +83,16 @@ class Work nng::unique_msg make_request() { - mnode::Ping ping; + Ping ping; ping.set_peer_id(42); ping.set_sequence_number(++sequence_number); 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.ParseFromArray(nng_msg_body(msg.get()), nng_msg_len(msg.get())); + Pong pong; + pong.ParseFromArray(nng_msg_body(reply.get()), nng_msg_len(reply.get())); if (pong.peer_id() != 42 || pong.sequence_number() != sequence_number) { spdlog::error("received pong with unexpected values: {}", pong.ShortDebugString()); @@ -106,13 +112,15 @@ class Work RECEIVE, // receive response }; - mnode::Ping ping; - mnode::Pong pong; size_t sequence_number = 0; State state = INIT; nng_socket socket; nng_aio *aio = nullptr; nng_ctx ctx; + + nng::unique_msg request_; + Ping ping; + Pong pong; }; void client_cb(void *arg) diff --git a/src/mnode_proto_ping_server.cc b/src/mnode_proto_ping_server.cc index fd75ff6..0ac204e 100644 --- a/src/mnode_proto_ping_server.cc +++ b/src/mnode_proto_ping_server.cc @@ -3,9 +3,10 @@ #include #include #include "proto/service.pb.h" -#include +#include using namespace mesytec; +using namespace mesytec::mnode; using namespace std::literals; void server_cb(void *arg); diff --git a/src/mnode_proto_test1.cc b/src/mnode_proto_test1.cc index c829352..b0b1e4b 100644 --- a/src/mnode_proto_test1.cc +++ b/src/mnode_proto_test1.cc @@ -1,8 +1,9 @@ #include #include "proto/service.pb.h" -#include +#include using namespace mesytec; +using namespace mesytec::mnode; using namespace std::literals; void requester(nng_socket socket, unsigned id) diff --git a/src/mvlc_nng_replay.cc b/src/mvlc_nng_replay.cc index 5eee331..f75fbb6 100644 --- a/src/mvlc_nng_replay.cc +++ b/src/mvlc_nng_replay.cc @@ -3,11 +3,11 @@ #include #include #include -#include +#include using namespace mesytec; using namespace mesytec::mvlc; -using namespace mesytec::nng; +using namespace mesytec::mnode::nng; static const size_t DefaultOutputMessageReserve = mvlc::util::Megabytes(1); diff --git a/src/test_producer_consumer.h b/src/test_producer_consumer.h index f4010b0..1ab5577 100644 --- a/src/test_producer_consumer.h +++ b/src/test_producer_consumer.h @@ -1,9 +1,9 @@ #ifndef E6EFFE63_EB2C_4573_B723_61840850BBF6 #define E6EFFE63_EB2C_4573_B723_61840850BBF6 -#include +#include -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 BuffersToSend = 100000u;