Commit 06f76c3c7f
06f76c3c7f76bcb6aaeed9eb824febbb477dbd52
parent: a531ca9739
Unsigned
Damir Jelić <poljar@termina.org.uk> · 2019-11-16 17:30 UTC
server: Move the runtime into the server.
Tokio currently doesn't seem to have a way to cancel tasks, since we
want to be able to connect and disconnect a server we move the runtime
into the server and if we want to disconnect we shutdown the whole
runtime.
Layout: unified · split
src/lib.rs
+20 −14
| @@ -3,48 +3,54 @@ mod room_buffer; |
| 3 | 3 | mod server; |
| 4 | 4 | |
| 5 | 5 | use std::collections::HashMap; |
| 6 | | use tokio::runtime::Runtime; |
| 6 | use std::time::Duration; |
| 7 | 7 | |
| 8 | 8 | use weechat::{ |
| 9 | 9 | weechat_plugin, ArgsWeechat, Weechat, WeechatPlugin, WeechatResult, |
| 10 | 10 | }; |
| 11 | 11 | |
| 12 | | use crate::executor::cleanup_executor; |
| 12 | use crate::executor::{cleanup_executor, spawn_weechat}; |
| 13 | 13 | use crate::server::MatrixServer; |
| 14 | 14 | |
| 15 | 15 | const PLUGIN_NAME: &str = "matrix"; |
| 16 | 16 | |
| 17 | 17 | struct Matrix { |
| 18 | | tokio: Option<Runtime>, |
| 19 | 18 | servers: HashMap<String, MatrixServer>, |
| 20 | 19 | } |
| 21 | 20 | |
| 21 | impl Matrix { |
| 22 | fn autoconnect(&mut self) { |
| 23 | for server in self.servers.values_mut() { |
| 24 | server.connect(); |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 22 | 29 | impl WeechatPlugin for Matrix { |
| 23 | 30 | fn init(weechat: &Weechat, _args: ArgsWeechat) -> WeechatResult<Self> { |
| 24 | | let runtime = Runtime::new().unwrap(); |
| 25 | | |
| 26 | 31 | let server_name = "localhost"; |
| 27 | 32 | |
| 28 | 33 | let mut server = MatrixServer::new(server_name); |
| 29 | 34 | let mut servers = HashMap::new(); |
| 30 | 35 | |
| 31 | | server.connect(&runtime); |
| 32 | 36 | servers.insert(server_name.to_owned(), server); |
| 33 | 37 | |
| 34 | | Ok(Matrix { |
| 35 | | tokio: Some(runtime), |
| 36 | | servers, |
| 37 | | }) |
| 38 | spawn_weechat(async move { |
| 39 | async_std::task::sleep(Duration::from_secs(1)).await; |
| 40 | let matrix = plugin(); |
| 41 | matrix.autoconnect(); |
| 42 | }); |
| 43 | |
| 44 | Ok(Matrix { servers }) |
| 38 | 45 | } |
| 39 | 46 | } |
| 40 | 47 | |
| 41 | 48 | impl Drop for Matrix { |
| 42 | 49 | fn drop(&mut self) { |
| 43 | | let runtime = self.tokio.take(); |
| 44 | | |
| 45 | | if let Some(r) = runtime { |
| 46 | | r.shutdown_now(); |
| 50 | for server in self.servers.values_mut() { |
| 51 | server.disconnect(); |
| 47 | 52 | } |
| 53 | |
| 48 | 54 | cleanup_executor(); |
| 49 | 55 | } |
| 50 | 56 | } |
src/room_buffer.rs
+1 −1
| @@ -89,7 +89,7 @@ impl RoomBuffer { |
| 89 | 89 | let message = match content.membership { |
| 90 | 90 | MembershipState::Join => "joined", |
| 91 | 91 | MembershipState::Leave => "left", |
| 92 | | _ => return (), |
| 92 | _ => return, |
| 93 | 93 | }; |
| 94 | 94 | |
| 95 | 95 | let message = format!( |
src/server.rs
+11 −1
| @@ -51,6 +51,7 @@ pub struct LoginState { |
| 51 | 51 | struct ConnectedState { |
| 52 | 52 | client_channel: Sender<ServerMessage>, |
| 53 | 53 | homeserver: Url, |
| 54 | runtime: Runtime, |
| 54 | 55 | } |
| 55 | 56 | |
| 56 | 57 | pub(crate) struct MatrixServer { |
| @@ -78,8 +79,9 @@ impl MatrixServer { |
| 78 | 79 | &self.server_name |
| 79 | 80 | } |
| 80 | 81 | |
| 81 | | pub fn connect(&mut self, runtime: &Runtime) { |
| 82 | pub fn connect(&mut self) { |
| 82 | 83 | let homeserver = Url::parse("http://localhost:8008").unwrap(); |
| 84 | let runtime = Runtime::new().unwrap(); |
| 83 | 85 | |
| 84 | 86 | let config = AsyncClientConfig::new() |
| 85 | 87 | .proxy("http://localhost:8080") |
| @@ -102,9 +104,17 @@ impl MatrixServer { |
| 102 | 104 | self.connected_state = Some(ConnectedState { |
| 103 | 105 | client_channel: client_sender, |
| 104 | 106 | homeserver, |
| 107 | runtime, |
| 105 | 108 | }); |
| 106 | 109 | } |
| 107 | 110 | |
| 111 | pub fn disconnect(&mut self) { |
| 112 | let connected_state = self.connected_state.take(); |
| 113 | if let Some(s) = connected_state { |
| 114 | s.runtime.shutdown_now(); |
| 115 | } |
| 116 | } |
| 117 | |
| 108 | 118 | pub async fn sync_loop( |
| 109 | 119 | mut client: AsyncClient, |
| 110 | 120 | channel: Sender<Result<ThreadMessage, String>>, |