From df5eb8cc5caed487bb01c848324f9a2069fdca77 Mon Sep 17 00:00:00 2001 From: Mathias Kraus Date: Wed, 6 Nov 2024 13:13:10 +0100 Subject: [PATCH] [#503] Add socket to event multiplexing example --- examples/Cargo.toml | 4 +++ examples/rust/event_multiplexing/socket.rs | 42 ++++++++++++++++++++++ examples/rust/event_multiplexing/wait.rs | 26 ++++++++++++-- 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 examples/rust/event_multiplexing/socket.rs diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 16195e450..286b0a052 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -14,6 +14,7 @@ version = { workspace = true } iceoryx2 = { workspace = true } iceoryx2-bb-container = { workspace = true } iceoryx2-bb-log = { workspace = true } +iceoryx2-bb-posix = { workspace = true } iceoryx2-bb-system-types = { workspace = true } clap = { workspace = true } @@ -48,6 +49,9 @@ path = "rust/event/notifier.rs" [[example]] name = "event_multiplexing_notifier" path = "rust/event_multiplexing/notifier.rs" +[[example]] +name = "event_multiplexing_socket" +path = "rust/event_multiplexing/socket.rs" [[example]] name = "event_multiplexing_wait" diff --git a/examples/rust/event_multiplexing/socket.rs b/examples/rust/event_multiplexing/socket.rs new file mode 100644 index 000000000..7abdacfbc --- /dev/null +++ b/examples/rust/event_multiplexing/socket.rs @@ -0,0 +1,42 @@ +// Copyright (c) 2024 Contributors to the Eclipse Foundation +// +// See the NOTICE file(s) distributed with this work for additional +// information regarding copyright ownership. +// +// This program and the accompanying materials are made available under the +// terms of the Apache Software License 2.0 which is available at +// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license +// which is available at https://opensource.org/licenses/MIT. +// +// SPDX-License-Identifier: Apache-2.0 OR MIT + +use iceoryx2_bb_container::semantic_string::SemanticString; +use iceoryx2_bb_posix::unix_datagram_socket::*; +use iceoryx2_bb_system_types::file_path::FilePath; + +use core::time::Duration; +use iceoryx2::prelude::*; + +const CYCLE_TIME: Duration = Duration::from_secs(1); + +fn main() -> Result<(), Box> { + let node = NodeBuilder::new().create::()?; + + let socket_name = FilePath::new(b"mySocket").unwrap(); + + let sender = UnixDatagramSenderBuilder::new(&socket_name) + .create() + .unwrap(); + + while node.wait(CYCLE_TIME).is_ok() { + // send some data + let data: Vec = vec![1u8, 2u8, 3u8, 4u8, 5u8]; + sender.try_send(data.as_slice()).unwrap(); + + println!("[sending data: \"{:?}\"]", data); + } + + println!("exit"); + + Ok(()) +} diff --git a/examples/rust/event_multiplexing/wait.rs b/examples/rust/event_multiplexing/wait.rs index 65276aaa0..ae50e3042 100644 --- a/examples/rust/event_multiplexing/wait.rs +++ b/examples/rust/event_multiplexing/wait.rs @@ -12,7 +12,12 @@ use clap::Parser; use iceoryx2::{port::listener::Listener, prelude::*}; -use std::collections::HashMap; +use iceoryx2_bb_container::semantic_string::SemanticString; +use iceoryx2_bb_posix::file_descriptor::FileDescriptorBased; +use iceoryx2_bb_posix::permission::*; +use iceoryx2_bb_posix::unix_datagram_socket::*; +use iceoryx2_bb_system_types::file_path::FilePath; +use std::{alloc::GlobalAlloc, collections::HashMap, time::Duration}; fn main() -> Result<(), Box> { let args = Args::parse(); @@ -45,17 +50,28 @@ fn main() -> Result<(), Box> { // attach all listeners to the waitset and store the guard for (service, listener) in &listeners { + let fd = (*listener).file_descriptor(); let guard = waitset.attach_notification(listener)?; listener_attachments.insert(WaitSetAttachmentId::from_guard(&guard), (service, listener)); guards.push(guard); } + let socket_name = FilePath::new(b"mySocket").unwrap(); + let receiver = UnixDatagramReceiverBuilder::new(&socket_name) + .permission(Permission::OWNER_ALL) + .creation_mode(CreationMode::PurgeAndCreate) + .create() + .unwrap(); + + let socket_guard = waitset.attach_notification(&receiver)?; + let socket_id = WaitSetAttachmentId::from_guard(&socket_guard); + println!("Waiting on the following services: {:?}", args.services); // the callback that is called when a listener has received an event let on_event = |attachment_id: WaitSetAttachmentId| { if let Some((service_name, listener)) = listener_attachments.get(&attachment_id) { - print!("Received trigger from \"{}\" ::", service_name); + print!("#### Received trigger from \"{}\" ::", service_name); // IMPORTANT: // We need to collect all notifications since the WaitSet will wake us up as long as @@ -68,6 +84,12 @@ fn main() -> Result<(), Box> { .unwrap(); println!(""); + } else if attachment_id == socket_id { + println!("#### Received trigger from UnixDatagrammSocket"); + let mut recv_data: Vec = vec![]; + recv_data.resize(5, 0); + receiver.try_receive(recv_data.as_mut_slice()).unwrap(); + println!("Received data: {:?}", recv_data); } };