kompact/messaging/
mod.rs

1//! Messaging types for sending and receiving messages between remote actors.
2use crate::{
3    actors::{ActorPath, DynActorRef, MessageBounds, PathParseError},
4    net::{
5        buffers::{BufferChunk, BufferEncoder, ChunkLease, ChunkRef},
6        frames::FRAME_HEAD_LEN,
7    },
8    serialisation::{
9        ser_helpers::{deserialise_chunk_lease, deserialise_chunk_ref},
10        Deserialiser,
11        SerError,
12        SerId,
13        Serialisable,
14        Serialiser,
15        TryClone,
16    },
17    utils,
18};
19use bytes::{Buf, Bytes};
20use std::{any::Any, convert::TryFrom, ops::Deref, str::FromStr};
21use uuid::Uuid;
22mod net_message;
23pub use net_message::*;
24mod registration;
25pub use registration::*;
26mod serialised;
27pub use serialised::*;
28pub(crate) mod dispatch;
29pub use dispatch::*;
30mod deser_macro;
31use crate::{net::SocketAddr, prelude::NetworkStatus};
32#[allow(unused_imports)]
33pub use deser_macro::*;
34pub mod bitfields;
35pub mod framing;
36
37/// An event from the network
38///
39/// This is left as an enum for future extension.
40#[derive(Debug)]
41pub enum EventEnvelope {
42    /// An event from the network
43    Network(NetworkStatus),
44    /// Rejected DispatchData, NetworkThread is unable to send it
45    RejectedData((SocketAddr, Box<DispatchData>)),
46}
47
48/// A message that is accepted by an actor's mailbox
49#[derive(Debug)]
50#[allow(clippy::large_enum_variant)]
51pub enum MsgEnvelope<M: MessageBounds> {
52    /// A message of the actor's `Message` type
53    Typed(M),
54    /// A message from the network
55    Net(NetMessage),
56}
57
58/// Something that can resolved to some kind of path by the dispatcher
59#[derive(Debug, Clone)]
60pub enum PathResolvable {
61    /// An actual actor path
62    Path(ActorPath),
63    /// The unique id of an actor
64    ///
65    /// Can be resolved to a [unique path](ActorPath::Unique) in the current system.
66    ActorId(Uuid),
67    /// An actor path alias with all segments merged
68    ///
69    /// Can be resolved to a [named path](ActorPath::Named) in the current system.
70    Alias(String),
71    /// An actor path alias with all segments individually
72    ///
73    /// Can be resolved to a [named path](ActorPath::Named) in the current system.
74    Segments(Vec<String>),
75    /// The system path (as provided by the dispatcher)
76    System,
77}
78
79impl From<ActorPath> for PathResolvable {
80    fn from(path: ActorPath) -> Self {
81        PathResolvable::Path(path)
82    }
83}
84impl TryFrom<String> for PathResolvable {
85    type Error = PathParseError;
86
87    fn try_from(value: String) -> Result<Self, Self::Error> {
88        let parsed = crate::actors::parse_path(&value);
89        crate::actors::validate_lookup_path(&parsed).map(|_| PathResolvable::Alias(value))
90    }
91}
92impl FromStr for PathResolvable {
93    type Err = PathParseError;
94
95    fn from_str(value: &str) -> Result<Self, Self::Err> {
96        let parsed = crate::actors::parse_path(value);
97        crate::actors::validate_lookup_path(&parsed)
98            .map(|_| PathResolvable::Alias(value.to_string()))
99    }
100}