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};
8#[derive(Clone)] 8#[derive(Clone)]
9pub struct Config(Rc<RefCell<weechat::config::Config>>); 9pub struct Config(Rc<RefCell<weechat::config::Config>>);
10 10
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
11fn server_write_cb( 24fn server_write_cb(
12 _weechat: &Weechat, 25 _weechat: &Weechat,
13 config: &Conf, 26 config: &Conf,
@@ -23,7 +36,7 @@ fn server_write_cb(
23impl Config { 36impl Config {
24 pub fn new(weechat: &Weechat, servers: &Servers) -> Config { 37 pub fn new(weechat: &Weechat, servers: &Servers) -> Config {
25 let mut config = weechat 38 let mut config = weechat
26 .config_new("matrix-rust", |weechat, conf| {}) 39 .config_new("matrix-rust")
27 .expect("Can't create new config"); 40 .expect("Can't create new config");
28 41
29 let servers = servers.clone_weak(); 42 let servers = servers.clone_weak();
@@ -100,7 +113,7 @@ impl Config {
100 self.0.borrow_mut() 113 self.0.borrow_mut()
101 } 114 }
102 115
103 pub fn clone_weak(&self) -> Weak<RefCell<weechat::config::Config>> { 116 pub fn clone_weak(&self) -> ConfigHandle {
104 Rc::downgrade(&self.0) 117 ConfigHandle(Rc::downgrade(&self.0))
105 } 118 }
106} 119}
src/room_buffer.rs +2 −1
@@ -8,7 +8,7 @@ use url::Url;
8 8
9use crate::executor::spawn_weechat; 9use crate::executor::spawn_weechat;
10use crate::server::Connection; 10use crate::server::Connection;
11use crate::PLUGIN_NAME; 11use crate::{Config, PLUGIN_NAME};
12use std::borrow::Cow; 12use std::borrow::Cow;
13use std::cell::RefCell; 13use std::cell::RefCell;
14use std::rc::{Rc, Weak}; 14use std::rc::{Rc, Weak};
@@ -36,6 +36,7 @@ impl RoomBuffer {
36 server_name: &str, 36 server_name: &str,
37 connected_state: &Rc<RefCell<Option<Connection>>>, 37 connected_state: &Rc<RefCell<Option<Connection>>>,
38 homeserver: &Url, 38 homeserver: &Url,
39 config: &Config,
39 room_id: &str, 40 room_id: &str,
40 own_user_id: &str, 41 own_user_id: &str,
41 ) -> Self { 42 ) -> Self {
src/server.rs +23 −7
@@ -56,7 +56,7 @@ pub struct LoginState {
56 56
57pub struct Connection { 57pub struct Connection {
58 client_channel: Sender<ServerMessage>, 58 client_channel: Sender<ServerMessage>,
59 sync_receiver: JoinHandle<(), ()>, 59 response_receiver: JoinHandle<(), ()>,
60 runtime: Runtime, 60 runtime: Runtime,
61} 61}
62 62
@@ -83,6 +83,7 @@ pub(crate) struct InnerServer {
83 connected: bool, 83 connected: bool,
84 room_buffers: HashMap<String, RoomBuffer>, 84 room_buffers: HashMap<String, RoomBuffer>,
85 settings: ServerSettings, 85 settings: ServerSettings,
86 config: Config,
86 homeserver: Url, 87 homeserver: Url,
87 login_state: Option<LoginState>, 88 login_state: Option<LoginState>,
88 connected_state: Rc<RefCell<Option<Connection>>>, 89 connected_state: Rc<RefCell<Option<Connection>>>,
@@ -115,6 +116,7 @@ impl MatrixServer {
115 connected: false, 116 connected: false,
116 room_buffers: HashMap::new(), 117 room_buffers: HashMap::new(),
117 settings: ServerSettings { homeserver: None }, 118 settings: ServerSettings { homeserver: None },
119 config: config.clone(),
118 homeserver, 120 homeserver,
119 login_state: None, 121 login_state: None,
120 connected_state: Rc::new(RefCell::new(None)), 122 connected_state: Rc::new(RefCell::new(None)),
@@ -171,6 +173,7 @@ impl InnerServer {
171 &self.server_name, 173 &self.server_name,
172 &self.connected_state, 174 &self.connected_state,
173 &self.homeserver, 175 &self.homeserver,
176 &self.config,
174 room_id, 177 room_id,
175 &login_state.user_id, 178 &login_state.user_id,
176 ); 179 );
@@ -214,19 +217,23 @@ impl MatrixServer {
214 217
215 let send_client = self.client.clone(); 218 let send_client = self.client.clone();
216 let (tx, rx) = async_channel(1000); 219 let (tx, rx) = async_channel(1000);
217 runtime.spawn(MatrixServer::sync_loop(self.client.clone(), tx)); 220 runtime.spawn(MatrixServer::sync_loop(self.client.clone(), tx.clone()));
218 let sync_receiver_handle = spawn_weechat(MatrixServer::sync_receiver( 221 let response_receiver = spawn_weechat(MatrixServer::response_receiver(
219 rx, 222 rx,
220 Rc::downgrade(&self.inner), 223 Rc::downgrade(&self.inner),
221 )); 224 ));
222 225
223 let (client_sender, client_receiver) = async_channel(10); 226 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 ));
225 232
226 let mut connected_state = server.connected_state.borrow_mut(); 233 let mut connected_state = server.connected_state.borrow_mut();
227 234
228 *connected_state = Some(Connection { 235 *connected_state = Some(Connection {
229 sync_receiver: sync_receiver_handle, 236 response_receiver,
230 client_channel: client_sender, 237 client_channel: client_sender,
231 runtime, 238 runtime,
232 }); 239 });
@@ -239,10 +246,13 @@ impl MatrixServer {
239 246
240 if let Some(s) = state { 247 if let Some(s) = state {
241 s.runtime.shutdown_now(); 248 s.runtime.shutdown_now();
242 s.sync_receiver.cancel(); 249 s.response_receiver.cancel();
243 } 250 }
244 } 251 }
245 252
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.
246 pub async fn sync_loop( 256 pub async fn sync_loop(
247 mut client: AsyncClient, 257 mut client: AsyncClient,
248 channel: Sender<Result<ThreadMessage, String>>, 258 channel: Sender<Result<ThreadMessage, String>>,
@@ -305,7 +315,10 @@ impl MatrixServer {
305 .await; 315 .await;
306 } 316 }
307 317
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(
309 receiver: Receiver<Result<ThreadMessage, String>>, 322 receiver: Receiver<Result<ThreadMessage, String>>,
310 server: Weak<RefCell<InnerServer>>, 323 server: Weak<RefCell<InnerServer>>,
311 ) { 324 ) {
@@ -340,9 +353,12 @@ impl MatrixServer {
340 } 353 }
341 } 354 }
342 355
356 /// Send loop that waits for requests that need to be sent out using our
357 /// Matrix client.
343 pub async fn send_loop( 358 pub async fn send_loop(
344 mut client: AsyncClient, 359 mut client: AsyncClient,
345 channel: Receiver<ServerMessage>, 360 channel: Receiver<ServerMessage>,
361 sender: Sender<Result<ThreadMessage, String>>,
346 ) { 362 ) {
347 while let Some(message) = channel.recv().await { 363 while let Some(message) = channel.recv().await {
348 match message { 364 match message {