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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use std::fmt::Debug;
use crate::runtime::BlockDescription;
use crate::runtime::BlockId;
use crate::runtime::Error;
use crate::runtime::FlowgraphDescription;
use crate::runtime::FlowgraphMessage;
use crate::runtime::Pmt;
use crate::runtime::PortId;
use crate::runtime::Timer;
use crate::runtime::channel::mpsc::Sender;
use crate::runtime::channel::oneshot;
/// Clonable control handle for a running [`crate::runtime::Flowgraph`].
///
/// Use this handle to post or call message handlers, inspect the running
/// flowgraph, or request shutdown. `post` only waits until the runtime accepts
/// and forwards the message, while `call` waits for the handler result.
///
/// A handle remains cheap to clone, but operations can fail with
/// [`Error::FlowgraphTerminated`] or [`Error::BlockTerminated`] after the graph
/// or target block has stopped.
#[derive(Debug, Clone)]
pub struct FlowgraphHandle {
inbox: Sender<FlowgraphMessage>,
}
/// Control handle scoped to one block in a running [`crate::runtime::Flowgraph`].
///
/// This is a convenience wrapper around [`FlowgraphHandle`] that stores the
/// target block id for repeated message calls or description requests.
#[derive(Debug, Clone)]
pub struct FlowgraphBlockHandle {
flowgraph: FlowgraphHandle,
block_id: BlockId,
}
impl FlowgraphHandle {
pub(crate) fn new(inbox: Sender<FlowgraphMessage>) -> FlowgraphHandle {
FlowgraphHandle { inbox }
}
/// Get a handle scoped to one block in the running flowgraph.
///
/// The block id is not validated until an operation is performed on the
/// returned handle.
pub fn block(&self, block_id: impl Into<BlockId>) -> FlowgraphBlockHandle {
FlowgraphBlockHandle {
flowgraph: self.clone(),
block_id: block_id.into(),
}
}
/// Post a message to a handler without waiting for the handler to finish.
///
/// This only waits until the runtime accepts and forwards the message. Use
/// [`Self::call`] if you need to wait for handler completion.
pub async fn post(
&self,
block_id: impl Into<BlockId>,
port_id: impl Into<PortId>,
data: Pmt,
) -> Result<(), Error> {
let block_id = block_id.into();
let (tx, rx) = oneshot::channel::<Result<(), Error>>();
self.inbox
.send(FlowgraphMessage::BlockCall {
block_id,
port_id: port_id.into(),
data,
tx,
})
.await
.or(Err(Error::InvalidBlock(block_id)))?;
rx.await?
}
/// Call a handler and return its result.
///
/// Unlike [`Self::post`], this waits for the message handler to complete and
/// returns the handler's [`Pmt`] response.
pub async fn call(
&self,
block_id: impl Into<BlockId>,
port_id: impl Into<PortId>,
data: Pmt,
) -> Result<Pmt, Error> {
let block_id = block_id.into();
let (tx, rx) = oneshot::channel::<Result<Pmt, Error>>();
self.inbox
.send(FlowgraphMessage::BlockCallback {
block_id,
port_id: port_id.into(),
data,
tx,
})
.await
.map_err(|_| Error::InvalidBlock(block_id))?;
rx.await?
}
/// Describe the running flowgraph.
///
/// The description contains block metadata plus type-erased stream and
/// message edges. It is the same shape served by the native control-port
/// API.
pub async fn describe(&self) -> Result<FlowgraphDescription, Error> {
let (tx, rx) = oneshot::channel::<FlowgraphDescription>();
self.inbox
.send(FlowgraphMessage::FlowgraphDescription { tx })
.await
.or(Err(Error::FlowgraphTerminated))?;
let d = rx.await.or(Err(Error::FlowgraphTerminated))?;
Ok(d)
}
/// Describe one block in the running flowgraph.
pub async fn describe_block(
&self,
block_id: impl Into<BlockId>,
) -> Result<BlockDescription, Error> {
let block_id = block_id.into();
let (tx, rx) = oneshot::channel::<Result<BlockDescription, Error>>();
self.inbox
.send(FlowgraphMessage::BlockDescription { block_id, tx })
.await
.map_err(|_| Error::InvalidBlock(block_id))?;
let d = rx.await.map_err(|_| Error::InvalidBlock(block_id))??;
Ok(d)
}
/// Send a stop message to the [`crate::runtime::Flowgraph`].
///
/// Does not wait until the [`crate::runtime::Flowgraph`] is actually terminated.
pub async fn stop(&self) -> Result<(), Error> {
self.inbox
.send(FlowgraphMessage::Terminate)
.await
.map_err(|_| Error::FlowgraphTerminated)?;
Ok(())
}
/// Stop the [`crate::runtime::Flowgraph`].
///
/// Send a terminate message to the [`crate::runtime::Flowgraph`] and wait until it shuts down.
///
/// This method observes shutdown through the control channel closing. It
/// does not return the finished [`crate::runtime::Flowgraph`]; use
/// [`crate::runtime::RunningFlowgraph::stop_and_wait`] when the caller needs
/// to recover and inspect the finished graph.
pub async fn stop_and_wait(&self) -> Result<(), Error> {
self.stop().await.map_err(|_| Error::FlowgraphTerminated)?;
while !self.inbox.is_closed() {
Timer::after(std::time::Duration::from_millis(200)).await;
}
Ok(())
}
}
impl FlowgraphBlockHandle {
/// Get the block id this handle targets.
pub fn id(&self) -> BlockId {
self.block_id
}
/// Post a message to a handler on this block without waiting for completion.
pub async fn post(&self, port_id: impl Into<PortId>, data: Pmt) -> Result<(), Error> {
self.flowgraph.post(self.block_id, port_id, data).await
}
/// Call a handler on this block and return its result.
pub async fn call(&self, port_id: impl Into<PortId>, data: Pmt) -> Result<Pmt, Error> {
self.flowgraph.call(self.block_id, port_id, data).await
}
/// Describe this block.
pub async fn describe(&self) -> Result<BlockDescription, Error> {
self.flowgraph.describe_block(self.block_id).await
}
}