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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use crate::host::{Invocation, InvocationResponse};
use crate::Result;
use std::sync::Arc;
use std::sync::RwLock;
use wapc::WapcHost;
lazy_static! {
pub(crate) static ref MIDDLEWARES: Arc<RwLock<Vec<Box<dyn Middleware>>>> =
{ Arc::new(RwLock::new(Vec::new())) };
}
pub trait Middleware: Send + Sync + 'static {
fn actor_pre_invoke(&self, inv: Invocation) -> Result<Invocation>;
fn actor_post_invoke(&self, response: InvocationResponse) -> Result<InvocationResponse>;
fn capability_pre_invoke(&self, inv: Invocation) -> Result<Invocation>;
fn capability_post_invoke(&self, response: InvocationResponse) -> Result<InvocationResponse>;
}
pub(crate) fn invoke_capability(inv: Invocation) -> Result<InvocationResponse> {
let inv = match run_capability_pre_invoke(inv.clone(), &MIDDLEWARES.read().unwrap()) {
Ok(i) => i,
Err(e) => {
error!("Middleware failure: {}", e);
inv
}
};
let lock = crate::plugins::PLUGMAN.read().unwrap();
let r = lock.call(&inv).unwrap();
match run_capability_post_invoke(r.clone(), &MIDDLEWARES.read().unwrap()) {
Ok(r) => Ok(r),
Err(e) => {
error!("Middleware failure: {}", e);
Ok(r)
}
}
}
pub(crate) fn invoke_actor(inv: Invocation, guest: &mut WapcHost) -> Result<InvocationResponse> {
let inv = match run_actor_pre_invoke(inv.clone(), &MIDDLEWARES.read().unwrap()) {
Ok(i) => i,
Err(e) => {
error!("Middleware failure: {}", e);
inv
}
};
let inv_r = match guest.call(&inv.operation, &inv.msg) {
Ok(v) => InvocationResponse::success(v),
Err(e) => InvocationResponse::error(&format!("Failed to invoke guest call: {}", e)),
};
match run_actor_post_invoke(inv_r.clone(), &MIDDLEWARES.read().unwrap()) {
Ok(r) => Ok(r),
Err(e) => {
error!("Middleware failure: {}", e);
Ok(inv_r)
}
}
}
fn run_actor_pre_invoke(
inv: Invocation,
middlewares: &[Box<dyn Middleware>],
) -> Result<Invocation> {
let mut cur_inv = inv;
for m in middlewares {
match m.actor_pre_invoke(cur_inv) {
Ok(i) => cur_inv = i.clone(),
Err(e) => return Err(e),
}
}
Ok(cur_inv)
}
fn run_actor_post_invoke(
resp: InvocationResponse,
middlewares: &[Box<dyn Middleware>],
) -> Result<InvocationResponse> {
let mut cur_resp = resp;
for m in middlewares {
match m.actor_post_invoke(cur_resp) {
Ok(i) => cur_resp = i.clone(),
Err(e) => return Err(e),
}
}
Ok(cur_resp)
}
pub(crate) fn run_capability_pre_invoke(
inv: Invocation,
middlewares: &[Box<dyn Middleware>],
) -> Result<Invocation> {
let mut cur_inv = inv;
for m in middlewares {
match m.capability_pre_invoke(cur_inv) {
Ok(i) => cur_inv = i.clone(),
Err(e) => return Err(e),
}
}
Ok(cur_inv)
}
pub(crate) fn run_capability_post_invoke(
resp: InvocationResponse,
middlewares: &[Box<dyn Middleware>],
) -> Result<InvocationResponse> {
let mut cur_resp = resp;
for m in middlewares {
match m.capability_post_invoke(cur_resp) {
Ok(i) => cur_resp = i.clone(),
Err(e) => return Err(e),
}
}
Ok(cur_resp)
}
#[cfg(test)]
mod test {
use std::sync::atomic::{AtomicUsize, Ordering};
lazy_static! {
pub static ref PRE: AtomicUsize = { AtomicUsize::new(0) };
pub static ref POST: AtomicUsize = { AtomicUsize::new(0) };
pub static ref CAP_PRE: AtomicUsize = { AtomicUsize::new(0) };
pub static ref CAP_POST: AtomicUsize = { AtomicUsize::new(0) };
}
use super::Middleware;
use crate::host::Invocation;
use crate::host::InvocationResponse;
use crate::Result;
struct IncMiddleware {
pre: &'static AtomicUsize,
post: &'static AtomicUsize,
cap_pre: &'static AtomicUsize,
cap_post: &'static AtomicUsize,
}
impl Middleware for IncMiddleware {
fn actor_pre_invoke(&self, inv: Invocation) -> Result<Invocation> {
self.pre.fetch_add(1, Ordering::SeqCst);
Ok(inv)
}
fn actor_post_invoke(&self, response: InvocationResponse) -> Result<InvocationResponse> {
self.post.fetch_add(1, Ordering::SeqCst);
Ok(response)
}
fn capability_pre_invoke(&self, inv: Invocation) -> Result<Invocation> {
self.cap_pre.fetch_add(1, Ordering::SeqCst);
Ok(inv)
}
fn capability_post_invoke(
&self,
response: InvocationResponse,
) -> Result<InvocationResponse> {
self.cap_post.fetch_add(1, Ordering::SeqCst);
Ok(response)
}
}
#[test]
fn simple_add() {
let inc_mid = IncMiddleware {
pre: &PRE,
post: &POST,
cap_pre: &CAP_PRE,
cap_post: &CAP_POST,
};
let mids: Vec<Box<dyn Middleware>> = vec![Box::new(inc_mid)];
let inv = Invocation::new(
"test".to_string(),
"testing:sample!Foo",
b"abc1234".to_vec(),
);
let res = super::run_actor_pre_invoke(inv.clone(), &mids);
assert!(res.is_ok());
let res2 = super::run_actor_pre_invoke(inv, &mids);
assert!(res2.is_ok());
assert_eq!(PRE.fetch_add(0, Ordering::SeqCst), 2);
}
}