-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1165 from ApexAI/iox-#1142-icediscovery-in-c
Iox #1142 icediscovery in c
- Loading branch information
Showing
17 changed files
with
608 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 !} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 !} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
# icediscovery in C | ||
|
||
## Introduction | ||
|
||
This example demonstrates how to search for specific services using iceoryx's | ||
service discovery. It provides two applications - one offering different | ||
services and one searching for those with different search queries. The | ||
behavior and structure is quite similar to the [icediscovery C++ example](https://github.com/eclipse-iceoryx/iceoryx/tree/v2.0.0/iceoryx_examples/icedelivery). | ||
|
||
<!--## Expected Output--> | ||
<!-- @todo Add expected output with asciinema recording before v2.0--> | ||
|
||
## Code walkthrough | ||
|
||
### Offer services | ||
|
||
We create several publisher ports which offer their services on construction by | ||
default. For more dynamism the `cameraPublishers` offer/stop their services | ||
periodically. If you want more information on how to create publisher ports, | ||
have a look at the [icedelivery C example](https://github.com/eclipse-iceoryx/iceoryx/tree/v2.0.0/iceoryx_examples/icedelivery_in_c). | ||
|
||
### Find services | ||
|
||
To be able to search for services, we need to include: | ||
|
||
<!--[geoffrey][iceoryx_examples/icediscovery_in_c/iox_c_find_service.c][include service discovery]--> | ||
```c | ||
#include "iceoryx_binding_c/service_discovery.h" | ||
``` | ||
|
||
We create some storage for the service discovery and initialize it. | ||
|
||
<!--[geoffrey][iceoryx_examples/icediscovery_in_c/iox_c_find_service.c][create service discovery handle]--> | ||
```c | ||
iox_service_discovery_storage_t storage; | ||
iox_service_discovery_t serviceDiscovery = iox_service_discovery_init(&storage); | ||
``` | ||
|
||
We can now call three different find service functions. Let's start with | ||
|
||
<!--[geoffrey][iceoryx_examples/icediscovery_in_c/iox_c_find_service.c][find service and apply callable]--> | ||
```c | ||
iox_service_discovery_find_service_apply_callable( | ||
serviceDiscovery, "Radar", "FrontLeft", "Image", printSearchResult, MessagingPattern_PUB_SUB); | ||
``` | ||
which searches for all `{Radar, FrontLeft, Image}` services offered by | ||
publishers and applies the provided function `printSearchResult` on each of | ||
them. The function must have the signature | ||
`void(const iox_service_description_t)`. Here we pass a function that prints the | ||
found services on the console: | ||
<!--[geoffrey][iceoryx_examples/icediscovery_in_c/iox_c_find_service.c][print function to be applied to search results]--> | ||
```c | ||
void printSearchResult(const iox_service_description_t service) | ||
{ | ||
printf( | ||
"- Service: %s, Instance: %s, Event: %s\n", service.serviceString, service.instanceString, service.eventString); | ||
} | ||
``` | ||
|
||
We cannot only search for exact matching services. In combination with | ||
wildcards (`NULL`) we can also search for all instances and events with the | ||
service `Radar`: | ||
|
||
<!--[geoffrey][iceoryx_examples/icediscovery_in_c/iox_c_find_service.c][search for all Radar services]--> | ||
```c | ||
iox_service_discovery_find_service_apply_callable( | ||
serviceDiscovery, "Radar", NULL, NULL, printSearchResult, MessagingPattern_PUB_SUB); | ||
``` | ||
The wildcard can be used for all strings which describe a service, i.e. service, | ||
instance and event. | ||
Let's now search for all `Camera` services and store the results in an array: | ||
<!--[geoffrey][iceoryx_examples/icediscovery_in_c/iox_c_find_service.c][search for all Camera services]--> | ||
```c | ||
numberFoundServices = iox_service_discovery_find_service(serviceDiscovery, | ||
"Camera", | ||
NULL, | ||
NULL, | ||
searchResult, | ||
SEARCH_RESULT_CAPACITY, | ||
&missedServices, | ||
MessagingPattern_PUB_SUB); | ||
``` | ||
|
||
`searchResult` is a `iox_service_description_t` array of size | ||
`SEARCH_RESULT_CAPACITY`. The matching services are written into this array, up | ||
to a maximum of `SEARCH_RESULT_CAPACITY`. If the number of found services | ||
exceeds the array's capacity, the number of services that could not be stored is | ||
written to `missedServices`. The number of stored services is returned. Since | ||
the `cameraPublishers` periodically offer/stop their services, you should see | ||
sometimes 5 `Camera` services and sometimes none. | ||
|
||
Finally, let's try out the third find service function. We search again for all | ||
`Camera` services but additionally count the front camera services: | ||
|
||
<!--[geoffrey][iceoryx_examples/icediscovery_in_c/iox_c_find_service.c][search for all front camera services]--> | ||
```c | ||
iox_service_discovery_find_service_apply_callable_with_context_data( | ||
serviceDiscovery, "Camera", NULL, NULL, searchFrontDevices, &numberFrontCameras, MessagingPattern_PUB_SUB); | ||
``` | ||
This function is quite similar to the first find service function, but we | ||
pass an additional argument `numberFrontCameras`: | ||
<!--[geoffrey][iceoryx_examples/icediscovery_in_c/iox_c_find_service.c][store number of front cameras]--> | ||
```c | ||
uint32_t numberFrontCameras = 0U; | ||
``` | ||
|
||
We use this variable to store the number of front cameras and provide it as | ||
second argument `count` to the function `searchFrontDevices` which is applied to | ||
all found services: | ||
|
||
<!--[geoffrey][iceoryx_examples/icediscovery_in_c/iox_c_find_service.c][search function for all front devices]--> | ||
```c | ||
void searchFrontDevices(const iox_service_description_t service, void* count) | ||
{ | ||
if (strncmp("FrontLeft", service.instanceString, IOX_CONFIG_SERVICE_STRING_SIZE) == 0 | ||
|| strncmp("FrontRight", service.instanceString, IOX_CONFIG_SERVICE_STRING_SIZE) == 0) | ||
{ | ||
++*(uint32_t*)count; | ||
} | ||
} | ||
``` | ||
<center> | ||
[Check out icediscovery on GitHub :fontawesome-brands-github:](https://github.com/eclipse-iceoryx/iceoryx/tree/v2.0.0/iceoryx_examples/icediscovery_in_c){ .md-button } | ||
</center> |
Oops, something went wrong.