paxakos/node/
req_handler.rs

1use std::future::Future;
2use std::sync::Arc;
3
4use crate::error::AcceptError;
5use crate::error::CommitError;
6use crate::error::PrepareError;
7use crate::invocation::CoordNumOf;
8use crate::invocation::Invocation;
9use crate::invocation::LogEntryIdOf;
10use crate::invocation::LogEntryOf;
11use crate::invocation::PromiseFor;
12use crate::invocation::RoundNumOf;
13use crate::invocation::YeaOf;
14
15use super::state_keeper::StateKeeperHandle;
16
17/// Used by [`Communicator`][crate::communicator::Communicator]s to prepare
18/// replies.
19#[derive(Debug)]
20pub struct RequestHandler<I: Invocation>(StateKeeperHandle<I>);
21
22impl<I: Invocation> RequestHandler<I> {
23    pub(crate) fn new(state_keeper: StateKeeperHandle<I>) -> Self {
24        Self(state_keeper)
25    }
26
27    /// Asks this node to answer a `send_prepare` sent by another node.
28    pub fn handle_prepare(
29        &self,
30        round_num: RoundNumOf<I>,
31        coord_num: CoordNumOf<I>,
32    ) -> impl Future<Output = Result<PromiseFor<I>, PrepareError<I>>> {
33        let handle = self.0.clone();
34        async move { handle.handle_prepare(round_num, coord_num).await }
35    }
36
37    /// Asks this node to answer a `send_proposal` sent by another node.
38    pub fn handle_proposal<E: Into<Arc<LogEntryOf<I>>>>(
39        &self,
40        round_num: RoundNumOf<I>,
41        coord_num: CoordNumOf<I>,
42        entry: E,
43    ) -> impl Future<Output = Result<YeaOf<I>, AcceptError<I>>> {
44        let handle = self.0.clone();
45        async move { handle.handle_proposal(round_num, coord_num, entry).await }
46    }
47
48    /// Asks this node to commit the given log entry for the given round number.
49    pub fn handle_commit<E: Into<Arc<LogEntryOf<I>>>>(
50        &self,
51        round_num: RoundNumOf<I>,
52        coord_num: CoordNumOf<I>,
53        entry: E,
54    ) -> impl Future<Output = Result<(), CommitError<I>>> {
55        self.0.handle_commit(round_num, coord_num, entry)
56    }
57
58    /// Asks this node to answer a `send_commit_by_id` sent by another node.
59    pub fn handle_commit_by_id(
60        &self,
61        round_num: RoundNumOf<I>,
62        coord_num: CoordNumOf<I>,
63        entry_id: LogEntryIdOf<I>,
64    ) -> impl Future<Output = Result<(), CommitError<I>>> {
65        let handle = self.0.clone();
66        async move {
67            handle
68                .handle_commit_by_id(round_num, coord_num, entry_id)
69                .await
70        }
71    }
72}