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
69
70
71
72
73
74
75
76
77
78
79
80
//! API implementations provided by application.

pub mod client;
pub mod control;

use std::fmt::Debug;

use actix::MailboxError;
use futures::future::LocalBoxFuture;
use medea_client_api_proto::{Command, Credential, MemberId};

use crate::{
    api::client::rpc_connection::{
        ClosedReason, RpcConnection, RpcConnectionSettings,
    },
    signalling::room::RoomError,
};

/// Errors which [`RpcServer`] can return.
#[derive(Debug)]
pub enum RpcServerError {
    /// Authorization on the [`RpcServer`] was failed.
    Authorization,

    /// [`Room`] returned some [`RoomError`].
    ///
    /// [`Room`]: crate::signalling::room::Room
    RoomError(RoomError),

    /// [`Room`]s [`MailboxError`] is closed or overflowed.
    ///
    /// [`Room`]: crate::signalling::room::Room
    RoomMailbox(MailboxError),
}

impl From<RoomError> for RpcServerError {
    fn from(err: RoomError) -> Self {
        match &err {
            RoomError::AuthorizationError => Self::Authorization,
            _ => Self::RoomError(err),
        }
    }
}

/// Server side of Medea RPC protocol.
#[cfg_attr(test, mockall::automock)]
pub trait RpcServer: Debug + Send {
    /// Send signal of new [`RpcConnection`] being established with specified
    /// [`Member`]. Transport should consider dropping connection if message
    /// result is err.
    ///
    /// [`Member`]: crate::signalling::elements::Member
    fn connection_established(
        &self,
        member_id: MemberId,
        credential: Credential,
        connection: Box<dyn RpcConnection>,
    ) -> LocalBoxFuture<'static, Result<RpcConnectionSettings, RpcServerError>>;

    /// Send signal of existing [`RpcConnection`] of specified [`Member`] being
    /// closed.
    ///
    /// [`Member`]: crate::signalling::elements::Member
    fn connection_closed(
        self: Box<Self>,
        member_id: MemberId,
        reason: ClosedReason,
    ) -> LocalBoxFuture<'static, ()>;

    /// Sends [`Command`].
    fn send_command(&self, member_id: MemberId, msg: Command);

    /// Sends [`Member`]'s request to synchronize its state.
    ///
    /// [`Member`]: crate::signalling::elements::Member
    fn synchronize(&self, member_id: MemberId) -> LocalBoxFuture<'static, ()>;
}

#[cfg(test)]
impl_debug_by_struct_name!(MockRpcServer);