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
//! Aldrin broker library
//!
//! This library implements the messaging broker according to the Aldrin specification. An Aldrin
//! broker sits at the center of every Aldrin bus and manages the state of all objects and services.
//!
//! The central [`Broker`] is completely transport-agnostic, in the sense that it doesn't know or
//! deal with the low-level details of how a client is connected. E.g. whether TCP/IP with JSON
//! serialization is used or some other means. These details are encapsulated in [`Connection`],
//! which in turn communicates with the `Broker` through internal channels.
//!
//! Furthermore, this crate does not depend on any async runtime, such as e.g. Tokio. Neither the
//! `Broker` nor `Connection` need to spawn additional tasks, nor perform any I/O on their
//! own. Users of this crate have full control over what runtime to use (if any at all) and how to
//! arrange the various parts into tasks.
//!
//! # Examples
//!
//! A typical use-case is to have a stand-alone broker application, which simply listens for new
//! connections in an infinite loop. In this example, we'll be using Tokio and TCP/IP connections.
//!
//! ```
//! use aldrin_broker::Broker;
//! use aldrin_broker::core::tokio::TokioTransport;
//! use anyhow::Result;
//! use std::net::Ipv4Addr;
//! use tokio::net::TcpListener;
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
//!     // Create and spawn the broker:
//!     let broker = Broker::new();
//!     let handle = broker.handle().clone();
//!     tokio::spawn(broker.run());
//!
//!     // Create a TcpListener to listen for new connections:
//!     let mut listener = TcpListener::bind((Ipv4Addr::new(127, 0, 0, 1), 24387)).await?;
//!
//!     loop {
//!         # return Ok(());
//!         let (socket, _) = listener.accept().await?;
//!
//!         // Create an AsyncTransport out of the socket:
//!         let transport = TokioTransport::new(socket);
//!
//!         // Add the connection and run it:
//!         let connection = handle.connect(transport).await?;
//!         tokio::spawn(connection.run());
//!     }
//! }
//! ```

#![deny(missing_debug_implementations)]
#![deny(missing_docs)]
#![deny(rustdoc::broken_intra_doc_links)]

mod broker;
mod bus_listener;
mod conn;
mod conn_id;
mod serial_map;

pub use aldrin_core as core;
#[cfg(feature = "statistics")]
pub use broker::BrokerStatistics;
pub use broker::{Broker, BrokerHandle, BrokerShutdown, PendingConnection};
pub use conn::{Connection, ConnectionError, ConnectionHandle, EstablishError};