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
use crate::host::{Invocation, InvocationResponse};
use crossbeam_channel::{Receiver, Sender};
use std::error::Error;
use wascc_codec::capabilities::Dispatcher;
#[derive(Clone)]
pub(crate) struct WasccNativeDispatcher {
resp_r: Receiver<InvocationResponse>,
invoc_s: Sender<Invocation>,
capid: String,
}
impl WasccNativeDispatcher {
pub fn new(
resp_r: Receiver<InvocationResponse>,
invoc_s: Sender<Invocation>,
capid: &str,
) -> Self {
WasccNativeDispatcher {
resp_r,
invoc_s,
capid: capid.to_string(),
}
}
}
impl Dispatcher for WasccNativeDispatcher {
fn dispatch(&self, op: &str, msg: &[u8]) -> Result<Vec<u8>, Box<dyn Error>> {
trace!(
"Dispatching operation '{}' ({} bytes) to actor",
op,
msg.len()
);
let inv = Invocation::new(self.capid.to_string(), op, msg.to_vec());
self.invoc_s.send(inv)?;
let resp = self.resp_r.recv();
match resp {
Ok(r) => match r.error {
Some(e) => Err(format!("Invocation failure: {}", e).into()),
None => Ok(r.msg),
},
Err(e) => Err(Box::new(e)),
}
}
}