Skip to main content

iroh_live/
rooms_node.rs

1//! A node with rooms, set up in one call.
2
3use iroh::{Endpoint, protocol::Router};
4use iroh_live_node::{EndpointConfig, EndpointError, Node, layout::LiveLayout};
5use iroh_live_rooms::{Rooms, RoomsLayout};
6use tracing::warn;
7
8/// A node that serves both live broadcasts and rooms, with its router.
9///
10/// The node takes the union of [`LiveLayout`] and [`RoomsLayout`], so a
11/// member publishes under its room paths and under its own live paths. The
12/// router accepts the node's sessions and the rooms' gossip, so other
13/// members can reach this one.
14///
15/// # Examples
16///
17/// ```no_run
18/// use iroh_live::{EndpointConfig, RoomsNode, rooms::RoomTicket};
19///
20/// # async fn run() -> Result<(), iroh_live::Error> {
21/// let live = RoomsNode::bind(EndpointConfig::from_env()?).await?;
22/// let room = live.rooms.join(&RoomTicket::generate(), Some("ada".into())).await?;
23/// println!("room ticket: {}", room.ticket());
24/// // At exit.
25/// live.shutdown().await;
26/// # Ok(())
27/// # }
28/// ```
29#[derive(Debug, Clone)]
30#[non_exhaustive]
31pub struct RoomsNode {
32    /// The node, configured with the live and the rooms layout.
33    pub node: Node,
34    /// The room service on the node's endpoint.
35    pub rooms: Rooms,
36    /// The router that accepts the node's sessions and the rooms' gossip.
37    pub router: Router,
38}
39
40impl RoomsNode {
41    /// Binds an endpoint with `config` and serves live broadcasts and rooms on it.
42    ///
43    /// # Errors
44    ///
45    /// Fails if the endpoint cannot be bound.
46    pub async fn bind(config: EndpointConfig) -> Result<Self, EndpointError> {
47        Ok(Self::new(config.bind().await?))
48    }
49
50    /// Serves live broadcasts and rooms on an endpoint bound already.
51    ///
52    /// Takes over the endpoint: [`shutdown`](Self::shutdown) closes it.
53    ///
54    /// # Panics
55    ///
56    /// Panics when called outside a Tokio runtime.
57    #[must_use]
58    pub fn new(endpoint: Endpoint) -> Self {
59        let node = Node::new(
60            endpoint.clone(),
61            iroh_live_node::layout::config((LiveLayout::default(), RoomsLayout)),
62        );
63        let rooms = Rooms::new(&node);
64        let router = rooms.mount(node.mount(Router::builder(endpoint))).spawn();
65        Self {
66            node,
67            rooms,
68            router,
69        }
70    }
71
72    /// Leaves every joined room, then shuts the node and the router down.
73    ///
74    /// Leaving tells the other members at once, see [`Rooms::leave_all`].
75    /// Only rooms that a handle still holds are left, since a room stops
76    /// with its last handle. The router's shutdown stops the node and the
77    /// rooms, then closes the endpoint. Each step is bounded: leaving takes
78    /// seconds at most even with every other member gone, and closing the
79    /// endpoint waits for each peer to acknowledge the close, for up to a
80    /// few seconds for a peer that vanished.
81    ///
82    /// Not cancellation safe. A shutdown cut short, such as by a timeout
83    /// around it, can leave the endpoint dropped without being closed, so
84    /// let it finish.
85    pub async fn shutdown(&self) {
86        self.rooms.leave_all().await;
87        if let Err(err) = self.router.shutdown().await {
88            warn!(error = %err, "the router failed to shut down");
89        }
90    }
91}