Commit 61a3db2fae
Unsigned
Layout: unified · split
Cargo.toml +2 −5
| @@ -11,15 +11,12 @@ crate-type = ["dylib"] | ||
| 11 | 11 | |
| 12 | 12 | [dependencies] |
| 13 | 13 | libc = "0.2.66" |
| 14 | futures = "0.3.1" | |
| 15 | 14 | url = "2.1.1" |
| 16 | 15 | tokio = { version = "0.2.9", features = [ "rt-threaded" ] } |
| 17 | 16 | async-std = { version = "1.4.0", features = [ "unstable" ] } |
| 18 | async-task = "1.2.1" | |
| 19 | pipe-channel = "1.2.2" | |
| 20 | 17 | clap = "2.33.0" |
| 21 | 18 | |
| 22 | 19 | matrix-nio = { git = "https://github.com/poljar/nio-rust" } |
| 23 | 20 | |
| 24 | weechat = { git = "https://github.com/poljar/rust-weechat/" } | |
| 25 | weechat-sys = { git = "https://github.com/poljar/rust-weechat/" } | |
| 21 | weechat = { path = "/home/poljar/werk/weechat/rust-weechat/weechat-rs/", features = ["async-executor"] } | |
| 22 | weechat-sys = { path = "/home/poljar/werk/weechat/rust-weechat/weechat-sys" } | |
Makefile +1 −1
| @@ -3,7 +3,7 @@ PREFIX ?= $(WEECHAT_HOME) | ||
| 3 | 3 | |
| 4 | 4 | .PHONY: install install-dir phony lint |
| 5 | 5 | |
| 6 | target/debug/libmatrix.so: src/lib.rs src/server.rs src/room_buffer.rs src/executor.rs src/commands.rs src/config.rs | |
| 6 | target/debug/libmatrix.so: src/lib.rs src/server.rs src/room_buffer.rs src/commands.rs src/config.rs | |
| 7 | 7 | cargo build |
| 8 | 8 | |
| 9 | 9 | install: install-dir target/debug/libmatrix.so |
src/executor.rs deleted −117
| @@ -1,117 +0,0 @@ | ||
| 1 | use async_task::JoinHandle; | |
| 2 | use pipe_channel::{channel, Receiver, Sender}; | |
| 3 | use std::collections::VecDeque; | |
| 4 | use std::future::Future; | |
| 5 | use std::sync::{Arc, Mutex, Weak}; | |
| 6 | ||
| 7 | use weechat::{FdHook, FdHookMode, Weechat}; | |
| 8 | ||
| 9 | static mut _EXECUTOR: Option<WeechatExecutor> = None; | |
| 10 | ||
| 11 | type Job = async_task::Task<()>; | |
| 12 | type FutureQueue = Arc<Mutex<VecDeque<Job>>>; | |
| 13 | type FutureQueueHandle = Weak<Mutex<VecDeque<Job>>>; | |
| 14 | ||
| 15 | pub struct WeechatExecutor { | |
| 16 | _hook: Option<FdHook<FutureQueueHandle, Receiver<()>>>, | |
| 17 | sender: Arc<Mutex<Sender<()>>>, | |
| 18 | futures: FutureQueue, | |
| 19 | } | |
| 20 | ||
| 21 | impl WeechatExecutor { | |
| 22 | fn new() -> Self { | |
| 23 | let weechat = unsafe { Weechat::weechat() }; | |
| 24 | ||
| 25 | let (sender, receiver) = channel(); | |
| 26 | let sender = Arc::new(Mutex::new(sender)); | |
| 27 | let queue = Arc::new(Mutex::new(VecDeque::new())); | |
| 28 | ||
| 29 | let hook = weechat | |
| 30 | .hook_fd( | |
| 31 | receiver, | |
| 32 | FdHookMode::Read, | |
| 33 | WeechatExecutor::executor_cb, | |
| 34 | Some(Arc::downgrade(&queue)), | |
| 35 | ) | |
| 36 | .expect("Can't create executor FD hook"); | |
| 37 | ||
| 38 | WeechatExecutor { | |
| 39 | _hook: Some(hook), | |
| 40 | sender, | |
| 41 | futures: queue, | |
| 42 | } | |
| 43 | } | |
| 44 | ||
| 45 | fn executor_cb(future_queue: &FutureQueueHandle, receiver: &mut Receiver<()>) { | |
| 46 | if receiver.recv().is_err() { | |
| 47 | return; | |
| 48 | } | |
| 49 | ||
| 50 | let futures = future_queue.upgrade(); | |
| 51 | ||
| 52 | if let Some(q) = futures { | |
| 53 | let mut futures = q.lock().unwrap(); | |
| 54 | let task = futures.pop_front(); | |
| 55 | ||
| 56 | // Drop the lock here so we can spawn new futures from the currently | |
| 57 | // running one. | |
| 58 | drop(futures); | |
| 59 | ||
| 60 | if let Some(task) = task { | |
| 61 | task.run(); | |
| 62 | } | |
| 63 | } | |
| 64 | } | |
| 65 | ||
| 66 | pub fn free() { | |
| 67 | unsafe { | |
| 68 | _EXECUTOR.take(); | |
| 69 | } | |
| 70 | } | |
| 71 | } | |
| 72 | ||
| 73 | pub fn spawn_weechat<F, R>(future: F) -> JoinHandle<R, ()> | |
| 74 | where | |
| 75 | F: Future<Output = R> + 'static, | |
| 76 | R: 'static, | |
| 77 | { | |
| 78 | let executor = unsafe { | |
| 79 | if _EXECUTOR.is_none() { | |
| 80 | let executor = WeechatExecutor::new(); | |
| 81 | _EXECUTOR = Some(executor); | |
| 82 | } | |
| 83 | ||
| 84 | _EXECUTOR.as_ref().expect("Executor wasn't started") | |
| 85 | }; | |
| 86 | ||
| 87 | let sender = Arc::downgrade(&executor.sender); | |
| 88 | let queue = Arc::downgrade(&executor.futures); | |
| 89 | ||
| 90 | let schedule = move |task| { | |
| 91 | let sender = sender.upgrade(); | |
| 92 | let queue = queue.upgrade(); | |
| 93 | ||
| 94 | if let Some(q) = queue { | |
| 95 | let sender = sender | |
| 96 | .expect("Futures queue exists but the channel got dropped"); | |
| 97 | let mut weechat_notify = sender | |
| 98 | .lock() | |
| 99 | .expect("Weechat notification sender lock is poisoned"); | |
| 100 | ||
| 101 | let mut queue = q.lock().expect( | |
| 102 | "Lock of the future queue of the Weechat executor is poisoned", | |
| 103 | ); | |
| 104 | ||
| 105 | queue.push_back(task); | |
| 106 | weechat_notify | |
| 107 | .send(()) | |
| 108 | .expect("Can't notify Weechat to run a future"); | |
| 109 | } | |
| 110 | }; | |
| 111 | ||
| 112 | let (task, handle) = async_task::spawn_local(future, schedule, ()); | |
| 113 | ||
| 114 | task.schedule(); | |
| 115 | ||
| 116 | handle | |
| 117 | } | |
src/lib.rs +1 −5
| @@ -2,7 +2,6 @@ | ||
| 2 | 2 | |
| 3 | 3 | mod commands; |
| 4 | 4 | mod config; |
| 5 | mod executor; | |
| 6 | 5 | mod room_buffer; |
| 7 | 6 | mod server; |
| 8 | 7 | |
| @@ -16,7 +15,6 @@ use weechat::{ | ||
| 16 | 15 | |
| 17 | 16 | use crate::commands::Commands; |
| 18 | 17 | use crate::config::Config; |
| 19 | use crate::executor::{spawn_weechat, WeechatExecutor}; | |
| 20 | 18 | use crate::server::MatrixServer; |
| 21 | 19 | |
| 22 | 20 | const PLUGIN_NAME: &str = "matrix"; |
| @@ -111,7 +109,7 @@ impl WeechatPlugin for Matrix { | ||
| 111 | 109 | } |
| 112 | 110 | } |
| 113 | 111 | |
| 114 | spawn_weechat(async move { | |
| 112 | Weechat::spawn(async move { | |
| 115 | 113 | let mut servers = servers.borrow_mut(); |
| 116 | 114 | Matrix::autoconnect(&mut servers); |
| 117 | 115 | }); |
| @@ -126,8 +124,6 @@ impl Drop for Matrix { | ||
| 126 | 124 | for server in servers.values_mut() { |
| 127 | 125 | server.disconnect(); |
| 128 | 126 | } |
| 129 | ||
| 130 | WeechatExecutor::free(); | |
| 131 | 127 | } |
| 132 | 128 | } |
| 133 | 129 | |
src/room_buffer.rs +1 −2
| @@ -6,7 +6,6 @@ use matrix_nio::events::room::message::{ | ||
| 6 | 6 | use matrix_nio::Room; |
| 7 | 7 | use url::Url; |
| 8 | 8 | |
| 9 | use crate::executor::spawn_weechat; | |
| 10 | 9 | use crate::server::Connection; |
| 11 | 10 | use crate::{Config, PLUGIN_NAME}; |
| 12 | 11 | use std::borrow::Cow; |
| @@ -98,7 +97,7 @@ impl RoomBuffer { | ||
| 98 | 97 | s.send_message(&room_id, &input).await; |
| 99 | 98 | } |
| 100 | 99 | }; |
| 101 | spawn_weechat(task); | |
| 100 | Weechat::spawn(task); | |
| 102 | 101 | } |
| 103 | 102 | |
| 104 | 103 | pub fn close_callback(data: &String, buffer: &Buffer) {} |
src/server.rs +2 −3
| @@ -1,6 +1,5 @@ | ||
| 1 | 1 | use async_std::sync::channel as async_channel; |
| 2 | 2 | use async_std::sync::{Receiver, Sender}; |
| 3 | use async_task::JoinHandle; | |
| 4 | 3 | use std::cell::RefCell; |
| 5 | 4 | use std::collections::HashMap; |
| 6 | 5 | use std::rc::{Rc, Weak}; |
| @@ -19,12 +18,12 @@ use matrix_nio::{ | ||
| 19 | 18 | AsyncClient, AsyncClientConfig, SyncSettings, |
| 20 | 19 | }; |
| 21 | 20 | |
| 21 | use weechat::JoinHandle; | |
| 22 | 22 | use weechat::config::{ |
| 23 | 23 | BooleanOptionSettings, ConfigSection, StringOptionSettings, |
| 24 | 24 | }; |
| 25 | 25 | use weechat::Weechat; |
| 26 | 26 | |
| 27 | use crate::executor::spawn_weechat; | |
| 28 | 27 | use crate::room_buffer::RoomBuffer; |
| 29 | 28 | use crate::Config; |
| 30 | 29 | use crate::PLUGIN_NAME; |
| @@ -336,7 +335,7 @@ impl MatrixServer { | ||
| 336 | 335 | |
| 337 | 336 | let (tx, rx) = async_channel(1000); |
| 338 | 337 | runtime.spawn(MatrixServer::sync_loop(client.clone(), tx.clone())); |
| 339 | let response_receiver = spawn_weechat(MatrixServer::response_receiver( | |
| 338 | let response_receiver = Weechat::spawn(MatrixServer::response_receiver( | |
| 340 | 339 | rx, |
| 341 | 340 | Rc::downgrade(&self.inner), |
| 342 | 341 | )); |