├── .gitignore ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── examples └── basics │ ├── Cargo.toml │ └── src │ ├── main.rs │ └── msgs.rs └── src ├── lib.rs ├── msgs.rs ├── node.rs ├── protocol.rs ├── recipient.rs ├── remote.rs ├── utils.rs ├── worker.rs └── world.rs /.gitignore: -------------------------------------------------------------------------------- 1 | Cargo.lock 2 | /gh-pages 3 | __pycache__ 4 | 5 | *.so 6 | *.out 7 | *.pyc 8 | *.pid 9 | *.sock 10 | *~ 11 | target/ 12 | examples/chat/target/ 13 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "actix-remote" 3 | version = "0.0.1" 4 | authors = ["Nikolay Kim "] 5 | description = "Support for distributed actors for actix framework" 6 | license = "MIT/Apache-2.0" 7 | readme = "README.md" 8 | keywords = ["web", "async", "actix", "tokio"] 9 | homepage = "https://github.com/actix/actix-remote" 10 | repository = "https://github.com/actix/actix-remote.git" 11 | documentation = "https://docs.rs/actix-remote/" 12 | categories = ["network-programming", "asynchronous"] 13 | exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"] 14 | 15 | [lib] 16 | name = "actix_remote" 17 | path = "src/lib.rs" 18 | 19 | [badges] 20 | travis-ci = { repository = "actix/actix-remote", branch = "master" } 21 | codecov = { repository = "actix/actix-remote", branch = "master", service = "github" } 22 | 23 | [dependencies] 24 | actix = "0.5" 25 | 26 | log = "0.4" 27 | net2 = "0.2" 28 | backoff = "0.1" 29 | byteorder = "1" 30 | bytes = "0.4" 31 | failure = "^0.1.1" 32 | futures = "0.1" 33 | tokio-io = "0.1" 34 | tokio-core = "0.1" 35 | 36 | serde = "1.0" 37 | serde_json = "1.0" 38 | serde_derive = "1.0" 39 | 40 | [workspace] 41 | members = [ 42 | "./", 43 | "examples/basics", 44 | ] 45 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2017-NOW Nikolay Kim 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017 Nikilay Kim 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [WIP] Actix remote [![Build Status](https://travis-ci.org/actix/actix-remote.svg?branch=master)](https://travis-ci.org/actix/actix-remote) [![codecov](https://codecov.io/gh/actix/actix-remote/branch/master/graph/badge.svg)](https://codecov.io/gh/actix/actix-remote) [![crates.io](http://meritbadge.herokuapp.com/actix-remote)](https://crates.io/crates/actix-remote) 2 | 3 | Distributed actors for actix framework. 4 | 5 | ## Documentation 6 | 7 | * [API Documentation (Development)](http://actix.github.io/actix-remote/actix_remote/) 8 | * [API Documentation (Releases)](https://docs.rs/actix-remote/) 9 | * [Chat on gitter](https://gitter.im/actix/actix) 10 | * Cargo package: [actix-remote](https://crates.io/crates/actix-remote) 11 | * Minimum supported Rust version: 1.21 or later 12 | 13 | 14 | ## License 15 | 16 | This project is licensed under either of 17 | 18 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)) 19 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or [http://opensource.org/licenses/MIT](http://opensource.org/licenses/MIT)) 20 | 21 | at your option. 22 | 23 | ## Code of Conduct 24 | 25 | Contribution to the actix-redis crate is organized under the terms of the 26 | Contributor Covenant, the maintainer of actix-remote, @fafhrd91, promises to 27 | intervene to uphold that code of conduct. 28 | -------------------------------------------------------------------------------- /examples/basics/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "basics" 3 | version = "0.1.0" 4 | authors = ["Nikolay Kim "] 5 | workspace = "../.." 6 | 7 | [dependencies] 8 | futures = "0.1" 9 | log = "0.4" 10 | env_logger = "0.5" 11 | 12 | serde = "1.0" 13 | serde_json = "1.0" 14 | serde_derive = "1.0" 15 | 16 | # cli 17 | structopt = "0.2" 18 | structopt-derive = "0.2" 19 | 20 | actix = "0.5" 21 | actix-remote = { path="../.." } 22 | -------------------------------------------------------------------------------- /examples/basics/src/main.rs: -------------------------------------------------------------------------------- 1 | //! Start two `basic` instances 2 | //! 1. cargo run --example basic -- 127.0.0.1:7654 3 | //! 2. ./target/debug/examples/basic 127.0.0.1:7655 127.0.0.1:7654 4 | //! 5 | //! first instance sends messages, second instance respondes to messages from first instance 6 | //! 7 | #![allow(dead_code, unused_variables)] 8 | 9 | extern crate log; 10 | extern crate env_logger; 11 | extern crate futures; 12 | #[macro_use] extern crate actix; 13 | extern crate actix_remote; 14 | extern crate serde_json; 15 | #[macro_use] extern crate serde_derive; 16 | extern crate structopt; 17 | #[macro_use] extern crate structopt_derive; 18 | 19 | use std::time::Duration; 20 | 21 | use actix_remote::*; 22 | use actix::prelude::*; 23 | use futures::Future; 24 | use structopt::StructOpt; 25 | 26 | mod msgs; 27 | use msgs::TestMessage; 28 | 29 | 30 | struct MyActor { 31 | cnt: usize, 32 | hb: bool, 33 | recipient: Recipient, 34 | } 35 | 36 | impl MyActor { 37 | fn hb(&self, ctx: &mut Context) { 38 | self.recipient.send(TestMessage{msg: "TEST".to_owned()}) 39 | .and_then(|r| { 40 | println!("REMOTE RESULT: {:?}", r); 41 | Ok(()) 42 | }) 43 | .map_err(|_| ()) 44 | .into_actor(self) 45 | .spawn(ctx); 46 | 47 | ctx.run_later(Duration::from_secs(3), |act, ctx| act.hb(ctx)); 48 | } 49 | } 50 | 51 | impl Actor for MyActor { 52 | type Context = Context; 53 | 54 | fn started(&mut self, ctx: &mut Context) { 55 | if self.hb { 56 | self.hb(ctx); 57 | } 58 | } 59 | } 60 | 61 | impl Handler for MyActor { 62 | type Result = (); 63 | 64 | fn handle(&mut self, msg: TestMessage, _ctx: &mut Context) { 65 | println!("REMOTE MESSAGE: {:?}", msg); 66 | } 67 | } 68 | 69 | #[derive(StructOpt, Debug)] 70 | struct Cli { 71 | /// Network address 72 | addr: String, 73 | /// Network node address 74 | node: Option, 75 | } 76 | 77 | fn main() { 78 | ::std::env::set_var("RUST_LOG", "actix_remote=debug"); 79 | let _ = env_logger::init(); 80 | 81 | // cmd arguments 82 | let args = Cli::from_args(); 83 | let addr = args.addr.to_lowercase().trim().to_owned(); 84 | let node = args.node.map(|n| n.to_lowercase().trim().to_owned()); 85 | 86 | let sys = actix::System::new("remote-example"); 87 | 88 | // send messages from main instance 89 | let hb = node.is_none(); 90 | 91 | // create world 92 | let mut world = World::new(addr).unwrap().add_node(node); 93 | 94 | // get remote recipient 95 | let recipient = world.get_recipient::(); 96 | 97 | let addr = world.start(); 98 | let a: Addr = MyActor::create(move |ctx| { 99 | ctx.run_later(Duration::from_millis(5000), move |_, ctx| { 100 | // register actor as recipient for `TestMessage` message 101 | World::register_recipient( 102 | &addr, ctx.address::>().recipient()); 103 | }); 104 | 105 | MyActor{cnt: 0, hb, recipient} 106 | }); 107 | 108 | let _ = sys.run(); 109 | } 110 | -------------------------------------------------------------------------------- /examples/basics/src/msgs.rs: -------------------------------------------------------------------------------- 1 | use actix_remote::*; 2 | 3 | 4 | #[derive(Debug, Message, Serialize, Deserialize)] 5 | pub struct TestMessage { 6 | pub msg: String, 7 | } 8 | 9 | impl RemoteMessage for TestMessage { 10 | fn type_id() -> &'static str { 11 | "TestMessage" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] extern crate actix; 2 | extern crate backoff; 3 | extern crate bytes; 4 | extern crate byteorder; 5 | extern crate serde; 6 | extern crate serde_json; 7 | #[macro_use] extern crate serde_derive; 8 | extern crate net2; 9 | #[macro_use] extern crate log; 10 | extern crate futures; 11 | extern crate tokio_core; 12 | extern crate tokio_io; 13 | 14 | mod msgs; 15 | mod node; 16 | mod world; 17 | mod protocol; 18 | mod remote; 19 | mod recipient; 20 | mod worker; 21 | mod utils; 22 | 23 | pub use world::World; 24 | pub use remote::{Remote, RemoteMessage}; 25 | -------------------------------------------------------------------------------- /src/msgs.rs: -------------------------------------------------------------------------------- 1 | #![allow(dead_code)] 2 | 3 | use std::{net, io}; 4 | use std::sync::Arc; 5 | use serde::Serialize; 6 | use serde::de::DeserializeOwned; 7 | use futures::sync::mpsc::Receiver; 8 | use futures::unsync::oneshot::Sender; 9 | 10 | use actix::{Actor, Addr, Handler, Message, Unsync}; 11 | 12 | use node::NetworkNode; 13 | use remote::RemoteMessage; 14 | use recipient::RemoteMessageHandler; 15 | 16 | #[derive(Message)] 17 | pub(crate) struct RegisterNode { 18 | pub addr: net::SocketAddr, 19 | } 20 | 21 | #[derive(Message)] 22 | pub(crate) struct ReconnectNode; 23 | 24 | #[derive(Message)] 25 | pub(crate) struct NodeConnected(pub String); 26 | 27 | /// NetworkNode notifies world. 28 | /// New remote recipient is available. 29 | #[derive(Message, Clone)] 30 | pub(crate) struct NodeSupportedTypes { 31 | pub node: String, 32 | pub types: Vec, 33 | } 34 | 35 | #[derive(Message)] 36 | pub(crate) struct WorkerDisconnected(pub usize); 37 | 38 | /// Register new recipient provider 39 | #[derive(Message, Clone)] 40 | pub struct ProvideRecipient{ 41 | pub type_id: &'static str, 42 | pub handler: Arc} 43 | 44 | #[derive(Message)] 45 | pub(crate) struct GetRecipient 46 | where M: RemoteMessage + 'static, 47 | M::Result: Send + Serialize + DeserializeOwned 48 | { 49 | pub rx: Receiver, 50 | } 51 | 52 | #[derive(Message)] 53 | pub(crate) struct NodeGone(pub String); 54 | 55 | /// World sends this message to RecipientProxy. 56 | /// Notifies about new node with support of specific type_id. 57 | #[derive(Message)] 58 | pub(crate) struct TypeSupported { 59 | pub type_id: String, 60 | pub node_id: String, 61 | pub node: Addr } 62 | 63 | pub(crate) trait NodeOperations: Actor + Handler + Handler {} 64 | 65 | 66 | pub(crate) struct SendRemoteMessage{ 67 | pub type_id: String, 68 | pub data: String, 69 | pub tx: Sender, 70 | } 71 | 72 | impl Message for SendRemoteMessage { 73 | type Result = Result; 74 | } 75 | 76 | //=================================== 77 | // Worker messages 78 | //=================================== 79 | 80 | /// Stop worker 81 | #[derive(Message)] 82 | pub(crate) struct StopWorker; 83 | -------------------------------------------------------------------------------- /src/node.rs: -------------------------------------------------------------------------------- 1 | use std::io; 2 | use std::cell::Cell; 3 | use std::sync::Arc; 4 | use std::collections::HashMap; 5 | use backoff::ExponentialBackoff; 6 | use backoff::backoff::Backoff; 7 | use futures::unsync::oneshot; 8 | use tokio_core::net::TcpStream; 9 | use tokio_io::AsyncRead; 10 | use tokio_io::io::WriteHalf; 11 | use tokio_io::codec::FramedRead; 12 | use actix::prelude::*; 13 | use actix::prelude::{Response as ActixResponse}; 14 | 15 | use msgs; 16 | use world::World; 17 | use protocol::{Request, Response, NetworkClientCodec}; 18 | 19 | 20 | #[derive(Clone, Copy, PartialEq, Debug)] 21 | pub enum NodeStatus { 22 | New, 23 | Ok, 24 | Connecting, 25 | Failed, 26 | } 27 | 28 | pub struct NodeInformation { 29 | inner: Arc, 30 | } 31 | 32 | impl NodeInformation { 33 | pub fn new(addr: String) -> NodeInformation { 34 | NodeInformation{inner: Arc::new( 35 | Inner{addr: addr, 36 | status: Cell::new(NodeStatus::New)} 37 | )} 38 | } 39 | 40 | pub fn address(&self) -> &str { 41 | self.inner.as_ref().addr.as_str() 42 | } 43 | 44 | pub fn status(&self) -> NodeStatus { 45 | self.inner.as_ref().status.get() 46 | } 47 | 48 | pub fn set_status(&self, status: NodeStatus) { 49 | self.inner.as_ref().status.set(status) 50 | } 51 | } 52 | 53 | impl Clone for NodeInformation { 54 | fn clone(&self) -> Self { 55 | NodeInformation {inner: Arc::clone(&self.inner)} 56 | } 57 | } 58 | 59 | struct Inner { 60 | addr: String, 61 | status: Cell, 62 | } 63 | 64 | /// NetworkNode - Actor responsible for network node 65 | pub struct NetworkNode { 66 | mid: u64, 67 | world: Addr, 68 | addr: String, 69 | inner: NodeInformation, 70 | backoff: ExponentialBackoff, 71 | framed: Option, NetworkClientCodec>>, 72 | requests: HashMap>, 73 | } 74 | 75 | impl Actor for NetworkNode { 76 | type Context = Context; 77 | 78 | fn started(&mut self, ctx: &mut Context) { 79 | self.inner.set_status(NodeStatus::Connecting); 80 | 81 | // Connect to actix remote server 82 | actix::actors::Connector::from_registry() 83 | .send(actix::actors::Connect::host(self.inner.address().clone())) 84 | .into_actor(self) 85 | .map(|res, act, ctx| match res { 86 | Ok(stream) => { 87 | info!("Connected to network node: {}", act.inner.address()); 88 | 89 | let (r, w) = stream.split(); 90 | 91 | // configure write side of the connection 92 | let mut framed = 93 | actix::io::FramedWrite::new(w, NetworkClientCodec::default(), ctx); 94 | framed.write(Request::Handshake(act.addr.clone())); 95 | act.framed = Some(framed); 96 | 97 | // read side of the connection 98 | ctx.add_stream(FramedRead::new(r, NetworkClientCodec::default())); 99 | 100 | act.backoff.reset(); 101 | act.inner.set_status(NodeStatus::Ok); 102 | }, 103 | Err(err) => act.restart(Some(err), ctx), 104 | }) 105 | .map_err(|_, act, ctx| act.restart(None, ctx)) 106 | .wait(ctx); 107 | } 108 | } 109 | 110 | impl Supervised for NetworkNode { 111 | fn restarting(&mut self, _: &mut Self::Context) { 112 | self.framed.take(); 113 | self.inner.set_status(NodeStatus::Failed); 114 | //for tx in self.queue.drain(..) { 115 | //let _ = tx.send(Err(Error::Disconnected)); 116 | //} 117 | } 118 | } 119 | 120 | impl actix::io::WriteHandler for NetworkNode {} 121 | 122 | impl NetworkNode { 123 | pub fn new(addr: String, world: Addr, info: NodeInformation) -> NetworkNode { 124 | info!("New network node: {}", addr); 125 | NetworkNode {mid: 0, 126 | world: world, 127 | addr: addr, 128 | inner: info, 129 | framed: None, 130 | requests: HashMap::new(), 131 | backoff: ExponentialBackoff::default(), 132 | } 133 | } 134 | 135 | pub fn restart(&mut self, err: Option, ctx: &mut Context) 136 | { 137 | self.framed.take(); 138 | self.inner.set_status(NodeStatus::Failed); 139 | 140 | if let Some(err) = err { 141 | error!("Can not connect to network node: {}, err: {}", 142 | self.inner.address(), err); 143 | debug!("{:?}", err); 144 | } else { 145 | error!("Restart network node connection"); 146 | } 147 | // re-connect with backoff time. 148 | // we stop currect context, supervisor will restart it. 149 | if let Some(timeout) = self.backoff.next_backoff() { 150 | ctx.run_later(timeout, |act, ctx| act.stop_actor(ctx)); 151 | } else { 152 | self.stop_actor(ctx); 153 | } 154 | } 155 | 156 | fn stop_actor(&mut self, ctx: &mut Context) { 157 | if self.inner.status() == NodeStatus::Failed { 158 | ctx.stop() 159 | } 160 | } 161 | } 162 | 163 | impl StreamHandler for NetworkNode 164 | { 165 | fn error(&mut self, err: io::Error, _ctx: &mut Self::Context) -> Running { 166 | error!("Network node has been disconnected: {}, err: {}", 167 | self.inner.address(), err); 168 | Running::Stop 169 | } 170 | 171 | /// This is main event loop for server responses 172 | fn handle(&mut self, msg: Response, _ctx: &mut Self::Context) { 173 | match msg { 174 | Response::Supported(types) => { 175 | self.world.do_send(msgs::NodeSupportedTypes { 176 | node: self.inner.address().to_string(), 177 | types: types 178 | }); 179 | }, 180 | Response::Result(id, data) => { 181 | if let Some(tx) = self.requests.remove(&id) { 182 | debug!("GOT REMOTE RESULT: {:?} {:?}", id, data); 183 | let _ = tx.send(data); 184 | } 185 | }, 186 | _ => (), 187 | } 188 | } 189 | } 190 | 191 | /// Reconnect node if required 192 | impl Handler for NetworkNode { 193 | type Result = (); 194 | 195 | fn handle(&mut self, _: msgs::ReconnectNode, ctx: &mut Context) { 196 | self.stop_actor(ctx); 197 | } 198 | } 199 | 200 | 201 | /// Send remote mesage 202 | impl Handler for NetworkNode { 203 | type Result = ActixResponse; 204 | 205 | fn handle(&mut self, msg: msgs::SendRemoteMessage, _: &mut Context) -> Self::Result { 206 | if let Some(ref mut framed) = self.framed { 207 | self.mid += 1; 208 | self.requests.insert(self.mid, msg.tx); 209 | framed.write(Request::Message(self.mid, msg.type_id, "1.0".to_string(), msg.data)); 210 | } 211 | ActixResponse::reply(Err(io::Error::new(io::ErrorKind::Other, "test"))) 212 | } 213 | } 214 | -------------------------------------------------------------------------------- /src/protocol.rs: -------------------------------------------------------------------------------- 1 | use std::io; 2 | use serde_json as json; 3 | use byteorder::{NetworkEndian , ByteOrder}; 4 | use bytes::{BytesMut, BufMut}; 5 | use tokio_io::codec::{Encoder, Decoder}; 6 | 7 | const PREFIX: &[u8] = b"ACTIX/1.0\r\n"; 8 | 9 | 10 | /// Client request 11 | #[derive(Serialize, Deserialize, Debug, Message)] 12 | #[serde(tag="cmd", content="data")] 13 | pub enum Request { 14 | Handshake(String), 15 | Ping, 16 | Pong, 17 | /// Message(msg_id, type_id, ver, payload) 18 | Message(u64, String, String, String), 19 | } 20 | 21 | /// Server response 22 | #[derive(Serialize, Deserialize, Debug, Message)] 23 | #[serde(tag="cmd", content="data")] 24 | pub enum Response { 25 | Handshake, 26 | Ping, 27 | Pong, 28 | /// Announce supported message types 29 | Supported(Vec), 30 | /// Response(msg_id, payload) 31 | Result(u64, String), 32 | /// Error(msg_id, error-code) 33 | Error(u64, u16), 34 | } 35 | 36 | /// Codec for Client -> Server transport 37 | pub struct NetworkServerCodec { 38 | prefix: bool 39 | } 40 | 41 | impl Default for NetworkServerCodec { 42 | fn default() -> NetworkServerCodec { 43 | NetworkServerCodec{prefix: false} 44 | } 45 | } 46 | 47 | impl Decoder for NetworkServerCodec 48 | { 49 | type Item = Request; 50 | type Error = io::Error; 51 | 52 | fn decode(&mut self, src: &mut BytesMut) -> Result, Self::Error> { 53 | if !self.prefix { 54 | if src.len() < 11 { 55 | return Ok(None) 56 | } 57 | if &src[..11] == PREFIX { 58 | src.split_to(11); 59 | self.prefix = true; 60 | } else { 61 | return Err(io::Error::new(io::ErrorKind::Other, "Prefix mismatch")) 62 | } 63 | } 64 | 65 | let size = { 66 | if src.len() < 2 { 67 | return Ok(None) 68 | } 69 | NetworkEndian::read_u16(src.as_ref()) as usize 70 | }; 71 | 72 | if src.len() >= size + 2 { 73 | src.split_to(2); 74 | let buf = src.split_to(size); 75 | Ok(Some(json::from_slice::(&buf)?)) 76 | } else { 77 | Ok(None) 78 | } 79 | } 80 | } 81 | 82 | impl Encoder for NetworkServerCodec 83 | { 84 | type Item = Response; 85 | type Error = io::Error; 86 | 87 | fn encode(&mut self, msg: Response, dst: &mut BytesMut) -> Result<(), Self::Error> { 88 | match msg { 89 | Response::Handshake => dst.extend_from_slice(PREFIX), 90 | _ => { 91 | let msg = json::to_string(&msg).unwrap(); 92 | let msg_ref: &[u8] = msg.as_ref(); 93 | 94 | dst.reserve(msg_ref.len() + 2); 95 | dst.put_u16::(msg_ref.len() as u16); 96 | dst.put(msg_ref); 97 | } 98 | } 99 | 100 | Ok(()) 101 | } 102 | } 103 | 104 | 105 | /// Codec for Server -> Client transport 106 | pub struct NetworkClientCodec { 107 | prefix: bool, 108 | } 109 | 110 | impl Default for NetworkClientCodec { 111 | fn default() -> NetworkClientCodec { 112 | NetworkClientCodec{prefix: false} 113 | } 114 | } 115 | 116 | impl Decoder for NetworkClientCodec 117 | { 118 | type Item = Response; 119 | type Error = io::Error; 120 | 121 | fn decode(&mut self, src: &mut BytesMut) -> Result, Self::Error> { 122 | if !self.prefix { 123 | if src.len() < 11 { 124 | return Ok(None) 125 | } 126 | if &src[..11] == PREFIX { 127 | src.split_to(11); 128 | self.prefix = true; 129 | } else { 130 | return Err(io::Error::new(io::ErrorKind::Other, "Prefix mismatch")) 131 | } 132 | } 133 | 134 | let size = { 135 | if src.len() < 2 { 136 | return Ok(None) 137 | } 138 | NetworkEndian::read_u16(src.as_ref()) as usize 139 | }; 140 | 141 | if src.len() >= size + 2 { 142 | src.split_to(2); 143 | let buf = src.split_to(size); 144 | Ok(Some(json::from_slice::(&buf)?)) 145 | } else { 146 | Ok(None) 147 | } 148 | } 149 | } 150 | 151 | impl Encoder for NetworkClientCodec 152 | { 153 | type Item = Request; 154 | type Error = io::Error; 155 | 156 | fn encode(&mut self, msg: Request, dst: &mut BytesMut) -> Result<(), Self::Error> { 157 | if let Request::Handshake(_) = msg { 158 | dst.extend_from_slice(PREFIX); 159 | } 160 | 161 | let msg = json::to_string(&msg).unwrap(); 162 | let msg_ref: &[u8] = msg.as_ref(); 163 | 164 | dst.reserve(msg_ref.len() + 2); 165 | dst.put_u16::(msg_ref.len() as u16); 166 | dst.put(msg_ref); 167 | Ok(()) 168 | } 169 | } 170 | -------------------------------------------------------------------------------- /src/recipient.rs: -------------------------------------------------------------------------------- 1 | #![allow(dead_code, unused_variables)] 2 | use std::marker::PhantomData; 3 | use std::collections::HashMap; 4 | 5 | use serde::Serialize; 6 | use serde::de::DeserializeOwned; 7 | use serde_json; 8 | use futures::Future; 9 | use futures::unsync::oneshot::{self, Sender}; 10 | 11 | use actix::prelude::*; 12 | use actix::dev::{MessageResponse, ResponseChannel, SendError}; 13 | 14 | use msgs; 15 | use node::NetworkNode; 16 | use remote::{Remote, RemoteMessage}; 17 | 18 | pub trait RemoteMessageHandler: Send + Sync { 19 | fn handle(&self, msg: String, sender: Sender); 20 | } 21 | 22 | /// Remote message handler 23 | pub(crate) 24 | struct Provider 25 | where M: RemoteMessage + 'static, 26 | M::Result: Send + Serialize + DeserializeOwned 27 | { 28 | pub recipient: Recipient, 29 | } 30 | 31 | impl RemoteMessageHandler for Provider 32 | where M: RemoteMessage + 'static, M::Result: Send + Serialize + DeserializeOwned 33 | { 34 | fn handle(&self, msg: String, sender: Sender) { 35 | let msg = serde_json::from_slice::(msg.as_ref()).unwrap(); 36 | Arbiter::handle().spawn( 37 | self.recipient.send(msg).then(|res| { 38 | match res { 39 | Ok(res) => { 40 | let body = serde_json::to_string(&res).unwrap(); 41 | let _ = sender.send(body); 42 | }, 43 | Err(e) => (), 44 | } 45 | Ok::<_, ()>(()) 46 | })) 47 | } 48 | } 49 | 50 | /// Recipient proxy actor 51 | pub(crate) 52 | struct RecipientProxy 53 | where M: RemoteMessage + 'static, 54 | M::Result: Send + Serialize + DeserializeOwned 55 | { 56 | m: PhantomData, 57 | nodes: HashMap>, 58 | } 59 | 60 | impl RecipientProxy 61 | where M: RemoteMessage + 'static, 62 | M::Result: Send + Serialize + DeserializeOwned 63 | { 64 | pub fn new() -> Self { 65 | RecipientProxy{m: PhantomData, nodes: HashMap::new()} 66 | } 67 | } 68 | 69 | /// Actor definition 70 | impl Actor for RecipientProxy 71 | where M: RemoteMessage + 'static, 72 | M::Result: Send + Serialize + DeserializeOwned 73 | { 74 | type Context = Context; 75 | } 76 | 77 | impl msgs::NodeOperations for RecipientProxy 78 | where M: RemoteMessage + 'static, 79 | M::Result: Send + Serialize + DeserializeOwned {} 80 | 81 | /// Handler for proxied message 82 | impl Handler for RecipientProxy 83 | where M: RemoteMessage + 'static, 84 | M::Result: Send + Serialize + DeserializeOwned 85 | { 86 | type Result = RecipientProxyResult; 87 | 88 | fn handle(&mut self, msg: M, ctx: &mut Context) -> RecipientProxyResult { 89 | let (tx, rx) = oneshot::channel(); 90 | let body = serde_json::to_string(&msg).unwrap(); 91 | 92 | for node in self.nodes.values() { 93 | node.do_send(msgs::SendRemoteMessage{ 94 | type_id: M::type_id().to_string(), data: body, tx: tx}); 95 | break 96 | } 97 | RecipientProxyResult{m: PhantomData, rx: rx} 98 | } 99 | } 100 | 101 | /// Handle notificartion from World, new node with support has been connected. 102 | /// 103 | /// RecipientProxy can start sending messages 104 | impl Handler for RecipientProxy 105 | where M: RemoteMessage + 'static, 106 | M::Result: Send + Serialize + DeserializeOwned 107 | { 108 | type Result = (); 109 | 110 | fn handle(&mut self, msg: msgs::TypeSupported, ctx: &mut Context) { 111 | debug!("Remote provider {} is registerd for {}", msg.node_id, msg.type_id); 112 | self.nodes.insert(msg.node_id, msg.node); 113 | } 114 | } 115 | 116 | impl Handler for RecipientProxy 117 | where M: RemoteMessage + 'static, 118 | M::Result: Send + Serialize + DeserializeOwned 119 | { 120 | type Result = (); 121 | 122 | fn handle(&mut self, msg: msgs::NodeGone, ctx: &mut Context) { 123 | unimplemented!() 124 | } 125 | } 126 | 127 | /// Proxied message result 128 | pub struct RecipientProxyResult 129 | where M: RemoteMessage + 'static, 130 | M::Result: Send + Serialize + DeserializeOwned 131 | { 132 | m: PhantomData, 133 | rx: oneshot::Receiver, 134 | } 135 | 136 | impl MessageResponse, M> for RecipientProxyResult 137 | where M: RemoteMessage + 'static, 138 | M::Result: Send + Serialize + DeserializeOwned 139 | { 140 | fn handle>(self, _: &mut Context>, tx: Option) { 141 | Arbiter::handle().spawn( 142 | self.rx 143 | .map_err(|e| ()) 144 | .and_then(move |msg| { 145 | let msg = serde_json::from_slice::(msg.as_ref()).unwrap(); 146 | if let Some(tx) = tx { 147 | let _ = tx.send(msg); 148 | } 149 | Ok(()) 150 | }) 151 | ); 152 | } 153 | } 154 | 155 | /// Sender proxy 156 | pub struct RecipientProxySender 157 | where M: RemoteMessage + 'static, 158 | M::Result: Send + Serialize + DeserializeOwned 159 | { 160 | m: PhantomData, 161 | tx: Addr>, 162 | } 163 | 164 | use remote::RemoteRecipientRequest; 165 | 166 | impl RecipientProxySender 167 | where M: RemoteMessage, 168 | M::Result: Send + Serialize + DeserializeOwned 169 | { 170 | pub(crate) fn new(addr: Addr>) -> RecipientProxySender { 171 | RecipientProxySender{m: PhantomData, tx: addr} 172 | } 173 | 174 | pub fn do_send(&self, msg: M) -> Result<(), SendError> { 175 | self.tx.do_send(msg); 176 | Ok(()) 177 | } 178 | 179 | pub fn try_send(&self, msg: M) -> Result<(), SendError> { 180 | self.tx.try_send(msg) 181 | } 182 | 183 | pub fn send(&self, msg: M) -> RemoteRecipientRequest { 184 | RemoteRecipientRequest::new(self.tx.send(msg)) 185 | } 186 | } 187 | 188 | impl Clone for RecipientProxySender 189 | where M: RemoteMessage, M::Result: Send + Serialize + DeserializeOwned, 190 | { 191 | fn clone(&self) -> Self { 192 | RecipientProxySender {m: PhantomData, tx: self.tx.clone()} 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /src/remote.rs: -------------------------------------------------------------------------------- 1 | use std::time::Duration; 2 | use std::marker::PhantomData; 3 | 4 | use serde::Serialize; 5 | use serde::de::DeserializeOwned; 6 | use futures::{Async, Future, Poll}; 7 | use tokio_core::reactor::Timeout; 8 | 9 | use actix::prelude::*; 10 | use actix::dev::{Message, MessageRecipient, SendError, MailboxError}; 11 | 12 | use recipient::RecipientProxySender; 13 | 14 | 15 | pub trait RemoteMessage: Message + Send + Serialize + DeserializeOwned 16 | where Self::Result: Send + Serialize + DeserializeOwned 17 | { 18 | fn type_id() -> &'static str; 19 | } 20 | 21 | pub struct Remote; 22 | 23 | impl MessageRecipient for Remote 24 | where M: RemoteMessage + 'static, M::Result: Send + Serialize + DeserializeOwned 25 | { 26 | type Envelope = RemoteMessageEnvelope; 27 | type Transport = RecipientProxySender; 28 | 29 | type SendError = SendError; 30 | type MailboxError = MailboxError; 31 | type Request = RemoteRecipientRequest; 32 | 33 | fn do_send(tx: &Self::Transport, msg: M) -> Result<(), SendError> { 34 | tx.do_send(msg) 35 | } 36 | 37 | fn try_send(tx: &Self::Transport, msg: M) -> Result<(), SendError> { 38 | tx.try_send(msg) 39 | } 40 | 41 | fn send(tx: &Self::Transport, msg: M) -> RemoteRecipientRequest { 42 | tx.send(msg) 43 | } 44 | 45 | fn clone(tx: &Self::Transport) -> Self::Transport { 46 | tx.clone() 47 | } 48 | } 49 | 50 | pub struct RemoteMessageEnvelope 51 | where M::Result: Send + Serialize + DeserializeOwned 52 | { 53 | msg: M, 54 | } 55 | 56 | impl RemoteMessageEnvelope 57 | where M::Result: Send + Serialize + DeserializeOwned 58 | { 59 | pub fn into_inner(self) -> M { 60 | self.msg 61 | } 62 | } 63 | 64 | impl From for RemoteMessageEnvelope 65 | where M::Result: Send + Serialize + DeserializeOwned 66 | { 67 | fn from(msg: M) -> RemoteMessageEnvelope { 68 | RemoteMessageEnvelope{msg: msg} 69 | } 70 | } 71 | 72 | use recipient::RecipientProxy; 73 | 74 | /// `RecipientRequest` is a `Future` which represents asynchronous message sending process. 75 | #[must_use = "future do nothing unless polled"] 76 | pub struct RemoteRecipientRequest 77 | where T: MessageRecipient, 78 | M: RemoteMessage + 'static, M::Result: Send + Serialize + DeserializeOwned 79 | { 80 | rx: actix::dev::Request, M>, 81 | timeout: Option, 82 | _t: PhantomData, 83 | } 84 | 85 | impl RemoteRecipientRequest 86 | where T: MessageRecipient, 87 | M: RemoteMessage + 'static, M::Result: Send + Serialize + DeserializeOwned 88 | { 89 | pub(crate) fn new(rx: actix::dev::Request, M>) 90 | -> RemoteRecipientRequest 91 | { 92 | RemoteRecipientRequest{rx: rx, timeout: None, _t: PhantomData} 93 | } 94 | 95 | /// Set message delivery timeout 96 | pub fn timeout(mut self, dur: Duration) -> Self { 97 | self.timeout = Some(Timeout::new(dur, Arbiter::handle()).unwrap()); 98 | self 99 | } 100 | 101 | fn poll_timeout(&mut self) -> Poll { 102 | if let Some(ref mut timeout) = self.timeout { 103 | match timeout.poll() { 104 | Ok(Async::Ready(())) => Err(MailboxError::Timeout), 105 | Ok(Async::NotReady) => Ok(Async::NotReady), 106 | Err(_) => unreachable!() 107 | } 108 | } else { 109 | Ok(Async::NotReady) 110 | } 111 | } 112 | } 113 | 114 | impl Future for RemoteRecipientRequest 115 | where T: MessageRecipient, MailboxError=MailboxError>, 116 | M: RemoteMessage + 'static, M::Result: Send + Serialize + DeserializeOwned 117 | { 118 | type Item = M::Result; 119 | type Error = T::MailboxError; 120 | 121 | fn poll(&mut self) -> Poll { 122 | match self.rx.poll() { 123 | Ok(Async::Ready(item)) => Ok(Async::Ready(item)), 124 | Ok(Async::NotReady) => { 125 | self.poll_timeout() 126 | } 127 | Err(_) => Err(MailboxError::Closed), 128 | } 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /src/utils.rs: -------------------------------------------------------------------------------- 1 | use std::{io, net}; 2 | use net2::TcpBuilder; 3 | 4 | 5 | pub fn tcp_listener(addr: net::SocketAddr, backlog: i32) -> io::Result { 6 | let builder = match addr { 7 | net::SocketAddr::V4(_) => TcpBuilder::new_v4()?, 8 | net::SocketAddr::V6(_) => TcpBuilder::new_v6()?, 9 | }; 10 | builder.bind(addr)?; 11 | builder.reuse_address(true)?; 12 | Ok(builder.listen(backlog)?) 13 | } 14 | -------------------------------------------------------------------------------- /src/worker.rs: -------------------------------------------------------------------------------- 1 | use std::io; 2 | use std::sync::Arc; 3 | use std::collections::HashMap; 4 | 5 | use futures::unsync::oneshot::channel; 6 | use tokio_io::{AsyncRead, AsyncWrite}; 7 | use tokio_io::io::WriteHalf; 8 | use tokio_io::codec::FramedRead; 9 | use actix::prelude::*; 10 | 11 | use msgs; 12 | use msgs::NodeConnected; 13 | use world::World; 14 | use recipient::RemoteMessageHandler; 15 | use protocol::{Request, Response, NetworkServerCodec}; 16 | 17 | /// Worker accepts messages from other network hosts and 18 | /// pass them to local recipients 19 | pub struct NetworkWorker where T: AsyncRead + AsyncWrite { 20 | id: usize, 21 | net: Addr, 22 | handlers: HashMap<&'static str, Arc>, 23 | framed: actix::io::FramedWrite, NetworkServerCodec>, 24 | } 25 | 26 | impl NetworkWorker 27 | where T: AsyncRead + AsyncWrite + 'static 28 | { 29 | pub fn start(id: usize, io: T, 30 | handlers: HashMap<&'static str, Arc>, 31 | net: Addr) -> Addr 32 | { 33 | Actor::create(move |ctx| { 34 | let (r, w) = io.split(); 35 | 36 | // read side of the connection 37 | ctx.add_stream(FramedRead::new(r, NetworkServerCodec::default())); 38 | 39 | // write side of the connection 40 | let mut framed = actix::io::FramedWrite::new( 41 | w, NetworkServerCodec::default(), ctx); 42 | framed.write(Response::Handshake); 43 | 44 | // send list of supported messages 45 | framed.write(Response::Supported( 46 | handlers.keys().map(|s| s.to_string()).collect())); 47 | NetworkWorker{id: id, net: net, handlers: handlers, framed: framed} 48 | }) 49 | } 50 | } 51 | 52 | impl Actor for NetworkWorker where T: AsyncRead + AsyncWrite + 'static { 53 | type Context = Context; 54 | } 55 | 56 | impl actix::io::WriteHandler for NetworkWorker 57 | where T: AsyncRead + AsyncWrite + 'static { 58 | } 59 | 60 | impl StreamHandler for NetworkWorker 61 | where T: AsyncRead + AsyncWrite + 'static 62 | { 63 | fn finished(&mut self, ctx: &mut Self::Context) { 64 | self.net.do_send(msgs::WorkerDisconnected(self.id)); 65 | ctx.stop(); 66 | } 67 | 68 | /// This is main event loop for client connection 69 | fn handle(&mut self, msg: Request, ctx: &mut Self::Context) { 70 | match msg { 71 | Request::Handshake(addr) => self.net.do_send(NodeConnected(addr)), 72 | Request::Message(msg_id, type_id, _, body) => { 73 | debug!("RECEIVED MESSAGE: {:?} {:?} {:?}", msg_id, type_id, body); 74 | if let Some(ref handler) = self.handlers.get(type_id.as_str()) { 75 | let (tx, rx) = channel(); 76 | handler.handle(body, tx); 77 | 78 | rx.into_actor(self) 79 | .then(move |res, act, _| { 80 | match res { 81 | Ok(res) => act.framed.write(Response::Result(msg_id, res)), 82 | Err(_) => (), 83 | } 84 | actix::fut::ok(()) 85 | }) 86 | .spawn(ctx) 87 | } 88 | }, 89 | _ => { 90 | println!("CLIENT REQ: {:?}", msg); 91 | } 92 | } 93 | } 94 | } 95 | 96 | /// World is dead 97 | impl Handler for NetworkWorker 98 | where T: AsyncRead + AsyncWrite + 'static 99 | { 100 | type Result = (); 101 | 102 | fn handle(&mut self, _: msgs::StopWorker, ctx: &mut Self::Context) { 103 | ctx.stop(); 104 | } 105 | } 106 | 107 | /// New recipient is registered 108 | impl Handler for NetworkWorker 109 | where T: AsyncRead + AsyncWrite + 'static 110 | { 111 | type Result = (); 112 | 113 | fn handle(&mut self, msg: msgs::ProvideRecipient, _: &mut Self::Context) { 114 | self.framed.write(Response::Supported(vec![msg.type_id.to_owned()])); 115 | self.handlers.insert(msg.type_id, msg.handler); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/world.rs: -------------------------------------------------------------------------------- 1 | use std::{io, net}; 2 | use std::any::Any; 3 | use std::sync::Arc; 4 | use std::time::Duration; 5 | use std::collections::{HashMap, HashSet}; 6 | 7 | use actix::prelude::*; 8 | use actix::actors::signal; 9 | use futures::Future; 10 | use serde::Serialize; 11 | use serde::de::DeserializeOwned; 12 | use tokio_core::net::{TcpStream, TcpListener}; 13 | use tokio_core::reactor::Timeout; 14 | 15 | use msgs; 16 | use utils; 17 | use worker::NetworkWorker; 18 | use node::{NetworkNode, NodeInformation}; 19 | use remote::{Remote, RemoteMessage}; 20 | use recipient::{Provider, RecipientProxy, 21 | RecipientProxySender, RemoteMessageHandler}; 22 | 23 | 24 | struct Proxy { 25 | addr: Box, 26 | service: Recipient, 27 | } 28 | 29 | pub struct World { 30 | addr: String, 31 | addrs: HashMap, 32 | nodes: HashMap>, 33 | types: HashMap>, 34 | sockets: HashMap, 35 | wid: usize, 36 | workers: HashMap>>, 37 | handlers: HashMap<&'static str, Arc>, 38 | recipients: HashMap<&'static str, Proxy>, 39 | exit: bool, 40 | } 41 | 42 | impl Actor for World { 43 | type Context = Context; 44 | } 45 | 46 | impl World { 47 | pub fn new(addr: String) -> io::Result { 48 | let net = World{addr: addr.clone(), 49 | addrs: HashMap::new(), 50 | nodes: HashMap::new(), 51 | types: HashMap::new(), 52 | sockets: HashMap::new(), 53 | wid: 0, 54 | workers: HashMap::new(), 55 | handlers: HashMap::new(), 56 | recipients: HashMap::new(), 57 | exit: false}; 58 | Ok(net.bind(addr)?) 59 | } 60 | 61 | /// The socket address to bind 62 | /// 63 | /// To bind multiple addresses this method can be call multiple times. 64 | pub fn bind(mut self, addr: S) -> io::Result { 65 | let mut err = None; 66 | let mut succ = false; 67 | for addr in addr.to_socket_addrs()? { 68 | match utils::tcp_listener(addr, 256) { 69 | Ok(lst) => { 70 | succ = true; 71 | self.sockets.insert(lst.local_addr().unwrap(), lst); 72 | }, 73 | Err(e) => err = Some(e), 74 | } 75 | } 76 | 77 | if !succ { 78 | if let Some(e) = err.take() { 79 | Err(e) 80 | } else { 81 | Err(io::Error::new(io::ErrorKind::Other, "Can not bind to address.")) 82 | } 83 | } else { 84 | Ok(self) 85 | } 86 | } 87 | 88 | /// Register network node 89 | pub fn add_node>(mut self, addr: Option) -> Self { 90 | addr.map(|addr| { 91 | let addr = addr.into(); 92 | self.addrs.insert(addr.clone(), NodeInformation::new(addr)); 93 | }); 94 | self 95 | } 96 | 97 | /// Create remote recipient for specific message type 98 | pub fn get_recipient(&mut self) -> Recipient 99 | where M: RemoteMessage + 'static, 100 | M::Result: Send + Serialize + DeserializeOwned 101 | { 102 | if let Some(info) = self.recipients.get(M::type_id()) { 103 | if let Some(&(_, ref saddr)) = info.addr.downcast_ref 104 | ::<(Addr>, Addr>)>() 105 | { 106 | return Recipient::new(RecipientProxySender::new(saddr.clone())) 107 | } 108 | } 109 | 110 | let (addr, saddr): (Addr>, 111 | Addr>) = RecipientProxy::new().start(); 112 | self.recipients.insert( 113 | M::type_id(), Proxy{addr: Box::new(addr.clone()), 114 | service: addr.clone().recipient()}); 115 | 116 | return Recipient::new(RecipientProxySender::new(saddr)) 117 | } 118 | 119 | /// Register remote recipient provider. 120 | /// 121 | /// Announce recipient availability to all connected nodes. 122 | pub fn register_recipient(world: &Addr, recipient: Recipient) 123 | where M: RemoteMessage + 'static, M::Result: Send + Serialize + DeserializeOwned 124 | { 125 | let r = Provider{recipient: recipient}; 126 | world.do_send(msgs::ProvideRecipient{ 127 | type_id: M::type_id(), handler: Arc::new(r)}) 128 | } 129 | 130 | fn stop(&mut self, ctx: &mut Context) { 131 | if !self.exit { 132 | self.exit = true; 133 | 134 | if self.workers.is_empty() { 135 | self.stop_system_with_delay(); 136 | } else { 137 | for (wid, worker) in &self.workers { 138 | let id: usize = *wid; 139 | worker.send(msgs::StopWorker).into_actor(self) 140 | .then(move |_, slf, ctx| { 141 | slf.workers.remove(&id); 142 | if slf.workers.is_empty() { 143 | ctx.stop(); 144 | slf.stop_system_with_delay(); 145 | } 146 | actix::fut::ok(()) 147 | }).spawn(ctx); 148 | } 149 | } 150 | } 151 | } 152 | 153 | fn stop_system_with_delay(&self) { 154 | Arbiter::handle().spawn( 155 | Timeout::new(Duration::from_secs(1), Arbiter::handle()).unwrap() 156 | .then(|_| { 157 | Arbiter::system().do_send(actix::msgs::SystemExit(0)); 158 | Ok(()) 159 | })); 160 | } 161 | 162 | /// Create network nodes, and start listening for incoming connections 163 | pub fn start(mut self) -> Addr { 164 | let addrs: Vec<(net::SocketAddr, net::TcpListener)> = 165 | self.sockets.drain().collect(); 166 | 167 | // start network 168 | Actor::create(move |ctx| { 169 | let h = Arbiter::handle(); 170 | 171 | // subscribe to signals 172 | signal::ProcessSignals::from_registry().do_send( 173 | signal::Subscribe(ctx.address::>().recipient())); 174 | 175 | // start workers 176 | for (addr, sock) in addrs { 177 | info!("Starting actix remote server on {}", addr); 178 | let lst = TcpListener::from_listener(sock, &addr, h) 179 | .unwrap(); 180 | ctx.add_stream(lst.incoming()); 181 | } 182 | 183 | for info in self.addrs.values() { 184 | let net = ctx.address(); 185 | let info2 = info.clone(); 186 | let addr2 = self.addr.clone(); 187 | let node: Addr = 188 | Supervisor::start(move |_| NetworkNode::new(addr2, net, info2)); 189 | self.nodes.insert(info.address().to_string(), node); 190 | } 191 | 192 | self 193 | }) 194 | } 195 | } 196 | 197 | /// Register remote message recipient 198 | impl Handler for World { 199 | type Result = (); 200 | 201 | fn handle(&mut self, msg: msgs::ProvideRecipient, _: &mut Self::Context) { 202 | // notify all workers 203 | for addr in self.workers.values() { 204 | addr.do_send(msg.clone()); 205 | } 206 | 207 | self.handlers.insert(msg.type_id, msg.handler); 208 | } 209 | } 210 | 211 | /// New client connection, create new downstream connection or re-connect existing 212 | impl StreamHandler<(TcpStream, net::SocketAddr), io::Error> for World 213 | { 214 | fn handle(&mut self, msg: (TcpStream, net::SocketAddr), ctx: &mut Context) { 215 | self.wid += 1; 216 | let addr = NetworkWorker::start( 217 | self.wid, msg.0, self.handlers.clone(), ctx.address()); 218 | self.workers.insert(self.wid, addr); 219 | } 220 | } 221 | 222 | /// Worker disconnected notification 223 | impl Handler for World { 224 | type Result = (); 225 | 226 | fn handle(&mut self, msg: msgs::WorkerDisconnected, _: &mut Self::Context) { 227 | self.workers.remove(&msg.0); 228 | } 229 | } 230 | 231 | /// Connected to remote node 232 | impl Handler for World { 233 | type Result = (); 234 | 235 | fn handle(&mut self, msg: msgs::NodeConnected, ctx: &mut Context) { 236 | if let Some(node) = self.nodes.get(&msg.0) { 237 | node.do_send(msgs::ReconnectNode); 238 | return 239 | } 240 | 241 | let addr = msg.0.clone(); 242 | let naddr = self.addr.clone(); 243 | let net = ctx.address(); 244 | let info = NodeInformation::new(msg.0.clone()); 245 | let node: Addr = 246 | Supervisor::start(move |_| NetworkNode::new(naddr, net, info)); 247 | self.nodes.insert(addr, node); 248 | } 249 | } 250 | 251 | /// Handle NodeSupportedTypes message 252 | /// 253 | /// Node notifies about supported remote types 254 | impl Handler for World { 255 | type Result = (); 256 | 257 | fn handle(&mut self, msg: msgs::NodeSupportedTypes, _: &mut Context) { 258 | // register in internal registry 259 | for tp in &msg.types { 260 | if !self.types.contains_key(tp) { 261 | self.types.insert(tp.clone(), HashSet::new()); 262 | } 263 | self.types.get_mut(tp).unwrap().insert(msg.node.clone()); 264 | } 265 | 266 | // notify all recipient proxies 267 | if let Some(node) = self.nodes.get(&msg.node) { 268 | for tp in msg.types { 269 | if let Some(proxy) = self.recipients.get(tp.as_str()) { 270 | let _ = proxy.service.do_send( 271 | msgs::TypeSupported { 272 | type_id: tp, 273 | node_id: msg.node.clone(), 274 | node: node.clone(), 275 | }); 276 | } 277 | } 278 | } 279 | } 280 | } 281 | 282 | /// Signals support 283 | /// Handle `SIGINT`, `SIGTERM`, `SIGQUIT` signals and send `SystemExit(0)` 284 | /// message to `System` actor. 285 | impl Handler for World { 286 | type Result = (); 287 | 288 | fn handle(&mut self, msg: signal::Signal, ctx: &mut Context) { 289 | match msg.0 { 290 | signal::SignalType::Int => { 291 | info!("SIGINT received, exiting"); 292 | self.stop(ctx); 293 | } 294 | signal::SignalType::Term => { 295 | info!("SIGTERM received, stopping"); 296 | self.stop(ctx); 297 | } 298 | signal::SignalType::Quit => { 299 | info!("SIGQUIT received, exiting"); 300 | self.stop(ctx); 301 | } 302 | _ => (), 303 | } 304 | } 305 | } 306 | --------------------------------------------------------------------------------