1use crate::{
2 route::{DynEndpoint, Endpoint},
3 types::GlobalVars,
4 Frame, Session, StratumRequest,
5};
6use std::collections::HashMap;
7use tracing::warn;
8
9pub struct Router<State, CState> {
10 routes: HashMap<String, Box<DynEndpoint<State, CState>>>,
11}
12
13impl<State: Clone + Send + Sync + 'static, CState: Clone + Send + Sync + 'static>
14 Router<State, CState>
15{
16 pub fn new() -> Router<State, CState> {
17 Router {
18 routes: HashMap::new(),
19 }
20 }
21
22 pub fn add(&mut self, method: &str, ep: impl Endpoint<State, CState>) {
23 self.routes.insert(method.to_owned(), Box::new(ep));
24 }
25
26 pub async fn call(
27 &self,
28 value: Frame,
29 state: State,
30 connection: Session<CState>,
31 global_vars: GlobalVars,
32 ) {
33 let Some(endpoint) = self.routes.get(value.method()) else {
34 warn!("Method {} was not found", value.method());
35 return;
36 };
37
38 tracing::debug!(
52 ip = connection.ip().to_string(),
53 "Calling method: {} for connection: {}",
54 value.method(),
55 connection.id()
56 );
57 let request = StratumRequest {
61 state,
62 values: value,
63 global_vars,
64 };
65
66 endpoint.call(request, connection).await;
67 }
68}