forked from eclipse-iceoryx/iceoryx2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriber.rs
More file actions
154 lines (131 loc) · 5.13 KB
/
subscriber.rs
File metadata and controls
154 lines (131 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// 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 core::time::Duration;
use examples_common::{PubSubEvent, TransmissionData};
use iceoryx2::{
port::{listener::Listener, notifier::Notifier, subscriber::Subscriber},
prelude::*,
sample::Sample,
};
const HISTORY_SIZE: usize = 20;
const DEADLINE: Duration = Duration::from_secs(2);
fn main() -> Result<(), Box<dyn core::error::Error>> {
let node = NodeBuilder::new().create::<ipc::Service>()?;
let subscriber = CustomSubscriber::new(&node, &"My/Funk/ServiceName".try_into()?)?;
let waitset = WaitSetBuilder::new().create::<ipc::Service>()?;
// The subscriber is attached as a deadline, meaning that we expect some activity
// latest after the deadline has passed.
let subscriber_guard = waitset.attach_deadline(&subscriber, DEADLINE)?;
let on_event = |attachment_id: WaitSetAttachmentId<ipc::Service>| {
// If we have received a new event on the subscriber we handle it.
if attachment_id.has_event_from(&subscriber_guard) {
subscriber.handle_event().unwrap();
// If the subscriber did not receive an event until DEADLINE has
// passed, we print out a warning.
} else if attachment_id.has_missed_deadline(&subscriber_guard) {
println!(
"Contract violation! The subscriber did not receive a message for {:?}.",
DEADLINE
);
}
CallbackProgression::Continue
};
waitset.wait_and_process(on_event)?;
println!("exit");
Ok(())
}
#[derive(Debug)]
struct CustomSubscriber {
subscriber: Subscriber<ipc::Service, TransmissionData, ()>,
notifier: Notifier<ipc::Service>,
listener: Listener<ipc::Service>,
}
impl FileDescriptorBased for CustomSubscriber {
fn file_descriptor(&self) -> &FileDescriptor {
self.listener.file_descriptor()
}
}
impl SynchronousMultiplexing for CustomSubscriber {}
// High-level subscriber class that contains besides a subscriber also a notifier
// and a listener. The notifier is used to send events like
// `PubSubEvent::ReceivedSample` or to notify the publisher that a new subscriber
// connected.
// The listener waits for events originating from the publisher like
// `PubSubEvent::SentSample`.
impl CustomSubscriber {
fn new(
node: &Node<ipc::Service>,
service_name: &ServiceName,
) -> Result<Self, Box<dyn core::error::Error>> {
let pubsub_service = node
.service_builder(service_name)
.publish_subscribe::<TransmissionData>()
.history_size(HISTORY_SIZE)
.subscriber_max_buffer_size(HISTORY_SIZE)
.open_or_create()?;
let event_service = node
.service_builder(service_name)
.event()
.open_or_create()?;
let listener = event_service.listener_builder().create()?;
let notifier = event_service.notifier_builder().create()?;
let subscriber = pubsub_service.subscriber_builder().create()?;
notifier.notify_with_custom_event_id(PubSubEvent::SubscriberConnected.into())?;
Ok(Self {
subscriber,
listener,
notifier,
})
}
fn handle_event(&self) -> Result<(), Box<dyn core::error::Error>> {
while let Some(event) = self.listener.try_wait_one()? {
let event: PubSubEvent = event.into();
match event {
PubSubEvent::SentHistory => {
println!("History delivered");
while let Ok(Some(sample)) = self.receive() {
println!(" history: {:?}", sample.x);
}
}
PubSubEvent::SentSample => {
while let Ok(Some(sample)) = self.receive() {
println!("received: {:?}", sample.x);
}
}
PubSubEvent::PublisherConnected => println!("new publisher connected"),
PubSubEvent::PublisherDisconnected => println!("publisher disconnected"),
_ => (),
}
}
Ok(())
}
fn receive(
&self,
) -> Result<Option<Sample<ipc::Service, TransmissionData, ()>>, Box<dyn core::error::Error>>
{
match self.subscriber.receive()? {
Some(sample) => {
self.notifier
.notify_with_custom_event_id(PubSubEvent::ReceivedSample.into())?;
Ok(Some(sample))
}
None => Ok(None),
}
}
}
impl Drop for CustomSubscriber {
fn drop(&mut self) {
self.notifier
.notify_with_custom_event_id(PubSubEvent::SubscriberDisconnected.into())
.unwrap();
}
}