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;
3mod server; 3mod server;
4 4
5use std::collections::HashMap; 5use std::collections::HashMap;
6use tokio::runtime::Runtime; 6use std::time::Duration;
7 7
8use weechat::{ 8use weechat::{
9 weechat_plugin, ArgsWeechat, Weechat, WeechatPlugin, WeechatResult, 9 weechat_plugin, ArgsWeechat, Weechat, WeechatPlugin, WeechatResult,
10}; 10};
11 11
12use crate::executor::cleanup_executor; 12use crate::executor::{cleanup_executor, spawn_weechat};
13use crate::server::MatrixServer; 13use crate::server::MatrixServer;
14 14
15const PLUGIN_NAME: &str = "matrix"; 15const PLUGIN_NAME: &str = "matrix";
16 16
17struct Matrix { 17struct Matrix {
18 tokio: Option<Runtime>,
19 servers: HashMap<String, MatrixServer>, 18 servers: HashMap<String, MatrixServer>,
20} 19}
21 20
21impl Matrix {
22 fn autoconnect(&mut self) {
23 for server in self.servers.values_mut() {
24 server.connect();
25 }
26 }
27}
28
22impl WeechatPlugin for Matrix { 29impl WeechatPlugin for Matrix {
23 fn init(weechat: &Weechat, _args: ArgsWeechat) -> WeechatResult<Self> { 30 fn init(weechat: &Weechat, _args: ArgsWeechat) -> WeechatResult<Self> {
24 let runtime = Runtime::new().unwrap();
25
26 let server_name = "localhost"; 31 let server_name = "localhost";
27 32
28 let mut server = MatrixServer::new(server_name); 33 let mut server = MatrixServer::new(server_name);
29 let mut servers = HashMap::new(); 34 let mut servers = HashMap::new();
30 35
31 server.connect(&runtime);
32 servers.insert(server_name.to_owned(), server); 36 servers.insert(server_name.to_owned(), server);
33 37
34 Ok(Matrix { 38 spawn_weechat(async move {
35 tokio: Some(runtime), 39 async_std::task::sleep(Duration::from_secs(1)).await;
36 servers, 40 let matrix = plugin();
37 }) 41 matrix.autoconnect();
42 });
43
44 Ok(Matrix { servers })
38 } 45 }
39} 46}
40 47
41impl Drop for Matrix { 48impl Drop for Matrix {
42 fn drop(&mut self) { 49 fn drop(&mut self) {
43 let runtime = self.tokio.take(); 50 for server in self.servers.values_mut() {
44 51 server.disconnect();
45 if let Some(r) = runtime {
46 r.shutdown_now();
47 } 52 }
53
48 cleanup_executor(); 54 cleanup_executor();
49 } 55 }
50} 56}
src/room_buffer.rs +1 −1
@@ -89,7 +89,7 @@ impl RoomBuffer {
89 let message = match content.membership { 89 let message = match content.membership {
90 MembershipState::Join => "joined", 90 MembershipState::Join => "joined",
91 MembershipState::Leave => "left", 91 MembershipState::Leave => "left",
92 _ => return (), 92 _ => return,
93 }; 93 };
94 94
95 let message = format!( 95 let message = format!(
src/server.rs +11 −1
@@ -51,6 +51,7 @@ pub struct LoginState {
51struct ConnectedState { 51struct ConnectedState {
52 client_channel: Sender<ServerMessage>, 52 client_channel: Sender<ServerMessage>,
53 homeserver: Url, 53 homeserver: Url,
54 runtime: Runtime,
54} 55}
55 56
56pub(crate) struct MatrixServer { 57pub(crate) struct MatrixServer {
@@ -78,8 +79,9 @@ impl MatrixServer {
78 &self.server_name 79 &self.server_name
79 } 80 }
80 81
81 pub fn connect(&mut self, runtime: &Runtime) { 82 pub fn connect(&mut self) {
82 let homeserver = Url::parse("http://localhost:8008").unwrap(); 83 let homeserver = Url::parse("http://localhost:8008").unwrap();
84 let runtime = Runtime::new().unwrap();
83 85
84 let config = AsyncClientConfig::new() 86 let config = AsyncClientConfig::new()
85 .proxy("http://localhost:8080") 87 .proxy("http://localhost:8080")
@@ -102,9 +104,17 @@ impl MatrixServer {
102 self.connected_state = Some(ConnectedState { 104 self.connected_state = Some(ConnectedState {
103 client_channel: client_sender, 105 client_channel: client_sender,
104 homeserver, 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 pub async fn sync_loop( 118 pub async fn sync_loop(
109 mut client: AsyncClient, 119 mut client: AsyncClient,
110 channel: Sender<Result<ThreadMessage, String>>, 120 channel: Sender<Result<ThreadMessage, String>>,