acton_core/traits/actor.rs
1/*
2 * Copyright (c) 2024. Govcraft
3 *
4 * Licensed under either of
5 * * Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8 * * MIT license: http://opensource.org/licenses/MIT
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the applicable License for the specific language governing permissions and
14 * limitations under that License.
15 */
16
17use std::future::Future;
18
19use acton_ern::{Ern};
20use async_trait::async_trait;
21use dashmap::DashMap;
22use tokio_util::task::TaskTracker;
23use tracing::*;
24
25use crate::common::*;
26use crate::message::{BrokerRequest, MessageAddress};
27use crate::traits::acton_message::ActonMessage;
28
29/// Trait for actor context, defining common methods for actor management.
30#[async_trait]
31pub trait Actor {
32 /// Returns the message address for this agent.
33 fn reply_address(&self) -> MessageAddress;
34 /// Returns an envelope for the specified recipient and message, ready to send.
35 fn create_envelope(&self, recipient_address: Option<MessageAddress>) -> OutboundEnvelope;
36 /// Returns a map of the actor's children.
37 fn children(&self) -> DashMap<String, AgentHandle>;
38
39 /// Finds a child actor by its ERN.
40 ///
41 /// # Arguments
42 ///
43 /// * `arn` - The ERN of the child actor to find.
44 ///
45 /// # Returns
46 ///
47 /// An `Option<ActorRef>` containing the child actor if found, or `None` if not found.
48 fn find_child(&self, arn: &Ern) -> Option<AgentHandle>;
49
50 /// Returns the actor's task tracker.
51 fn tracker(&self) -> TaskTracker;
52
53 /// Returns the actor's ERN.
54 fn id(&self) -> Ern;
55
56 /// Returns the actor's root from the ERN.
57 fn name(&self) -> String;
58
59 /// Creates a clone of the actor's reference.
60 fn clone_ref(&self) -> AgentHandle;
61
62 /// Emits a message from the actor, possibly to a pool item.
63 ///
64 /// # Arguments
65 ///
66 /// * `message` - The message to emit, implementing `ActonMessage`.
67 ///
68 /// # Returns
69 ///
70 /// A `Future` that resolves when the message has been emitted.
71 #[instrument(skip(self), fields(children = self.children().len()))]
72 fn send(
73 &self,
74 message: impl ActonMessage,
75 ) -> impl Future<Output=()> + Send + Sync + '_
76 where
77 Self: Sync,
78 {
79 async move {
80 let envelope = self.create_envelope(None);
81 trace!("Envelope sender is {:?}", envelope.return_address.sender.root.to_string());
82 envelope.send(message).await;
83 }
84 }
85 /// Send a message synchronously.
86 fn send_sync(&self, message: impl ActonMessage, recipient: &AgentHandle) -> anyhow::Result<()>
87 where
88 Self: Actor,
89 {
90 let envelope = self.create_envelope(Some(recipient.reply_address()));
91 envelope.reply(BrokerRequest::new(message))?;
92 Ok(())
93 }
94
95 /// Suspends the actor.
96 fn stop(&self) -> impl Future<Output=anyhow::Result<()>> + Send + Sync + '_;
97}