p2panda_net/iroh_endpoint/
supervisor.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3use ractor::ActorCell;
4use ractor::thread_local::{ThreadLocalActor, ThreadLocalActorSpawner};
5
6use crate::iroh_endpoint::actors::IrohEndpoint;
7use crate::iroh_endpoint::{Builder, Endpoint, EndpointError};
8use crate::supervisor::{ChildActor, ChildActorFut, Supervisor};
9
10impl Builder {
11    pub async fn spawn_linked(self, supervisor: &Supervisor) -> Result<Endpoint, EndpointError> {
12        let endpoint = Endpoint::new(None, self.build_args());
13        supervisor.start_child_actor(endpoint.clone()).await?;
14        Ok(endpoint)
15    }
16}
17
18impl ChildActor for Endpoint {
19    fn on_start(
20        &self,
21        supervisor: ActorCell,
22        thread_pool: ThreadLocalActorSpawner,
23    ) -> ChildActorFut<'_> {
24        Box::pin(async move {
25            // Spawn our actor as a child of the supervisor.
26            let (actor_ref, _) =
27                IrohEndpoint::spawn_linked(None, self.args.clone(), supervisor, thread_pool)
28                    .await?;
29
30            // Update the reference to our actor, we need this to send messages to it.
31            let mut inner = self.inner.write().await;
32            inner.actor_ref.replace(actor_ref.clone());
33
34            Ok(actor_ref.into())
35        })
36    }
37}