[Fix] Connection Management
This commit is contained in:
parent
9e9e3597da
commit
3f2ac18333
122 changed files with 19970 additions and 5263 deletions
|
|
@ -1,36 +1,89 @@
|
|||
use mtp::codec::CommunicationValue;
|
||||
use std::future::Future;
|
||||
use async_trait::async_trait;
|
||||
use iota_identity::{AuthorityId, IotaNodeId};
|
||||
use mtp::codec::{CommunicationType, CommunicationValue};
|
||||
use std::fmt;
|
||||
use std::time::Duration;
|
||||
|
||||
/// Unified interface for all connection types (Omikron, Direct, future modes).
|
||||
///
|
||||
/// Provides the common messaging API that the rest of the codebase uses,
|
||||
/// regardless of whether the connection goes through Omikron or is direct.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum ConnectionError {
|
||||
Disconnected(String),
|
||||
Timeout(String),
|
||||
Protocol(String),
|
||||
}
|
||||
|
||||
impl fmt::Display for ConnectionError {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Disconnected(message) | Self::Timeout(message) | Self::Protocol(message) => {
|
||||
formatter.write_str(message)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for ConnectionError {}
|
||||
|
||||
#[async_trait]
|
||||
pub trait ConnectionHandler: Send + Sync {
|
||||
/// Send a message to the remote end.
|
||||
fn send_message(
|
||||
&self,
|
||||
cv: &CommunicationValue,
|
||||
) -> impl Future<Output = Result<(), String>> + Send;
|
||||
|
||||
/// Send a message and wait for a correlated response.
|
||||
///
|
||||
/// The implementation correlates requests/responses by message ID and
|
||||
/// enforces the given `timeout`. Returns an error on timeout or if the
|
||||
/// connection drops while waiting.
|
||||
fn await_response(
|
||||
async fn send_message(&self, value: &CommunicationValue) -> Result<(), ConnectionError>;
|
||||
|
||||
async fn await_response(
|
||||
&self,
|
||||
cv: &CommunicationValue,
|
||||
value: &CommunicationValue,
|
||||
timeout: Option<Duration>,
|
||||
) -> impl Future<Output = Result<CommunicationValue, String>> + Send;
|
||||
) -> Result<CommunicationValue, ConnectionError>;
|
||||
|
||||
async fn is_connected(&self) -> bool;
|
||||
|
||||
/// Returns `true` when the connection is alive and ready for traffic.
|
||||
fn is_connected(&self) -> impl Future<Output = bool> + Send;
|
||||
async fn is_identified(&self) -> bool;
|
||||
|
||||
/// Returns `true` when the connection has completed identification /
|
||||
/// registration and is fully operational.
|
||||
fn is_identified(&self) -> impl Future<Output = bool> + Send;
|
||||
async fn stop(&self);
|
||||
}
|
||||
|
||||
/// Gracefully tear down the connection.
|
||||
fn stop(&self) -> impl Future<Output = ()> + Send;
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum RouteDestination {
|
||||
Iota(IotaNodeId),
|
||||
LegacyOmegaIota { omega: AuthorityId, iota_id: u64 },
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum RouteOutcome {
|
||||
Accepted {
|
||||
relay_message_id: String,
|
||||
destination_accepted_at: i64,
|
||||
},
|
||||
Rejected {
|
||||
response_type: CommunicationType,
|
||||
},
|
||||
Retryable {
|
||||
reason: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum RouteError {
|
||||
NoRoute(IotaNodeId),
|
||||
Delivery(String),
|
||||
}
|
||||
|
||||
impl fmt::Display for RouteError {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::NoRoute(destination) => {
|
||||
write!(formatter, "no route to Iota {}", destination.as_str())
|
||||
}
|
||||
Self::Delivery(message) => formatter.write_str(message),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for RouteError {}
|
||||
|
||||
#[async_trait]
|
||||
pub trait PeerRouter: Send + Sync {
|
||||
async fn route(
|
||||
&self,
|
||||
destination: &RouteDestination,
|
||||
frame: CommunicationValue,
|
||||
) -> Result<RouteOutcome, RouteError>;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue