1use std::sync::{Arc, Mutex};
2
3use consensus::HandledConsensus;
4use error::Error;
5use handler::ConsensusHandler;
6use message::*;
7use persistent_log::Log;
8use state_machine::StateMachine;
9use {ClientId, ServerId};
10
11#[derive(Debug, Clone)]
14pub struct SharedConsensus<L, M, H> {
15 inner: Arc<Mutex<HandledConsensus<L, M, H>>>,
16}
17
18impl<L, M, H> SharedConsensus<L, M, H>
19where
20 L: Log,
21 M: StateMachine,
22 H: ConsensusHandler,
23{
24 pub fn new(consensus: HandledConsensus<L, M, H>) -> Self {
25 Self {
26 inner: Arc::new(Mutex::new(consensus)),
27 }
28 }
29 pub fn init(&self) {
31 self.inner.lock().unwrap().init()
32 }
33
34 pub fn apply_peer_message(&self, from: ServerId, message: PeerMessage) -> Result<(), Error> {
36 self.inner.lock().unwrap().apply_peer_message(from, message)
37 }
38
39 pub fn apply_client_message(
41 &self,
42 from: ClientId,
43 message: ClientRequest,
44 ) -> Result<(), Error> {
45 self.inner
46 .lock()
47 .unwrap()
48 .apply_client_message(from, message)
49 }
50
51 pub fn apply_timeout(&self, timeout: ConsensusTimeout) -> Result<(), Error> {
53 self.inner.lock().unwrap().apply_timeout(timeout)
54 }
55
56 pub fn heartbeat_timeout(&self, peer: ServerId) -> Result<AppendEntriesRequest, Error> {
58 self.inner.lock().unwrap().heartbeat_timeout(peer)
59 }
60
61 pub fn election_timeout(&self) -> Result<(), Error> {
62 self.inner.lock().unwrap().election_timeout()
63 }
64}