Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1142 Add first findService example
Browse files Browse the repository at this point in the history
Signed-off-by: Marika Lehmann <[email protected]>
  • Loading branch information
FerdinandSpitzschnueffler authored and MatthiasKillat committed Mar 1, 2022
1 parent dbc73fd commit b4a922e
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 3 deletions.
2 changes: 1 addition & 1 deletion doc/website/examples/icediscovery.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Searching for currently available services
title: Searching for currently available services using C++
---

{! ../iceoryx/iceoryx_examples/icediscovery/README.md !}
5 changes: 5 additions & 0 deletions doc/website/examples/icediscovery_in_c.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Searching for currently available services using C
---

{! ../iceoryx/iceoryx_examples/icediscovery/README.md !}
1 change: 1 addition & 0 deletions iceoryx_examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
54 changes: 54 additions & 0 deletions iceoryx_examples/icediscovery_in_c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
83 changes: 83 additions & 0 deletions iceoryx_examples/icediscovery_in_c/iox_c_find_service.c
Original file line number Diff line number Diff line change
@@ -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 <signal.h>
#include <stdbool.h>
#include <stdio.h>

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;
}
89 changes: 89 additions & 0 deletions iceoryx_examples/icediscovery_in_c/iox_c_offer_service.c
Original file line number Diff line number Diff line change
@@ -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 <signal.h>
#include <stdbool.h>

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;
}
37 changes: 37 additions & 0 deletions iceoryx_examples/icediscovery_in_c/sleep_for.h
Original file line number Diff line number Diff line change
@@ -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 <windows.h>

void sleep_for(uint32_t milliseconds)
{
Sleep((uint64_t)milliseconds);
}

#else
#include <unistd.h>

void sleep_for(uint32_t milliseconds)
{
usleep(milliseconds * 1000U);
}
#endif

#endif
3 changes: 2 additions & 1 deletion iceoryx_integrationtest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()

Expand Down
Loading

0 comments on commit b4a922e

Please sign in to comment.