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
use std::io::Result;
use crate::{
async_api::Server as AsyncServer,
sync_api::{
async_impls::{AsyncConsensusImpl, AsyncInfoImpl, AsyncMempoolImpl, AsyncSnapshotImpl},
Consensus, Info, Mempool, Snapshot,
},
Address,
};
pub struct Server<C, M, I, S>
where
C: Consensus + Send + Sync + 'static,
M: Mempool + Send + Sync + 'static,
I: Info + Send + Sync + 'static,
S: Snapshot + Send + Sync + 'static,
{
async_server: AsyncServer<
AsyncConsensusImpl<C>,
AsyncMempoolImpl<M>,
AsyncInfoImpl<I>,
AsyncSnapshotImpl<S>,
>,
}
impl<C, M, I, S> Server<C, M, I, S>
where
C: Consensus + Send + Sync + 'static,
M: Mempool + Send + Sync + 'static,
I: Info + Send + Sync + 'static,
S: Snapshot + Send + Sync + 'static,
{
pub fn new(consensus: C, mempool: M, info: I, snapshot: S) -> Self {
Self {
async_server: AsyncServer::new(
AsyncConsensusImpl::new(consensus),
AsyncMempoolImpl::new(mempool),
AsyncInfoImpl::new(info),
AsyncSnapshotImpl::new(snapshot),
),
}
}
pub fn run<T>(&self, addr: T) -> Result<()>
where
T: Into<Address>,
{
cfg_if::cfg_if! {
if #[cfg(feature = "use-async-std")] {
async_std::task::block_on(async { self.async_server.run(addr).await })
} else if #[cfg(feature = "use-smol")] {
smol::block_on(async { self.async_server.run(addr).await })
} else if #[cfg(feature = "use-tokio")] {
let runtime = tokio::runtime::Runtime::new()?;
runtime.block_on(async { self.async_server.run(addr).await })
} else {
unreachable!()
}
}
}
}