Skip to content

Commit

Permalink
[eclipse-iceoryx#503] Add socket to event multiplexing example
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Nov 6, 2024
1 parent 29c71af commit df5eb8c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
4 changes: 4 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -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"
Expand Down
42 changes: 42 additions & 0 deletions examples/rust/event_multiplexing/socket.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::error::Error>> {
let node = NodeBuilder::new().create::<ipc::Service>()?;

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<u8> = vec![1u8, 2u8, 3u8, 4u8, 5u8];
sender.try_send(data.as_slice()).unwrap();

println!("[sending data: \"{:?}\"]", data);
}

println!("exit");

Ok(())
}
26 changes: 24 additions & 2 deletions examples/rust/event_multiplexing/wait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
let args = Args::parse();
Expand Down Expand Up @@ -45,17 +50,28 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// 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<ipc::Service>| {
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
Expand All @@ -68,6 +84,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.unwrap();

println!("");
} else if attachment_id == socket_id {
println!("#### Received trigger from UnixDatagrammSocket");
let mut recv_data: Vec<u8> = vec![];
recv_data.resize(5, 0);
receiver.try_receive(recv_data.as_mut_slice()).unwrap();
println!("Received data: {:?}", recv_data);
}
};

Expand Down

0 comments on commit df5eb8c

Please sign in to comment.