Skip to content

Commit ca22976

Browse files
authored
Merge pull request #454 from elfenpiff/iox2-390-c-cxx-examples
[#390] c cxx examples
2 parents ec5867f + 1436d6a commit ca22976

File tree

17 files changed

+701
-16
lines changed

17 files changed

+701
-16
lines changed

config/iceoryx2.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ subscriber-expired-connection-buffer = 128
3737
max-listeners = 16
3838
max-notifiers = 16
3939
max-nodes = 36
40-
event-id-max-value = 32
40+
event-id-max-value = 4294967295

doc/release-notes/iceoryx2-unreleased.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
-->
3535

3636
* Rename `NodeEvent` into `WaitEvent` [#390](https://github.com/eclipse-iceoryx/iceoryx2/issues/390)
37+
* Remove ACL dependency [#457](https://github.com/eclipse-iceoryx/iceoryx2/issues/457)
3738

3839
### Workflow
3940

examples/README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ These types are demonstrated in the complex data types example.
7070

7171
## Overview
7272

73-
| Name | Language | Description |
74-
| ---------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
75-
| complex data types | [C++](cxx/complex_data_types) [Rust](rust/complex_data_types) | Send zero-copy compatible versions of `Vec` and `String`. Introduces `PlacementDefault` trait for large data types to perform an in place initialization where otherwise a stack overflow would be encountered. |
76-
| discovery | [C](c/discovery) [C++](cxx/discovery) [Rust](rust/discovery) | List all available services in a system. |
77-
| docker | [all](rust/docker) | Communicate between different docker containers and the host. |
78-
| domains | [C](c/domains) [C++](cxx/domains) [Rust](rust/domains) | Establish separate domains that operate independently from one another. |
79-
| event | [C](c/event) [C++](cxx/event) [Rust](rust/event) | Push notifications - send event signals to wakeup processes that are waiting for them. |
80-
| event multiplexing | [Rust](rust/event_multiplexing) | Wait on multiple listeners or sockets with a single call. The WaitSet demultiplexes incoming events and notifies the user. |
81-
| publish subscribe | [C](c/publish_subscribe) [C++](cxx/publish_subscribe) [Rust](rust/publish_subscribe) | Communication between multiple processes with a [publish subscribe messaging pattern](https://en.wikipedia.org/wiki/Publish–subscribe_pattern). |
82-
| publish subscribe dynamic data | [Rust](rust/publish_subscribe_dynamic_data) | Communication between multiple processes with a [publish subscribe messaging pattern](https://en.wikipedia.org/wiki/Publish–subscribe_pattern) and payload data that consists of a slice of shared memory compatible types. |
83-
| publish subscribe with user header | [C](c/publish_subscribe_with_user_header) [C++](cxx/publish_subscribe_with_user_header) [Rust](rust/publish_subscribe_with_user_header) | Add a user header to the payload (samples) to transfer additional information. |
84-
| service attributes | [Rust](rust/service_attributes) | Creates a service with custom attributes that are available to every endpoint. If the attributes are not compatible the service will not open. |
73+
| Name | Language | Description |
74+
| ---------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
75+
| complex data types | [Rust](rust/complex_data_types) | Send zero-copy compatible versions of `Vec` and `String`. Introduces `PlacementDefault` trait for large data types to perform an in place initialization where otherwise a stack overflow would be encountered. |
76+
| discovery | [C](c/discovery) [C++](cxx/discovery) [Rust](rust/discovery) | List all available services in a system. |
77+
| docker | [all](rust/docker) | Communicate between different docker containers and the host. |
78+
| domains | [C](c/domains) [C++](cxx/domains) [Rust](rust/domains) | Establish separate domains that operate independently from one another. |
79+
| event | [C](c/event) [C++](cxx/event) [Rust](rust/event) | Push notifications - send event signals to wakeup processes that are waiting for them. |
80+
| event multiplexing | [C](c/event_multiplexing) [C++](cxx/event_multiplexing) [Rust](rust/event_multiplexing) | Wait on multiple listeners or sockets with a single call. The WaitSet demultiplexes incoming events and notifies the user. |
81+
| publish subscribe | [C](c/publish_subscribe) [C++](cxx/publish_subscribe) [Rust](rust/publish_subscribe) | Communication between multiple processes with a [publish subscribe messaging pattern](https://en.wikipedia.org/wiki/Publish–subscribe_pattern). |
82+
| publish subscribe dynamic data | [Rust](rust/publish_subscribe_dynamic_data) | Communication between multiple processes with a [publish subscribe messaging pattern](https://en.wikipedia.org/wiki/Publish–subscribe_pattern) and payload data that has a dynamic size. |
83+
| publish subscribe with user header | [C](c/publish_subscribe_with_user_header) [C++](cxx/publish_subscribe_with_user_header) [Rust](rust/publish_subscribe_with_user_header) | Add a user header to the payload (samples) to transfer additional information. |
84+
| service attributes | [Rust](rust/service_attributes) | Creates a service with custom attributes that are available to every endpoint. If the attributes are not compatible the service will not open. |

examples/c/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ project(examples_c LANGUAGES C)
1616
add_subdirectory(discovery)
1717
add_subdirectory(domains)
1818
add_subdirectory(event)
19+
add_subdirectory(event_multiplexing)
1920
add_subdirectory(publish_subscribe)
2021
add_subdirectory(publish_subscribe_with_user_header)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright (c) 2024 Contributors to the Eclipse Foundation
2+
#
3+
# See the NOTICE file(s) distributed with this work for additional
4+
# information regarding copyright ownership.
5+
#
6+
# This program and the accompanying materials are made available under the
7+
# terms of the Apache Software License 2.0 which is available at
8+
# https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9+
# which is available at https://opensource.org/licenses/MIT.
10+
#
11+
# SPDX-License-Identifier: Apache-2.0 OR MIT
12+
13+
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
14+
15+
cc_binary(
16+
name = "example_c_event_multiplexing_wait",
17+
srcs = [
18+
"src/wait.c",
19+
],
20+
deps = [
21+
"//:iceoryx2-c",
22+
],
23+
)
24+
25+
cc_binary(
26+
name = "example_c_event_multiplexing_notifier",
27+
srcs = [
28+
"src/notifier.c",
29+
],
30+
deps = [
31+
"//:iceoryx2-c",
32+
],
33+
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright (c) 2024 Contributors to the Eclipse Foundation
2+
#
3+
# See the NOTICE file(s) distributed with this work for additional
4+
# information regarding copyright ownership.
5+
#
6+
# This program and the accompanying materials are made available under the
7+
# terms of the Apache Software License 2.0 which is available at
8+
# https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9+
# which is available at https://opensource.org/licenses/MIT.
10+
#
11+
# SPDX-License-Identifier: Apache-2.0 OR MIT
12+
13+
cmake_minimum_required(VERSION 3.22)
14+
project(example_c_event_multiplexing LANGUAGES C)
15+
16+
find_package(iceoryx2-c 0.4.1 REQUIRED)
17+
18+
add_executable(example_c_event_multiplexing_wait src/wait.c)
19+
target_link_libraries(example_c_event_multiplexing_wait iceoryx2-c::static-lib)
20+
21+
add_executable(example_c_event_multiplexing_notifier src/notifier.c)
22+
target_link_libraries(example_c_event_multiplexing_notifier iceoryx2-c::static-lib)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Event
2+
3+
Before proceeding, all dependencies need to be installed. You can find
4+
instructions in the [C Examples Readme](../README.md).
5+
6+
## Running The Example
7+
8+
This example demonstrates iceoryx2's event multiplexing mechanism,
9+
called the `WaitSet`. It allows waiting, with a single call, on
10+
multiple `Listener` ports as well as external file descriptor-based
11+
events such as `sockets`.
12+
13+
In this setup, the `wait` process monitors two services, which the
14+
user can specify via the command line option `-s` and `-t`.
15+
The `notifier` can define the service to which it will send event
16+
notifications using the `-s` option and specify the event ID with
17+
the `-e` option.
18+
19+
In the example below, we are waiting for events on the services `fuu` and
20+
`bar`. Service `fuu` is notified with event ID `123`, and service `bar` is
21+
notified with event ID `456`.
22+
23+
### Terminal 1
24+
25+
```sh
26+
./target/ffi/build/examples/c/event_multiplexing/example_c_event_multiplexing_wait fuu bar
27+
```
28+
29+
### Terminal 2
30+
31+
```sh
32+
./target/ffi/build/examples/c/event_multiplexing/example_c_event_multiplexing_notifier 123 fuu
33+
```
34+
35+
### Terminal 3
36+
37+
```sh
38+
./target/ffi/build/examples/c/event_multiplexing/example_c_event_multiplexing_notifier 456 bar
39+
```
40+
41+
Feel free to instantiate multiple notifiers for the same service with the same
42+
or different event id's. Or to for different services.
43+
44+
## Technical Details
45+
46+
The `iox2_waitset_t` utilizes `epoll`, `select`, or other event-multiplexing
47+
mechanisms. Before the `iox2_waitset_t` can monitor a specific event, it must
48+
first be attached using `iox2_waitset_attach_notification()`, which returns a
49+
RAII `iox2_waitset_guard_t`. This `Guard` automatically detaches the attachment
50+
when it is cleaned up with `iox2_waitset_guard_drop()`.
51+
52+
The `iox2_waitset_wait_and_process()` call requires a callback that is invoked
53+
for each triggered attachment and provides the `iox2_waitset_attachment_id_h`.
54+
The user can either use `iox2_waitset_attachment_id_has_event_from()` to
55+
identify the object associated with the `iox2_waitset_attachment_id_h`.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright (c) 2024 Contributors to the Eclipse Foundation
2+
//
3+
// See the NOTICE file(s) distributed with this work for additional
4+
// information regarding copyright ownership.
5+
//
6+
// This program and the accompanying materials are made available under the
7+
// terms of the Apache Software License 2.0 which is available at
8+
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9+
// which is available at https://opensource.org/licenses/MIT.
10+
//
11+
// SPDX-License-Identifier: Apache-2.0 OR MIT
12+
13+
#include "iox2/iceoryx2.h"
14+
15+
#include <stdint.h>
16+
#include <stdio.h>
17+
#include <string.h>
18+
19+
const int BASE_10 = 10;
20+
21+
#ifdef _WIN64
22+
#include <windows.h>
23+
#define sleep Sleep
24+
#else
25+
#include <unistd.h>
26+
#endif
27+
28+
int main(int argc, char** argv) {
29+
if (argc != 3) {
30+
printf("Usage: %s EVENT_ID SERVICE_NAME\n", argv[0]);
31+
return -1;
32+
}
33+
34+
size_t event_id_value = strtoull(argv[1], NULL, BASE_10);
35+
36+
// create new node
37+
iox2_node_builder_h node_builder_handle = iox2_node_builder_new(NULL);
38+
iox2_node_h node_handle = NULL;
39+
if (iox2_node_builder_create(node_builder_handle, NULL, iox2_service_type_e_IPC, &node_handle) != IOX2_OK) {
40+
printf("Could not create node!\n");
41+
goto end;
42+
}
43+
44+
// create service name
45+
const char* service_name_value = argv[2];
46+
iox2_service_name_h service_name = NULL;
47+
if (iox2_service_name_new(NULL, service_name_value, strlen(service_name_value), &service_name) != IOX2_OK) {
48+
printf("Unable to create service name!\n");
49+
goto drop_node;
50+
}
51+
52+
// create service
53+
iox2_service_name_ptr service_name_ptr = iox2_cast_service_name_ptr(service_name);
54+
iox2_service_builder_h service_builder = iox2_node_service_builder(&node_handle, NULL, service_name_ptr);
55+
iox2_service_builder_event_h service_builder_event = iox2_service_builder_event(service_builder);
56+
iox2_port_factory_event_h service = NULL;
57+
if (iox2_service_builder_event_open_or_create(service_builder_event, NULL, &service) != IOX2_OK) {
58+
printf("Unable to create service!\n");
59+
goto drop_node;
60+
}
61+
62+
// create notifier
63+
iox2_port_factory_notifier_builder_h notifier_builder = iox2_port_factory_event_notifier_builder(&service, NULL);
64+
iox2_notifier_h notifier = NULL;
65+
if (iox2_port_factory_notifier_builder_create(notifier_builder, NULL, &notifier) != IOX2_OK) {
66+
printf("Unable to create notifier!\n");
67+
goto drop_service;
68+
}
69+
70+
// notifier with a period of 1 second
71+
while (iox2_node_wait(&node_handle, 0, 0) == IOX2_OK) {
72+
iox2_event_id_t event_id = { .value = event_id_value }; // NOLINT
73+
if (iox2_notifier_notify_with_custom_event_id(&notifier, &event_id, NULL) != IOX2_OK) {
74+
printf("Failed to notify listener!\n");
75+
goto drop_notifier;
76+
}
77+
78+
printf("[service: \"%s\"] Trigger event with id %lu ...\n", argv[2], (long unsigned) event_id.value);
79+
80+
sleep(1);
81+
}
82+
83+
drop_notifier:
84+
iox2_notifier_drop(notifier);
85+
86+
drop_service:
87+
iox2_port_factory_event_drop(service);
88+
89+
drop_service_name:
90+
iox2_service_name_drop(service_name);
91+
92+
drop_node:
93+
iox2_node_drop(node_handle);
94+
95+
end:
96+
return 0;
97+
}

0 commit comments

Comments
 (0)