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
use crate::{
route::{DynEndpoint, Endpoint},
types::GlobalVars,
Frame, Session, StratumRequest,
};
use std::collections::HashMap;
use tracing::warn;
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,
value: Frame,
state: State,
connection: Session<CState>,
global_vars: GlobalVars,
) {
let Some(endpoint) = self.routes.get(value.method()) else {
warn!("Method {} was not found", value.method());
return;
};
// if log::log_enabled!(log::Level::Trace) {
//@todo I think there is something really good we have going here, but needs to be
//refined a bit more before it's ready.
//@todo what we need to do here is idenitfy any message that is session specific -> I
//think one of the newer builds of btccom pool does this.
// if let MessageValue::ExMessage(msg) = value {
// if connection.is_agent().await {
// log::trace!(
// "Calling method: {} for connection: {} with miner ID: {}",
// method,
// connection.id(),
// );
// } else {
tracing::debug!(
ip = connection.ip().to_string(),
"Calling method: {} for connection: {}",
value.method(),
connection.id()
);
// }
// }
let request = StratumRequest {
state,
values: value,
global_vars,
};
endpoint.call(request, connection).await;
}
}