| @@ -1,27 +1,22 @@ |
| 1 | mod executor; |
| 1 | 2 | mod room_buffer; |
| 2 | 3 | mod server; |
| 3 | 4 | |
| 4 | | use std::sync::{Arc, Mutex}; |
| 5 | 5 | use std::time::Duration; |
| 6 | 6 | use url::Url; |
| 7 | 7 | |
| 8 | | use pipe_channel::{channel, Receiver, Sender}; |
| 9 | 8 | use tokio::runtime::Runtime; |
| 10 | 9 | |
| 11 | 10 | use async_std; |
| 12 | 11 | use async_std::sync::channel as async_channel; |
| 13 | 12 | use async_std::sync::Receiver as AsyncReceiver; |
| 14 | 13 | use async_std::sync::Sender as AsyncSender; |
| 15 | | use async_task; |
| 16 | 14 | use std::collections::HashMap; |
| 17 | | use std::collections::VecDeque; |
| 18 | | use std::future::Future; |
| 19 | 15 | |
| 20 | 16 | use server::{MatrixServer, ServerMessage}; |
| 21 | 17 | |
| 22 | 18 | use weechat::{ |
| 23 | | weechat_plugin, ArgsWeechat, FdHook, FdHookMode, Weechat, WeechatPlugin, |
| 24 | | WeechatResult, |
| 19 | weechat_plugin, ArgsWeechat, Weechat, WeechatPlugin, WeechatResult, |
| 25 | 20 | }; |
| 26 | 21 | |
| 27 | 22 | use matrix_nio::api::r0::session::login::Response as LoginResponse; |
| @@ -29,13 +24,13 @@ use matrix_nio::{ |
| 29 | 24 | self, |
| 30 | 25 | events::{ |
| 31 | 26 | collections::all::{RoomEvent, StateEvent}, |
| 32 | | room::message::{ |
| 33 | | MessageEvent, MessageEventContent, TextMessageEventContent, |
| 34 | | }, |
| 27 | room::message::{MessageEventContent, TextMessageEventContent}, |
| 35 | 28 | }, |
| 36 | 29 | AsyncClient, AsyncClientConfig, SyncSettings, |
| 37 | 30 | }; |
| 38 | 31 | |
| 32 | use crate::executor::{cleanup_executor, spawn_weechat}; |
| 33 | |
| 39 | 34 | pub enum ThreadMessage { |
| 40 | 35 | LoginMessage(LoginResponse), |
| 41 | 36 | SyncState(String, StateEvent), |
| @@ -49,78 +44,6 @@ struct Matrix { |
| 49 | 44 | servers: HashMap<String, MatrixServer>, |
| 50 | 45 | } |
| 51 | 46 | |
| 52 | | fn spawn_cb(future_queue: &FutureQueue, receiver: &mut Receiver<()>) { |
| 53 | | receiver.recv().unwrap(); |
| 54 | | |
| 55 | | let mut queue = future_queue.lock().unwrap(); |
| 56 | | let task = queue.pop_front(); |
| 57 | | |
| 58 | | if let Some(task) = task { |
| 59 | | task.run(); |
| 60 | | } |
| 61 | | } |
| 62 | | |
| 63 | | type Job = async_task::Task<()>; |
| 64 | | |
| 65 | | static mut _FUTURE_HOOK: Option<FdHook<FutureQueue, Receiver<()>>> = None; |
| 66 | | static mut _SENDER: Option<Arc<Mutex<Sender<()>>>> = None; |
| 67 | | static mut _FUTURE_QUEUE: Option<FutureQueue> = None; |
| 68 | | |
| 69 | | type FutureQueue = Arc<Mutex<VecDeque<Job>>>; |
| 70 | | |
| 71 | | fn spawn_weechat<F, T>(future: F) |
| 72 | | where |
| 73 | | F: Future<Output = T> + 'static, |
| 74 | | T: 'static, |
| 75 | | { |
| 76 | | let weechat = unsafe { Weechat::weechat() }; |
| 77 | | |
| 78 | | unsafe { |
| 79 | | if _FUTURE_HOOK.is_none() { |
| 80 | | let (sender, receiver) = channel(); |
| 81 | | let sender = Arc::new(Mutex::new(sender)); |
| 82 | | _SENDER = Some(sender); |
| 83 | | let queue = Arc::new(Mutex::new(VecDeque::new())); |
| 84 | | _FUTURE_QUEUE = Some(queue.clone()); |
| 85 | | |
| 86 | | let fd_hook = weechat.hook_fd( |
| 87 | | receiver, |
| 88 | | FdHookMode::Read, |
| 89 | | spawn_cb, |
| 90 | | Some(queue), |
| 91 | | ); |
| 92 | | _FUTURE_HOOK = Some(fd_hook); |
| 93 | | } |
| 94 | | } |
| 95 | | |
| 96 | | let weechat_notify = unsafe { |
| 97 | | if let Some(s) = &_SENDER { |
| 98 | | s.clone() |
| 99 | | } else { |
| 100 | | panic!("Future queue wasn't initialized") |
| 101 | | } |
| 102 | | }; |
| 103 | | |
| 104 | | let queue: FutureQueue = unsafe { |
| 105 | | if let Some(q) = &_FUTURE_QUEUE { |
| 106 | | q.clone() |
| 107 | | } else { |
| 108 | | panic!("Future queue wasn't initialized") |
| 109 | | } |
| 110 | | }; |
| 111 | | |
| 112 | | let schedule = move |task| { |
| 113 | | let mut weechat_notify = weechat_notify.lock().unwrap(); |
| 114 | | let mut queue = queue.lock().unwrap(); |
| 115 | | |
| 116 | | queue.push_back(task); |
| 117 | | weechat_notify.send(()).unwrap(); |
| 118 | | }; |
| 119 | | |
| 120 | | let (task, _handle) = async_task::spawn(future, schedule, ()); |
| 121 | | task.schedule(); |
| 122 | | } |
| 123 | | |
| 124 | 47 | async fn sync_loop( |
| 125 | 48 | mut client: AsyncClient, |
| 126 | 49 | channel: AsyncSender<Result<ThreadMessage, String>>, |
| @@ -163,10 +86,10 @@ async fn sync_loop( |
| 163 | 86 | Err(e) => continue, |
| 164 | 87 | }; |
| 165 | 88 | channel |
| 166 | | .send(Ok((ThreadMessage::SyncState( |
| 89 | .send(Ok(ThreadMessage::SyncState( |
| 167 | 90 | room_id.to_string(), |
| 168 | 91 | event, |
| 169 | | )))) |
| 92 | ))) |
| 170 | 93 | .await; |
| 171 | 94 | } |
| 172 | 95 | |
| @@ -176,10 +99,10 @@ async fn sync_loop( |
| 176 | 99 | Err(e) => continue, |
| 177 | 100 | }; |
| 178 | 101 | channel |
| 179 | | .send(Ok((ThreadMessage::SyncEvent( |
| 102 | .send(Ok(ThreadMessage::SyncEvent( |
| 180 | 103 | room_id.to_string(), |
| 181 | 104 | event, |
| 182 | | )))) |
| 105 | ))) |
| 183 | 106 | .await; |
| 184 | 107 | } |
| 185 | 108 | } |
| @@ -188,7 +111,6 @@ async fn sync_loop( |
| 188 | 111 | let err = format!("{:?}", e.to_string()); |
| 189 | 112 | channel.send(Err(err)).await; |
| 190 | 113 | async_std::task::sleep(Duration::from_secs(3)).await; |
| 191 | | () |
| 192 | 114 | } |
| 193 | 115 | } |
| 194 | 116 | } |
| @@ -301,12 +223,7 @@ impl Drop for Matrix { |
| 301 | 223 | if let Some(r) = runtime { |
| 302 | 224 | r.shutdown_now(); |
| 303 | 225 | } |
| 304 | | |
| 305 | | unsafe { |
| 306 | | _FUTURE_HOOK.take(); |
| 307 | | _SENDER.take(); |
| 308 | | _FUTURE_QUEUE.take(); |
| 309 | | } |
| 226 | cleanup_executor(); |
| 310 | 227 | } |
| 311 | 228 | } |
| 312 | 229 | |