1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//! Actix-Telepathy is an extension to [Actix](https://docs.rs/actix) that enables remote messaging and clustering support.
//!
//! Telepathy does not change Actix' messaging system but _extends_ the
//!
//! - [actix::Actor](https://docs.rs/actix/latest/actix/trait.Actor.html) with the [RemoteActor](./trait.RemoteActor.html) trait and the
//! - [actix::Message](https://docs.rs/actix/latest/actix/trait.Message.html) with the [RemoteMessage](./trait.RemoteMessage.html) trait.
//!
//! Hence, an example actor receiving a remote message is defined as follows.
//! To connect multiple computers in a cluster, a [Cluster](./struct.Cluster.html) must be generated.
//!
//! ```rust
//! use actix::prelude::*;
//! use actix_broker::BrokerSubscribe;
//! use actix_telepathy::prelude::*; // <-- Telepathy extension
//! use serde::{Serialize, Deserialize};
//! use std::net::SocketAddr;
//!
//! #[derive(RemoteMessage, Serialize, Deserialize)] // <-- Telepathy extension
//! struct MyMessage {}
//!
//! #[derive(RemoteActor)] // <-- Telepathy extension
//! #[remote_messages(MyMessage)] // <-- Telepathy extension
//! struct MyActor {
//! state: usize
//! }
//!
//! impl Actor for MyActor {
//! type Context = Context<Self>;
//!
//! fn started(&mut self, ctx: &mut Self::Context) {
//! self.register(ctx.address().recipient()); // <-- Telepathy extension
//! }
//! }
//!
//! impl Handler<MyMessage> for MyActor {
//! type Result = ();
//!
//! fn handle(&mut self, msg: MyMessage, ctx: &mut Self::Context) -> Self::Result {
//! todo!()
//! }
//! }
//!
//! #[actix_rt::main]
//! pub async fn start_cluster(own_addr: SocketAddr, seed_nodes: Vec<SocketAddr>) {
//! let _addr = MyActor { state: 0 }.start();
//! let _cluster = Cluster::new(own_addr, seed_nodes);
//! tokio::time::sleep(std::time::Duration::from_secs(5)).await;
//! }
//!
//! ```
//!
//! The previous example will not do anything. However, the cluster will try to connect to the given addresses in `seed_nodes`.
//! To react to new joining members, a [ClusterListener](./trait.ClusterListener.html) actor should be used:
//!
//! ```rust
//! use actix::prelude::*;
//! use actix_broker::BrokerSubscribe;
//! use actix_telepathy::prelude::*; // <-- Telepathy extension
//! use serde::{Serialize, Deserialize};
//! use std::net::SocketAddr;
//!
//! #[derive(RemoteMessage, Serialize, Deserialize)] // <-- Telepathy extension
//! struct MyMessage {}
//!
//! #[derive(RemoteActor)] // <-- Telepathy extension
//! #[remote_messages(MyMessage)] // <-- Telepathy extension
//! struct MyActor {
//! state: usize
//! }
//!
//! impl Actor for MyActor {
//! type Context = Context<Self>;
//!
//! fn started(&mut self, ctx: &mut Self::Context) {
//! self.register(ctx.address().recipient()); // <-- Telepathy extension
//! self.subscribe_system_async::<ClusterLog>(ctx); // <-- Telepathy extension
//! }
//! }
//!
//! impl Handler<MyMessage> for MyActor {
//! type Result = ();
//!
//! fn handle(&mut self, msg: MyMessage, ctx: &mut Self::Context) -> Self::Result {
//! todo!()
//! }
//! }
//!
//! impl Handler<ClusterLog> for MyActor { // <-- Telepathy extension
//! type Result = ();
//!
//! fn handle(&mut self, msg: ClusterLog, ctx: &mut Self::Context) -> Self::Result {
//! match msg {
//! ClusterLog::NewMember(_node) => {
//! println!("New member joined the cluster.")
//! },
//! ClusterLog::MemberLeft(_ip_addr) => {
//! println!("Member left the cluster.")
//! }
//! }
//! }
//! }
//! impl ClusterListener for MyActor {} // <-- Telepathy extension
//!
//! #[actix_rt::main]
//! pub async fn start_cluster(own_addr: SocketAddr, seed_nodes: Vec<SocketAddr>) {
//! let _addr = MyActor { state: 0 }.start();
//! let _cluster = Cluster::new(own_addr, seed_nodes); // <-- Telepathy extension
//! tokio::time::sleep(std::time::Duration::from_secs(5)).await;
//! }
//!
//! ```
//!
//! Now, we receive a printed message whenever a new member joins the cluster or when a member leaves.
//! To send messages between remote actors to other members in the cluster, we have to utilize the
//! [RemoteAddr](./struct.RemoteAddr.html) that the [ClusterListener](./trait.ClusterListener.html) receives.
//!
//! ```rust
//! use actix::prelude::*;
//! use actix_broker::BrokerSubscribe;
//! use actix_telepathy::prelude::*; // <-- Telepathy extension
//! use serde::{Serialize, Deserialize};
//! use std::net::SocketAddr;
//!
//! #[derive(RemoteMessage, Serialize, Deserialize)] // <-- Telepathy extension
//! struct MyMessage {}
//!
//! #[derive(RemoteActor)] // <-- Telepathy extension
//! #[remote_messages(MyMessage)] // <-- Telepathy extension
//! struct MyActor {
//! state: usize
//! }
//!
//! impl Actor for MyActor {
//! type Context = Context<Self>;
//!
//! fn started(&mut self, ctx: &mut Self::Context) {
//! self.register(ctx.address().recipient()); // <-- Telepathy extension
//! self.subscribe_system_async::<ClusterLog>(ctx); // <-- Telepathy extension
//! }
//! }
//!
//! impl Handler<MyMessage> for MyActor {
//! type Result = ();
//!
//! fn handle(&mut self, msg: MyMessage, ctx: &mut Self::Context) -> Self::Result {
//! println!("RemoteMessage received!")
//! }
//! }
//!
//! impl Handler<ClusterLog> for MyActor { // <-- Telepathy extension
//! type Result = ();
//!
//! fn handle(&mut self, msg: ClusterLog, ctx: &mut Self::Context) -> Self::Result {
//! match msg {
//! ClusterLog::NewMember(node) => {
//! println!("New member joined the cluster.");
//! let remote_addr = node.get_remote_addr(Self::ACTOR_ID.to_string());
//! remote_addr.do_send(MyMessage {})
//! },
//! ClusterLog::MemberLeft(_ip_addr) => {
//! println!("Member left the cluster.")
//! }
//! }
//! }
//! }
//! impl ClusterListener for MyActor {} // <-- Telepathy extension
//!
//! #[actix_rt::main]
//! pub async fn start_cluster(own_addr: SocketAddr, seed_nodes: Vec<SocketAddr>) {
//! let _addr = MyActor { state: 0 }.start();
//! let _cluster = Cluster::new(own_addr, seed_nodes); // <-- Telepathy extension
//! tokio::time::sleep(std::time::Duration::from_secs(5)).await;
//! }
//!
//! ```
//!
//! Now, every new member receives a `MyMessage` from every [ClusterListener](./trait.ClusterListener.html) in the cluster.
//!
//! Before we could use the [RemoteAddr](./struct.RemoteAddr.html), we had to make sure, that it is pointing to the correct [RemoteActor](./trait.RemoteActor.html), which is `MyActor` in that case.
//! Therefore, we had to call `get_remote_addr` on the [Node](./struct.Node.html). A [RemoteAddr](./struct.RemoteAddr.html) points to a specific actor on a remote machine.
pub use *;
pub
pub use crate*;
pub use crateClusterMessage;
pub use crate*;
pub use crate*;
pub use crate*;
pub use crate*;