| @@ -1,3 +1,55 @@ |
| |
1 | //! Matrix server abstraction. |
| |
2 | //! |
| |
3 | //! A MatrixServer is created for every server the user configures. |
| |
4 | //! |
| |
5 | //! It will create a per server config subsection. If options are added to the |
| |
6 | //! server they need to be removed from the server section when the server is |
| |
7 | //! dropped. |
| |
8 | //! |
| |
9 | //! The server will create a tokio runtime which will spawn two tasks, one for |
| |
10 | //! the sync loop and one to send out room messages. |
| |
11 | //! |
| |
12 | //! If will also spawn a task on the Weechat mainloop, this one waits for |
| |
13 | //! responses from the sync loop. |
| |
14 | //! |
| |
15 | //! |
| |
16 | //! Schematically this looks like the following diagram. |
| |
17 | //! |
| |
18 | //! MatrixServer |
| |
19 | //! +--------------------------------------------------------------------+ |
| |
20 | //! | | |
| |
21 | //! | Weechat mainloop Tokio runtime | |
| |
22 | //! | +---------------------------+ +------------------------+ | |
| |
23 | //! | | | | | | |
| |
24 | //! | | +--------------------+ | | +----------------+ | | |
| |
25 | //! | | | | | | | | | | |
| |
26 | //! | | | Response receiver +<---------------+ Sync loop | | | |
| |
27 | //! | | | | | | | | | | |
| |
28 | //! | | | | | | | | | | |
| |
29 | //! | | +--------------------+ | | +----------------+ | | |
| |
30 | //! | | | | | | |
| |
31 | //! | | +--------------------+ | | +----------------+ | | |
| |
32 | //! | | | | | | | | | | |
| |
33 | //! | | | Roombuffer input +--------------->+ Send loop | | | |
| |
34 | //! | | | callback +<---------------+ | | | |
| |
35 | //! | | | | | | | | | | |
| |
36 | //! | | +--------------------+ | | +----------------+ | | |
| |
37 | //! | | | | | | |
| |
38 | //! | +---------------------------+ +------------------------+ | |
| |
39 | //! | | |
| |
40 | //! +--------------------------------------------------------------------+ |
| |
41 | //! |
| |
42 | //! |
| |
43 | //! The tokio runtime and response receiver task will be alive only if the user |
| |
44 | //! connects to the server while the room buffer input callback will print an |
| |
45 | //! error if the server is disconnected. |
| |
46 | //! |
| |
47 | //! The server holds all the rooms which in turn hold the buffers, users, and |
| |
48 | //! room metadata. |
| |
49 | //! |
| |
50 | //! The response receiver forwards events to the correct room. The response |
| |
51 | //! receiver fetches events individually from a mpsc channel. This makes sure |
| |
52 | //! that processing events will not block the Weechat mainloop for too long. |
| 1 | use async_std::sync::channel as async_channel; |
53 | use async_std::sync::channel as async_channel; |
| 2 | use async_std::sync::{Receiver, Sender}; |
54 | use async_std::sync::{Receiver, Sender}; |
| 3 | use std::cell::RefCell; |
55 | use std::cell::RefCell; |
| @@ -6,9 +58,9 @@ use std::rc::{Rc, Weak}; |
| 6 | use tokio::runtime::Runtime; |
58 | use tokio::runtime::Runtime; |
| 7 | use url::Url; |
59 | use url::Url; |
| 8 | |
60 | |
| 9 | use matrix_nio::api::r0::session::login::Response as LoginResponse; |
61 | use matrix_sdk::api::r0::session::login::Response as LoginResponse; |
| 10 | |
62 | |
| 11 | use matrix_nio::{ |
63 | use matrix_sdk::{ |
| 12 | self, |
64 | self, |
| 13 | events::{ |
65 | events::{ |
| 14 | collections::all::{RoomEvent, StateEvent}, |
66 | collections::all::{RoomEvent, StateEvent}, |
| @@ -66,6 +118,8 @@ pub struct LoginState { |
| 66 | pub struct Connection { |
118 | pub struct Connection { |
| 67 | client_channel: Sender<ServerMessage>, |
119 | client_channel: Sender<ServerMessage>, |
| 68 | response_receiver: JoinHandle<(), ()>, |
120 | response_receiver: JoinHandle<(), ()>, |
| |
121 | // TODO move the runtime into the plugin, once we're able to cancel tokio |
| |
122 | // tasks. |
| 69 | runtime: Runtime, |
123 | runtime: Runtime, |
| 70 | } |
124 | } |
| 71 | |
125 | |