Commit cf4168417d

cf4168417d7d9917c674e9464bb3122f2cab9845

parent: 9f10743365

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-10-17 18:43 UTC

connection: Make the connection cloneable.

Layout: unified · split

src/connection.rs +5 −5
@@ -82,12 +82,12 @@ pub enum ClientMessage {
82/// 82///
83/// While this struct is alive a sync loop will be going on. To cancel the sync 83/// While this struct is alive a sync loop will be going on. To cancel the sync
84/// loop drop the object. 84/// loop drop the object.
85#[derive(Debug, Clone)]
85pub struct Connection { 86pub struct Connection {
86 #[used] 87 #[used]
87 receiver_task: Task<()>, 88 receiver_task: Rc<Task<()>>,
88 client: Client, 89 client: Client,
89 #[used] 90 pub runtime: Rc<Runtime>,
90 runtime: Runtime,
91} 91}
92 92
93impl Connection { 93impl Connection {
@@ -129,8 +129,8 @@ impl Connection {
129 129
130 Self { 130 Self {
131 client: client.clone(), 131 client: client.clone(),
132 runtime, 132 runtime: Rc::new(runtime),
133 receiver_task, 133 receiver_task: Rc::new(receiver_task),
134 } 134 }
135 } 135 }
136 136
src/server.rs +7 −9
@@ -180,12 +180,11 @@ impl MatrixServer {
180 Rc::downgrade(&self.inner) 180 Rc::downgrade(&self.inner)
181 } 181 }
182 182
183 pub fn connection(&self) -> Rc<RefCell<Option<Connection>>> { 183 pub fn connection(&self) -> Option<Connection> {
184 self.inner().connection.clone() 184 self.inner().connection.borrow().clone()
185 } 185 }
186 186
187 pub async fn delete_devices(&self, devices: Vec<DeviceIdBox>) { 187 pub async fn delete_devices(&self, devices: Vec<DeviceIdBox>) {
188 let connection = self.connection();
189 let formatted = devices 188 let formatted = devices
190 .iter() 189 .iter()
191 .map(|d| d.to_string()) 190 .map(|d| d.to_string())
@@ -193,7 +192,7 @@ impl MatrixServer {
193 .join(", "); 192 .join(", ");
194 193
195 let print_success = || { 194 let print_success = || {
196 self.print(&format!( 195 self.print_network(&format!(
197 "Successfully deleted device(s) {}", 196 "Successfully deleted device(s) {}",
198 formatted 197 formatted
199 )); 198 ));
@@ -206,7 +205,7 @@ impl MatrixServer {
206 )); 205 ));
207 }; 206 };
208 207
209 if let Some(c) = &*connection.borrow() { 208 if let Some(c) = self.connection() {
210 match c.delete_devices(devices.clone(), None).await { 209 match c.delete_devices(devices.clone(), None).await {
211 Ok(_) => print_success(), 210 Ok(_) => print_success(),
212 Err(e) => { 211 Err(e) => {
@@ -234,9 +233,8 @@ impl MatrixServer {
234 } 233 }
235 234
236 pub async fn devices(&self) { 235 pub async fn devices(&self) {
237 let connection = self.connection();
238 236
239 if let Some(c) = &*connection.borrow() { 237 if let Some(c) = self.connection() {
240 let response = match c.devices().await { 238 let response = match c.devices().await {
241 Ok(r) => r, 239 Ok(r) => r,
242 Err(e) => { 240 Err(e) => {
@@ -475,7 +473,7 @@ impl MatrixServer {
475 return Ok(()); 473 return Ok(());
476 } 474 }
477 475
478 let client = self.inner.borrow_mut().get_or_creaet_client()?; 476 let client = self.inner.borrow_mut().get_or_create_client()?;
479 let connection = Connection::new(&self, &client); 477 let connection = Connection::new(&self, &client);
480 478
481 *self.inner.borrow_mut().connection.borrow_mut() = Some(connection); 479 *self.inner.borrow_mut().connection.borrow_mut() = Some(connection);
@@ -677,7 +675,7 @@ impl InnerServer {
677 buffer_handle 675 buffer_handle
678 } 676 }
679 677
680 fn get_or_creaet_client(&mut self) -> Result<Client, ServerError> { 678 fn get_or_create_client(&mut self) -> Result<Client, ServerError> {
681 let client = if let Some(c) = self.client.as_ref() { 679 let client = if let Some(c) = self.client.as_ref() {
682 c.clone() 680 c.clone()
683 } else { 681 } else {