Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into 49-server-has-to-remov…
Browse files Browse the repository at this point in the history
…e-ungracefull-disconnected-zombie-clients
  • Loading branch information
Michael-M-Baron committed Nov 5, 2024
2 parents f70bf11 + 431bec7 commit bff6d82
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 62 deletions.
18 changes: 18 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/ubuntu
{
"name": "libjapi_ci_build",
"image": "ghcr.io/fraunhofer-iis/libjapi_ci"

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
4 changes: 3 additions & 1 deletion .github/workflows/CMake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ jobs:
working-directory: ${{ env.GITHUB_WORKSPACE }}/build
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: make run_test
run: |
make &&
ctest3
3 changes: 3 additions & 0 deletions .github/workflows/package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ on:
jobs:
package:
runs-on: ubuntu-latest
env:
ACTIONS_RUNNER_FORCE_ACTIONS_NODE_VERSION: node16
ACTIONS_ALLOW_USE_UNSECURE_NODE_VERSION: true
container:
image: ghcr.io/fraunhofer-iis/libjapi_ci
credentials:
Expand Down
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

88 changes: 48 additions & 40 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.6)

project(libjapi VERSION 0.3)
project(libjapi VERSION 0.4)
set(SOVERSION 1)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand All @@ -9,7 +9,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON)

# 'cmake -DCMAKE_BUILD_TYPE=Debug ../' from build folder for debug output #
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_BUILD_TYPE "Release")
endif()

file(GLOB SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*)
Expand All @@ -24,17 +24,22 @@ pkg_search_module(JSONC REQUIRED IMPORTED_TARGET json-c)

# create an object lib to avoid double compilation
add_library(japi_objs OBJECT ${SOURCES} ${HEADERS})
if(DEFINED CREADLINE_MAX_LINE_SIZE)
if(CREADLINE_MAX_LINE_SIZE LESS 1024)
message(FATAL_ERROR "Minimal linesize is CREADLINE_BLOCK_SIZE=1024 bytes")
endif()
target_compile_definitions(japi_objs PRIVATE CREADLINE_MAX_LINE_SIZE=${CREADLINE_MAX_LINE_SIZE})
endif()
target_compile_options(japi_objs PUBLIC "-pthread" "-D_POSIX_C_SOURCE=200809L")
target_include_directories(japi_objs PUBLIC include/ ${JSONC_INCLUDE_DIRS})
set_property(TARGET japi_objs PROPERTY POSITION_INDEPENDENT_CODE ON)

# set up actual libraries
add_library(japi SHARED $<TARGET_OBJECTS:japi_objs>)
add_library(japi SHARED $<TARGET_OBJECTS:japi_objs>)
add_library(japi-static STATIC $<TARGET_OBJECTS:japi_objs>)

set_target_properties(japi PROPERTIES
PUBLIC_HEADER "${PUBLIC_HEADERS}"
SOVERSION ${SOVERSION})
set_target_properties(japi PROPERTIES PUBLIC_HEADER "${PUBLIC_HEADERS}"
SOVERSION ${SOVERSION})

target_link_libraries(japi PkgConfig::JSONC)
target_link_libraries(japi-static PkgConfig::JSONC)
Expand All @@ -54,8 +59,10 @@ install(TARGETS japi
find_package(Doxygen)

if(DOXYGEN_FOUND)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doxydir/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
add_custom_target(doc
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doxydir/Doxyfile.in
${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
add_custom_target(
doc
${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen" VERBATIM
Expand All @@ -64,44 +71,45 @@ endif(DOXYGEN_FOUND)

################################
# Create unit test excecutable #
option(LIBJAPI_ENABLE_TESTING "Enable testing with googletest" ON)
if(LIBJAPI_ENABLE_TESTING)
cmake_minimum_required(VERSION 3.14)
enable_testing()

add_subdirectory(googletest/googletest
EXCLUDE_FROM_ALL)
# Include GoogleTest in the project
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.8.0)
FetchContent_MakeAvailable(googletest)
include(GoogleTest)

add_executable(testsuite
EXCLUDE_FROM_ALL
test/japi_test.cc
)
# Define test executable
add_executable(testsuite test/japi_test.cc)

target_compile_options(testsuite PUBLIC "-pthread")
target_compile_options(testsuite PUBLIC "-pthread")

target_link_libraries(testsuite
gtest_main
japi
)
target_link_libraries(testsuite gtest_main japi)

target_include_directories(testsuite
PUBLIC
include/
src/
${JSONC_INCLUDE_DIRS}
)
# Include directories
target_include_directories(testsuite PUBLIC include/ src/
${JSONC_INCLUDE_DIRS})

add_custom_target(run_test COMMAND testsuite DEPENDS testsuite)
gtest_discover_tests(testsuite)

################################
# Test code coverage #

if(CMAKE_BUILD_TYPE MATCHES "Debug")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(${CMAKE_SOURCE_DIR}/cmake/CodeCoverage.cmake)
set(COVERAGE_EXCLUDES
"doxydir"
"/usr/include/*"
"googletest/*"
"test/*"
)
append_coverage_compiler_flags()

setup_target_for_coverage_lcov(NAME coverage EXECUTABLE testsuite)
endif()
if(CMAKE_BUILD_TYPE MATCHES "Debug")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(${CMAKE_SOURCE_DIR}/cmake/CodeCoverage.cmake)
set(COVERAGE_EXCLUDES
"doxydir"
"/usr/include"
"_deps/googletest"
"test/*"
)
append_coverage_compiler_flags()
setup_target_for_coverage_lcov(NAME coverage EXECUTABLE testsuite)
endif()
endif(LIBJAPI_ENABLE_TESTING)
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Prebuild packages for CentOS 7 can be downloaded from the [latest package Action

### Prerequisites
* [json-c](https://github.com/json-c/json-c)
* [cmake version 3.6](https://cmake.org/)
* [cmake version 3.6](https://cmake.org/) for libjapi build
* [cmake version 3.14](https://cmake.org/) for libjapi test-framework build

### Installation
Clone the git repository and it's submodules:
Expand All @@ -38,7 +39,11 @@ A Makefile is generated. Run 'make' to build the libjapi libraries.

$ make

A shared and a static library is built.
A shared and a static library is built. Tests are built by default. They can be disabled using the command `cmake -DLIBJAPI_ENABLE_TESTING=OFF ../` variable.

To run the internal tests run

$ ctest

### Tests
The testsuite from [googletest](https://github.com/google/googletest) is used. To run tests, call
Expand Down
1 change: 0 additions & 1 deletion googletest
Submodule googletest deleted from 3f5b5b
1 change: 0 additions & 1 deletion include/japi.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ typedef struct __japi_context {
struct __japi_client *clients; /*!< Pointer to the JAPI client context */
bool include_args_in_response; /*!< Flag to include request args in response */
bool shutdown; /*!< Flag to shutdown the JAPI server */
bool init; /*!< Flag to mark finished initialization */
} japi_context;

/*!
Expand Down
36 changes: 22 additions & 14 deletions src/japi.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,9 @@ int japi_destroy(japi_context *ctx)
return 0;
}

int japi_register_request(japi_context *ctx, const char *req_name,
japi_req_handler req_handler)
static int japi_register_request_internal(japi_context *ctx, const char *req_name,
japi_req_handler req_handler,
bool allow_internal_req_name)
{
japi_request *req;
char *bad_req_name = "japi_";
Expand Down Expand Up @@ -265,9 +266,11 @@ int japi_register_request(japi_context *ctx, const char *req_name,
return -4;
}

if (ctx->init && strncmp(req_name, bad_req_name, strlen(bad_req_name)) == 0) {
fprintf(stderr, "ERROR: Request name is not allowed.\n");
return -6;
if (!allow_internal_req_name) {
if (strncmp(req_name, bad_req_name, strlen(bad_req_name)) == 0) {
fprintf(stderr, "ERROR: Request name is not allowed.\n");
return -6;
}
}

req = (japi_request *)malloc(sizeof(japi_request));
Expand All @@ -285,6 +288,12 @@ int japi_register_request(japi_context *ctx, const char *req_name,
return 0;
}

int japi_register_request(japi_context *ctx, const char *req_name,
japi_req_handler req_handler)
{
return japi_register_request_internal(ctx, req_name, req_handler, false);
}

japi_context *japi_init(void *userptr)
{
japi_context *ctx;
Expand All @@ -295,7 +304,6 @@ japi_context *japi_init(void *userptr)
return NULL;
}

ctx->init = false;
ctx->userptr = userptr;
ctx->requests = NULL;
ctx->push_services = NULL;
Expand All @@ -319,16 +327,16 @@ japi_context *japi_init(void *userptr)
signal(SIGPIPE, SIG_IGN);

/* Register the default fallback handler */
japi_register_request(ctx, "japi_request_not_found_handler",
&japi_request_not_found_handler);
japi_register_request_internal(ctx, "japi_request_not_found_handler",
&japi_request_not_found_handler, true);
/* Register subscribe/unsubscribe service function */
japi_register_request(ctx, "japi_pushsrv_subscribe", &japi_pushsrv_subscribe);
japi_register_request(ctx, "japi_pushsrv_unsubscribe", &japi_pushsrv_unsubscribe);
japi_register_request_internal(ctx, "japi_pushsrv_subscribe",
&japi_pushsrv_subscribe, true);
japi_register_request_internal(ctx, "japi_pushsrv_unsubscribe",
&japi_pushsrv_unsubscribe, true);
/* Register list_push_service function */
japi_register_request(ctx, "japi_pushsrv_list", &japi_pushsrv_list);
japi_register_request(ctx, "japi_cmd_list", &japi_cmd_list);

ctx->init = true;
japi_register_request_internal(ctx, "japi_pushsrv_list", &japi_pushsrv_list, true);
japi_register_request_internal(ctx, "japi_cmd_list", &japi_cmd_list, true);

return ctx;
}
Expand Down

0 comments on commit bff6d82

Please sign in to comment.