-
-
Save neilisaac/7427ef223b4e91dae8bb6142b30b8a10 to your computer and use it in GitHub Desktop.
Example usage of high-level Rust bindings for IceOryx C API
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
use bytes::{Buf, BufMut}; | |
use iceoryx::{NotificationBuffer, PubOptions, Publisher, SubOptions, Subscriber, WaitSet}; | |
#[test] | |
fn test_pub_sub_with_wait_set() { | |
iceoryx::set_log_level(log::Level::Trace); | |
iceoryx::runtime_init("example"); | |
let sub = Subscriber::new( | |
"service", | |
"instance", | |
"event", | |
SubOptions::default() | |
.with_subscribe_on_create(true) | |
.with_queue_capacity(10) | |
.with_history_request(1), | |
); | |
let mut ws = WaitSet::new(); | |
ws.attach_subscriber_state(&sub, 123).unwrap(); | |
let thread = std::thread::spawn(|| { | |
let publisher = Publisher::new( | |
"service", | |
"instance", | |
"event", | |
PubOptions::default() | |
.with_offer_on_create(true) | |
.with_history_capacity(10), | |
); | |
std::thread::sleep(std::time::Duration::from_millis(10)); | |
let mut chunk = publisher | |
.loan_chunk(3 * std::mem::size_of::<i32>()) | |
.unwrap(); | |
let mut slice = chunk.as_slice(); | |
slice.put_i32(1); | |
slice.put_i32(2); | |
slice.put_i32(3); | |
chunk.publish(); | |
}); | |
let mut notifications = NotificationBuffer::new(10); | |
assert_eq!(ws.wait(&mut notifications, None), (1, 0)); | |
let notification = notifications.iter().next().unwrap(); | |
assert_eq!(notification.get_notification_id(), 123); | |
assert!(notification.does_originate_from_subscriber(&sub)); | |
assert_eq!(notification.get_subscriber_origin(), sub); | |
let chunk = sub.take_chunk().unwrap(); | |
let mut slice = chunk.as_slice(); | |
assert_eq!(slice.len(), 12); | |
assert_eq!( | |
(slice.get_i32(), slice.get_i32(), slice.get_i32()), | |
(1, 2, 3) | |
); | |
thread.join().unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment