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;
33mod server;
44
55use std::collections::HashMap;
6use tokio::runtime::Runtime;
6use std::time::Duration;
77
88use weechat::{
99 weechat_plugin, ArgsWeechat, Weechat, WeechatPlugin, WeechatResult,
1010};
1111
12use crate::executor::cleanup_executor;
12use crate::executor::{cleanup_executor, spawn_weechat};
1313use crate::server::MatrixServer;
1414
1515const PLUGIN_NAME: &str = "matrix";
1616
1717struct Matrix {
18 tokio: Option<Runtime>,
1918 servers: HashMap<String, MatrixServer>,
2019}
2120
21impl Matrix {
22 fn autoconnect(&mut self) {
23 for server in self.servers.values_mut() {
24 server.connect();
25 }
26 }
27}
28
2229impl WeechatPlugin for Matrix {
2330 fn init(weechat: &Weechat, _args: ArgsWeechat) -> WeechatResult<Self> {
24 let runtime = Runtime::new().unwrap();
25
2631 let server_name = "localhost";
2732
2833 let mut server = MatrixServer::new(server_name);
2934 let mut servers = HashMap::new();
3035
31 server.connect(&runtime);
3236 servers.insert(server_name.to_owned(), server);
3337
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 })
3845 }
3946}
4047
4148impl Drop for Matrix {
4249 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();
4752 }
53
4854 cleanup_executor();
4955 }
5056}
src/room_buffer.rs +1 −1
@@ -89,7 +89,7 @@ impl RoomBuffer {
8989 let message = match content.membership {
9090 MembershipState::Join => "joined",
9191 MembershipState::Leave => "left",
92 _ => return (),
92 _ => return,
9393 };
9494
9595 let message = format!(
src/server.rs +11 −1
@@ -51,6 +51,7 @@ pub struct LoginState {
5151struct ConnectedState {
5252 client_channel: Sender<ServerMessage>,
5353 homeserver: Url,
54 runtime: Runtime,
5455}
5556
5657pub(crate) struct MatrixServer {
@@ -78,8 +79,9 @@ impl MatrixServer {
7879 &self.server_name
7980 }
8081
81 pub fn connect(&mut self, runtime: &Runtime) {
82 pub fn connect(&mut self) {
8283 let homeserver = Url::parse("http://localhost:8008").unwrap();
84 let runtime = Runtime::new().unwrap();
8385
8486 let config = AsyncClientConfig::new()
8587 .proxy("http://localhost:8080")
@@ -102,9 +104,17 @@ impl MatrixServer {
102104 self.connected_state = Some(ConnectedState {
103105 client_channel: client_sender,
104106 homeserver,
107 runtime,
105108 });
106109 }
107110
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
108118 pub async fn sync_loop(
109119 mut client: AsyncClient,
110120 channel: Sender<Result<ThreadMessage, String>>,