Repository: nifi-minifi-cpp Updated Branches: refs/heads/master 1a50a4ab0 -> 0043155dc
MINIFICPP-644 - C API: add support to register terminate handler This closes #422. Signed-off-by: Marc Parisi <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/repo Commit: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/commit/0043155d Tree: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/tree/0043155d Diff: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/diff/0043155d Branch: refs/heads/master Commit: 0043155dce73ded463f42c80de142ce908a9c08b Parents: 1a50a4a Author: Arpad Boda <[email protected]> Authored: Tue Oct 9 17:56:38 2018 +0200 Committer: Marc Parisi <[email protected]> Committed: Wed Oct 24 18:50:34 2018 -0400 ---------------------------------------------------------------------- LibExample/CMakeLists.txt | 28 ++++++++++------ LibExample/generate_flow.c | 3 +- LibExample/terminate_handler.c | 58 ++++++++++++++++++++++++++++++++++ libminifi/include/capi/api.h | 2 ++ libminifi/src/capi/api.cpp | 5 +++ libminifi/test/capi/CAPITests.cpp | 2 +- 6 files changed, 86 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/0043155d/LibExample/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/LibExample/CMakeLists.txt b/LibExample/CMakeLists.txt index ff076b0..88901cc 100644 --- a/LibExample/CMakeLists.txt +++ b/LibExample/CMakeLists.txt @@ -23,7 +23,7 @@ IF(POLICY CMP0048) CMAKE_POLICY(SET CMP0048 OLD) ENDIF(POLICY CMP0048) -include_directories(../blocks/ ../libminifi/include ../libminifi/include/c2 ../libminifi/include/c2/protocols/ ../libminifi/include/core/state ./libminifi/include/core/statemanagement/metrics ../libminifi/include/core/yaml ../libminifi/include/core ../thirdparty/spdlog-20170710/include ../thirdparty/concurrentqueue ../thirdparty/yaml-cpp-yaml-cpp-20171024/include ../thirdparty/civetweb-1.9.1/include ../thirdparty/) +include_directories(../blocks/ ../libminifi/include ../libminifi/include/c2 ../libminifi/include/c2/protocols/ ../libminifi/include/core/state ./libminifi/include/core/statemanagement/metrics ../libminifi/include/core/yaml ../libminifi/include/core ../thirdparty/spdlog-20170710/include ../thirdparty/concurrentqueue ../thirdparty/yaml-cpp-yaml-cpp-20171024/include ../thirdparty/civetweb-1.9.1/include ../thirdparty/) include(CheckCXXCompilerFlag) if (WIN32) @@ -46,23 +46,33 @@ endif() endif() +if (WIN32) + set(LINK_FLAGS "/WHOLEARCHIVE") + set(LINK_END_FLAGS "") +elseif (APPLE) + set(LINK_FLAGS "-Wl,-all_load") + set(LINK_END_FLAGS "") +else () + set(LINK_FLAGS "-Wl,--whole-archive") + set(LINK_END_FLAGS "-Wl,--no-whole-archive") +endif () add_executable(generate_flow generate_flow.c) -target_link_libraries(generate_flow capi core-minifi minifi ${CMAKE_THREAD_LIBS_INIT}) - -if (NOT WIN32) - -add_executable(transmit_flow transmit_flow.c) +add_executable(terminate_handler terminate_handler.c) -target_link_libraries(transmit_flow capi core-minifi minifi ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(generate_flow capi ${CMAKE_THREAD_LIBS_INIT} ${LINK_FLAGS} minifi ${LINK_END_FLAGS}) +target_link_libraries(terminate_handler capi ${CMAKE_THREAD_LIBS_INIT} ${LINK_FLAGS} minifi ${LINK_END_FLAGS}) +if (NOT WIN32) +add_executable(transmit_flow transmit_flow.c) +target_link_libraries(transmit_flow capi ${CMAKE_THREAD_LIBS_INIT} ${LINK_FLAGS} minifi ${LINK_END_FLAGS}) add_executable(monitor_directory monitor_directory.c) -target_link_libraries(monitor_directory capi core-minifi minifi ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(monitor_directory capi ${CMAKE_THREAD_LIBS_INIT} ${LINK_FLAGS} minifi ${LINK_END_FLAGS}) -endif() \ No newline at end of file +endif() http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/0043155d/LibExample/generate_flow.c ---------------------------------------------------------------------- diff --git a/LibExample/generate_flow.c b/LibExample/generate_flow.c index 923ddb4..eb5e7a3 100644 --- a/LibExample/generate_flow.c +++ b/LibExample/generate_flow.c @@ -22,7 +22,6 @@ #include <sys/stat.h> #include "capi/api.h" - /** * This is an example of the C API that transmits a flow file to a remote instance. */ @@ -42,7 +41,7 @@ int main(int argc, char **argv) { nifi_instance *instance = create_instance(instance_str, &port); - flow *new_flow = create_flow(instance,"GenerateFlowFile"); + flow *new_flow = create_flow(instance, "GenerateFlowFile"); flow_file_record *record = get_next_flow_file(instance, new_flow ); http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/0043155d/LibExample/terminate_handler.c ---------------------------------------------------------------------- diff --git a/LibExample/terminate_handler.c b/LibExample/terminate_handler.c new file mode 100644 index 0000000..daa9f3b --- /dev/null +++ b/LibExample/terminate_handler.c @@ -0,0 +1,58 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more +* contributor license agreements. See the NOTICE file distributed with +* this work for additional information regarding copyright ownership. +* The ASF licenses this file to You under the Apache License, Version 2.0 +* (the "License"); you may not use this file except in compliance with +* the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include "capi/api.h" + +/** + * This is an example of the C API that registers terminate handler and generates an exception. + */ + +void example_terminate_handler() { + fprintf(stderr, "Unhandled exception! Let's pretend that this is normal!"); + exit(0); +} + +int main(int argc, char **argv) { + + nifi_port port; + + port.port_id = "12345"; + + set_terminate_callback(example_terminate_handler); + + nifi_instance *instance = create_instance("random instance", &port); + + flow *new_flow = create_flow(instance, "GenerateFlowFile"); + + processor *put_proc = add_processor_with_linkage(new_flow, "PutFile"); + + // Target directory for PutFile is missing, it's not allowed to create, so tries to transmit to failure relationship + // As it doesn't exist, an exception is thrown + set_property(put_proc, "Directory", "/tmp/never_existed"); + set_property(put_proc, "Create Missing Directories", "false"); + + flow_file_record *record = get_next_flow_file(instance, new_flow ); + + // Here be dragons - nothing below this line gets executed + fprintf(stderr, "Dragons!!!"); + free_flowfile(record); + free_flow(new_flow); + free_instance(instance); +} http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/0043155d/libminifi/include/capi/api.h ---------------------------------------------------------------------- diff --git a/libminifi/include/capi/api.h b/libminifi/include/capi/api.h index 2b3622d..4e319ef 100644 --- a/libminifi/include/capi/api.h +++ b/libminifi/include/capi/api.h @@ -37,6 +37,8 @@ extern "C" { void enable_logging(); +void set_terminate_callback(void (*terminate_callback)()); + /**** * ################################################################## * BASE NIFI OPERATIONS http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/0043155d/libminifi/src/capi/api.cpp ---------------------------------------------------------------------- diff --git a/libminifi/src/capi/api.cpp b/libminifi/src/capi/api.cpp index 66ed19b..21010a1 100644 --- a/libminifi/src/capi/api.cpp +++ b/libminifi/src/capi/api.cpp @@ -19,6 +19,7 @@ #include <map> #include <memory> #include <utility> +#include <exception> #include "core/Core.h" #include "capi/api.h" #include "capi/expect.h" @@ -47,6 +48,10 @@ void enable_logging() { logging::LoggerConfiguration::getConfiguration().enableLogging(); } +void set_terminate_callback(void (*terminate_callback)()) { + std::set_terminate(terminate_callback); +} + class DirectoryConfiguration { protected: DirectoryConfiguration() { http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/0043155d/libminifi/test/capi/CAPITests.cpp ---------------------------------------------------------------------- diff --git a/libminifi/test/capi/CAPITests.cpp b/libminifi/test/capi/CAPITests.cpp index 08214ac..d4e1c49 100644 --- a/libminifi/test/capi/CAPITests.cpp +++ b/libminifi/test/capi/CAPITests.cpp @@ -185,7 +185,7 @@ TEST_CASE("Test manipulation of attributes", "[testAttributes]") { const char * new_testattr_value = "S0me t3st t3xt"; // Attribute already exist, should fail - REQUIRE(add_attribute(record, test_attr.key, (void* )new_testattr_value, strlen(new_testattr_value)) != 0); // NOLINT + REQUIRE(add_attribute(record, test_attr.key, (void*) new_testattr_value, strlen(new_testattr_value)) != 0); // NOLINT // Update overwrites values update_attribute(record, test_attr.key, (void*) new_testattr_value, strlen(new_testattr_value)); // NOLINT
