acton_core/actor/
managed_agent.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::fmt;
18use std::fmt::Debug;
19use std::fmt::Formatter;
20
21use acton_ern::prelude::*;
22use tokio::sync::mpsc::Receiver;
23use tokio_util::task::TaskTracker;
24
25pub use idle::Idle;
26
27use crate::common::{
28    AgentHandle, AsyncLifecycleHandler, BrokerRef, HaltSignal, ParentRef, ReactorMap,
29};
30use crate::message::Envelope;
31use crate::prelude::AgentRuntime;
32
33mod idle;
34pub mod started;
35
36/// A managed agent is a wrapper around an actor that provides a set of lifecycle hooks and
37///  message handling reactors.
38pub struct ManagedAgent<AgentState, ManagedAgent: Default + Send + Debug + 'static> {
39    pub(crate) handle: AgentHandle,
40
41    pub(crate) parent: Option<ParentRef>,
42
43    pub(crate) broker: BrokerRef,
44
45    pub(crate) halt_signal: HaltSignal,
46
47    pub(crate) id: Ern,
48    pub(crate) runtime: AgentRuntime,
49    /// The actor model.
50    pub model: ManagedAgent,
51
52    pub(crate) tracker: TaskTracker,
53
54    pub(crate) inbox: Receiver<Envelope>,
55    /// Reactor called when the actor wakes up but before listening begins.
56    pub(crate) before_start: AsyncLifecycleHandler<ManagedAgent>,
57    /// Reactor called when the actor wakes up but before listening begins.
58    pub(crate) after_start: AsyncLifecycleHandler<ManagedAgent>,
59    /// Reactor called just before the actor stops listening for messages.
60    pub(crate) before_stop: AsyncLifecycleHandler<ManagedAgent>,
61    /// Reactor called when the actor stops listening for messages.
62    pub(crate) after_stop: AsyncLifecycleHandler<ManagedAgent>,
63    /// Map of reactors for handling different message types.
64    pub(crate) reactors: ReactorMap<ManagedAgent>,
65    _actor_state: std::marker::PhantomData<AgentState>,
66}
67
68// implement getter functions for ManagedAgent
69impl<ActorState, ManagedEntity: Default + Send + Debug + 'static>
70    ManagedAgent<ActorState, ManagedEntity>
71{
72    /// Returns the unique identifier of the actor.
73    pub fn id(&self) -> &Ern {
74        &self.id
75    }
76    /// Returns the name of the actor.
77    pub fn name(&self) -> &str {
78        self.id.root.as_str()
79    }
80
81    /// Returns the handle of the actor.
82    pub fn handle(&self) -> &AgentHandle {
83        &self.handle
84    }
85
86    /// Returns the parent of the actor.
87    pub fn parent(&self) -> &Option<ParentRef> {
88        &self.parent
89    }
90    /// Returns the broker of the actor.
91    pub fn broker(&self) -> &BrokerRef {
92        &self.broker
93    }
94    /// Returns the app runtime.
95    pub fn runtime(&self) -> &AgentRuntime {
96        &self.runtime
97    }
98}
99
100impl<ActorState, ManagedEntity: Default + Send + Debug + 'static> Debug
101    for ManagedAgent<ActorState, ManagedEntity>
102{
103    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
104        f.debug_struct("ManagedActor")
105            .field("key", &self.id)
106            .finish()
107    }
108}