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
use crate::{
router::{Router, SenderPair},
NodeMessage,
};
use ockam_core::{Address, Result};
use crate::tokio::{runtime::Runtime, sync::mpsc::Sender};
use core::future::Future;
use ockam_core::compat::sync::Arc;
pub struct Executor {
rt: Arc<Runtime>,
router: Router,
}
impl Default for Executor {
fn default() -> Self {
let rt = Arc::new(Runtime::new().unwrap());
let router = Router::new();
Self { rt, router }
}
}
impl Executor {
pub fn new() -> Self {
Executor::default()
}
pub(crate) fn sender(&self) -> Sender<NodeMessage> {
self.router.sender()
}
pub(crate) fn runtime(&self) -> Arc<Runtime> {
self.rt.clone()
}
pub(crate) fn initialize_system<S: Into<Address>>(&mut self, address: S, senders: SenderPair) {
trace!("Initializing node executor");
self.router.init(address.into(), senders);
}
#[cfg(feature = "std")]
pub fn execute<F>(&mut self, future: F) -> Result<F::Output>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
let rt = Arc::clone(&self.rt);
let join_body = rt.spawn(future);
crate::block_future(&rt, async move { self.router.run().await })?;
let res = crate::block_future(&rt, async move { join_body.await })
.map_err(|_| crate::error::Error::ExecutorBodyJoinError)?;
Ok(res)
}
#[cfg(not(feature = "std"))]
pub fn execute<F>(&mut self, future: F) -> Result<()>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
let rt = Arc::clone(&self.rt);
let _join = rt.spawn(future);
crate::tokio::runtime::execute(&rt, async move { self.router.run().await.unwrap() });
Ok(())
}
}