diff options
| author | tv <tv@krebsco.de> | 2026-10-19 23:44:20 +0200 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-10-19 23:44:20 +0200 |
| commit | d3a9d5d3ebff98e342dff1df0ebad86f346719f5 (patch) | |
| tree | f24b52636c4d6aca66849d6deecc79e3d1f96eb5 /src/transport.rs | |
initial commit
Diffstat (limited to 'src/transport.rs')
| -rw-r--r-- | src/transport.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/transport.rs b/src/transport.rs new file mode 100644 index 0000000..95d3046 --- /dev/null +++ b/src/transport.rs @@ -0,0 +1,74 @@ +use anyhow::Result; +use async_trait::async_trait; +use automerge::sync::Message; +use std::boxed::Box; +use std::future::Future; +use std::pin::Pin; + +pub type HeadsDigest = [u8; 32]; + +#[derive(Debug, Clone)] +pub struct Keepalive { + pub heads_digest: HeadsDigest, +} + +#[derive(Debug)] +pub enum ParsedMessage { + Hello(HelloRequest), + HelloReply(HelloReply), + Sync(SyncEnvelope), + Ping(Keepalive), + Pong(Keepalive), +} + +#[derive(Debug)] +pub struct HelloRequest {} + +#[derive(Debug, Clone)] +pub struct HelloReply { + pub cluster_id: String, + pub peers: Vec<String>, +} + +#[derive(Debug, Clone)] +pub struct SyncEnvelope { + pub cluster_id: String, + pub message: Message, +} + +/// A message serialized for the wire. Cloning is cheap (shared buffer). +#[derive(Clone, Debug)] +pub struct EncodedMessage { + pub peer: String, + pub body: bytes::Bytes, +} + +pub type HandlerFn = dyn Fn(String, ParsedMessage) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> + + Send + + Sync + + 'static; +pub type MessageHandler = Box<HandlerFn>; + +#[async_trait] +pub trait Transport: Send + Sync { + /// This node's identity on the transport (for Mycelium: its public key). + fn local_id(&self) -> &str; + + /// Resolves a user-supplied peer hint (e.g. a seed) to a peer identity. + async fn resolve_peer(&self, hint: &str) -> Result<String>; + + fn encode(&self, peer: &str, message: &ParsedMessage) -> Result<EncodedMessage>; + async fn send_encoded(&self, message: &EncodedMessage) -> Result<()>; + + /// Sends an outbound message payload to a targeted peer on a specific topic. + async fn send_message(&self, peer: &str, message: &ParsedMessage) -> Result<()> { + let encoded = self.encode(peer, message)?; + self.send_encoded(&encoded).await + } + + /// Starts the long-polling inbound event loops, driving messages to the provided handler closure. + async fn start_consumer(&self, handler: MessageHandler) -> Result<()>; + + /// Asynchronously monitors for fatal network client or sub-process failures. + async fn wait_for_exit(&self) -> std::io::Result<std::process::ExitStatus>; +} |
