From b4a922e1205166700bee86c815e9f29fb7a0bf84 Mon Sep 17 00:00:00 2001 From: Marika Lehmann Date: Thu, 24 Feb 2022 19:12:15 +0100 Subject: [PATCH] iox-#1142 Add first findService example Signed-off-by: Marika Lehmann --- doc/website/examples/icediscovery.md | 2 +- doc/website/examples/icediscovery_in_c.md | 5 + iceoryx_examples/README.md | 1 + .../icediscovery_in_c/CMakeLists.txt | 54 ++++++++ .../icediscovery_in_c/iox_c_find_service.c | 83 ++++++++++++ .../icediscovery_in_c/iox_c_offer_service.c | 89 +++++++++++++ .../icediscovery_in_c/sleep_for.h | 37 ++++++ iceoryx_integrationtest/CMakeLists.txt | 3 +- .../test_icediscovery_in_c_example.py | 119 ++++++++++++++++++ iceoryx_meta/CMakeLists.txt | 3 +- 10 files changed, 393 insertions(+), 3 deletions(-) create mode 100644 doc/website/examples/icediscovery_in_c.md create mode 100644 iceoryx_examples/icediscovery_in_c/CMakeLists.txt create mode 100644 iceoryx_examples/icediscovery_in_c/iox_c_find_service.c create mode 100644 iceoryx_examples/icediscovery_in_c/iox_c_offer_service.c create mode 100644 iceoryx_examples/icediscovery_in_c/sleep_for.h create mode 100644 iceoryx_integrationtest/iceoryx_integrationtest/test_icediscovery_in_c_example.py diff --git a/doc/website/examples/icediscovery.md b/doc/website/examples/icediscovery.md index 376287657ea..8820cef30aa 100644 --- a/doc/website/examples/icediscovery.md +++ b/doc/website/examples/icediscovery.md @@ -1,5 +1,5 @@ --- -title: Searching for currently available services +title: Searching for currently available services using C++ --- {! ../iceoryx/iceoryx_examples/icediscovery/README.md !} diff --git a/doc/website/examples/icediscovery_in_c.md b/doc/website/examples/icediscovery_in_c.md new file mode 100644 index 00000000000..de0d67a29a8 --- /dev/null +++ b/doc/website/examples/icediscovery_in_c.md @@ -0,0 +1,5 @@ +--- +title: Searching for currently available services using C +--- + +{! ../iceoryx/iceoryx_examples/icediscovery/README.md !} diff --git a/iceoryx_examples/README.md b/iceoryx_examples/README.md index 24c050e6d19..5bbb4dbc267 100644 --- a/iceoryx_examples/README.md +++ b/iceoryx_examples/README.md @@ -18,6 +18,7 @@ |[singleprocess](./singleprocess/) | Communicating in a single process between threads | :star::star: | |[user_header](./user_header/) | Using a user-header for additional meta-information like timestamps | :star::star: | |[icediscovery](./icediscovery) | Searching for currently available services | :star::star: | +|[icediscovery_in_c](./icediscovery_in_c/) | Searching for currently available services using C | :star::star: | |[ice_access_control](./ice_access_control/) | Configuring access rights for shared memory segments | :star::star::star: | |[iceperf](./iceperf/) | Measuring the latency of different IPC mechanisms | :star::star::star: | |[icecrystal](./icecrystal/) | Using the introspection client for debugging | :star::star::star: | diff --git a/iceoryx_examples/icediscovery_in_c/CMakeLists.txt b/iceoryx_examples/icediscovery_in_c/CMakeLists.txt new file mode 100644 index 00000000000..503403ceddf --- /dev/null +++ b/iceoryx_examples/icediscovery_in_c/CMakeLists.txt @@ -0,0 +1,54 @@ +# Copyright (c) 2022 by Apex.AI Inc. All rights reserved. +# +# Licensed 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. +# +# SPDX-License-Identifier: Apache-2.0 + +# Build icediscovery example +cmake_minimum_required(VERSION 3.16) +project(example_icediscovery_in_c) + +include(GNUInstallDirs) + +find_package(iceoryx_posh CONFIG REQUIRED) +find_package(iceoryx_hoofs CONFIG REQUIRED) +find_package(iceoryx_binding_c CONFIG REQUIRED) + +get_target_property(ICEORYX_CXX_STANDARD iceoryx_posh::iceoryx_posh CXX_STANDARD) +include(IceoryxPlatform) + +add_executable(iox-c-offer-service ./iox_c_offer_service.c) +set_source_files_properties(./ice_c_offer_service.c PROPERTIES LANGUAGE C) +target_link_libraries(iox-c-offer-service + iceoryx_binding_c::iceoryx_binding_c +) +set_target_properties(iox-c-offer-service PROPERTIES + POSITION_INDEPENDENT_CODE ON +) +target_compile_options(iox-c-offer-service PRIVATE ${ICEORYX_WARNINGS} ${ICEORYX_SANITIZER_FLAGS}) + +add_executable(iox-c-find-service ./iox_c_find_service.c) +set_source_files_properties(./ice_c_find_service.c PROPERTIES LANGUAGE C) +target_link_libraries(iox-c-find-service + iceoryx_binding_c::iceoryx_binding_c +) +set_target_properties(iox-c-offer-service PROPERTIES + POSITION_INDEPENDENT_CODE ON +) +target_compile_options(iox-c-find-service PRIVATE ${ICEORYX_WARNINGS} ${ICEORYX_SANITIZER_FLAGS}) + +# ========================================================== // + +install(TARGETS iox-c-offer-service + iox-c-find-service + RUNTIME DESTINATION bin) diff --git a/iceoryx_examples/icediscovery_in_c/iox_c_find_service.c b/iceoryx_examples/icediscovery_in_c/iox_c_find_service.c new file mode 100644 index 00000000000..54556501075 --- /dev/null +++ b/iceoryx_examples/icediscovery_in_c/iox_c_find_service.c @@ -0,0 +1,83 @@ +// Copyright (c) 2022 by Apex.AI Inc. All rights reserved. +// +// Licensed 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. +// +// SPDX-License-Identifier: Apache-2.0 + +#include "iceoryx_binding_c/runtime.h" +#include "iceoryx_binding_c/service_discovery.h" +#include "sleep_for.h" + +#include +#include +#include + +bool keepRunning = true; + +const char APP_NAME[] = "iox-c-find-service"; + +static void sigHandler(int signalValue) +{ + // Ignore unused variable warning + (void)signalValue; + // caught SIGINT or SIGTERM, now exit gracefully + keepRunning = false; +} + +void printSearchResult(const iox_service_description_t service) +{ + printf( + "- Service: %s, Instance: %s, Event: %s\n", service.serviceString, service.instanceString, service.eventString); +} + +int main() +{ + signal(SIGINT, sigHandler); + signal(SIGTERM, sigHandler); + + iox_runtime_init(APP_NAME); + + iox_service_discovery_storage_t storage; + iox_service_discovery_t serviceDiscovery = iox_service_discovery_init(&storage); + + while (keepRunning) + { + printf("\n=========================================\n"); + + printf("\nSearched for {'Radar', 'FrontLeft', 'Image'}. Found the following services:\n"); + iox_service_discovery_find_service_apply_callable( + serviceDiscovery, "Radar", "FrontLeft", "Image", printSearchResult, MessagingPattern_PUB_SUB); + + printf("\nSearched for {'Radar', *, *}. Found the following services:\n"); + iox_service_discovery_find_service_apply_callable( + serviceDiscovery, "Radar", NULL, NULL, printSearchResult, MessagingPattern_PUB_SUB); + + printf("\nSearched for {*, 'FrontLeft', *}. Found the following services:\n"); + iox_service_discovery_find_service_apply_callable( + serviceDiscovery, NULL, "FrontLeft", NULL, printSearchResult, MessagingPattern_PUB_SUB); + + printf("\nSearched for {*, 'FrontRight', 'Image'}. Found the following services:\n"); + iox_service_discovery_find_service_apply_callable( + serviceDiscovery, NULL, "FrontRight", "Image", printSearchResult, MessagingPattern_PUB_SUB); + + printf("\nSearched for {'Camera', *, *}. Found the following services:\n"); + iox_service_discovery_find_service_apply_callable( + serviceDiscovery, "Camera", NULL, NULL, printSearchResult, MessagingPattern_PUB_SUB); + + sleep_for(1000); + } + + iox_service_discovery_deinit(serviceDiscovery); + + return 0; +} diff --git a/iceoryx_examples/icediscovery_in_c/iox_c_offer_service.c b/iceoryx_examples/icediscovery_in_c/iox_c_offer_service.c new file mode 100644 index 00000000000..d12684cdcb6 --- /dev/null +++ b/iceoryx_examples/icediscovery_in_c/iox_c_offer_service.c @@ -0,0 +1,89 @@ +// Copyright (c) 2022 by Apex.AI Inc. All rights reserved. +// +// Licensed 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. +// +// SPDX-License-Identifier: Apache-2.0 + +#include "iceoryx_binding_c/publisher.h" +#include "iceoryx_binding_c/runtime.h" +#include "sleep_for.h" + +#include +#include + +bool keepRunning = true; + +const char APP_NAME[] = "iox-c-offer-service"; + +static void sigHandler(int signalValue) +{ + // Ignore unused variable warning + (void)signalValue; + // caught SIGINT or SIGTERM, now exit gracefully + keepRunning = false; +} + +int main() +{ + signal(SIGINT, sigHandler); + signal(SIGTERM, sigHandler); + + iox_runtime_init(APP_NAME); + + iox_pub_storage_t publisherStorage; + iox_pub_options_t options; + iox_pub_options_init(&options); + iox_pub_t radarLeft = iox_pub_init(&publisherStorage, "Radar", "FrontLeft", "Image", &options); + iox_pub_t radarRight = iox_pub_init(&publisherStorage, "Radar", "FrontRight", "Image", &options); + iox_pub_t lidarLeft = iox_pub_init(&publisherStorage, "Lidar", "FrontLeft", "Counter", &options); + + const int numberCameraPublishers = 5; + iox_pub_storage_t cameraPublisherStorage[numberCameraPublishers]; + iox_pub_t cameraPublishers[numberCameraPublishers]; + cameraPublishers[0] = iox_pub_init(&cameraPublisherStorage[0], "Camera", "FrontLeft", "Counter", &options); + cameraPublishers[1] = iox_pub_init(&cameraPublisherStorage[1], "Camera", "FrontLeft", "Image", &options); + cameraPublishers[2] = iox_pub_init(&cameraPublisherStorage[2], "Camera", "FrontRight", "Counter", &options); + cameraPublishers[3] = iox_pub_init(&cameraPublisherStorage[3], "Camera", "FrontRight", "Image", &options); + cameraPublishers[4] = iox_pub_init(&cameraPublisherStorage[4], "Camera", "BackLeft", "Image", &options); + + bool offer = false; + while (keepRunning) + { + if (offer) + { + for (int i = 0; i < numberCameraPublishers; ++i) + { + iox_pub_offer(cameraPublishers[i]); + } + } + else + { + for (int i = 0; i < numberCameraPublishers; ++i) + { + iox_pub_stop_offer(cameraPublishers[i]); + } + } + offer = !offer; + sleep_for(1000); + } + + iox_pub_deinit(radarLeft); + iox_pub_deinit(radarRight); + iox_pub_deinit(lidarLeft); + for (int i = 0; i < numberCameraPublishers; ++i) + { + iox_pub_deinit(cameraPublishers[i]); + } + + return 0; +} diff --git a/iceoryx_examples/icediscovery_in_c/sleep_for.h b/iceoryx_examples/icediscovery_in_c/sleep_for.h new file mode 100644 index 00000000000..1a57d870fdc --- /dev/null +++ b/iceoryx_examples/icediscovery_in_c/sleep_for.h @@ -0,0 +1,37 @@ +// Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved. +// +// Licensed 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. +// +// SPDX-License-Identifier: Apache-2.0 + +#ifndef IOX_BINDING_C_SLEEP_FOR_H +#define IOX_BINDING_C_SLEEP_FOR_H + +#ifdef _WIN32 +#include + +void sleep_for(uint32_t milliseconds) +{ + Sleep((uint64_t)milliseconds); +} + +#else +#include + +void sleep_for(uint32_t milliseconds) +{ + usleep(milliseconds * 1000U); +} +#endif + +#endif diff --git a/iceoryx_integrationtest/CMakeLists.txt b/iceoryx_integrationtest/CMakeLists.txt index 211e1114efd..7389e43b49f 100644 --- a/iceoryx_integrationtest/CMakeLists.txt +++ b/iceoryx_integrationtest/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2021 by Apex.AI Inc. All rights reserved. +# Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved. # # Licensed 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 @@ -41,6 +41,7 @@ if(BUILD_TESTING) add_ros_test(iceoryx_integrationtest/test_request_response_basic.py) add_ros_test(iceoryx_integrationtest/test_request_response_listener.py) add_ros_test(iceoryx_integrationtest/test_request_response_untyped.py) + add_ros_test(iceoryx_integrationtest/test_icediscovery_in_c_example.py) endif() diff --git a/iceoryx_integrationtest/iceoryx_integrationtest/test_icediscovery_in_c_example.py b/iceoryx_integrationtest/iceoryx_integrationtest/test_icediscovery_in_c_example.py new file mode 100644 index 00000000000..da3dad5d2a8 --- /dev/null +++ b/iceoryx_integrationtest/iceoryx_integrationtest/test_icediscovery_in_c_example.py @@ -0,0 +1,119 @@ +# Copyright (c) 2022 by Apex.AI Inc. All rights reserved. +# +# Licensed 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. +# +# SPDX-License-Identifier: Apache-2.0 + +import os + +import unittest + +import launch +from launch_ros.substitutions import ExecutableInPackage +import launch_testing +import launch_testing.actions +from launch_testing.asserts import assertSequentialStdout + +import pytest + +# @brief Test goal: "Integrationtest for the icediscovery in C example of iceoryx" +# @pre setup ROS2 launch executables for RouDi (debug mode) and the example processes +# @post check if all applications return exitcode 0 (success) after test run +@pytest.mark.launch_test +def generate_test_description(): + + proc_env = os.environ.copy() + colcon_prefix_path = os.environ.get('COLCON_PREFIX_PATH', '') + # executable_list = ['iox-offer-service', 'iox-find-service'] + # process_list = [] + + # for exec in executable_list: + # tmp_exec = os.path.join( + # colcon_prefix_path, + # 'example_icediscovery/bin/', + # exec) + # tmp_process = launch.actions.ExecuteProcess( + # cmd=[tmp_exec], + # env=proc_env, output='screen') + # process_list.append(tmp_process) + + # print("Process list:", process_list) + + # roudi_executable = os.path.join( + # colcon_prefix_path, + # 'iceoryx_posh/bin/', + # 'iox-roudi' + # ) + # roudi_process = launch.actions.ExecuteProcess( + # cmd=[roudi_executable, '-l', 'debug'], + # env=proc_env, output='screen', + # sigterm_timeout='20') + + # return launch.LaunchDescription([ + # process_list[0], + # process_list[1], + # roudi_process, + # launch_testing.actions.ReadyToTest() + # ]), {'iox-offer-service': process_list[0], 'iox-find-service': process_list[1], + # 'roudi_process': roudi_process} + +# These tests will run concurrently with the dut process. After this test is done, +# the launch system will shut down RouDi + + +class TestIcediscoveryExample(unittest.TestCase): + def test_roudi_ready(self, proc_output): + proc_output.assertWaitFor( + 'RouDi is ready for clients', timeout=45, stream='stdout') + + def test_find_service(self, proc_output): + # proc_output.assertWaitFor( + # 'Searched for {\'Radar\', \'FrontLeft\', \'Image\'}. Found the following services:\n- Service: Radar, Instance: FrontLeft, Event: Image', + # timeout=45, stream='stdout') + # proc_output.assertWaitFor( + # 'Searched for {\'Radar\', *, *}. Found the following services:\n- Service: Radar, Instance: FrontLeft, Event: Image\n- Service: Radar, Instance: FrontRight, Event: Image', + # timeout=45, stream='stdout') + # proc_output.assertWaitFor( + # 'Searched for {*, \'FrontLeft\', *}. Found the following services:\n- Service: Radar, Instance: FrontLeft, Event: Image\n- Service: Lidar, Instance: FrontLeft, Event: Counter\n- Service: Camera, Instance: FrontLeft, Event: Image\n- Service: Camera, Instance: FrontLeft, Event: Counter', + # timeout=45, stream='stdout') + # proc_output.assertWaitFor( + # 'Searched for {*, \'FrontRight\', \'Image\'}. Found the following services:\n- Service: Radar, Instance: FrontRight, Event: Image\n- Service: Camera, Instance: FrontRight, Event: Image', + # timeout=45, stream='stdout') + # proc_output.assertWaitFor( + # 'Searched for {\'Camera\', *, *}. Found the following services:\n- Service: Camera, Instance: FrontLeft, Event: Image\n- Service: Camera, Instance: FrontRight, Event: Counter\n- Service: Camera, Instance: FrontRight, Event: Image\n- Service: Camera, Instance: BackLeft, Event: Image\n- Service: Camera, Instance: FrontLeft, Event: Counter', + # timeout=45, stream='stdout') + + # proc_output.assertWaitFor( + # 'Searched for {\'Radar\', \'FrontLeft\', \'Image\'}. Found the following services:\n- Service: Radar, Instance: FrontLeft, Event: Image', + # timeout=45, stream='stdout') + # proc_output.assertWaitFor( + # 'Searched for {\'Radar\', *, *}. Found the following services:\n- Service: Radar, Instance: FrontLeft, Event: Image\n- Service: Radar, Instance: FrontRight, Event: Image', + # timeout=45, stream='stdout') + # proc_output.assertWaitFor( + # 'Searched for {*, \'FrontLeft\', *}. Found the following services:\n- Service: Radar, Instance: FrontLeft, Event: Image\n- Service: Lidar, Instance: FrontLeft, Event: Counter', + # timeout=45, stream='stdout') + # proc_output.assertWaitFor( + # 'Searched for {*, \'FrontRight\', \'Image\'}. Found the following services:\n- Service: Radar, Instance: FrontRight, Event: Image', + # timeout=45, stream='stdout') + # proc_output.assertWaitFor( + # 'Searched for {\'Camera\', *, *}. Found the following services:', + # timeout=45, stream='stdout') + +# These tests run after shutdown and examine the stdout log + + +@ launch_testing.post_shutdown_test() +class TestIcediscoveryExampleExitCodes(unittest.TestCase): + def test_exit_code(self, proc_info): + launch_testing.asserts.assertExitCodes(proc_info) + diff --git a/iceoryx_meta/CMakeLists.txt b/iceoryx_meta/CMakeLists.txt index aba4024f098..c9aec6e00df 100644 --- a/iceoryx_meta/CMakeLists.txt +++ b/iceoryx_meta/CMakeLists.txt @@ -1,5 +1,5 @@ # Copyright (c) 2019 - 2020 by Robert Bosch GmbH. All rights reserved. -# Copyright (c) 2020 - 2021 by Apex.AI Inc. All rights reserved. +# Copyright (c) 2020 - 2022 by Apex.AI Inc. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -72,6 +72,7 @@ if(EXAMPLES) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../iceoryx_examples/waitset_in_c ${CMAKE_BINARY_DIR}/iceoryx_examples/waitset_in_c) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../iceoryx_examples/request_response_in_c ${CMAKE_BINARY_DIR}/iceoryx_examples/request_response_in_c) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../iceoryx_examples/iceperf ${CMAKE_BINARY_DIR}/iceoryx_examples/iceperf) + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../iceoryx_examples/icediscovery_in_c ${CMAKE_BINARY_DIR}/iceoryx_examples/icediscovery_in_c) endif() add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../iceoryx_examples/waitset ${CMAKE_BINARY_DIR}/iceoryx_examples/waitset) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../iceoryx_examples/callbacks ${CMAKE_BINARY_DIR}/iceoryx_examples/callbacks)