coerce/remote/config/
mod.rs1use crate::actor::message::Message;
2use crate::actor::Actor;
3use crate::remote::actor::{BoxedActorHandler, BoxedMessageHandler};
4use crate::remote::cluster::node::NodeAttributesRef;
5use crate::remote::handler::{RemoteActorMarker, RemoteActorMessageMarker};
6use crate::remote::heartbeat::HeartbeatConfig;
7use crate::remote::net::security::ClientAuth;
8use std::any::TypeId;
9use std::collections::HashMap;
10
11#[derive(Serialize, Deserialize, Clone, Default)]
12pub struct SystemCapabilities {
13 pub actors: Vec<String>,
14 pub messages: Vec<String>,
15}
16
17pub struct RemoteSystemConfig {
18 node_tag: String,
19 node_version: String,
20 actor_types: HashMap<TypeId, String>,
21 handler_types: HashMap<TypeId, String>,
22 message_handlers: HashMap<String, BoxedMessageHandler>,
23 actor_handlers: HashMap<String, BoxedActorHandler>,
24 heartbeat_config: HeartbeatConfig,
25 node_attributes: NodeAttributesRef,
26 security: RemoteSystemSecurity,
27}
28
29#[derive(Default)]
30pub struct RemoteSystemSecurity {
31 client_auth: ClientAuth,
32}
33
34impl RemoteSystemConfig {
35 pub fn new(
36 node_tag: String,
37 node_version: String,
38 actor_types: HashMap<TypeId, String>,
39 handler_types: HashMap<TypeId, String>,
40 message_handlers: HashMap<String, BoxedMessageHandler>,
41 actor_handlers: HashMap<String, BoxedActorHandler>,
42 heartbeat_config: HeartbeatConfig,
43 node_attributes: NodeAttributesRef,
44 security: RemoteSystemSecurity,
45 ) -> RemoteSystemConfig {
46 RemoteSystemConfig {
47 node_tag,
48 node_version,
49 actor_types,
50 handler_types,
51 message_handlers,
52 actor_handlers,
53 heartbeat_config,
54 node_attributes,
55 security,
56 }
57 }
58
59 pub fn node_tag(&self) -> &str {
60 &self.node_tag
61 }
62
63 pub fn node_version(&self) -> &str {
64 &self.node_version
65 }
66
67 pub fn handler_name<A: Actor, M: Message>(&self) -> Option<String> {
68 let marker = RemoteActorMessageMarker::<A, M>::new();
69 self.handler_types.get(&marker.id()).cloned()
70 }
71
72 pub fn actor_name<A: Actor>(&self, marker: RemoteActorMarker<A>) -> Option<String>
73 where
74 A: 'static + Send + Sync,
75 {
76 self.actor_types.get(&marker.id()).map(|name| name.clone())
77 }
78
79 pub fn message_handler(&self, key: &str) -> Option<BoxedMessageHandler> {
80 self.message_handlers
81 .get(key)
82 .map(|handler| handler.new_boxed())
83 }
84
85 pub fn actor_handler(&self, key: &str) -> Option<BoxedActorHandler> {
86 self.actor_handlers
87 .get(key)
88 .map(|handler| handler.new_boxed())
89 }
90
91 pub fn heartbeat_config(&self) -> &HeartbeatConfig {
92 &self.heartbeat_config
93 }
94
95 pub fn get_capabilities(&self) -> SystemCapabilities {
96 let mut actors: Vec<String> = self.actor_types.values().map(|a| a.clone()).collect();
97 actors.sort_by(|a, b| a.to_lowercase().cmp(&b.to_lowercase()));
98
99 let mut messages: Vec<String> = self.handler_types.values().map(|a| a.clone()).collect();
100 messages.sort_by(|a, b| a.to_lowercase().cmp(&b.to_lowercase()));
101
102 SystemCapabilities { actors, messages }
103 }
104
105 pub fn get_attributes(&self) -> &NodeAttributesRef {
106 &self.node_attributes
107 }
108
109 pub fn security(&self) -> &RemoteSystemSecurity {
110 &self.security
111 }
112}
113
114impl RemoteSystemSecurity {
115 pub fn new(client_auth: ClientAuth) -> Self {
116 Self { client_auth }
117 }
118
119 pub fn client_authentication(&self) -> &ClientAuth {
120 &self.client_auth
121 }
122}