This is an automated email from the ASF dual-hosted git repository. pnoltes pushed a commit to branch feature/use_ser_hander_in_psa in repository https://gitbox.apache.org/repos/asf/celix.git
commit 3695f46559a4d4c4b25b70b690890dc33492de65 Author: Pepijn Noltes <[email protected]> AuthorDate: Sat May 22 16:02:15 2021 +0200 Adds pubsub message serialization marker service to pubsub provided. Also refactors pubsub_utils so that it does not expose Celix::pubsub_spi --- .../pubsub_serializer_avrobin/gtest/CMakeLists.txt | 2 +- .../pubsub_serializer_json/gtest/CMakeLists.txt | 2 +- .../include/pubsub_message_serialization_service.h | 19 ++++----------- bundles/pubsub/pubsub_utils/CMakeLists.txt | 3 ++- bundles/pubsub/pubsub_utils/gtest/CMakeLists.txt | 2 +- .../gtest/src/PubSubMatchingTestSuite.cpp | 2 +- .../src/PubSubSerializationHandlerTestSuite.cc | 3 ++- .../src/PubSubSerializationProviderTestSuite.cc | 5 +++- .../include/pubsub_serializer_handler.h | 28 ++++++++++------------ .../src/pubsub_serialization_provider.c | 23 +++++++++++++++++- .../pubsub_utils/src/pubsub_serializer_handler.c | 5 +++- cmake/cmake_celix/Generic.cmake | 1 - 12 files changed, 56 insertions(+), 39 deletions(-) diff --git a/bundles/pubsub/pubsub_serializer_avrobin/gtest/CMakeLists.txt b/bundles/pubsub/pubsub_serializer_avrobin/gtest/CMakeLists.txt index 78ae8e1..59e4ca5 100644 --- a/bundles/pubsub/pubsub_serializer_avrobin/gtest/CMakeLists.txt +++ b/bundles/pubsub/pubsub_serializer_avrobin/gtest/CMakeLists.txt @@ -24,7 +24,7 @@ celix_bundle_files(pubsub_avrobin_serialization_descriptor add_executable(test_pubsub_serializer_avrobin src/PubSubAvrobinSerializationProviderTestSuite.cc ) -target_link_libraries(test_pubsub_serializer_avrobin PRIVATE Celix::framework Celix::dfi Celix::pubsub_utils GTest::gtest GTest::gtest_main) +target_link_libraries(test_pubsub_serializer_avrobin PRIVATE Celix::framework Celix::dfi Celix::pubsub_utils GTest::gtest GTest::gtest_main Celix::pubsub_spi) target_compile_options(test_pubsub_serializer_avrobin PRIVATE -std=c++14) #Note test code is allowed to be C++14 add_dependencies(test_pubsub_serializer_avrobin celix_pubsub_serializer_avrobin_bundle pubsub_avrobin_serialization_descriptor_bundle) diff --git a/bundles/pubsub/pubsub_serializer_json/gtest/CMakeLists.txt b/bundles/pubsub/pubsub_serializer_json/gtest/CMakeLists.txt index a065ebe..9a07c94 100644 --- a/bundles/pubsub/pubsub_serializer_json/gtest/CMakeLists.txt +++ b/bundles/pubsub/pubsub_serializer_json/gtest/CMakeLists.txt @@ -24,7 +24,7 @@ celix_bundle_files(pubsub_json_serialization_descriptor add_executable(test_pubsub_serializer_json src/PubSubJsonSerializationProviderTestSuite.cc ) -target_link_libraries(test_pubsub_serializer_json PRIVATE Celix::framework Celix::dfi Celix::pubsub_utils GTest::gtest GTest::gtest_main) +target_link_libraries(test_pubsub_serializer_json PRIVATE Celix::framework Celix::dfi Celix::pubsub_utils GTest::gtest GTest::gtest_main Celix::pubsub_spi) target_compile_options(test_pubsub_serializer_json PRIVATE -std=c++14) #Note test code is allowed to be C++14 add_dependencies(test_pubsub_serializer_json celix_pubsub_serializer_json_bundle pubsub_json_serialization_descriptor_bundle) diff --git a/bundles/pubsub/pubsub_spi/include/pubsub_message_serialization_service.h b/bundles/pubsub/pubsub_spi/include/pubsub_message_serialization_service.h index 4464cb3..09df5a5 100644 --- a/bundles/pubsub/pubsub_spi/include/pubsub_message_serialization_service.h +++ b/bundles/pubsub/pubsub_spi/include/pubsub_message_serialization_service.h @@ -25,15 +25,6 @@ #include "celix_bundle.h" #include "sys/uio.h" -/** - * There should be a pubsub_serializer_t - * per msg type (msg id) per bundle - * - * The pubsub_serializer_service can create - * a serializer_map per bundle. Potentially using - * the extender pattern. - */ - #define PUBSUB_MESSAGE_SERIALIZATION_SERVICE_NAME "pubsub_message_serialization_service" #define PUBSUB_MESSAGE_SERIALIZATION_SERVICE_VERSION "1.0.0" #define PUBSUB_MESSAGE_SERIALIZATION_SERVICE_RANGE "[1,2)" @@ -44,7 +35,7 @@ #define PUBSUB_MESSAGE_SERIALIZATION_SERVICE_MSG_ID_PROPERTY "msg.id" /** - * A message serialization service for a serialization type (e.g. json) and + * @brief A message serialization service for a serialization type (e.g. json) and * for a specific msg type (based on the fully qualified name) and version. * * The properties serialization.type, msg,fqn, msg.version and msg.id are mandatory @@ -53,7 +44,7 @@ typedef struct pubsub_message_serialization_service { void* handle; /** - * Serialize a message into iovec structs (set of structures with buffer pointer and length) + * @brief Serialize a message into iovec structs (set of structures with buffer pointer and length) * * The correct message serialization services will be selected based on the provided msgId. * @@ -67,12 +58,12 @@ typedef struct pubsub_message_serialization_service { celix_status_t (*serialize)(void* handle, const void* input, struct iovec** output, size_t* outputIovLen); /** - * Free the memory of for the serialized msg. + * @brief Free the memory of for the serialized msg. */ void (*freeSerializedMsg)(void* handle, struct iovec* input, size_t inputIovLen); /** - * Deserialize a message using the provided iovec buffers. + * @brief Deserialize a message using the provided iovec buffers. * * The deserialize function will also check if the target major/minor version of the message is valid with the version * of the serialized data. @@ -95,7 +86,7 @@ typedef struct pubsub_message_serialization_service { celix_status_t (*deserialize)(void* handle, const struct iovec* input, size_t inputIovLen, void** out); //note inputLen can be 0 if predefined size is not needed /** - * Free the memory for the deserialized message. + * @brief Free the memory for the deserialized message. */ void (*freeDeserializedMsg)(void* handle, void* msg); diff --git a/bundles/pubsub/pubsub_utils/CMakeLists.txt b/bundles/pubsub/pubsub_utils/CMakeLists.txt index 8426737..0c285aa 100644 --- a/bundles/pubsub/pubsub_utils/CMakeLists.txt +++ b/bundles/pubsub/pubsub_utils/CMakeLists.txt @@ -28,7 +28,8 @@ target_include_directories(pubsub_utils PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include> $<INSTALL_INTERFACE:include/celix/pubsub_utils> ) -target_link_libraries(pubsub_utils PUBLIC Celix::framework Celix::pubsub_api Celix::pubsub_spi Celix::log_helper Celix::shell_api) +target_link_libraries(pubsub_utils PUBLIC Celix::framework Celix::pubsub_api Celix::log_helper Celix::shell_api) +target_link_libraries(pubsub_utils PRIVATE Celix::pubsub_spi) add_library(Celix::pubsub_utils ALIAS pubsub_utils) diff --git a/bundles/pubsub/pubsub_utils/gtest/CMakeLists.txt b/bundles/pubsub/pubsub_utils/gtest/CMakeLists.txt index ad7cce4..a883776 100644 --- a/bundles/pubsub/pubsub_utils/gtest/CMakeLists.txt +++ b/bundles/pubsub/pubsub_utils/gtest/CMakeLists.txt @@ -51,7 +51,7 @@ add_executable(test_pubsub_utils src/PubSubSerializationProviderTestSuite.cc src/PubSubMatchingTestSuite.cpp ) -target_link_libraries(test_pubsub_utils PRIVATE Celix::framework Celix::pubsub_utils GTest::gtest GTest::gtest_main) +target_link_libraries(test_pubsub_utils PRIVATE Celix::framework Celix::pubsub_utils GTest::gtest GTest::gtest_main Celix::pubsub_spi) target_compile_options(test_pubsub_utils PRIVATE -std=c++14) #Note test code is allowed to be C++14 add_test(NAME test_pubsub_utils COMMAND test_pubsub_utils) setup_target_for_coverage(test_pubsub_utils SCAN_DIR ..) diff --git a/bundles/pubsub/pubsub_utils/gtest/src/PubSubMatchingTestSuite.cpp b/bundles/pubsub/pubsub_utils/gtest/src/PubSubMatchingTestSuite.cpp index ff26f25..85c86c1 100644 --- a/bundles/pubsub/pubsub_utils/gtest/src/PubSubMatchingTestSuite.cpp +++ b/bundles/pubsub/pubsub_utils/gtest/src/PubSubMatchingTestSuite.cpp @@ -22,7 +22,7 @@ #include <memory> #include <celix_api.h> -#include "pubsub_serializer_handler.h" +#include "pubsub_message_serialization_service.h" #include "dyn_message.h" #include <cstdarg> #include <pubsub_protocol.h> diff --git a/bundles/pubsub/pubsub_utils/gtest/src/PubSubSerializationHandlerTestSuite.cc b/bundles/pubsub/pubsub_utils/gtest/src/PubSubSerializationHandlerTestSuite.cc index ce7948f..2448c18 100644 --- a/bundles/pubsub/pubsub_utils/gtest/src/PubSubSerializationHandlerTestSuite.cc +++ b/bundles/pubsub/pubsub_utils/gtest/src/PubSubSerializationHandlerTestSuite.cc @@ -21,7 +21,8 @@ #include <memory> -#include <celix_api.h> +#include "celix_api.h" +#include "pubsub_message_serialization_service.h" #include "pubsub_serializer_handler.h" #include "dyn_message.h" #include <cstdarg> diff --git a/bundles/pubsub/pubsub_utils/gtest/src/PubSubSerializationProviderTestSuite.cc b/bundles/pubsub/pubsub_utils/gtest/src/PubSubSerializationProviderTestSuite.cc index a7f660c..48883ed 100644 --- a/bundles/pubsub/pubsub_utils/gtest/src/PubSubSerializationProviderTestSuite.cc +++ b/bundles/pubsub/pubsub_utils/gtest/src/PubSubSerializationProviderTestSuite.cc @@ -21,7 +21,8 @@ #include <memory> -#include <celix_api.h> +#include "celix_api.h" +#include "pubsub_message_serialization_marker.h" #include "pubsub_serialization_provider.h" class PubSubSerializationProviderTestSuite : public ::testing::Test { @@ -48,6 +49,8 @@ public: TEST_F(PubSubSerializationProviderTestSuite, CreateDestroy) { //checks if the bundles are started and stopped correctly (no mem leaks). auto* provider = pubsub_serializationProvider_create(ctx.get(), "test", 0, nullptr, nullptr, nullptr, nullptr); + auto count = celix_bundleContext_useService(ctx.get(), PUBSUB_MESSAGE_SERIALIZATION_MARKER_NAME, nullptr, nullptr); + EXPECT_EQ(1, count); pubsub_serializationProvider_destroy(provider); } diff --git a/bundles/pubsub/pubsub_utils/include/pubsub_serializer_handler.h b/bundles/pubsub/pubsub_utils/include/pubsub_serializer_handler.h index b873901..06c18de 100644 --- a/bundles/pubsub/pubsub_utils/include/pubsub_serializer_handler.h +++ b/bundles/pubsub/pubsub_utils/include/pubsub_serializer_handler.h @@ -21,10 +21,9 @@ #define CELIX_PUBSUB_SERIALIZER_HANDLER_H #include <stdint.h> +#include <sys/uio.h> #include "celix_api.h" -#include "pubsub_message_serialization_service.h" - #ifdef __cplusplus extern "C" { @@ -34,7 +33,7 @@ typedef struct pubsub_serializer_handler pubsub_serializer_handler_t; //opaque t /** - * Creates a handler which track pubsub_custom_msg_serialization_service services with a (serialization.type=<serializerType)) filter. + * @brief Creates a handler which track pubsub_custom_msg_serialization_service services with a (serialization.type=<serializerType)) filter. * If multiple pubsub_message_serialization_service for the same msg fqn (targeted.msg.fqn property) the highest ranking service will be used. * * The message handler assumes (and checks) that all provided serialization services do not clash in message ids (so every msgId should have its own msgFqn) @@ -53,14 +52,13 @@ typedef struct pubsub_serializer_handler pubsub_serializer_handler_t; //opaque t */ pubsub_serializer_handler_t* pubsub_serializerHandler_create(celix_bundle_context_t* ctx, const char* serializerType, bool backwardCompatible); - +/** + * @brief destroy the pubsub_serializer_handler and free the used memory. + */ void pubsub_serializerHandler_destroy(pubsub_serializer_handler_t* handler); -void pubsub_serializerHandler_addSerializationService(pubsub_serializer_handler_t* handler, pubsub_message_serialization_service_t* svc, const celix_properties_t* svcProperties); -void pubsub_serializerHandler_removeSerializationService(pubsub_serializer_handler_t* handler, pubsub_message_serialization_service_t* svc, const celix_properties_t* svcProperties); - /** - * Serialize a message into iovec structs (set of structures with buffer pointer and length) + * @brief Serialize a message into iovec structs (set of structures with buffer pointer and length) * * The correct message serialization services will be selected based on the provided msgId. * @@ -74,12 +72,12 @@ void pubsub_serializerHandler_removeSerializationService(pubsub_serializer_handl celix_status_t pubsub_serializerHandler_serialize(pubsub_serializer_handler_t* handler, uint32_t msgId, const void* input, struct iovec** output, size_t* outputIovLen); /** - * Free the memory of for the serialized msg. + * @brief Free the memory of for the serialized msg. */ celix_status_t pubsub_serializerHandler_freeSerializedMsg(pubsub_serializer_handler_t* handler, uint32_t msgId, struct iovec* input, size_t inputIovLen); /** - * Deserialize a message using the provided iovec buffers. + * @brief Deserialize a message using the provided iovec buffers. * * The deserialize function will also check if the target major/minor version of the message is valid with the version * of the serialized data. @@ -102,31 +100,31 @@ celix_status_t pubsub_serializerHandler_freeSerializedMsg(pubsub_serializer_hand celix_status_t pubsub_serializerHandler_deserialize(pubsub_serializer_handler_t* handler, uint32_t msgId, int serializedMajorVersion, int serializedMinorVersion, const struct iovec* input, size_t inputIovLen, void** out); /** - * Free the memory for the deserialized message. + * @brief Free the memory for the deserialized message. */ celix_status_t pubsub_serializerHandler_freeDeserializedMsg(pubsub_serializer_handler_t* handler, uint32_t msgId, void* msg); /** - * Whether the msg is support. More specifically: + * @brief Whether the msg is support. More specifically: * - msg id is known and * - a serialized msg with the provided major and minor version can be deserialized. */ bool pubsub_serializerHandler_isMessageSupported(pubsub_serializer_handler_t* handler, uint32_t msgId, int majorVersion, int minorVersion); /** - * Get msg fqn from a msg id. + * @brief Get msg fqn from a msg id. * @return msg fqn or NULL if msg id is not known. */ char* pubsub_serializerHandler_getMsgFqn(pubsub_serializer_handler_t* handler, uint32_t msgId); /** - * Get a msg id from a msgFqn. + * @brief Get a msg id from a msgFqn. * @return msg id or 0 if msg fqn is not known. */ uint32_t pubsub_serializerHandler_getMsgId(pubsub_serializer_handler_t* handler, const char* msgFqn); /** - * nr of serialization services found. + * @brief nr of serialization services found. */ size_t pubsub_serializerHandler_messageSerializationServiceCount(pubsub_serializer_handler_t* handler); diff --git a/bundles/pubsub/pubsub_utils/src/pubsub_serialization_provider.c b/bundles/pubsub/pubsub_utils/src/pubsub_serialization_provider.c index d008191..ebba555 100644 --- a/bundles/pubsub/pubsub_utils/src/pubsub_serialization_provider.c +++ b/bundles/pubsub/pubsub_utils/src/pubsub_serialization_provider.c @@ -24,6 +24,7 @@ #include <dirent.h> #include <string.h> +#include "pubsub_message_serialization_marker.h" #include "celix_constants.h" #include "dyn_function.h" #include "celix_version.h" @@ -71,6 +72,9 @@ struct pubsub_serialization_provider { celix_shell_command_t cmdSvc; long cmdSvcId; + pubsub_message_serialization_marker_t markerSvc; + long markerSvcId; + celix_thread_mutex_t mutex; //protects below celix_array_list_t *serializationSvcEntries; //key = pubsub_serialization_entry; }; @@ -608,6 +612,7 @@ pubsub_serialization_provider_t *pubsub_serializationProvider_create( dynCommon_logSetup(dfi_log, provider, 1); { + //Start bundle tracker and register pubsub_message_serialization services celix_bundle_tracking_options_t opts = CELIX_EMPTY_BUNDLE_TRACKING_OPTIONS; opts.callbackHandle = provider; opts.onInstalled = pubsub_serializationProvider_onInstalledBundle; @@ -616,13 +621,13 @@ pubsub_serialization_provider_t *pubsub_serializationProvider_create( } { + //Register shell command to query serializers provider->cmdSvc.handle = provider; provider->cmdSvc.executeCommand = pubsub_serializationProvider_executeCommand; char *name = NULL; asprintf(&name,"celix::%s_message_serialization", provider->serializationType); char *usage = NULL; - //TODO add support for listing invalid entries asprintf(&usage,"celix::%s_message_serialization [verbose | invalids | <msg id> | <msg fqn>]", provider->serializationType); celix_properties_t* props = celix_properties_create(); @@ -640,11 +645,27 @@ pubsub_serialization_provider_t *pubsub_serializationProvider_create( free(name); free(usage); } + + { + //Register pubsub_message_serialization_marker service to indicate the availability of this message serialization type. + celix_properties_t* props = celix_properties_create(); + provider->markerSvc.handle = provider; + celix_properties_set(props, PUBSUB_MESSAGE_SERIALIZATION_MARKER_SERIALIZATION_TYPE_PROPERTY, provider->serializationType); + celix_service_registration_options_t opts = CELIX_EMPTY_SERVICE_REGISTRATION_OPTIONS; + opts.svc = &provider->markerSvc; + opts.serviceName = PUBSUB_MESSAGE_SERIALIZATION_MARKER_NAME; + opts.serviceVersion = PUBSUB_MESSAGE_SERIALIZATION_MARKER_VERSION; + opts.properties = props; + provider->markerSvcId = celix_bundleContext_registerServiceWithOptions(ctx, &opts); + } + return provider; } void pubsub_serializationProvider_destroy(pubsub_serialization_provider_t* provider) { if (provider != NULL) { + celix_bundleContext_unregisterService(provider->ctx, provider->markerSvcId); + celix_bundleContext_stopTracker(provider->ctx, provider->bundleTrackerId); celix_bundleContext_unregisterService(provider->ctx, provider->cmdSvcId); diff --git a/bundles/pubsub/pubsub_utils/src/pubsub_serializer_handler.c b/bundles/pubsub/pubsub_utils/src/pubsub_serializer_handler.c index f5962f3..cb06107 100644 --- a/bundles/pubsub/pubsub_utils/src/pubsub_serializer_handler.c +++ b/bundles/pubsub/pubsub_utils/src/pubsub_serializer_handler.c @@ -54,6 +54,9 @@ struct pubsub_serializer_handler { hash_map_t *serializationServices; //key = msg id, value = sorted array list with pubsub_serialization_service_entry_t* }; +static void pubsub_serializerHandler_addSerializationService(pubsub_serializer_handler_t* handler, pubsub_message_serialization_service_t* svc, const celix_properties_t* svcProperties); +static void pubsub_serializerHandler_removeSerializationService(pubsub_serializer_handler_t* handler, pubsub_message_serialization_service_t* svc, const celix_properties_t* svcProperties); + static void addSerializationService(void *handle, void* svc, const celix_properties_t *props) { pubsub_serializer_handler_t* handler = handle; pubsub_message_serialization_service_t* serSvc = svc; @@ -66,7 +69,7 @@ static void removeSerializationService(void *handle, void* svc, const celix_prop pubsub_serializerHandler_removeSerializationService(handler, serSvc, props); } -int compareEntries(const void *a, const void *b) { +static int compareEntries(const void *a, const void *b) { const pubsub_serialization_service_entry_t* aEntry = a; const pubsub_serialization_service_entry_t* bEntry = b; diff --git a/cmake/cmake_celix/Generic.cmake b/cmake/cmake_celix/Generic.cmake index ae82881..e44d3fa 100644 --- a/cmake/cmake_celix/Generic.cmake +++ b/cmake/cmake_celix/Generic.cmake @@ -30,7 +30,6 @@ add_celix_bundle_dependencies(<cmake_target> function(add_celix_bundle_dependencies) list(GET ARGN 0 TARGET) list(REMOVE_AT ARGN 0) - message("TEST") foreach(BUNDLE_TARGET IN LISTS ARGN) if (TARGET ${BUNDLE_TARGET}) get_target_property(IMPORT ${BUNDLE_TARGET} BUNDLE_IMPORTED)
