Commit 5c1cf0dafd

5c1cf0dafd71169d39428b82706d761dcb59012e

parent: 28e2ed64fa

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2019-12-22 21:09 UTC

config: Let the server and buffers keep a handle to the config.

Layout: unified · split

src/config.rs +16 −3
@@ -8,6 +8,19 @@ use std::rc::{Rc, Weak};
88#[derive(Clone)]
99pub struct Config(Rc<RefCell<weechat::config::Config>>);
1010
11#[derive(Clone)]
12pub struct ConfigHandle(Weak<RefCell<weechat::config::Config>>);
13
14impl ConfigHandle {
15 fn upgrade(&self) -> Config {
16 Config(
17 self.0
18 .upgrade()
19 .expect("Config got deleted before plugin unload"),
20 )
21 }
22}
23
1124fn server_write_cb(
1225 _weechat: &Weechat,
1326 config: &Conf,
@@ -23,7 +36,7 @@ fn server_write_cb(
2336impl Config {
2437 pub fn new(weechat: &Weechat, servers: &Servers) -> Config {
2538 let mut config = weechat
26 .config_new("matrix-rust", |weechat, conf| {})
39 .config_new("matrix-rust")
2740 .expect("Can't create new config");
2841
2942 let servers = servers.clone_weak();
@@ -100,7 +113,7 @@ impl Config {
100113 self.0.borrow_mut()
101114 }
102115
103 pub fn clone_weak(&self) -> Weak<RefCell<weechat::config::Config>> {
104 Rc::downgrade(&self.0)
116 pub fn clone_weak(&self) -> ConfigHandle {
117 ConfigHandle(Rc::downgrade(&self.0))
105118 }
106119}
src/room_buffer.rs +2 −1
@@ -8,7 +8,7 @@ use url::Url;
88
99use crate::executor::spawn_weechat;
1010use crate::server::Connection;
11use crate::PLUGIN_NAME;
11use crate::{Config, PLUGIN_NAME};
1212use std::borrow::Cow;
1313use std::cell::RefCell;
1414use std::rc::{Rc, Weak};
@@ -36,6 +36,7 @@ impl RoomBuffer {
3636 server_name: &str,
3737 connected_state: &Rc<RefCell<Option<Connection>>>,
3838 homeserver: &Url,
39 config: &Config,
3940 room_id: &str,
4041 own_user_id: &str,
4142 ) -> Self {
src/server.rs +23 −7
@@ -56,7 +56,7 @@ pub struct LoginState {
5656
5757pub struct Connection {
5858 client_channel: Sender<ServerMessage>,
59 sync_receiver: JoinHandle<(), ()>,
59 response_receiver: JoinHandle<(), ()>,
6060 runtime: Runtime,
6161}
6262
@@ -83,6 +83,7 @@ pub(crate) struct InnerServer {
8383 connected: bool,
8484 room_buffers: HashMap<String, RoomBuffer>,
8585 settings: ServerSettings,
86 config: Config,
8687 homeserver: Url,
8788 login_state: Option<LoginState>,
8889 connected_state: Rc<RefCell<Option<Connection>>>,
@@ -115,6 +116,7 @@ impl MatrixServer {
115116 connected: false,
116117 room_buffers: HashMap::new(),
117118 settings: ServerSettings { homeserver: None },
119 config: config.clone(),
118120 homeserver,
119121 login_state: None,
120122 connected_state: Rc::new(RefCell::new(None)),
@@ -171,6 +173,7 @@ impl InnerServer {
171173 &self.server_name,
172174 &self.connected_state,
173175 &self.homeserver,
176 &self.config,
174177 room_id,
175178 &login_state.user_id,
176179 );
@@ -214,19 +217,23 @@ impl MatrixServer {
214217
215218 let send_client = self.client.clone();
216219 let (tx, rx) = async_channel(1000);
217 runtime.spawn(MatrixServer::sync_loop(self.client.clone(), tx));
218 let sync_receiver_handle = spawn_weechat(MatrixServer::sync_receiver(
220 runtime.spawn(MatrixServer::sync_loop(self.client.clone(), tx.clone()));
221 let response_receiver = spawn_weechat(MatrixServer::response_receiver(
219222 rx,
220223 Rc::downgrade(&self.inner),
221224 ));
222225
223226 let (client_sender, client_receiver) = async_channel(10);
224 runtime.spawn(MatrixServer::send_loop(send_client, client_receiver));
227 runtime.spawn(MatrixServer::send_loop(
228 send_client,
229 client_receiver,
230 tx,
231 ));
225232
226233 let mut connected_state = server.connected_state.borrow_mut();
227234
228235 *connected_state = Some(Connection {
229 sync_receiver: sync_receiver_handle,
236 response_receiver,
230237 client_channel: client_sender,
231238 runtime,
232239 });
@@ -239,10 +246,13 @@ impl MatrixServer {
239246
240247 if let Some(s) = state {
241248 s.runtime.shutdown_now();
242 s.sync_receiver.cancel();
249 s.response_receiver.cancel();
243250 }
244251 }
245252
253 /// Main client sync loop.
254 /// This runs on the per server tokio executor.
255 /// It communicates with the main Weechat thread using a async channel.
246256 pub async fn sync_loop(
247257 mut client: AsyncClient,
248258 channel: Sender<Result<ThreadMessage, String>>,
@@ -305,7 +315,10 @@ impl MatrixServer {
305315 .await;
306316 }
307317
308 pub async fn sync_receiver(
318 /// Response receiver loop.
319 /// This runs on the main Weechat thread and listens for responses coming
320 /// from the client running in the tokio executor.
321 pub async fn response_receiver(
309322 receiver: Receiver<Result<ThreadMessage, String>>,
310323 server: Weak<RefCell<InnerServer>>,
311324 ) {
@@ -340,9 +353,12 @@ impl MatrixServer {
340353 }
341354 }
342355
356 /// Send loop that waits for requests that need to be sent out using our
357 /// Matrix client.
343358 pub async fn send_loop(
344359 mut client: AsyncClient,
345360 channel: Receiver<ServerMessage>,
361 sender: Sender<Result<ThreadMessage, String>>,
346362 ) {
347363 while let Some(message) = channel.recv().await {
348364 match message {