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
use crate::route::{DynEndpoint, Endpoint};
use crate::types::MessageValue;
use crate::{Connection, StratumRequest};
use async_std::sync::Arc;
use std::collections::HashMap;
pub struct Router<State, CState> {
routes: HashMap<String, Box<DynEndpoint<State, CState>>>,
}
impl<State: Clone + Send + Sync + 'static, CState: Clone + Send + Sync + 'static>
Router<State, CState>
{
pub fn new() -> Router<State, CState> {
Router {
routes: HashMap::new(),
}
}
pub fn add(&mut self, method: &str, ep: impl Endpoint<State, CState>) {
self.routes.insert(method.to_owned(), Box::new(ep));
}
pub async fn call(
&self,
method: &str,
value: MessageValue,
state: State,
connection: Arc<Connection<CState>>,
) {
let endpoint = match self.routes.get(method) {
Some(endpoint) => endpoint,
None => return,
};
let request = StratumRequest {
state,
values: value,
};
endpoint.call(request, connection).await;
}
}