stratum_server/
router.rs

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        // if log::log_enabled!(log::Level::Trace) {
39        //@todo I think there is something really good we have going here, but needs to be
40        //refined a bit more before it's ready.
41        //@todo what we need to do here is idenitfy any message that is session specific -> I
42        //think one of the newer builds of btccom pool does this.
43        // if let MessageValue::ExMessage(msg) = value {
44        // if connection.is_agent().await {
45        // log::trace!(
46        //     "Calling method: {} for connection: {} with miner ID: {}",
47        //     method,
48        //     connection.id(),
49        // );
50        // } else {
51        tracing::debug!(
52            ip = connection.ip().to_string(),
53            "Calling method: {} for connection: {}",
54            value.method(),
55            connection.id()
56        );
57        // }
58        // }
59
60        let request = StratumRequest {
61            state,
62            values: value,
63            global_vars,
64        };
65
66        endpoint.call(request, connection).await;
67    }
68}