Skip to main content

coreon_core/
endpoint.rs

1//! Endpoint — addressable source/sink of exchanges.
2//!
3//! An Endpoint can produce (send a message to) or consume (receive messages
4//! from) an underlying transport. Components create Endpoints from URIs.
5
6use crate::{error::Result, exchange::Exchange, processor::Processor};
7use async_trait::async_trait;
8use std::sync::Arc;
9
10/// Sends an Exchange into the endpoint's underlying transport.
11#[async_trait]
12pub trait Producer: Send + Sync {
13    async fn send(&self, exchange: &mut Exchange) -> Result<()>;
14}
15
16/// Pulls exchanges off the underlying transport and feeds them to the
17/// registered Processor pipeline. Consumers are started/stopped by the
18/// route lifecycle.
19#[async_trait]
20pub trait Consumer: Send + Sync {
21    async fn start(&self) -> Result<()>;
22    async fn stop(&self) -> Result<()>;
23}
24
25/// An Endpoint is the bridge between URIs and Producer/Consumer pairs.
26#[async_trait]
27pub trait Endpoint: Send + Sync {
28    fn uri(&self) -> &str;
29    async fn create_producer(&self) -> Result<Arc<dyn Producer>>;
30    async fn create_consumer(&self, pipeline: Arc<dyn Processor>) -> Result<Arc<dyn Consumer>>;
31}