kompact/messaging/
registration.rs

1use super::*;
2use crate::routing::groups::StorePolicy;
3
4/// An error that can occur during [actor path](ActorPath) registration
5#[derive(Debug, PartialEq, Eq, Clone)]
6pub enum RegistrationError {
7    /// An actor path with the same name exists already
8    DuplicateEntry,
9    /// This kind of registration is unsupported by the system's dispatcher implementation
10    Unsupported,
11    /// The supplied path was not invalid
12    InvalidPath(PathParseError),
13}
14
15/// Convenience alias for the result of a path registration attempt
16pub type RegistrationResult = Result<ActorPath, RegistrationError>;
17
18/// A holder for different variants of how feedback for a registration can be provided
19#[derive(Debug)]
20pub enum RegistrationPromise {
21    /// Provide feedback via fulfilling the promise
22    Fulfil(utils::KPromise<RegistrationResult>),
23    /// Do not provide feedback
24    None,
25}
26
27/// An actor registration event
28///
29/// This is used for registering an [ActorRef](crate::prelude::ActorRef)
30/// with an [ActorPath](crate::prelude::ActorPath) on a dispatcher.
31#[derive(Debug)]
32pub struct ActorRegistration {
33    /// A network-only reference to the registered actor
34    pub actor: DynActorRef,
35    /// The path we want to register
36    pub path: PathResolvable,
37}
38
39/// A routing policy registration event
40///
41/// This is used for registering an [RoutingPolicy](crate::routing::groups::RoutingPolicy)
42/// (wrapped into a [StorePolicy](crate::routing::groups::RoutingPolicy) for abstraction)
43/// with a named [ActorPath](crate::prelude::ActorPath) on a dispatcher.
44#[derive(Debug)]
45pub struct PolicyRegistration {
46    /// A network-only reference to the registered actor
47    pub policy: StorePolicy,
48    /// The path we want to register
49    pub path: Vec<String>,
50}
51
52/// One of the two registration event types
53#[derive(Debug)]
54pub enum RegistrationEvent {
55    /// An actor registration event
56    Actor(ActorRegistration),
57    /// A routing policy registration event
58    Policy(PolicyRegistration),
59}
60impl From<ActorRegistration> for RegistrationEvent {
61    fn from(r: ActorRegistration) -> Self {
62        RegistrationEvent::Actor(r)
63    }
64}
65impl From<PolicyRegistration> for RegistrationEvent {
66    fn from(r: PolicyRegistration) -> Self {
67        RegistrationEvent::Policy(r)
68    }
69}
70
71/// Envelope representing some registration event
72///
73/// Supported registration events are:
74/// 1. Binding an actor reference to an actor path.
75/// 2. Binding a routing policy to an named path.
76#[derive(Debug)]
77pub struct RegistrationEnvelope {
78    /// The actual registration event
79    pub event: RegistrationEvent,
80    /// Allow existing registrations to be replaced by this registration
81    ///
82    /// If `false`, attempting to register an existing path will result in an error.
83    pub update: bool,
84    /// An optional feedback promise, which returns the newly registered actor path or an error
85    pub promise: RegistrationPromise,
86}
87
88impl RegistrationEnvelope {
89    /// Create an actor registration envelope without a promise for feedback
90    pub fn actor(actor: DynActorRef, path: PathResolvable, update: bool) -> Self {
91        let event = ActorRegistration { actor, path };
92        RegistrationEnvelope {
93            event: event.into(),
94            update,
95            promise: RegistrationPromise::None,
96        }
97    }
98
99    /// Create an actor registration envelope using a promise for feedback
100    pub fn actor_with_promise(
101        actor: DynActorRef,
102        path: PathResolvable,
103        update: bool,
104        promise: utils::KPromise<RegistrationResult>,
105    ) -> Self {
106        let event = ActorRegistration { actor, path };
107        RegistrationEnvelope {
108            event: event.into(),
109            update,
110            promise: RegistrationPromise::Fulfil(promise),
111        }
112    }
113
114    /// Create a policy registration envelope without a promise for feedback
115    pub fn policy<P>(policy: P, path: Vec<String>, update: bool) -> Self
116    where
117        P: Into<StorePolicy>,
118    {
119        let event = PolicyRegistration {
120            policy: policy.into(),
121            path,
122        };
123        RegistrationEnvelope {
124            event: event.into(),
125            update,
126            promise: RegistrationPromise::None,
127        }
128    }
129
130    /// Create a policy registration envelope without a promise for feedback
131    pub fn policy_with_promise<P>(
132        policy: P,
133        path: Vec<String>,
134        update: bool,
135        promise: utils::KPromise<RegistrationResult>,
136    ) -> Self
137    where
138        P: Into<StorePolicy>,
139    {
140        let event = PolicyRegistration {
141            policy: policy.into(),
142            path,
143        };
144        RegistrationEnvelope {
145            event: event.into(),
146            update,
147            promise: RegistrationPromise::Fulfil(promise),
148        }
149    }
150}