├── .gitignore ├── Cargo.toml ├── LICENSE-2.0 ├── README.md ├── archive └── recon-gossip │ ├── .gitignore │ ├── Cargo.toml │ ├── LICENSE-2.0 │ ├── README.md │ ├── examples │ └── gossip.rs │ ├── gossip.sh │ └── src │ ├── client.rs │ ├── conn.rs │ ├── framing.rs │ ├── fut.rs │ ├── lib.rs │ ├── link.rs │ ├── lpb.rs │ ├── multiplex.rs │ ├── serde_types.rs │ ├── service.rs │ ├── switchboard.rs │ ├── upb.rs │ └── watchdog.rs ├── recon-link ├── Cargo.toml ├── LICENSE-2.0 ├── examples │ └── reconnect.rs └── src │ ├── conn.rs │ ├── framing.rs │ └── lib.rs ├── recon-service ├── Cargo.toml └── src │ ├── lib.rs │ └── service.rs └── recon ├── Cargo.toml ├── LICENSE-2.0 └── src └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | target/ 3 | **/*.rs.bk 4 | Cargo.lock 5 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [workspace] 2 | members = [ 3 | "recon", 4 | "recon-link", 5 | "recon-service", 6 | ] 7 | 8 | -------------------------------------------------------------------------------- /LICENSE-2.0: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Recon 2 | 3 | Asynchronous messaging on tokio-rs 4 | 5 | ## Description 6 | 7 | Tokio provides low-level building blocks to use mio with futures-rs. 8 | The related crates tokio-service and tokio-proto are designed around 9 | synchronous client/server applications. 10 | 11 | This project is designed to provide the building blocks for 12 | implementing asynchronous messaging algorithms between nodes. 13 | 14 | ## Components 15 | 16 | ### recon-link 17 | 18 | This is an abstraction that sits on top of a TcpStream. 19 | It will persistently connect and re-connect to a SocketAddr to maintain a working open connection. 20 | 21 | Messages are sent and received using a Stream and a Sink pair locally and remotely. A framed TcpStream works with the remote side. 22 | 23 | Message delivery is not guaranteed: within a tcp session any suffix of the stream of Messages may be dropped. 24 | 25 | Within a single tcp session messages will arrive in order as per Tcp's message ordering guarantees. 26 | 27 | Upon reconnection a control message is sent to the local side with an incremented session id. Messages sent to the local side are tagged with the session id. 28 | -------------------------------------------------------------------------------- /archive/recon-gossip/.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /archive/recon-gossip/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | authors = ["Chris Dawes "] 3 | name = "recon" 4 | version = "0.1.0" 5 | 6 | [dependencies] 7 | bincode = "0.9.2" 8 | bytes = "0.4.5" 9 | chrono = "0.4.0" 10 | futures = "0.1.17" 11 | futures-cpupool = "0.1.7" 12 | futures-mpsc = "0.1.1" 13 | log = "0.3.8" 14 | quick-error = "1.2.1" 15 | rand = "0.3.18" 16 | rustc-serialize = "0.3.24" 17 | serde = "1.0.24" 18 | serde_derive = "1.0.24" 19 | serde_json = "1.0.8" 20 | tokio-core = "0.1.10" 21 | tokio-io = "0.1.4" 22 | tokio-proto = "0.1.1" 23 | tokio-retry = "0.1.1" 24 | tokio-service = "0.1.0" 25 | tokio-timer = "0.1.2" 26 | 27 | [dependencies.bytes-more] 28 | git = "https://github.com/carllerche/bytes-more" 29 | 30 | [dependencies.tokio-serde-json] 31 | git = "https://github.com/carllerche/tokio-serde-json" 32 | 33 | [dev-dependencies] 34 | slog = "2.0.12" 35 | slog-envlogger = "2.0.0" 36 | slog-stdlog = "3.0.2" 37 | slog-term = "2.3.0" 38 | 39 | [features] 40 | -------------------------------------------------------------------------------- /archive/recon-gossip/LICENSE-2.0: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /archive/recon-gossip/README.md: -------------------------------------------------------------------------------- 1 | # Recon 2 | 3 | Asynchronous messaging on tokio-rs 4 | 5 | ## Description 6 | 7 | Tokio provides low-level building blocks to use mio with futures-rs. 8 | The related crates tokio-service and tokio-proto are designed around 9 | synchronous client/server applications. 10 | 11 | This project is designed to provide the building blocks for 12 | implementing asynchronous messaging algorithms between nodes. 13 | 14 | An example implementation of a gossip protocol is included. 15 | 16 | ## Running the examples 17 | 18 | ### Gossip 19 | 20 | The included bash script creates a peers.txt file with a list of ip addresses and ports. 21 | It then launches multiple copies of the gossip example program which form a mesh 22 | network and begin exchanging messages every 2 seconds. 23 | 24 | Messages are broadcast with a fan-out factor k, in a number of rounds. 25 | Messages are stored for resending with probability alpha. 26 | When a later message is received than expected, indicating a missing message, 27 | a resend request is broadcast. 28 | After waiting for a timeout delta, if the resend request was not fulfilled, 29 | the missing message is skipped over. 30 | Messages are delivered in order. 31 | 32 | ``` 33 | ./gossip.sh 34 | ``` 35 | 36 | ## Warning 37 | 38 | Tokio is still evolving and occasionally makes breaking changes. 39 | Also this is extremely early in development and may not amount to 40 | anything. 41 | 42 | ## Compatability 43 | 44 | For best results use Linux. 45 | Tokio/mio on windows seems to behave a bit differently e.g. with not noticing when a tcp connection is closed. 46 | Not tested on Darwin. -------------------------------------------------------------------------------- /archive/recon-gossip/examples/gossip.rs: -------------------------------------------------------------------------------- 1 | extern crate futures; 2 | extern crate tokio_core; 3 | extern crate tokio_proto; 4 | extern crate tokio_service; 5 | extern crate tokio_timer; 6 | #[macro_use] 7 | extern crate slog; 8 | extern crate slog_term; 9 | extern crate slog_stdlog; 10 | extern crate slog_envlogger; 11 | #[macro_use] 12 | extern crate log; 13 | extern crate recon; 14 | extern crate chrono; 15 | 16 | use std::cell::RefCell; 17 | use std::net::{self, SocketAddr}; 18 | use std::io::{self, Write, BufRead}; 19 | use std::fs; 20 | use std::collections::{VecDeque, HashSet}; 21 | use futures::{Future, Poll, Async, Stream}; 22 | use tokio_core::reactor::{Core, Handle}; 23 | use tokio_core::net::{TcpStream}; 24 | use tokio_core::io::{copy, write_all, Io}; 25 | use tokio_core::channel; 26 | use tokio_service::Service; 27 | use tokio_timer::Timer; 28 | use chrono::Duration; 29 | 30 | use recon::client::{NewClient, Client, LineClient, NewLineClient}; 31 | use recon::framing::{ReconFrame, FramedLineTransport, new_line_transport}; 32 | use recon::service::{AsyncService}; 33 | use recon::conn::*; 34 | use recon::link::{Link}; 35 | use recon::multiplex::{self, MultiplexLink}; 36 | use recon::watchdog::{Watchdog}; 37 | use recon::upb::{UnreliableProbabilisticBroadcast}; 38 | use recon::lpb::{LazyProbabilisticBroadcast}; 39 | 40 | struct App { 41 | delay: Option>>, 42 | interval: Duration, 43 | seq: u32, 44 | link: MultiplexLink, 45 | link_rx: channel::Receiver, 46 | multiplex_key: String, 47 | gossip: LazyProbabilisticBroadcast, 48 | } 49 | 50 | impl App { 51 | pub fn new(handle: Handle, id: String, interval: Duration, addr: SocketAddr, conn_factory: NewLineClient, 52 | peers: HashSet, k: usize, rounds: u32, alpha: f32, delta: Duration) -> io::Result { 53 | let (link_tx, link_rx) = try!(channel::channel(&handle)); 54 | 55 | let link = try!(MultiplexLink::new(handle.clone(), id.clone(), addr, conn_factory)); 56 | let gossip = try!(LazyProbabilisticBroadcast::new( 57 | handle.clone(), id.clone(), peers, link.channel(), k, rounds, alpha, delta, 58 | "gossip".to_owned(), delta * 2)); 59 | 60 | let key = "app"; 61 | try!(link.subscribe(key, link_tx)); 62 | 63 | Ok(App { 64 | delay: Some(try!(Self::delay(interval))), 65 | interval: interval, 66 | seq: 0, 67 | link: link, 68 | link_rx: link_rx, 69 | multiplex_key: key.to_string(), 70 | gossip: gossip, 71 | }) 72 | } 73 | 74 | pub fn delay(interval: Duration) -> Result>, io::Error> { 75 | let std_interval = try!(interval.to_std() 76 | .map_err(|e| io::Error::new(io::ErrorKind::Other, "duration out of range"))); 77 | 78 | Ok(Box::new(Timer::default() 79 | .sleep(std_interval) 80 | .map_err(|_| std::io::Error::new(std::io::ErrorKind::Other, "timer error"))) 81 | as Box>) 82 | } 83 | 84 | pub fn add_conn(&self, client_id: &str, addr: SocketAddr) -> io::Result<()> { 85 | self.link.add(client_id, addr) 86 | } 87 | } 88 | 89 | impl Future for App { 90 | type Item=(); 91 | type Error=io::Error; 92 | 93 | fn poll(&mut self) -> Poll<(), io::Error> { 94 | let mut delay = self.delay.take().unwrap(); 95 | 96 | match try!(delay.poll()) { 97 | Async::Ready(()) => { 98 | self.seq += 1; 99 | let value = format!("hello {}", self.seq); 100 | try!(self.gossip.broadcast(value)); 101 | 102 | self.delay = Some(try!(Self::delay(self.interval))); 103 | try!(self.poll()); 104 | }, 105 | Async::NotReady => { 106 | self.delay = Some(delay); 107 | } 108 | } 109 | 110 | try!(self.link.poll()); 111 | 112 | while let Async::Ready(Some(msg)) = try!(self.link_rx.poll()) { 113 | info!("received: {:?}", msg); 114 | } 115 | 116 | while let Async::Ready(Some(gossip_msg)) = try!(self.gossip.poll()) { 117 | info!("received gossip: {:?}", gossip_msg); 118 | } 119 | 120 | trace!("done polling app"); 121 | 122 | Ok(Async::NotReady) 123 | } 124 | } 125 | 126 | pub fn load_node_ids(filename: &str) -> io::Result> { 127 | let mut result = HashSet::new(); 128 | let f = try!(fs::File::open(filename)); 129 | let mut f = io::BufReader::new(&f); 130 | for line in f.lines() { 131 | result.insert(try!(line)); 132 | } 133 | Ok(result) 134 | } 135 | 136 | // args: 137 | // [0]: program name 138 | // [1]: listen address (== node id) 139 | // [2]: file containing list of all node ids 140 | pub fn main() { 141 | slog_envlogger::init().unwrap(); 142 | 143 | let mut core = Core::new().unwrap(); 144 | 145 | // This brings up our server. 146 | let node_id = std::env::args().nth(1).unwrap(); 147 | let peers: HashSet = load_node_ids(&std::env::args().nth(2).unwrap()) 148 | .expect("error loading peer addresses") 149 | .into_iter() 150 | .filter(|i| *i != node_id) 151 | .collect(); 152 | 153 | let addr: SocketAddr = node_id.parse().unwrap(); 154 | let factory = NewLineClient::new(core.handle()); 155 | 156 | info!("node_id={} {} peers listening on {}", node_id, peers.len(), addr); 157 | 158 | // gossip params 159 | let k = 3; 160 | let rounds = 2; 161 | let alpha = 0.5; 162 | let delta = Duration::milliseconds(200); 163 | 164 | let app = App::new(core.handle(), node_id, Duration::seconds(2), addr, factory, peers.clone(), k, rounds, alpha, delta).expect("error creating app"); 165 | //app.add_conn("9000", addr).expect("error adding connection"); 166 | for p in peers { 167 | app.add_conn(&p, p.parse().unwrap()).expect("error adding connection"); 168 | } 169 | 170 | 171 | let client = core.run(app).expect("error running future"); 172 | } 173 | -------------------------------------------------------------------------------- /archive/recon-gossip/gossip.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export RUST_LOG="recon=trace,gossip=trace" 4 | 5 | trap 'kill $(jobs -pr)' SIGINT SIGTERM EXIT 6 | 7 | truncate -s 0 peers.txt 8 | 9 | for PORT in `seq 9000 9008`; do 10 | echo "127.0.0.1:$PORT" >> peers.txt 11 | done 12 | 13 | cargo build --example gossip 14 | 15 | for PORT in `seq 9000 9008`; do 16 | ./target/debug/examples/gossip 127.0.0.1:$PORT peers.txt > $PORT.txt 2>&1 & 17 | done 18 | 19 | tail -f 9000.txt 20 | -------------------------------------------------------------------------------- /archive/recon-gossip/src/client.rs: -------------------------------------------------------------------------------- 1 | use futures::{self, Async, Future, Sink, Stream, AsyncSink, StartSend, Poll}; 2 | use std::io; 3 | use std::net::SocketAddr; 4 | use std::collections::VecDeque; 5 | use tokio_core::reactor::Handle; 6 | use tokio_core::net::TcpStream; 7 | use tokio_timer::*; 8 | use std::cell::RefCell; 9 | use std::mem; 10 | use std::time::Duration; 11 | 12 | use super::service::*; 13 | use super::framing::{new_line_transport, FramedLineTransport, ReconFrame}; 14 | use super::fut::*; 15 | 16 | pub trait NewClient where Self::Item: AsyncService, Self::Io: Io { 17 | type Request; 18 | type Error; 19 | type Item; 20 | type Io; 21 | 22 | fn new_service(&self, io: Self::Io) -> io::Result; 23 | 24 | fn box_clone(&self) -> Box>; 25 | } 26 | 27 | impl Clone for Box> where I: AsyncService, O: Io { 28 | fn clone(&self) -> Box> { 29 | self.box_clone() 30 | } 31 | } 32 | 33 | /// And the client handle. 34 | pub struct LineClient where S: Sink + 'static { 35 | inner: RefCell, 36 | //reqs: RefCell>, 37 | } 38 | 39 | impl LineClient where S: Sink + 'static { 40 | pub fn new(inner: S) -> LineClient { 41 | LineClient { 42 | inner: RefCell::new(inner), 43 | //reqs: RefCell::new(VecDeque::new()), 44 | } 45 | } 46 | } 47 | 48 | impl AsyncService for LineClient where S: Sink + 'static { 49 | type Request = S::SinkItem; 50 | type Error = io::Error; 51 | 52 | fn send(&self, req: Self::Request) -> io::Result<()> { 53 | trace!("lineclient sending request"); 54 | //self.reqs.borrow_mut().push_back(self.inner.send(req)); 55 | let mut sink = self.inner.borrow_mut(); 56 | 57 | if let AsyncSink::NotReady(req) = try!(sink.start_send(req) 58 | .map_err(|e| { 59 | debug!("error sending to sink"); 60 | io::Error::new(io::ErrorKind::Other, "sink start_send error") 61 | }) 62 | ) { 63 | debug!("sink not ready for send"); 64 | } 65 | 66 | Ok(()) 67 | } 68 | 69 | fn poll(&mut self) -> Poll<(), Self::Error> { 70 | trace!("polling lineclient"); 71 | 72 | try!(self.inner.borrow_mut().poll_complete().map_err(|e| { 73 | debug!("error polling sink completion"); 74 | io::Error::new(io::ErrorKind::Other, "sink error") 75 | })); 76 | 77 | Ok(Async::NotReady) 78 | } 79 | } 80 | 81 | pub struct Client where S: AsyncService { 82 | service: S 83 | } 84 | 85 | impl Client where S: AsyncService { 86 | pub fn new(inner: S) -> Client { 87 | Client { 88 | service: inner 89 | } 90 | } 91 | } 92 | 93 | impl AsyncService for Client where S: AsyncService + 'static { 94 | type Request = String; 95 | type Error = io::Error; 96 | 97 | fn send(&self, req: String) -> io::Result<()> { 98 | if req.chars().find(|&c| c == '\n').is_some() { 99 | debug!("failure: message contained newline"); 100 | let err = io::Error::new(io::ErrorKind::InvalidInput, "message contained new line"); 101 | return Err(err); 102 | } 103 | 104 | trace!("sending message"); 105 | 106 | self.service.send(ReconFrame::Message(req)) 107 | } 108 | 109 | fn poll(&mut self) -> Poll<(), Self::Error> { 110 | self.service.poll() 111 | } 112 | } 113 | 114 | #[derive(Clone)] 115 | pub struct NewLineClient { 116 | handle: Handle 117 | } 118 | 119 | impl NewLineClient { 120 | pub fn new(handle: Handle) -> NewLineClient { 121 | NewLineClient { 122 | handle: handle, 123 | } 124 | } 125 | } 126 | 127 | impl NewClient for NewLineClient { 128 | type Request=String; 129 | type Error=io::Error; 130 | type Item=Client>>; 131 | type Io=TcpStream; 132 | 133 | fn new_service(&self, io: TcpStream) -> io::Result { 134 | Ok(Client::new(LineClient::new(new_line_transport(io)))) 135 | } 136 | 137 | fn box_clone(&self) -> Box> { 138 | Box::new(self.clone()) 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /archive/recon-gossip/src/conn.rs: -------------------------------------------------------------------------------- 1 | use std::cell::RefCell; 2 | use std::time::Duration; 3 | use std::net::{self, SocketAddr}; 4 | use std::io; 5 | use std::io::{Write}; 6 | use std::collections::VecDeque; 7 | use futures::{self, Future, Poll, Async, Stream}; 8 | use futures::sync::mpsc::{channel, Sender, Receiver}; 9 | use tokio_core::reactor::{Core, Handle}; 10 | use tokio_core::net::{TcpStream}; 11 | use tokio_io::io::{copy, write_all}; 12 | use tokio_service::Service; 13 | use tokio_timer::Timer; 14 | 15 | use super::client::{NewClient, Client, LineClient}; 16 | use super::framing::{ReconFrame, FramedLineTransport, new_line_transport}; 17 | use super::service::{AsyncService}; 18 | 19 | pub enum ConnStream where S: AsyncService { 20 | Connected(S), 21 | Connecting(Box>), 22 | NotConnected 23 | } 24 | 25 | pub struct Conn where S: AsyncService + 'static { 26 | handle: Handle, 27 | addr: SocketAddr, 28 | stream: Option>, 29 | channel_tx: Sender, 30 | channel_rx: Receiver, 31 | factory: Box>, 32 | } 33 | 34 | impl Conn where S: AsyncService + 'static { 35 | pub fn new(handle: Handle, addr: SocketAddr, factory: Box>) -> io::Result> { 36 | let (tx, rx) = try!(channel(&handle)); 37 | 38 | Ok(Conn { 39 | handle: handle, 40 | addr: addr, 41 | stream: Some(ConnStream::NotConnected), 42 | channel_tx: tx, 43 | channel_rx: rx, 44 | factory: factory, 45 | }) 46 | } 47 | 48 | pub fn channel(&self) -> Sender { 49 | self.channel_tx.clone() 50 | } 51 | 52 | pub fn send(&self, msg: M) where M: Into { 53 | self.channel_tx.send(msg.into()); 54 | } 55 | 56 | fn write_to_stream(&mut self, stream: &mut S) -> io::Result<()> { 57 | while let Async::Ready(Some(m)) = try!(self.channel_rx.poll()) { 58 | try!(stream.send(m)); 59 | } 60 | 61 | try!(stream.poll()); 62 | 63 | Ok(()) 64 | } 65 | 66 | fn poll_stream(&mut self, stream: ConnStream) -> io::Result> { 67 | Ok(match stream { 68 | ConnStream::NotConnected => { 69 | trace!("not connected to {}", self.addr); 70 | 71 | let delay = Timer::default().sleep(Duration::from_secs(1)) 72 | .map_err(|e| io::Error::new(io::ErrorKind::Other, "timer error")); 73 | 74 | let addr = self.addr.clone(); 75 | let handle = self.handle.clone(); 76 | 77 | /*let mut f = Box::new(delay.and_then(move |_| { 78 | TcpStream::connect(&addr, &handle) 79 | .map(|s| new_line_transport(s)) 80 | .and_then(|s| { 81 | Box::new(Client { service: LineClient::new(s) }) as Box,Error=io::Error>> 82 | }) 83 | }));*/ 84 | 85 | let factory = self.factory.clone(); 86 | 87 | let mut f = Box::new(delay.and_then(move |_| { 88 | TcpStream::connect(&addr, &handle) 89 | .and_then(move |s| futures::done(factory.new_service(s))) 90 | })) 91 | as Box>; 92 | 93 | try!(f.poll().map_err(|e| { 94 | debug!("error connecting 1"); 95 | e 96 | })); 97 | 98 | ConnStream::Connecting::(f) 99 | }, 100 | ConnStream::Connecting(mut f) => { 101 | trace!("connecting to {}", self.addr); 102 | 103 | match f.poll() { 104 | Ok(Async::Ready(mut stream)) => { 105 | match self.write_to_stream(&mut stream) { 106 | Ok(()) => ConnStream::Connected(stream), 107 | Err(e) => { 108 | debug!("error writing to stream 1"); 109 | ConnStream::NotConnected 110 | } 111 | } 112 | }, 113 | Ok(Async::NotReady) => { 114 | ConnStream::Connecting(f) 115 | }, 116 | Err(e) => { 117 | debug!("error connecting 2"); 118 | ConnStream::NotConnected 119 | } 120 | } 121 | }, 122 | ConnStream::Connected(mut stream) => { 123 | trace!("connected to {}", self.addr); 124 | 125 | match self.write_to_stream(&mut stream) { 126 | Ok(()) => ConnStream::Connected(stream), 127 | Err(e) => { 128 | debug!("error writing to stream 2"); 129 | ConnStream::NotConnected 130 | } 131 | } 132 | } 133 | }) 134 | } 135 | } 136 | 137 | impl Future for Conn where S: AsyncService + 'static { 138 | type Item=(); 139 | type Error=io::Error; 140 | 141 | fn poll(&mut self) -> Poll<(), io::Error> { 142 | let taken_stream = self.stream.take().unwrap(); 143 | 144 | self.stream = Some(try!(self.poll_stream(taken_stream))); 145 | 146 | if let Some(ConnStream::NotConnected) = self.stream { 147 | self.stream = Some(try!(self.poll_stream(ConnStream::NotConnected))); 148 | } 149 | 150 | Ok(Async::NotReady) 151 | } 152 | } -------------------------------------------------------------------------------- /archive/recon-gossip/src/framing.rs: -------------------------------------------------------------------------------- 1 | use bytes::*; 2 | use futures::{Async, Poll}; 3 | use std::{io, str}; 4 | use std::io::Write; 5 | use std::fmt; 6 | use tokio_io::AsyncRead; 7 | use tokio_io::codec::{BytesCodec, Framed, Encoder, Decoder}; 8 | 9 | #[derive(Debug, Clone)] 10 | pub enum ReconFrame { 11 | Message(String), 12 | Done, 13 | } 14 | 15 | pub struct Parser; 16 | 17 | impl Decoder for Parser { 18 | type Item=ReconFrame; 19 | type Error=io::Error; 20 | 21 | fn decode(&mut self, buf: &mut Bytes) -> Result, io::Error> { 22 | // If our buffer contains a newline... 23 | trace!("buffer has {} bytes available for decoding", buf.len()); 24 | 25 | if let Some(n) = buf.as_slice().iter().position(|b| *b == b'\n') { 26 | let mut buf = buf.get_mut(); 27 | // remove this line and the newline from the buffer. 28 | let line: Vec = buf.drain(0..n).collect(); 29 | buf.remove(0); 30 | 31 | trace!("buffer has {} bytes remainng after decoding", buf.len()); 32 | 33 | // Turn this data into a UTF string and return it in a Frame. 34 | return match str::from_utf8(&line[..]) { 35 | Ok(s) => Ok(Some(ReconFrame::Message(s.to_string()))), 36 | Err(_) => Err(io::Error::new(io::ErrorKind::Other, "invalid string")) 37 | } 38 | } 39 | 40 | Ok(None) 41 | } 42 | 43 | fn decode_eof(&mut self, buf: &mut Bytes) -> Result { 44 | if buf.len() == 0 { 45 | Ok(ReconFrame::Done) 46 | } else { 47 | Err(io::Error::new(io::ErrorKind::InvalidData, "trailing data found")) 48 | } 49 | } 50 | } 51 | 52 | impl Encoder for Parser { 53 | type Item=ReconFrame; 54 | type Error=io::Error; 55 | 56 | fn encode(&mut self, msg: ReconFrame, buf: &mut Vec) -> Result<(), io::Error> { 57 | match msg { 58 | ReconFrame::Message(text) => { 59 | buf.copy_from_slice(&text.as_bytes()); 60 | buf.copy_from_slice(&['\n' as u8]); 61 | } 62 | ReconFrame::Done => {} 63 | } 64 | 65 | Ok(()) 66 | } 67 | } 68 | 69 | pub type FramedLineTransport = Framed; 70 | 71 | pub fn new_line_transport(inner: T) -> FramedLineTransport 72 | where T: AsyncRead, 73 | { 74 | inner.framed(Parser) 75 | } 76 | -------------------------------------------------------------------------------- /archive/recon-gossip/src/fut.rs: -------------------------------------------------------------------------------- 1 | use futures::*; 2 | use std::cell::RefCell; 3 | use std::sync::Arc; 4 | 5 | pub struct CompletableFuture { 6 | value: Arc>>> 7 | } 8 | 9 | impl Clone for CompletableFuture { 10 | fn clone(&self) -> CompletableFuture { 11 | CompletableFuture { 12 | value: self.value.clone() 13 | } 14 | } 15 | } 16 | 17 | impl CompletableFuture { 18 | pub fn new() -> CompletableFuture { 19 | CompletableFuture { 20 | value: Arc::new(RefCell::new(None)) 21 | } 22 | } 23 | 24 | pub fn resolve(&self, value: T) { 25 | *self.value.borrow_mut() = Some(Ok(value)); 26 | } 27 | 28 | pub fn fail(&self, err: E) { 29 | *self.value.borrow_mut() = Some(Err(err)); 30 | } 31 | } 32 | 33 | impl Future for CompletableFuture { 34 | type Item=T; 35 | type Error=E; 36 | 37 | fn poll(&mut self) -> Poll { 38 | match self.value.borrow_mut().take() { 39 | None => Ok(Async::NotReady), 40 | Some(Ok(v)) => Ok(Async::Ready(v)), 41 | Some(Err(e)) => Err(e) 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /archive/recon-gossip/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![allow(unused_imports)] 2 | 3 | #[macro_use] 4 | extern crate serde_derive; 5 | 6 | extern crate serde; 7 | extern crate serde_json; 8 | 9 | extern crate rand; 10 | extern crate chrono; 11 | extern crate futures; 12 | extern crate futures_mpsc; 13 | extern crate tokio_core; 14 | extern crate tokio_service; 15 | extern crate tokio_proto; 16 | extern crate tokio_timer; 17 | extern crate tokio_io; 18 | extern crate bincode; 19 | extern crate rustc_serialize; 20 | #[macro_use] 21 | extern crate quick_error; 22 | #[macro_use] 23 | extern crate log; 24 | extern crate bytes; 25 | extern crate bytes_more; 26 | 27 | use std::env; 28 | use std::io; 29 | use std::net::SocketAddr; 30 | use std::io::{Read, Write}; 31 | 32 | use futures::Future; 33 | use futures::stream::Stream; 34 | use tokio_core::net::{TcpStream, TcpStreamNew}; 35 | use tokio_core::reactor::Core; 36 | use tokio_io::io::{copy}; 37 | 38 | pub mod framing; 39 | pub mod service; 40 | pub mod client; 41 | pub mod fut; 42 | pub mod switchboard; 43 | pub mod watchdog; 44 | pub mod conn; 45 | pub mod link; 46 | pub mod multiplex; 47 | pub mod upb; 48 | pub mod lpb; 49 | 50 | mod serde_types; 51 | pub use serde_types::*; 52 | 53 | #[cfg(test)] 54 | mod tests { 55 | #[test] 56 | fn it_works() { 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /archive/recon-gossip/src/link.rs: -------------------------------------------------------------------------------- 1 | 2 | use std::io; 3 | use std::time::Duration; 4 | use std::net::SocketAddr; 5 | use tokio_core::reactor::Handle; 6 | use tokio_core::net::TcpStream; 7 | use futures::{Async, Poll, Future, Stream}; 8 | use futures::sync::mpsc::{channel, Sender, Receiver}; 9 | 10 | use ::client::{NewClient, Client, LineClient, NewLineClient}; 11 | use ::framing::{ReconFrame, FramedLineTransport, new_line_transport}; 12 | use ::service::{AsyncService, Funnel, ServerHandle, serve}; 13 | use ::conn::Conn; 14 | use ::switchboard::{Switchboard, ClientImpl}; 15 | use ::{LinkMessage}; 16 | 17 | pub enum Command { 18 | SendMessage(LinkMessage), 19 | AddConnection{ id: String, addr: SocketAddr }, 20 | RemoveConnection{ id: String }, 21 | } 22 | 23 | pub struct Link { 24 | switchboard: Switchboard, 25 | channel_tx: Sender, 26 | channel_rx: Receiver, 27 | server_rx: Receiver, 28 | server_handle: ServerHandle, 29 | } 30 | 31 | impl Link { 32 | pub fn new(handle: Handle, id: String, addr: SocketAddr, conn_factory: F) -> io::Result 33 | where F: NewClient + Clone + 'static { 34 | 35 | let (server_tx, server_rx) = try!(channel(&handle)); 36 | let (tx, rx) = try!(channel(&handle)); 37 | let server_handle = try!(serve(&handle, addr, Funnel::new(server_tx))); 38 | 39 | Ok(Link { 40 | switchboard: try!(Switchboard::new(handle, id, conn_factory)), 41 | channel_tx: tx, 42 | channel_rx: rx, 43 | server_rx: server_rx, 44 | server_handle: server_handle, 45 | }) 46 | } 47 | 48 | pub fn channel(&self) -> Sender { 49 | self.channel_tx.clone() 50 | } 51 | 52 | fn handle(&mut self, cmd: Command) -> io::Result<()> { 53 | match cmd { 54 | Command::SendMessage(m) => self.switchboard.send(m), 55 | Command::AddConnection{id, addr} => self.switchboard.add(id, addr), 56 | Command::RemoveConnection{id} => self.switchboard.remove(id), 57 | } 58 | } 59 | 60 | pub fn add(&self, id: S, addr: SocketAddr) -> io::Result<()> where S: Into { 61 | self.switchboard.add(id.into(), addr) 62 | } 63 | 64 | pub fn remove(&self, id: &str) -> io::Result<()> { 65 | self.switchboard.remove(id) 66 | } 67 | 68 | pub fn send(&self, msg: LinkMessage) -> io::Result<()> { 69 | self.switchboard.send(msg) 70 | } 71 | } 72 | 73 | impl Stream for Link { 74 | type Item=String; 75 | type Error=io::Error; 76 | 77 | fn poll(&mut self) -> Poll, Self::Error> { 78 | try!(self.switchboard.poll()); 79 | 80 | while let Async::Ready(Some(cmd)) = try!(self.channel_rx.poll()) { 81 | try!(self.handle(cmd)) 82 | } 83 | 84 | self.server_rx.poll() 85 | } 86 | } -------------------------------------------------------------------------------- /archive/recon-gossip/src/lpb.rs: -------------------------------------------------------------------------------- 1 | use std::sync::{Arc, Mutex}; 2 | use std::cell::RefCell; 3 | use std::io; 4 | use std::collections::{VecDeque,HashMap,HashSet}; 5 | use std::net::SocketAddr; 6 | use std::marker::PhantomData; 7 | use tokio_core::reactor::Handle; 8 | use tokio_core::net::TcpStream; 9 | use tokio_timer::Timer; 10 | use futures::{self, Future, Poll, Async, Stream}; 11 | use futures::sync::mpsc::{channel, Sender, Receiver}; 12 | use serde_json; 13 | use serde::{Serialize, Deserialize}; 14 | use serde::de::DeserializeOwned; 15 | use rand::{self, Rng}; 16 | use chrono::*; 17 | 18 | use super::client::{NewClient, Client, LineClient, NewLineClient}; 19 | use super::framing::FramedLineTransport; 20 | use super::fut::*; 21 | use super::service::*; 22 | use super::conn::*; 23 | use super::{LinkMessage, LpbMessage, LpbLinkMessage, UpbMessage}; 24 | use super::multiplex; 25 | use super::upb::{self, NodeId, MessageId, UnreliableProbabilisticBroadcast}; 26 | 27 | /// The output type of this broadcast algorithm 28 | #[derive(Debug, Clone, PartialEq)] 29 | pub struct Message where T: Serialize + DeserializeOwned + Clone + Send + 'static { 30 | from: NodeId, 31 | seq: u64, 32 | value: T 33 | } 34 | 35 | /// Input commands 36 | pub enum Command where T: Serialize + DeserializeOwned + Clone + Send + 'static { 37 | Broadcast { msg: T } 38 | } 39 | 40 | pub struct LazyProbabilisticBroadcast where T: Serialize + DeserializeOwned + Clone + Send + 'static { 41 | id: NodeId, 42 | peers: HashSet, 43 | link: Sender, 44 | k: usize, // fan-out factor 45 | rounds: u32, // message ttl 46 | alpha: f32, // probability of storing message 47 | delta: Duration, 48 | multiplex_key: String, 49 | link_rx: Receiver, 50 | command_tx: Sender>, 51 | command_rx: Receiver>, 52 | to_deliver: VecDeque>, 53 | pending: HashMap>, 54 | seq: u64, // lpb's message sequence number 55 | stored: HashMap>, 56 | next_seq: HashMap, // sequence numbers for peers 57 | upb: UnreliableProbabilisticBroadcast, 58 | upb_tx: Sender>, 59 | timers: Option>>>, 60 | } 61 | 62 | impl LazyProbabilisticBroadcast where T: Serialize + DeserializeOwned + Clone + Send + 'static { 63 | pub fn new(handle: Handle, id: NodeId, peers: HashSet, 64 | link: Sender, k: usize, rounds: u32, alpha: f32, delta: Duration, multiplex_key: String, upb_gc_age: Duration) -> io::Result> { 65 | let (tx, rx) = try!(channel(&handle)); 66 | let (command_tx, command_rx) = try!(channel(&handle)); 67 | 68 | try!(link.send(multiplex::Command::Subscribe { key: multiplex_key.clone(), sender: tx })); 69 | 70 | let upb_impl = try!(UnreliableProbabilisticBroadcast::new( 71 | handle.clone(), 72 | id.clone(), 73 | peers.clone(), 74 | link.clone(), 75 | k, rounds, 76 | format!("{}/upb", multiplex_key.clone()), 77 | upb_gc_age 78 | )); 79 | 80 | let upb_tx = upb_impl.channel(); 81 | 82 | Ok(LazyProbabilisticBroadcast { 83 | id: id, 84 | peers: peers, 85 | k: k, 86 | rounds: rounds, 87 | alpha: alpha, 88 | delta: delta, 89 | multiplex_key: multiplex_key, 90 | link: link, 91 | link_rx: rx, 92 | command_tx: command_tx, 93 | command_rx: command_rx, 94 | to_deliver: VecDeque::new(), 95 | pending: HashMap::new(), 96 | seq: 0, 97 | stored: HashMap::new(), 98 | next_seq: HashMap::new(), 99 | upb: upb_impl, 100 | upb_tx: upb_tx, 101 | timers: Some(vec![]), 102 | }) 103 | } 104 | 105 | pub fn channel(&self) -> Sender> { 106 | self.command_tx.clone() 107 | } 108 | 109 | fn gossip(&self, gm: LpbLinkMessage) -> io::Result<()> { 110 | debug!("gossipping to {} targets: {:?}", self.k, gm); 111 | 112 | for t in self.picktargets(self.k) { 113 | debug!("gossipping {:?} to {}", gm, t); 114 | 115 | try!(self.send(t.to_owned(), gm.clone())); 116 | } 117 | 118 | Ok(()) 119 | } 120 | 121 | pub fn broadcast(&self, value: T) -> io::Result<()> { 122 | self.command_tx.send(Command::Broadcast {msg: value}) 123 | } 124 | 125 | fn do_broadcast(&mut self, msg: T) -> io::Result<()> { 126 | let value = serde_json::to_value(msg); 127 | let msg_seq = self.next_msg_seq(); 128 | let lm = LpbMessage::Data { seq: msg_seq, value: value }; 129 | 130 | self.upb_tx.send(upb::Command::Broadcast { msg: lm }) 131 | } 132 | 133 | fn handle_upb_broadcast(&mut self, from: String, seq: u64, value: serde_json::Value, ttl: u32) -> io::Result<()> { 134 | let lpb_msg = try!(serde_json::from_value(value) 135 | .map_err(|e| io::Error::new(io::ErrorKind::Other, "json decoding error"))); 136 | 137 | match lpb_msg { 138 | LpbMessage::Data { seq: lpb_seq, value: lpb_value } => { 139 | try!(self.handle_upb_data(from, seq, lpb_seq, lpb_value)); 140 | } 141 | } 142 | 143 | Ok(()) 144 | } 145 | 146 | fn handle_upb_data(&mut self, from: String, upb_seq: u64, lpb_seq: u64, value: serde_json::Value) -> io::Result<()> { 147 | let msg_id = MessageId(from.clone(), lpb_seq); 148 | let typed_value: T = try!(serde_json::from_value(value) 149 | .map_err(|e| io::Error::new(io::ErrorKind::Other, "json decoding error"))); 150 | 151 | let dm = Message { 152 | from: from.clone(), 153 | seq: lpb_seq, 154 | value: typed_value.clone(), 155 | }; 156 | 157 | let mut rng = rand::thread_rng(); 158 | if rng.gen_range(0f32, 1f32) > self.alpha { 159 | self.stored.insert(msg_id.clone(), dm.clone()); 160 | } 161 | 162 | let next_seq = self.next_seq.entry(from.clone()).or_insert(1).to_owned(); 163 | if lpb_seq == next_seq { 164 | self.next_seq.insert(from.clone(), next_seq + 1); 165 | 166 | self.to_deliver.push_back(dm); 167 | } else if lpb_seq > next_seq { 168 | self.pending.insert(msg_id.clone(), dm); 169 | 170 | for i in next_seq..lpb_seq { 171 | if !self.pending.contains_key(&MessageId(from.clone(), i)) { 172 | try!(self.gossip(LpbLinkMessage::Request { 173 | from: self.id.clone(), 174 | msg_from: from.clone(), 175 | msg_seq: i, 176 | ttl: self.rounds - 1 177 | })); 178 | } 179 | } 180 | 181 | self.timers.as_mut().unwrap().push(try!(Self::set_timeout(self.delta, msg_id))); 182 | } 183 | 184 | Ok(()) 185 | } 186 | 187 | fn handle_timeout(&mut self, msg_id: &MessageId) -> io::Result<()> { 188 | 189 | let mut next_seq = *self.next_seq.get(&msg_id.0).unwrap(); 190 | 191 | while msg_id.1 >= next_seq { 192 | debug!("timeout waiting for {:?}. next_seq = {}", msg_id, next_seq); 193 | 194 | if ! try!(self.deliver_pending(&msg_id.0)) { 195 | debug!("skipped over missing message {} {}", msg_id.0, next_seq); 196 | } 197 | 198 | next_seq += 1; 199 | self.next_seq.insert(msg_id.0.clone(), next_seq); 200 | } 201 | 202 | Ok(()) 203 | } 204 | 205 | fn set_timeout(interval: Duration, msg_id: MessageId) -> Result>, io::Error> { 206 | let std_interval = try!(interval.to_std() 207 | .map_err(|e| io::Error::new(io::ErrorKind::Other, "duration out of range"))); 208 | 209 | Ok(Box::new(Timer::default() 210 | .sleep(std_interval) 211 | .map_err(|_| io::Error::new(io::ErrorKind::Other, "timer error")) 212 | .map(|_| msg_id) 213 | ) as Box>) 214 | } 215 | 216 | fn handle_lpb_request(&mut self, from: String, msg_from: String, msg_seq: u64, ttl: u32) -> io::Result<()> { 217 | if let Some(dm) = self.stored.get(&MessageId(msg_from.clone(), msg_seq)) { 218 | debug!("have data to fulfil request for {}, {}", msg_from, msg_seq); 219 | try!(self.send(from, LpbLinkMessage::Data { 220 | from: dm.from.clone(), 221 | seq: dm.seq, 222 | value: serde_json::to_value(dm.value.clone()), 223 | })); 224 | } else if ttl > 0 { 225 | debug!("relaying request for {}, {}", msg_from, msg_seq); 226 | try!(self.gossip(LpbLinkMessage::Request { 227 | from: from, 228 | msg_from: msg_from, 229 | msg_seq: msg_seq, 230 | ttl: ttl - 1 231 | })); 232 | } 233 | 234 | Ok(()) 235 | } 236 | 237 | fn handle_lpb_data(&mut self, from: String, seq: u64, value: serde_json::Value) -> io::Result<()> { 238 | let msg_id = MessageId(from.clone(), seq); 239 | let typed_value = try!(serde_json::from_value(value) 240 | .map_err(|e| io::Error::new(io::ErrorKind::Other, "json decoding error"))); 241 | self.pending.insert(msg_id, Message { from: from.clone(), seq: seq, value: typed_value }); 242 | 243 | while try!(self.deliver_pending(&from)) { 244 | // pass 245 | } 246 | 247 | Ok(()) 248 | } 249 | 250 | fn deliver_pending(&mut self, node_id: &str) -> io::Result { 251 | if let Some(next_seq) = self.next_seq.get_mut(node_id) { 252 | let key = MessageId(node_id.to_owned(), *next_seq); 253 | if let Some(msg) = self.pending.remove(&key) { 254 | *next_seq += 1; 255 | self.to_deliver.push_back(msg); 256 | debug!("delivered pending {:?}", key); 257 | return Ok(true); 258 | } 259 | } 260 | 261 | Ok(false) 262 | } 263 | 264 | fn send(&self, to: NodeId, lm: LpbLinkMessage) -> io::Result<()> { 265 | let lm_value = serde_json::to_value(lm); 266 | 267 | let mm = multiplex::Message::new(Some(to), self.id.clone(), self.multiplex_key.clone(), lm_value); 268 | 269 | self.link.send(multiplex::Command::SendMessage { msg: mm }) 270 | } 271 | 272 | pub fn picktargets<'a>(&'a self, k: usize) -> Vec<&'a NodeId> { 273 | let mut rng = rand::thread_rng(); 274 | let candidates = self.peers.iter() 275 | .filter(|i| *i != &self.id); 276 | rand::sample(&mut rng, candidates, k) 277 | } 278 | 279 | fn next_msg_seq(&mut self) -> u64 { 280 | self.seq += 1; 281 | self.seq 282 | } 283 | 284 | fn decode_link_message(&self, link_msg: multiplex::Message) -> io::Result { 285 | let gm: LpbLinkMessage = try!(serde_json::from_value(link_msg.body) 286 | .map_err(|e| io::Error::new(io::ErrorKind::Other, "json decoding error"))); 287 | 288 | Ok(gm) 289 | } 290 | } 291 | 292 | impl Stream for LazyProbabilisticBroadcast where T: Serialize + DeserializeOwned + Clone + Send + 'static { 293 | type Item=Message; 294 | type Error=io::Error; 295 | 296 | fn poll(&mut self) -> Poll, Self::Error> { 297 | while let Async::Ready(Some(cmd)) = try!(self.command_rx.poll()) { 298 | match cmd { 299 | Command::Broadcast {msg} => { 300 | try!(self.do_broadcast(msg)); 301 | } 302 | } 303 | } 304 | 305 | while let Async::Ready(Some(upb::Message { from, seq: upb_seq, value: lpb_msg })) = try!(self.upb.poll()) { 306 | debug!("gossip received broadcast message from: {} {:?}", from, lpb_msg); 307 | 308 | match lpb_msg { 309 | LpbMessage::Data { seq: lpb_seq, value } => { 310 | try!(self.handle_upb_data(from, upb_seq, lpb_seq, value)); 311 | } 312 | } 313 | } 314 | 315 | while let Async::Ready(Some(link_msg)) = try!(self.link_rx.poll()) { 316 | debug!("gossip received link message: {:?}", link_msg); 317 | 318 | let gm = try!(self.decode_link_message(link_msg)); 319 | 320 | match gm { 321 | LpbLinkMessage::Data { from, seq, value } => { 322 | try!(self.handle_lpb_data(from, seq, value)); 323 | }, 324 | LpbLinkMessage::Request { from, msg_from, msg_seq, ttl } => { 325 | try!(self.handle_lpb_request(from, msg_from, msg_seq, ttl)); 326 | } 327 | } 328 | } 329 | 330 | let mut new_timers: Vec>> = vec![]; 331 | let mut old_timers = self.timers.take().unwrap(); 332 | for mut t in old_timers.drain(0..) { 333 | if let Async::Ready(msg_id) = try!(t.poll()) { 334 | try!(self.handle_timeout(&msg_id)); 335 | } else { 336 | new_timers.push(t); 337 | } 338 | } 339 | self.timers = Some(new_timers); 340 | 341 | Ok(match self.to_deliver.pop_front() { 342 | Some(v) => Async::Ready(Some(v)), 343 | None => Async::NotReady, 344 | }) 345 | } 346 | } -------------------------------------------------------------------------------- /archive/recon-gossip/src/multiplex.rs: -------------------------------------------------------------------------------- 1 | 2 | use std::io; 3 | use std::time::Duration; 4 | use std::collections::HashMap; 5 | use std::net::SocketAddr; 6 | use tokio_core::reactor::Handle; 7 | use tokio_core::net::TcpStream; 8 | use futures::{Async, Poll, Future, Stream}; 9 | use futures::sync::mpsc::{channel, Sender, Receiver}; 10 | use serde_json::{self, Value}; 11 | 12 | use ::client::{NewClient, Client, LineClient, NewLineClient}; 13 | use ::framing::{ReconFrame, FramedLineTransport, new_line_transport}; 14 | use ::service::{AsyncService, Funnel, serve}; 15 | use ::conn::Conn; 16 | use ::switchboard::{self, Switchboard, ClientImpl}; 17 | use ::link::{Link}; 18 | use ::{LinkMessage, MultiplexMessage}; 19 | 20 | #[derive(Debug, Clone)] 21 | pub struct Message { 22 | pub to: Option, 23 | pub from: String, 24 | pub key: String, 25 | pub body: Value, 26 | } 27 | 28 | impl Message { 29 | pub fn new(to: Option, from: String, module: M, body: Value) -> Message 30 | where M: Into { 31 | Message { 32 | to: to, 33 | from: from, 34 | key: module.into(), 35 | body: body, 36 | } 37 | } 38 | 39 | } 40 | 41 | pub enum Command { 42 | Subscribe {key: String, sender: Sender }, 43 | Unsubscribe {key: String}, 44 | SendMessage {msg: Message}, 45 | AddConnection{ id: String, addr: SocketAddr }, 46 | RemoveConnection{ id: String }, 47 | } 48 | 49 | pub struct MultiplexLink { 50 | id: String, 51 | link: Link, 52 | subscribers: HashMap>, 53 | channel_tx: Sender, 54 | channel_rx: Receiver, 55 | } 56 | 57 | impl MultiplexLink { 58 | pub fn new(handle: Handle, id: String, addr: SocketAddr, conn_factory: F) -> io::Result 59 | where F: NewClient + Clone + 'static { 60 | 61 | let (tx, rx) = try!(channel(&handle)); 62 | 63 | Ok(MultiplexLink { 64 | id: id.clone(), 65 | link: try!(Link::new(handle, id, addr, conn_factory)), 66 | subscribers: HashMap::new(), 67 | channel_tx: tx, 68 | channel_rx: rx, 69 | }) 70 | } 71 | 72 | pub fn channel(&self) -> Sender { 73 | self.channel_tx.clone() 74 | } 75 | 76 | // TODO: do encoding and decoding better 77 | 78 | fn encode(&self, msg: Message) -> io::Result { 79 | let mm = serde_json::to_value(MultiplexMessage::new(msg.key, msg.body)); 80 | Ok(LinkMessage::new(msg.to, msg.from, mm)) 81 | } 82 | 83 | fn decode<'a>(&self, msg: &'a str) -> io::Result { 84 | let lm: LinkMessage = try!(serde_json::from_str(msg) 85 | .map_err(|e| io::Error::new(io::ErrorKind::Other, "json from_value error"))); 86 | let mm: MultiplexMessage = try!(serde_json::from_value(lm.body) 87 | .map_err(|e| io::Error::new(io::ErrorKind::Other, "json from_value error"))); 88 | 89 | Ok(Message::new(lm.to, lm.from, mm.key, mm.body)) 90 | } 91 | 92 | fn dispatch(&mut self, msg: String) -> io::Result<()> { 93 | let msg = try!(self.decode(&msg)); 94 | 95 | if let Some(chan) = self.subscribers.get(&msg.key) { 96 | chan.send(msg) 97 | } else { 98 | debug!("unhandle message {:?}", msg); 99 | 100 | Ok(()) 101 | } 102 | } 103 | 104 | pub fn send(&self, msg: Message) -> io::Result<()> { 105 | self.do_send(msg) 106 | } 107 | 108 | fn do_send(&self, msg: Message) -> io::Result<()> { 109 | let encoded_msg = try!(self.encode(msg)); 110 | 111 | self.link.send(encoded_msg) 112 | } 113 | 114 | pub fn subscribe(&self, key: S, chan: Sender) -> io::Result<()> where S: Into { 115 | self.channel_tx.send(Command::Subscribe{key: key.into(), sender: chan}) 116 | } 117 | 118 | fn do_subscribe(&mut self, key: String, chan: Sender) -> io::Result<()> { 119 | self.subscribers.insert(key, chan); 120 | Ok(()) 121 | } 122 | 123 | pub fn unsubscribe(&self, key: S) -> io::Result<()> where S: Into { 124 | self.channel_tx.send(Command::Unsubscribe{key: key.into()}) 125 | } 126 | 127 | fn do_unsubscribe(&mut self, key: String) -> io::Result<()> { 128 | self.subscribers.remove(&key); 129 | Ok(()) 130 | } 131 | 132 | pub fn add(&self, id: S, addr: SocketAddr) -> io::Result<()> where S: Into { 133 | self.link.add(id.into(), addr) 134 | } 135 | 136 | pub fn remove(&self, id: &str) -> io::Result<()> { 137 | self.link.remove(id) 138 | } 139 | 140 | fn handle(&mut self, cmd: Command) -> io::Result<()> { 141 | match cmd { 142 | Command::SendMessage {msg} => self.do_send(msg), 143 | Command::Subscribe {key, sender} => self.do_subscribe(key, sender), 144 | Command::Unsubscribe {key} => self.do_unsubscribe(key), 145 | Command::AddConnection{id, addr} => self.add(id, addr), 146 | Command::RemoveConnection{id} => self.remove(&id), 147 | } 148 | } 149 | } 150 | 151 | impl Future for MultiplexLink { 152 | type Item=(); 153 | type Error=io::Error; 154 | 155 | fn poll(&mut self) -> Poll { 156 | while let Async::Ready(Some(msg)) = try!(self.link.poll()) { 157 | try!(self.dispatch(msg)); 158 | } 159 | 160 | while let Async::Ready(Some(cmd)) = try!(self.channel_rx.poll()) { 161 | try!(self.handle(cmd)); 162 | } 163 | 164 | Ok(Async::NotReady) 165 | } 166 | } -------------------------------------------------------------------------------- /archive/recon-gossip/src/serde_types.rs: -------------------------------------------------------------------------------- 1 | use serde_json::Value; 2 | 3 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] 4 | pub struct LinkMessage { 5 | pub to: Option, 6 | pub from: String, 7 | pub body: Value, 8 | } 9 | 10 | impl LinkMessage { 11 | pub fn new(to: Option, from: String, body: Value) -> LinkMessage { 12 | LinkMessage { 13 | to: to, 14 | from: from, 15 | body: body, 16 | } 17 | } 18 | } 19 | 20 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] 21 | pub struct MultiplexMessage { 22 | pub key: String, 23 | pub body: Value, 24 | } 25 | 26 | impl MultiplexMessage { 27 | pub fn new(key: String, body: Value) -> MultiplexMessage { 28 | MultiplexMessage { 29 | key: key, 30 | body: body, 31 | } 32 | } 33 | } 34 | 35 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] 36 | pub enum WatchdogMessage { 37 | Ping { seq: u32 }, 38 | Pong { seq: u32 }, 39 | } 40 | 41 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] 42 | pub enum UpbMessage { 43 | Broadcast { from: String, seq: u64, value: Value, ttl: u32 }, 44 | } 45 | 46 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] 47 | pub enum LpbMessage { 48 | Data { seq: u64, value: Value }, 49 | } 50 | 51 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] 52 | pub enum LpbLinkMessage { 53 | Data { from: String, seq: u64, value: Value }, 54 | Request { from: String, msg_from: String, msg_seq: u64, ttl: u32 }, 55 | } 56 | -------------------------------------------------------------------------------- /archive/recon-gossip/src/service.rs: -------------------------------------------------------------------------------- 1 | use tokio_core::reactor::Handle; 2 | use futures::{self, Async, Future, Stream, Sink, Poll}; 3 | use futures::sync::mpsc::{channel, Sender, Receiver}; 4 | use std::io; 5 | use std::net::{SocketAddr, TcpListener}; 6 | use ::framing::*; 7 | 8 | pub trait AsyncService { 9 | type Request; 10 | type Error; 11 | fn send(&self, req: Self::Request) -> io::Result<()>; 12 | fn poll(&mut self) -> Poll<(), Self::Error>; 13 | } 14 | 15 | impl Future for AsyncService { 16 | type Item=(); 17 | type Error=E; 18 | 19 | fn poll(&mut self) -> Poll { 20 | AsyncService::poll(self) 21 | } 22 | } 23 | 24 | pub trait NewService { 25 | type Request; 26 | type Error; 27 | type Item; 28 | 29 | fn new_service(&self) -> io::Result; 30 | } 31 | 32 | pub trait Service { 33 | /// Requests handled by the service. 34 | type Request; 35 | 36 | /// Errors produced by the service. 37 | type Error; 38 | 39 | /// The future response value. 40 | type Future: Future; 41 | 42 | /// Process the request and return the response asynchronously. 43 | fn call(&self, req: Self::Request) -> Self::Future; 44 | 45 | /// Returns `Async::Ready` when the service is ready to accept a request. 46 | fn poll_ready(&self) -> Async<()>; 47 | } 48 | 49 | #[derive(Clone)] 50 | pub struct Funnel { 51 | channel: Sender, 52 | } 53 | 54 | impl Funnel { 55 | pub fn new(chan: Sender) -> Funnel { 56 | Funnel { 57 | channel: chan 58 | } 59 | } 60 | } 61 | 62 | impl NewService for Funnel { 63 | type Request = String; 64 | type Error = io::Error; 65 | type Item = Self; 66 | 67 | fn new_service(&self) -> io::Result { 68 | Ok(self.clone()) 69 | } 70 | } 71 | 72 | impl Service for Funnel { 73 | type Request = String; 74 | type Error = io::Error; 75 | type Future = Box>; 76 | 77 | fn call(&self, req: String) -> Self::Future { 78 | // don't get Sink::send mixed up with Sender::send 79 | let res: io::Result<()> = Sender::send(&self.channel, req); 80 | 81 | Box::new(futures::done(res)) 82 | } 83 | 84 | fn poll_ready(&self) -> Async<()> { 85 | Async::Ready(()) 86 | } 87 | } 88 | 89 | pub struct Server where S: Service, 90 | T: Stream + Sink { 91 | service: S, 92 | transport: T 93 | } 94 | 95 | impl Server where S: Service, 96 | T: Stream + Sink { 97 | pub fn new(service: S, transport: T) -> Server { 98 | Server { 99 | service: service, 100 | transport: transport, 101 | } 102 | } 103 | } 104 | 105 | impl Future for Server 106 | where T: Stream + Sink, 107 | S: Service + 'static 108 | { 109 | type Item = (); 110 | type Error = io::Error; 111 | 112 | fn poll(&mut self) -> Poll<(), io::Error> { 113 | loop { 114 | match self.transport.poll() { 115 | Ok(Async::Ready(None)) => { 116 | info!("transport eof"); 117 | return Ok(Async::Ready(())) 118 | }, 119 | Ok(Async::Ready(Some(item))) => { 120 | match item { 121 | ReconFrame::Message(s) => { 122 | trace!("calling service with frame {:?}", s); 123 | self.service.call(s); 124 | }, 125 | ReconFrame::Done => { 126 | debug!("service got end frame"); 127 | return Ok(Async::Ready(())) 128 | } 129 | } 130 | }, 131 | Ok(Async::NotReady) => { 132 | return Ok(Async::NotReady) 133 | }, 134 | Err(e) => { 135 | return Err(e) 136 | } 137 | } 138 | } 139 | } 140 | } 141 | 142 | pub struct ServerHandle { 143 | 144 | } 145 | 146 | pub fn listen(handle: &Handle, addr: SocketAddr, f: F) -> Result where F: FnMut<(), ()> { 147 | let listener = TcpListener::bind(addr)?; 148 | 149 | Ok(ServerHandle{}) 150 | } 151 | 152 | /// Serve a service up. Secret sauce here is 'NewService', a helper that must be able to create a 153 | /// new 'Service' for each connection that we receive. 154 | pub fn serve(handle: &Handle, addr: SocketAddr, new_service: T) 155 | -> io::Result 156 | where T: NewService + Send + 'static, 157 | S: Service + 'static, 158 | S::Future: Future 159 | { 160 | let server_handle = try!(listen(handle, addr, move |stream| { 161 | // Initialize the pipeline dispatch with the service and the line 162 | // transport 163 | let service = try!(new_service.new_service()); 164 | Ok(Server::new(service, new_line_transport(stream))) 165 | })); 166 | 167 | Ok(server_handle) 168 | } -------------------------------------------------------------------------------- /archive/recon-gossip/src/switchboard.rs: -------------------------------------------------------------------------------- 1 | use std::sync::{Arc, Mutex}; 2 | use std::cell::RefCell; 3 | use std::io; 4 | use std::collections::{VecDeque,HashMap}; 5 | use std::net::SocketAddr; 6 | use std::time::Duration; 7 | use tokio_core::reactor::Handle; 8 | use tokio_core::net::TcpStream; 9 | use futures::{self, Future, Poll, Async, Stream}; 10 | use futures::sync::mpsc::{channel, Sender, Receiver}; 11 | use serde_json; 12 | 13 | use super::client::{NewClient, Client, LineClient, NewLineClient}; 14 | use super::framing::FramedLineTransport; 15 | use super::fut::*; 16 | use super::service::*; 17 | use super::conn::*; 18 | use super::{LinkMessage}; 19 | 20 | pub type ClientImpl = Client>>; 21 | pub type ConnImpl = Conn; 22 | pub type ConnFactory = NewClient; 23 | 24 | pub struct SwitchboardConnection { 25 | conn: ConnImpl, 26 | pub sender: Sender, 27 | } 28 | 29 | impl SwitchboardConnection { 30 | pub fn new(conn: ConnImpl) -> SwitchboardConnection { 31 | SwitchboardConnection { 32 | sender: conn.channel(), 33 | conn: conn, 34 | } 35 | } 36 | } 37 | 38 | #[derive(Debug, Clone, PartialEq)] 39 | pub struct Message { 40 | pub to: Option, 41 | pub body: String, 42 | } 43 | 44 | impl Message { 45 | pub fn new(to: S, body: T) -> Message where S: Into>, T: Into { 46 | Message { 47 | to: to.into(), 48 | body: body.into(), 49 | } 50 | } 51 | } 52 | 53 | pub enum Command { 54 | SendMessage(LinkMessage), 55 | AddConnection{ id: String, addr: SocketAddr }, 56 | RemoveConnection{ id: String }, 57 | } 58 | 59 | pub struct Switchboard { 60 | id: String, 61 | handle: Handle, 62 | connections: HashMap, 63 | channel_tx: Sender, 64 | channel_rx: Receiver, 65 | conn_factory: Box>, 66 | } 67 | 68 | impl Switchboard { 69 | pub fn new(handle: Handle, id: String, conn_factory: F) -> io::Result where F: NewClient + Clone + 'static { 70 | let (tx, rx) = try!(channel(&handle)); 71 | 72 | Ok(Switchboard { 73 | id: id, 74 | handle: handle, 75 | connections: HashMap::new(), 76 | channel_tx: tx, 77 | channel_rx: rx, 78 | conn_factory: Box::new(conn_factory), 79 | }) 80 | } 81 | 82 | pub fn channel(&self) -> Sender { 83 | self.channel_tx.clone() 84 | } 85 | 86 | pub fn connection_ids(&self) -> Vec { 87 | self.connections.keys().map(|s| s.to_owned()).collect() 88 | } 89 | 90 | pub fn do_broadcast(&self, msg: LinkMessage) -> io::Result<()> { 91 | let json_msg = try!(self.encode(&msg)); 92 | 93 | for c in self.connections.values() { 94 | try!(c.sender.send(json_msg.clone())); 95 | } 96 | 97 | Ok(()) 98 | } 99 | 100 | pub fn send(&self, msg: LinkMessage) -> io::Result<()> { 101 | self.channel_tx.send(Command::SendMessage(msg)) 102 | } 103 | 104 | fn do_send(&self, msg: LinkMessage) -> io::Result<()> { 105 | if let Some(ref to) = msg.to { 106 | match self.connections.get(to) { 107 | Some(conn) => { 108 | try!(conn.sender.send(try!(self.encode(&msg)))); 109 | }, 110 | None => { 111 | debug!("no connection registered with switchboard id {} for msg {:?}", to, msg); 112 | } 113 | } 114 | } else { 115 | try!(self.do_broadcast(msg)); 116 | } 117 | 118 | Ok(()) 119 | } 120 | 121 | fn encode(&self, msg: &LinkMessage) -> io::Result { 122 | serde_json::to_string(&msg).map_err(|e| { 123 | io::Error::new(io::ErrorKind::InvalidInput, "json encoding error") 124 | }) 125 | } 126 | 127 | pub fn add(&self, client_id: S, addr: SocketAddr) -> io::Result<()> where S: Into { 128 | self.channel_tx.send(Command::AddConnection{id: client_id.into(), addr: addr}) 129 | } 130 | 131 | fn do_add(&mut self, client_id: String, addr: SocketAddr) -> io::Result<()> { 132 | let conn = try!(Conn::new(self.handle.clone(), addr, self.conn_factory.clone())); 133 | 134 | self.connections.insert(client_id.into(), SwitchboardConnection::new(conn)); 135 | 136 | Ok(()) 137 | } 138 | 139 | pub fn remove(&self, client_id: S) -> io::Result<()> where S: Into { 140 | self.channel_tx.send(Command::RemoveConnection{id: client_id.into()}) 141 | } 142 | 143 | fn do_remove(&mut self, client_id: &str) -> io::Result<()> { 144 | self.connections.remove(client_id); 145 | 146 | Ok(()) 147 | } 148 | 149 | fn handle(&mut self, command: Command) -> io::Result<()> { 150 | match command { 151 | Command::SendMessage(msg) => try!(self.do_send(msg)), 152 | Command::AddConnection{id, addr} => try!(self.do_add(id, addr)), 153 | Command::RemoveConnection{id} => try!(self.do_remove(&id)), 154 | } 155 | 156 | Ok(()) 157 | } 158 | } 159 | 160 | impl Future for Switchboard { 161 | type Item=(); 162 | type Error=io::Error; 163 | 164 | fn poll(&mut self) -> Poll<(), io::Error> { 165 | while let Async::Ready(Some(command)) = try!(self.channel_rx.poll()) { 166 | try!(self.handle(command)); 167 | } 168 | 169 | for mut c in self.connections.values_mut() { 170 | try!(c.conn.poll()); 171 | } 172 | 173 | Ok(Async::NotReady) 174 | } 175 | } -------------------------------------------------------------------------------- /archive/recon-gossip/src/upb.rs: -------------------------------------------------------------------------------- 1 | use std::sync::{Arc, Mutex}; 2 | use std::cell::RefCell; 3 | use std::io; 4 | use std::collections::{VecDeque,HashMap,HashSet}; 5 | use std::net::SocketAddr; 6 | use std::marker::PhantomData; 7 | use tokio_core::reactor::Handle; 8 | use tokio_core::net::TcpStream; 9 | use futures::{self, Future, Poll, Async, Stream}; 10 | use futures::sync::mpsc::{channel, Sender, Receiver}; 11 | use serde_json; 12 | use serde::{Serialize, Deserialize}; 13 | use serde::de::DeserializeOwned; 14 | use rand; 15 | use chrono::{DateTime, Duration, Utc}; 16 | 17 | use super::client::{NewClient, Client, LineClient, NewLineClient}; 18 | use super::framing::FramedLineTransport; 19 | use super::fut::*; 20 | use super::service::*; 21 | use super::conn::*; 22 | use super::{LinkMessage, UpbMessage}; 23 | use super::multiplex; 24 | 25 | pub type NodeId = String; 26 | 27 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] 28 | pub struct MessageId(pub NodeId, pub u64); 29 | 30 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] 31 | pub struct Delivery { 32 | pub message_id: MessageId, 33 | pub delivered_at: DateTime, 34 | } 35 | 36 | impl Delivery { 37 | pub fn new(sender: NodeId, seq: u64) -> Delivery { 38 | Delivery { 39 | message_id: MessageId(sender, seq), 40 | delivered_at: Utc::now(), 41 | } 42 | } 43 | } 44 | 45 | pub enum Command where T: Serialize + DeserializeOwned + Clone + Send + 'static { 46 | Broadcast { msg: T } 47 | } 48 | 49 | #[derive(Debug, Clone, PartialEq)] 50 | pub struct Message where T: Serialize + DeserializeOwned + Clone + Send + 'static { 51 | pub from: NodeId, 52 | pub seq: u64, 53 | pub value: T, 54 | } 55 | 56 | pub struct UnreliableProbabilisticBroadcast where T: Serialize + DeserializeOwned + Clone + Send + 'static { 57 | id: NodeId, 58 | delivered: HashSet, 59 | delivered_gc_age: Duration, 60 | peers: HashSet, 61 | link: Sender, 62 | k: usize, 63 | multiplex_key: String, 64 | link_rx: Receiver, 65 | command_tx: Sender>, 66 | command_rx: Receiver>, 67 | rounds: u32, 68 | pending: VecDeque>, 69 | seq: u64, 70 | } 71 | 72 | impl UnreliableProbabilisticBroadcast where T: Serialize + DeserializeOwned + Clone + Send + 'static { 73 | pub fn new(handle: Handle, id: NodeId, peers: HashSet, 74 | link: Sender, k: usize, rounds: u32, multiplex_key: String, delivered_gc_age: Duration) -> io::Result> { 75 | let (tx, rx) = try!(channel(&handle)); 76 | let (command_tx, command_rx) = try!(channel(&handle)); 77 | 78 | try!(link.send(multiplex::Command::Subscribe { key: multiplex_key.clone(), sender: tx })); 79 | 80 | Ok(UnreliableProbabilisticBroadcast { 81 | id: id, 82 | delivered: HashSet::new(), 83 | delivered_gc_age: delivered_gc_age, 84 | peers: peers, 85 | k: k, 86 | multiplex_key: multiplex_key, 87 | link: link, 88 | link_rx: rx, 89 | command_tx: command_tx, 90 | command_rx: command_rx, 91 | rounds: rounds, 92 | pending: VecDeque::new(), 93 | seq: 0, 94 | }) 95 | } 96 | 97 | pub fn channel(&self) -> Sender> { 98 | self.command_tx.clone() 99 | } 100 | 101 | fn decode_link_message(&self, link_msg: multiplex::Message) -> io::Result { 102 | let gm: UpbMessage = try!(serde_json::from_value(link_msg.body) 103 | .map_err(|e| io::Error::new(io::ErrorKind::Other, "json decoding error"))); 104 | 105 | Ok(gm) 106 | } 107 | 108 | fn handle_broadcast(&mut self, from: String, seq: u64, value: serde_json::Value, ttl: u32) -> io::Result<()> { 109 | let msg_id = Delivery::new(from.clone(), seq); 110 | 111 | if ttl > 0 { 112 | try!(self.gossip(UpbMessage::Broadcast { from: from.clone(), seq: seq, value: value.clone(), ttl: ttl - 1 })); 113 | } 114 | 115 | if self.delivered.insert(msg_id.clone()) { 116 | let typed_value = try!(serde_json::from_value(value) 117 | .map_err(|e| io::Error::new(io::ErrorKind::Other, "json decoding error"))); 118 | 119 | self.pending.push_back(Message { 120 | from: from, 121 | seq: seq, 122 | value: typed_value 123 | }); 124 | } 125 | 126 | Ok(()) 127 | } 128 | 129 | fn gossip(&self, gm: UpbMessage) -> io::Result<()> { 130 | debug!("gossipping to {} targets: {:?}", self.k, gm); 131 | 132 | let gm_value = serde_json::to_value(gm.clone()); 133 | 134 | for t in self.picktargets(self.k) { 135 | debug!("gossipping {:?} to {}", gm, t); 136 | 137 | let mm = multiplex::Message::new(Some(t.to_owned()), self.id.clone(), self.multiplex_key.clone(), gm_value.clone()); 138 | 139 | try!(self.link.send(multiplex::Command::SendMessage {msg: mm})); 140 | } 141 | 142 | Ok(()) 143 | } 144 | 145 | pub fn broadcast(&self, value: T) -> io::Result<()> { 146 | self.command_tx.send(Command::Broadcast {msg: value}) 147 | } 148 | 149 | fn do_broadcast(&mut self, msg: T) -> io::Result<()> { 150 | let value = serde_json::to_value(msg.clone()); 151 | let msg_seq = self.next_msg_seq(); 152 | 153 | self.delivered.insert(Delivery::new(self.id.clone(), msg_seq)); 154 | self.pending.push_back(Message { 155 | from: self.id.clone(), 156 | seq: msg_seq, 157 | value: msg 158 | }); 159 | 160 | let gm = UpbMessage::Broadcast { from: self.id.clone(), seq: msg_seq, value: value, ttl: self.rounds - 1 }; 161 | 162 | self.gossip(gm) 163 | } 164 | 165 | pub fn picktargets<'a>(&'a self, k: usize) -> Vec<&'a NodeId> { 166 | let mut rng = rand::thread_rng(); 167 | let candidates = self.peers.iter() 168 | .filter(|i| *i != &self.id); 169 | rand::sample(&mut rng, candidates, k) 170 | } 171 | 172 | fn next_msg_seq(&mut self) -> u64 { 173 | self.seq += 1; 174 | self.seq 175 | } 176 | 177 | fn delivered_gc(&mut self) { 178 | let mut keep = HashSet::new(); 179 | 180 | for msg_id in self.delivered.drain() { 181 | if msg_id.delivered_at + self.delivered_gc_age > Utc::now() { 182 | keep.insert(msg_id); 183 | } 184 | } 185 | 186 | self.delivered = keep; 187 | } 188 | 189 | pub fn set_delivered_gc_age(&mut self, age: Duration) { 190 | self.delivered_gc_age = age; 191 | } 192 | 193 | pub fn get_delivered_gc_age(&self) -> Duration { 194 | self.delivered_gc_age 195 | } 196 | } 197 | 198 | impl Stream for UnreliableProbabilisticBroadcast where T: Serialize + DeserializeOwned + Clone + Send + 'static { 199 | type Item=Message; 200 | type Error=io::Error; 201 | 202 | fn poll(&mut self) -> Poll, Self::Error> { 203 | while let Async::Ready(Some(cmd)) = try!(self.command_rx.poll()) { 204 | match cmd { 205 | Command::Broadcast {msg} => { 206 | try!(self.do_broadcast(msg)); 207 | } 208 | } 209 | } 210 | 211 | while let Async::Ready(Some(link_msg)) = try!(self.link_rx.poll()) { 212 | debug!("gossip received link message: {:?}", link_msg); 213 | 214 | let gm = try!(self.decode_link_message(link_msg)); 215 | 216 | match gm { 217 | UpbMessage::Broadcast {from, seq, value, ttl} => { 218 | try!(self.handle_broadcast(from, seq, value, ttl)); 219 | } 220 | } 221 | } 222 | 223 | self.delivered_gc(); 224 | 225 | Ok(match self.pending.pop_front() { 226 | Some(v) => Async::Ready(Some(v)), 227 | None => Async::NotReady, 228 | }) 229 | } 230 | } -------------------------------------------------------------------------------- /archive/recon-gossip/src/watchdog.rs: -------------------------------------------------------------------------------- 1 | use std::fmt; 2 | use std::time::{Duration, SystemTime}; 3 | use std::sync::{Arc, Mutex}; 4 | use std::collections::{HashMap, HashSet}; 5 | use std::collections::hash_map; 6 | use std::io; 7 | use futures::{self, Async, Poll, Stream, Future}; 8 | use futures::sync::mpsc::{channel, Sender, Receiver}; 9 | use tokio_timer; 10 | use tokio_core::reactor::Handle; 11 | use tokio_core::net::TcpStream; 12 | use serde_json::{self, Value}; 13 | 14 | use ::switchboard::{self, Switchboard}; 15 | use ::client::{NewClient}; 16 | use ::multiplex; 17 | use ::{WatchdogMessage}; 18 | 19 | struct Message { 20 | pub to: String, 21 | pub key: String, 22 | pub body: WatchdogMessage, 23 | } 24 | 25 | #[derive(Clone)] 26 | pub struct Heartbeat { 27 | pub client_id: String, 28 | pub time: SystemTime, 29 | } 30 | 31 | impl Heartbeat { 32 | pub fn new(client_id: S, time: SystemTime) -> Heartbeat where S: Into { 33 | Heartbeat { 34 | client_id: client_id.into(), 35 | time: time, 36 | } 37 | } 38 | 39 | pub fn max<'a>(&'a self, other: &'a Heartbeat) -> &'a Heartbeat { 40 | if self.time > other.time { 41 | self 42 | } else { 43 | other 44 | } 45 | } 46 | 47 | pub fn age(&self, now: SystemTime) -> io::Result { 48 | now.duration_since(self.time) 49 | .map_err(|e| io::Error::new(io::ErrorKind::Other, "systemtime duration_since")) 50 | } 51 | } 52 | 53 | pub struct Watchdog { 54 | id: String, 55 | handle: Handle, 56 | timer: tokio_timer::Timer, 57 | sleep: Option, 58 | interval: Duration, 59 | max_age: Duration, 60 | link: Sender, 61 | link_rx: Receiver, 62 | multiplex_key: String, 63 | heartbeats: HashMap, 64 | failed: HashSet, 65 | seq: u32, 66 | } 67 | 68 | impl Watchdog { 69 | 70 | pub fn new(handle: Handle, id: String, link: Sender, interval: Duration, max_age: Duration) -> io::Result { 71 | let timer = tokio_timer::Timer::default(); 72 | let (link_tx, link_rx) = try!(channel(&handle)); 73 | let multiplex_key = "watchdog"; 74 | 75 | try!(link.send(multiplex::Command::Subscribe {key: multiplex_key.to_string(), sender: link_tx})); 76 | 77 | Ok(Watchdog { 78 | id: id, 79 | handle: handle, 80 | sleep: Some(timer.sleep(interval.clone())), 81 | timer: timer, 82 | interval: interval, 83 | max_age: max_age, 84 | link: link, 85 | link_rx: link_rx, 86 | multiplex_key: multiplex_key.to_string(), 87 | heartbeats: HashMap::new(), 88 | failed: HashSet::new(), 89 | seq: 0, 90 | }) 91 | } 92 | 93 | fn handle(&mut self, msg: multiplex::Message) -> io::Result<()> { 94 | debug!("watchdog received {:?}", msg); 95 | 96 | let wm = try!(serde_json::from_value(msg.body) 97 | .map_err(|e| { 98 | io::Error::new(io::ErrorKind::Other, "json decoding error") 99 | })); 100 | 101 | match wm { 102 | WatchdogMessage::Ping { seq } => { 103 | try!(self.handle_ping(msg.from, seq)); 104 | }, 105 | WatchdogMessage::Pong { seq } => { 106 | try!(self.handle_pong(msg.from, seq)); 107 | } 108 | } 109 | 110 | Ok(()) 111 | } 112 | 113 | fn handle_ping(&mut self, id: String, seq: u32) -> io::Result<()> { 114 | let wm = WatchdogMessage::Pong { seq: seq }; 115 | 116 | let body = serde_json::to_value(wm); 117 | 118 | let m = multiplex::Message::new(Some(id), self.id.clone(), self.multiplex_key.clone(), body); 119 | 120 | self.link.send(multiplex::Command::SendMessage {msg: m}) 121 | } 122 | 123 | fn handle_pong(&mut self, id: String, seq: u32) -> io::Result<()> { 124 | let heartbeat = Heartbeat::new(id, SystemTime::now()); 125 | 126 | try!(self.heartbeat(heartbeat)); 127 | 128 | Ok(()) 129 | } 130 | 131 | pub fn poll(&mut self) -> Poll<(), io::Error> { 132 | trace!("polling watchdog"); 133 | 134 | try!(self.poll_timer()); 135 | 136 | try!(self.check_failed()); 137 | 138 | while let Async::Ready(Some(msg)) = try!(self.link_rx.poll()) { 139 | try!(self.handle(msg)); 140 | } 141 | 142 | Ok(Async::NotReady) 143 | } 144 | 145 | pub fn poll_timer(&mut self) -> io::Result<()> { 146 | if let Some(mut sleep) = self.sleep.take() { 147 | trace!("checking timer"); 148 | match try!(sleep.poll().map_err(|_te| io::Error::new(io::ErrorKind::Other, "timer error"))) { 149 | Async::Ready(()) => { 150 | try!(self.send_pings()); 151 | }, 152 | Async::NotReady => { 153 | self.sleep = Some(sleep); 154 | } 155 | } 156 | } 157 | 158 | if self.sleep.is_none() { 159 | self.sleep = Some(self.timer.sleep(self.interval.clone())); 160 | 161 | try!(self.poll_timer()); 162 | } 163 | 164 | Ok(()) 165 | } 166 | 167 | pub fn send_pings(&mut self) -> io::Result<()> { 168 | trace!("pinging peers"); 169 | 170 | self.seq += 1; 171 | 172 | let body = serde_json::to_value(WatchdogMessage::Ping {seq: self.seq}); 173 | 174 | let msg = multiplex::Message::new(None, self.id.clone(), self.multiplex_key.clone(), body); 175 | 176 | try!(self.link.send(multiplex::Command::SendMessage{ msg: msg})); 177 | 178 | Ok(()) 179 | } 180 | 181 | pub fn heartbeat(&mut self, heartbeat: Heartbeat) -> io::Result<()> { 182 | match self.heartbeats.entry(heartbeat.client_id.clone()) { 183 | hash_map::Entry::Occupied(e) => { 184 | let h = e.into_mut(); 185 | *h = h.max(&heartbeat).to_owned(); 186 | }, 187 | hash_map::Entry::Vacant(e) => { 188 | e.insert(heartbeat); 189 | } 190 | } 191 | 192 | Ok(()) 193 | } 194 | 195 | pub fn check_failed(&mut self) -> io::Result<()> { 196 | let now = SystemTime::now(); 197 | 198 | for h in self.heartbeats.values() { 199 | let age = try!(h.age(now)); 200 | 201 | if age > self.max_age { 202 | if !self.failed.contains(&h.client_id) { 203 | info!("marking peer {} as failed", h.client_id); 204 | self.failed.insert(h.client_id.clone()); 205 | } 206 | } else { 207 | if self.failed.contains(&h.client_id) { 208 | info!("marking peer {} as recovered", h.client_id); 209 | self.failed.remove(&h.client_id); 210 | } 211 | } 212 | } 213 | 214 | Ok(()) 215 | } 216 | } 217 | 218 | impl Future for Watchdog { 219 | type Item=(); 220 | type Error=io::Error; 221 | 222 | fn poll(&mut self) -> Poll<(), io::Error> { 223 | Watchdog::poll(self) 224 | } 225 | } -------------------------------------------------------------------------------- /recon-link/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | authors = ["Chris Dawes "] 3 | categories = ["asynchronous", "network-programming"] 4 | description = "Persistent tcp connections" 5 | keywords = ["reconnect", "async", "tokio", "futures", "tcp"] 6 | license = "Apache-2.0" 7 | name = "recon-link" 8 | repository = "https://github.com/cmsd2/recon" 9 | version = "0.3.1-alpha.0" 10 | [dependencies] 11 | bytes = "0.4.5" 12 | futures = "0.1.17" 13 | log = "0.4.1" 14 | state_machine_future = "0.1.5" 15 | tokio-codec = "0.1.1" 16 | tokio-core = "0.1.11" 17 | tokio-io = "0.1.4" 18 | tokio-retry = "0.1.1" 19 | tokio-timer = "0.1.2" 20 | 21 | [dependencies.env_logger] 22 | optional = true 23 | version = "0.4.3" 24 | 25 | [features] 26 | logger = ["env_logger"] 27 | -------------------------------------------------------------------------------- /recon-link/LICENSE-2.0: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /recon-link/examples/reconnect.rs: -------------------------------------------------------------------------------- 1 | #[cfg(feature="logger")] 2 | extern crate env_logger; 3 | #[macro_use] 4 | extern crate log; 5 | extern crate futures; 6 | extern crate tokio_core; 7 | extern crate tokio_timer; 8 | extern crate recon_link; 9 | 10 | use std::io; 11 | use std::net::SocketAddr; 12 | use std::time::Duration; 13 | use futures::{Async, AsyncSink, Poll, StartSend}; 14 | use futures::future::Future; 15 | use futures::stream::{self, Stream}; 16 | use futures::sink::{Sink}; 17 | use tokio_core::reactor::{Core, Handle}; 18 | use tokio_core::net::TcpStream; 19 | use tokio_timer::Timer; 20 | use recon_link::conn::{self, Connection, Message, NewTransport}; 21 | use recon_link::framing::{FramedLineTransport, new_line_transport, ReconFrame}; 22 | 23 | pub type MessageContent = ReconFrame; 24 | 25 | #[cfg(feature="logger")] 26 | mod logging { 27 | pub fn init_logger() { 28 | use env_logger; 29 | env_logger::init().unwrap(); 30 | } 31 | } 32 | 33 | #[cfg(not(feature="logger"))] 34 | mod logging { 35 | pub fn init_logger() {} 36 | } 37 | 38 | fn main() { 39 | logging::init_logger(); 40 | 41 | info!("hello, world!"); 42 | 43 | let timer = Timer::default(); 44 | let mut core = Core::new().unwrap(); 45 | let handle = core.handle(); 46 | 47 | let addr = "127.0.0.1:6666".parse().unwrap(); 48 | 49 | let stream = stream::iter_ok((1..1000).map(|n| ReconFrame::Message(format!("{}", n)))) 50 | .and_then(|value| { 51 | debug!("next value is {:?}", value); 52 | timer.sleep(Duration::from_millis(500)) 53 | .map_err(|e| io::Error::new(io::ErrorKind::Other, e)) 54 | .map(|_| { 55 | debug!("producing delayed value {:?}", value); 56 | value 57 | }) 58 | }); 59 | 60 | let conn_config = conn::Config { 61 | inbound_max: 10, 62 | outbound_max: 10, 63 | outbound_max_age: Duration::from_millis(1000), 64 | }; 65 | let conn = Connection::new(core.handle(), stream, PrinterSink, NewTcpTransport(addr, handle), conn_config); 66 | 67 | let result = core.run(conn); 68 | 69 | println!("event loop terminated: {:?}", result); 70 | } 71 | 72 | struct PrinterSink; 73 | 74 | impl Sink for PrinterSink { 75 | type SinkItem = Message; 76 | type SinkError = io::Error; 77 | 78 | fn start_send(&mut self, item: Self::SinkItem) -> StartSend { 79 | println!("{:?}", item); 80 | Ok(AsyncSink::Ready) 81 | } 82 | 83 | fn poll_complete(&mut self) -> Poll<(), Self::SinkError> { 84 | Ok(Async::Ready(())) 85 | } 86 | } 87 | 88 | #[derive(Clone)] 89 | struct NewTcpTransport(SocketAddr, Handle); 90 | 91 | impl NewTransport for NewTcpTransport { 92 | type Transport = FramedLineTransport; 93 | type Error = io::Error; 94 | type Future = Box>; 95 | 96 | fn new_transport(&self) -> Self::Future { 97 | Box::new(TcpStream::connect(&self.0, &self.1).map(|stream| new_line_transport(stream))) as Self::Future 98 | } 99 | } -------------------------------------------------------------------------------- /recon-link/src/conn.rs: -------------------------------------------------------------------------------- 1 | use futures::{Async, AsyncSink, Future, Poll}; 2 | use futures::stream::Stream; 3 | use futures::sink::Sink; 4 | use state_machine_future::RentToOwn; 5 | use tokio_core::reactor::Handle; 6 | use tokio_retry::{self, Retry}; 7 | use tokio_retry::strategy::{FibonacciBackoff, jitter}; 8 | use std::io; 9 | use std::error; 10 | use std::time::{Duration, Instant}; 11 | use std::fmt; 12 | use std::collections::VecDeque; 13 | 14 | pub type ConnectionError = io::Error; 15 | pub type SessionId = u32; 16 | 17 | #[derive(Debug, Clone, PartialEq)] 18 | pub struct Config { 19 | pub inbound_max: usize, 20 | pub outbound_max: usize, 21 | pub outbound_max_age: Duration, 22 | } 23 | 24 | #[derive(Debug, Clone, PartialEq)] 25 | pub enum Event { 26 | Connected { 27 | session_id: SessionId, 28 | } 29 | } 30 | 31 | pub enum Message { 32 | Packet { 33 | session_id: SessionId, 34 | content: Item, 35 | }, 36 | Control { 37 | event: Event, 38 | } 39 | } 40 | 41 | pub struct TimestampedItem { 42 | pub timestamp: Instant, 43 | pub item: Item, 44 | } 45 | 46 | impl TimestampedItem { 47 | pub fn new(item: Item) -> TimestampedItem { 48 | TimestampedItem { 49 | timestamp: Instant::now(), 50 | item: item, 51 | } 52 | } 53 | 54 | pub fn age(&self) -> Duration { 55 | Instant::now().duration_since(self.timestamp) 56 | } 57 | 58 | pub fn older_than(&self, age: Duration) -> bool { 59 | self.age().gt(&age) 60 | } 61 | } 62 | 63 | impl Clone for Message where Item: Clone { 64 | fn clone(&self) -> Message { 65 | match self { 66 | &Message::Packet { session_id, ref content } => { 67 | Message::Packet { 68 | session_id: session_id, 69 | content: content.clone() 70 | } 71 | }, 72 | &Message::Control { ref event } => { 73 | Message::Control { 74 | event: event.clone() 75 | } 76 | } 77 | } 78 | } 79 | } 80 | 81 | impl fmt::Debug for Message where Item: fmt::Debug { 82 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 83 | match self { 84 | &Message::Packet { session_id, ref content } => { 85 | write!(f, "Message::Packet", session_id, content) 86 | }, 87 | &Message::Control { ref event } => { 88 | write!(f, "Message::Control", event) 89 | } 90 | } 91 | } 92 | } 93 | 94 | pub trait NewTransport { 95 | type Future: Future; 96 | type Transport; 97 | type Error: error::Error + Sync + Send; 98 | 99 | fn new_transport(&self) -> Self::Future; 100 | } 101 | 102 | #[derive(StateMachineFuture)] 103 | pub enum Connection where S: Stream, K: Sink,SinkError=io::Error>, T: Stream+Sink, N: NewTransport+Clone+'static { 104 | #[state_machine_future(start, transitions(Connecting, Finished, Error))] 105 | NotConnected { 106 | handle: Handle, 107 | stream: S, 108 | sink: K, 109 | error_count: u32, 110 | session_id: u32, 111 | new_transport: N, 112 | config: Config, 113 | }, 114 | 115 | #[state_machine_future(transitions(NotConnected, Connected, Finished, Error))] 116 | Connecting { 117 | handle: Handle, 118 | stream: S, 119 | sink: K, 120 | tcp_future: Box>, 121 | error_count: u32, 122 | session_id: u32, 123 | new_transport: N, 124 | config: Config, 125 | }, 126 | 127 | #[state_machine_future(transitions(Connected, NotConnected, Finished, Error))] 128 | Connected { 129 | handle: Handle, 130 | stream: S, 131 | sink: K, 132 | tcp: T, 133 | outbound: VecDeque>, 134 | outbound_inflight: bool, 135 | inbound: VecDeque>, 136 | inbound_inflight: bool, 137 | error_count: u32, 138 | session_id: u32, 139 | new_transport: N, 140 | config: Config, 141 | }, 142 | 143 | #[state_machine_future(ready)] 144 | Finished(()), 145 | 146 | #[state_machine_future(error)] 147 | Error(ConnectionError), 148 | } 149 | 150 | impl Connection where S: Stream, K: Sink,SinkError=io::Error>, T: Stream+Sink, N: NewTransport+Clone+'static { 151 | pub fn new(handle: Handle, stream: S, sink: K, new_transport: N, config: Config) -> ConnectionFuture { 152 | debug!("constructing new connection future"); 153 | Connection::start(handle, stream, sink, 0, 0, new_transport, config) 154 | } 155 | } 156 | 157 | impl PollConnection for Connection where S: Stream, K: Sink,SinkError=io::Error>, T: Stream+Sink, N: NewTransport+Clone+'static { 158 | fn poll_not_connected<'a>(not_connected: &'a mut RentToOwn<'a, NotConnected>) -> Poll, ConnectionError> { 159 | trace!("not connected"); 160 | 161 | let retry_strategy = FibonacciBackoff ::from_millis(1) 162 | .max_delay(Duration::from_millis(2000)) 163 | .map(jitter); 164 | 165 | let handle = not_connected.handle.clone(); 166 | let new_transport = not_connected.new_transport.clone(); 167 | 168 | let tcp_future = Box::new(Retry::spawn(handle, retry_strategy, move || { 169 | new_transport.new_transport() 170 | }).map_err(|e| { 171 | match e { 172 | tokio_retry::Error::OperationError(e) => io::Error::new(io::ErrorKind::Other, e), 173 | tokio_retry::Error::TimerError(e) => e, 174 | } 175 | })) as (Box>); 176 | 177 | let not_connected = not_connected.take(); 178 | Ok(Async::Ready(Connecting { 179 | handle: not_connected.handle, 180 | stream: not_connected.stream, 181 | sink: not_connected.sink, 182 | tcp_future: tcp_future, 183 | error_count: not_connected.error_count, 184 | session_id: not_connected.session_id, 185 | new_transport: not_connected.new_transport, 186 | config: not_connected.config, 187 | }.into())) 188 | } 189 | 190 | fn poll_connecting<'a>(connecting: &'a mut RentToOwn<'a, Connecting>) -> Poll, ConnectionError> { 191 | trace!("connecting"); 192 | 193 | let tcp = try_ready!(connecting.tcp_future.poll()); 194 | 195 | let connecting = connecting.take(); 196 | Ok(Async::Ready(Connected { 197 | handle: connecting.handle, 198 | stream: connecting.stream, 199 | sink: connecting.sink, 200 | tcp: tcp, 201 | inbound: VecDeque::from(vec![ 202 | Message::Control { 203 | event: Event::Connected { 204 | session_id: connecting.session_id 205 | } 206 | } 207 | ]), 208 | inbound_inflight: false, 209 | outbound: VecDeque::new(), 210 | outbound_inflight: false, 211 | error_count: connecting.error_count, 212 | session_id: connecting.session_id, 213 | new_transport: connecting.new_transport, 214 | config: connecting.config, 215 | }.into())) 216 | } 217 | 218 | fn poll_connected<'a>(connected: &'a mut RentToOwn<'a, Connected>) -> Poll, ConnectionError> { 219 | trace!("connected"); 220 | 221 | // we make progress if any poll returns Ready 222 | // if we make progress then we return a state or state change 223 | // otherwise return NotReady 224 | let mut progress = false; 225 | let session_id = connected.session_id; 226 | 227 | while connected.inbound.len() < connected.config.inbound_max { 228 | match connected.tcp.poll() { 229 | Ok(Async::Ready(Some(msg))) => { 230 | trace!("transport received msg"); 231 | progress = true; 232 | connected.inbound.push_back(Message::Packet{session_id, content: msg}); 233 | }, 234 | Ok(Async::Ready(None)) => { 235 | trace!("transport returned end of stream"); 236 | 237 | let connected = connected.take(); 238 | 239 | return Ok(Async::Ready(NotConnected { 240 | handle: connected.handle, 241 | stream: connected.stream, 242 | sink: connected.sink, 243 | error_count: connected.error_count, 244 | session_id: connected.session_id + 1, 245 | new_transport: connected.new_transport, 246 | config: connected.config, 247 | }.into())); 248 | }, 249 | Ok(Async::NotReady) => { 250 | trace!("transport not ready to read"); 251 | break; 252 | }, 253 | Err(e) => { 254 | trace!("transport read error: {:?}", e); 255 | let connected = connected.take(); 256 | 257 | return Ok(Async::Ready(NotConnected { 258 | handle: connected.handle, 259 | stream: connected.stream, 260 | sink: connected.sink, 261 | error_count: connected.error_count + 1, 262 | session_id: connected.session_id + 1, 263 | new_transport: connected.new_transport, 264 | config: connected.config, 265 | }.into())); 266 | } 267 | }; 268 | } 269 | 270 | while connected.outbound.len() < connected.config.outbound_max { 271 | match try!(connected.stream.poll()) { 272 | Async::Ready(Some(msg)) => { 273 | progress = true; 274 | connected.outbound.push_back(TimestampedItem::new(msg)); 275 | }, 276 | Async::Ready(None) => { 277 | trace!("stream finished"); 278 | return Ok(Async::Ready(Finished(()).into())); 279 | }, 280 | Async::NotReady => { 281 | trace!("stream not ready to read"); 282 | break; 283 | } 284 | }; 285 | } 286 | 287 | if let Some(TimestampedItem{ timestamp, item: sending }) = connected.outbound.pop_front() { 288 | match connected.tcp.start_send(sending) { 289 | Ok(AsyncSink::Ready) => { 290 | trace!("transport wrote message"); 291 | progress = true; 292 | connected.outbound_inflight = true; 293 | }, 294 | Ok(AsyncSink::NotReady(sending)) => { 295 | trace!("tcpstream not ready to write"); 296 | 297 | let timestamped_msg = TimestampedItem { timestamp: timestamp, item: sending }; 298 | if timestamped_msg.older_than(connected.config.outbound_max_age) { 299 | trace!("message older than max age {:?}. reconnecting", timestamped_msg.age()); 300 | 301 | let connected = connected.take(); 302 | 303 | return Ok(Async::Ready(NotConnected { 304 | handle: connected.handle, 305 | stream: connected.stream, 306 | sink: connected.sink, 307 | error_count: connected.error_count, 308 | session_id: connected.session_id + 1, 309 | new_transport: connected.new_transport, 310 | config: connected.config, 311 | }.into())); 312 | } else { 313 | connected.outbound.push_front(timestamped_msg); 314 | } 315 | }, 316 | Err(err) => { 317 | trace!("transport write error: {:?}", err); 318 | let connected = connected.take(); 319 | 320 | return Ok(Async::Ready(NotConnected { 321 | handle: connected.handle, 322 | stream: connected.stream, 323 | sink: connected.sink, 324 | error_count: connected.error_count + 1, 325 | session_id: connected.session_id + 1, 326 | new_transport: connected.new_transport, 327 | config: connected.config, 328 | }.into())); 329 | } 330 | } 331 | } 332 | 333 | if let Some(msg) = connected.inbound.pop_front() { 334 | match try!(connected.sink.start_send(msg)) { 335 | AsyncSink::Ready => { 336 | trace!("sink started send"); 337 | progress = true; 338 | connected.inbound_inflight = true; 339 | }, 340 | AsyncSink::NotReady(msg) => { 341 | trace!("sink not ready"); 342 | connected.inbound.push_front(msg); 343 | }, 344 | } 345 | } 346 | 347 | if connected.inbound_inflight { 348 | match try!(connected.sink.poll_complete()) { 349 | Async::Ready(()) => { 350 | trace!("sink polled complete"); 351 | progress = true; 352 | connected.inbound_inflight = false; 353 | }, 354 | Async::NotReady => { 355 | trace!("sink polled not ready"); 356 | // do nothing 357 | } 358 | } 359 | } 360 | 361 | if connected.outbound_inflight { 362 | match try!(connected.tcp.poll_complete()) { 363 | Async::Ready(()) => { 364 | trace!("transport poll complete"); 365 | progress = true; 366 | connected.outbound_inflight = false; 367 | }, 368 | Async::NotReady => { 369 | trace!("transport sink polled not ready"); 370 | // do nothing 371 | } 372 | } 373 | } 374 | 375 | if progress { 376 | trace!("made progress"); 377 | 378 | let connected = connected.take(); 379 | 380 | return Ok(Async::Ready(Connected { 381 | handle: connected.handle, 382 | stream: connected.stream, 383 | sink: connected.sink, 384 | tcp: connected.tcp, 385 | inbound: connected.inbound, 386 | inbound_inflight: connected.inbound_inflight, 387 | outbound: connected.outbound, 388 | outbound_inflight: connected.outbound_inflight, 389 | error_count: connected.error_count, 390 | session_id: connected.session_id, 391 | new_transport: connected.new_transport, 392 | config: connected.config, 393 | }.into())); 394 | } else { 395 | trace!("did not make progress"); 396 | 397 | return Ok(Async::NotReady); 398 | } 399 | } 400 | } 401 | -------------------------------------------------------------------------------- /recon-link/src/framing.rs: -------------------------------------------------------------------------------- 1 | use bytes::BytesMut; 2 | use std::{io, str}; 3 | use tokio_io::{AsyncRead, AsyncWrite}; 4 | use tokio_codec::{Framed, Encoder, Decoder}; 5 | 6 | #[derive(Debug, Clone)] 7 | pub enum ReconFrame { 8 | Message(String), 9 | Done, 10 | } 11 | 12 | pub struct Parser; 13 | 14 | impl Decoder for Parser { 15 | type Item=ReconFrame; 16 | type Error=io::Error; 17 | 18 | fn decode(&mut self, buf: &mut BytesMut) -> Result, io::Error> { 19 | trace!("buffer has {} bytes available for decoding", buf.len()); 20 | 21 | if let Some(n) = buf.iter().position(|b| *b == b'\n') { 22 | // remove this line and the newline from the buffer. 23 | let line = buf.split_to(n); 24 | buf.split_to(1); 25 | 26 | trace!("buffer has {} bytes remaining after decoding", buf.len()); 27 | 28 | // Turn this data into a UTF string and return it in a Frame. 29 | return match str::from_utf8(&line[..]) { 30 | Ok(msg) => { 31 | Ok(Some(ReconFrame::Message(msg.to_string()))) 32 | }, 33 | Err(_) => Err(io::Error::new(io::ErrorKind::Other, "invalid string")) 34 | } 35 | } 36 | 37 | Ok(None) 38 | } 39 | 40 | fn decode_eof(&mut self, buf: &mut BytesMut) -> Result, io::Error> { 41 | if buf.len() == 0 { 42 | //Ok(Some(ReconFrame::Done)) 43 | Err(io::Error::new(io::ErrorKind::UnexpectedEof, "stream eof")) 44 | } else { 45 | Err(io::Error::new(io::ErrorKind::InvalidData, "trailing data found")) 46 | } 47 | } 48 | } 49 | 50 | impl Encoder for Parser { 51 | type Item=ReconFrame; 52 | type Error=io::Error; 53 | 54 | fn encode(&mut self, msg: ReconFrame, buf: &mut BytesMut) -> Result<(), io::Error> { 55 | match msg { 56 | ReconFrame::Message(text) => { 57 | if text.contains('\n') { 58 | return Err(io::Error::new(io::ErrorKind::Other, "line transport can't handle newlines")); 59 | } 60 | 61 | buf.extend_from_slice(&text.as_bytes()); 62 | buf.extend_from_slice(&['\n' as u8]); 63 | } 64 | ReconFrame::Done => {} 65 | } 66 | 67 | Ok(()) 68 | } 69 | } 70 | 71 | pub type FramedLineTransport = Framed; 72 | 73 | pub fn new_line_transport(inner: T) -> FramedLineTransport 74 | where T: AsyncRead + AsyncWrite, 75 | { 76 | FramedLineTransport::::new(inner, Parser) 77 | } 78 | -------------------------------------------------------------------------------- /recon-link/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate log; 3 | #[macro_use] 4 | extern crate futures; 5 | extern crate tokio_core; 6 | extern crate tokio_io; 7 | extern crate tokio_retry; 8 | extern crate tokio_timer; 9 | extern crate tokio_codec; 10 | #[macro_use] 11 | extern crate state_machine_future; 12 | extern crate bytes; 13 | 14 | pub mod conn; 15 | pub mod framing; 16 | 17 | #[cfg(test)] 18 | mod tests { 19 | #[test] 20 | fn it_works() { 21 | assert_eq!(2 + 2, 4); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /recon-service/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | authors = ["Chris Dawes "] 3 | name = "recon-service" 4 | version = "0.1.0" 5 | license = "Apache-2.0" 6 | description = "Asynchronous service types for Tokio" 7 | repository = "https://github.com/cmsd2/recon" 8 | keywords = ["reconnect", "async", "tokio", "futures", "service"] 9 | categories = ["asynchronous", "network-programming"] 10 | 11 | [dependencies] 12 | futures = "0.1.17" 13 | -------------------------------------------------------------------------------- /recon-service/src/lib.rs: -------------------------------------------------------------------------------- 1 | extern crate futures; 2 | 3 | pub mod service; 4 | 5 | #[cfg(test)] 6 | mod tests { 7 | #[test] 8 | fn it_works() { 9 | assert_eq!(2 + 2, 4); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /recon-service/src/service.rs: -------------------------------------------------------------------------------- 1 | use futures::{Async, Future, Poll}; 2 | 3 | pub trait AsyncService { 4 | type Request; 5 | type Error; 6 | 7 | fn send(&self, req: Self::Request) -> Result<(), Self::Error>; 8 | fn poll(&mut self) -> Poll<(), Self::Error>; 9 | } 10 | 11 | impl Future for AsyncService { 12 | type Item=(); 13 | type Error=E; 14 | 15 | fn poll(&mut self) -> Poll { 16 | AsyncService::poll(self) 17 | } 18 | } 19 | 20 | pub trait NewService { 21 | type Error; 22 | type Item; 23 | 24 | fn new_service(&self) -> Result; 25 | } 26 | 27 | pub trait Service { 28 | /// Requests handled by the service. 29 | type Request; 30 | 31 | /// Errors produced by the service. 32 | type Error; 33 | 34 | /// The future response value. 35 | type Future: Future; 36 | 37 | /// Process the request and return the response asynchronously. 38 | fn post(&self, req: Self::Request) -> Self::Future; 39 | 40 | /// Returns `Async::Ready` when the service is ready to accept a request. 41 | fn poll_ready(&self) -> Async<()>; 42 | } 43 | -------------------------------------------------------------------------------- /recon/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "recon" 3 | version = "0.1.0" 4 | authors = ["Chris Dawes "] 5 | license = "Apache-2.0" 6 | 7 | [lib] 8 | 9 | [dependencies] 10 | recon-link = { version = "0.3.1-alpha.0", path = "../recon-link" } 11 | -------------------------------------------------------------------------------- /recon/LICENSE-2.0: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /recon/src/lib.rs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cmsd2/recon/373a7b15bf0525df2f11a24cd4de7ba1e5752ab2/recon/src/lib.rs --------------------------------------------------------------------------------