1use std::io::Result;
2
3use crate::{
4 async_api::Server as AsyncServer,
5 sync_api::{
6 async_impls::{AsyncConsensusImpl, AsyncInfoImpl, AsyncMempoolImpl, AsyncSnapshotImpl},
7 Consensus, Info, Mempool, Snapshot,
8 },
9 Address,
10};
11
12pub struct Server<C, M, I, S>
14where
15 C: Consensus + Send + Sync + 'static,
16 M: Mempool + Send + Sync + 'static,
17 I: Info + Send + Sync + 'static,
18 S: Snapshot + Send + Sync + 'static,
19{
20 async_server: AsyncServer<
21 AsyncConsensusImpl<C>,
22 AsyncMempoolImpl<M>,
23 AsyncInfoImpl<I>,
24 AsyncSnapshotImpl<S>,
25 >,
26}
27
28impl<C, M, I, S> Server<C, M, I, S>
29where
30 C: Consensus + Send + Sync + 'static,
31 M: Mempool + Send + Sync + 'static,
32 I: Info + Send + Sync + 'static,
33 S: Snapshot + Send + Sync + 'static,
34{
35 pub fn new(consensus: C, mempool: M, info: I, snapshot: S) -> Self {
37 Self {
38 async_server: AsyncServer::new(
39 AsyncConsensusImpl::new(consensus),
40 AsyncMempoolImpl::new(mempool),
41 AsyncInfoImpl::new(info),
42 AsyncSnapshotImpl::new(snapshot),
43 ),
44 }
45 }
46
47 pub fn run<T>(&self, addr: T) -> Result<()>
49 where
50 T: Into<Address>,
51 {
52 cfg_if::cfg_if! {
53 if #[cfg(feature = "use-async-std")] {
54 async_std::task::block_on(async { self.async_server.run(addr).await })
55 } else if #[cfg(feature = "use-smol")] {
56 smol::block_on(async { self.async_server.run(addr).await })
57 } else if #[cfg(feature = "use-tokio")] {
58 let runtime = tokio::runtime::Runtime::new()?;
59 runtime.block_on(async { self.async_server.run(addr).await })
60 } else {
61 unreachable!()
62 }
63 }
64 }
65}