Make disconnection clean up completely

Now we can disconnect and reconnect the same account multiple times.
This commit is contained in:
2020-05-16 19:26:52 +01:00
parent 169249b716
commit 411f34e6ce
3 changed files with 21 additions and 11 deletions

View File

@@ -31,7 +31,7 @@ Here's where we're at right now:
- [x] Connect to deltachat-core-rust
- [x] Set up an account via autoconfiguration
- [x] Appear as online in Empathy
- [~] Disconnect!
- [x] Disconnect!
- [ ] Set up an account manually
- [ ] Contacts handling
- [ ] Text messages

View File

@@ -28,7 +28,7 @@ use dc::context::Context;
use dc::Event;
use deltachat as dc;
use std::collections::{HashMap, VecDeque};
use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::{mpsc, Arc, Mutex, RwLock};
use std::time::Duration;
@@ -38,6 +38,9 @@ pub const CONN_OBJECT_PATH: &str = "/org/freedesktop/Telepathy/Connection/padfoo
#[derive(Debug)]
// A Deltachast connection uses email addresses as handles, and delta's Db IDs
pub struct Connection {
// Remove ourselves from this when done
conns: Arc<Mutex<HashSet<String>>>,
ctx: Arc<RwLock<Context>>,
settings: ConnSettings,
state: Arc<RwLock<ConnState>>,
@@ -96,7 +99,10 @@ impl ConnSettings {
}
impl Connection {
pub fn new(settings: ConnSettings) -> Result<Self, MethodErr> {
pub fn new(
settings: ConnSettings,
conns: Arc<Mutex<HashSet<String>>>,
) -> Result<Self, MethodErr> {
let mut dbfile = directories::ProjectDirs::from("gs", "ur", "telepathy-padfoot")
.ok_or_else(MethodErr::no_arg)
.and_then(|p| Ok(p.data_local_dir().to_path_buf()))?;
@@ -169,6 +175,7 @@ impl Connection {
};
Ok(Connection {
conns,
settings,
msgq,
ctx: Arc::new(RwLock::new(ctx)),
@@ -184,6 +191,7 @@ impl Connection {
pub fn run(self, signal: mpsc::Sender<Option<MethodErr>>) {
let bus = self.bus();
let path = self.path();
let conns = self.conns.clone();
let state = self.state.clone();
let msgq = self.msgq.clone();
let id = self.id();
@@ -215,7 +223,7 @@ impl Connection {
telepathy::connection_interface_simple_presence_server(&f, (), move |_| c_rc.clone());
tree = tree.add(
f.object_path(path, ())
f.object_path(path.clone(), ())
.introspectable()
.add(conn_iface)
.add(avatars_iface)
@@ -262,7 +270,7 @@ impl Connection {
if let Err(e) = c.process(Duration::from_millis(100)) {
println!("Error processing: {}", e);
return;
break;
};
// Spend a bit of time sending any outgoing messages - signals, mostly
@@ -276,8 +284,10 @@ impl Connection {
}
}
// TODO: clean up, emit disconnected signal. Join on threads started in
// connect() ?
// TODO: join on threads started in connect!
let mut conns = conns.lock().expect("Mutex access");
conns.remove(&path);
}
pub fn id(&self) -> String {

View File

@@ -4,7 +4,7 @@ use dbus::arg;
use dbus::message::SignalArgs;
use dbus::tree::MethodErr;
use std::collections::{HashMap, HashSet};
use std::sync::mpsc;
use std::sync::{mpsc, Arc, Mutex};
use super::{ConnSettings, Connection};
@@ -14,7 +14,7 @@ pub const CM_OBJECT_PATH: &str = "/org/freedesktop/Telepathy/ConnectionManager/p
#[derive(Debug)]
pub struct ConnectionManager {
conns: std::sync::Mutex<HashSet<String>>,
conns: Arc<Mutex<HashSet<String>>>,
sender: mpsc::Sender<dbus::Message>,
}
@@ -30,7 +30,7 @@ impl ConnectionManager {
(
ConnectionManager {
conns: std::sync::Mutex::new(HashSet::<String>::new()),
conns: Arc::new(Mutex::new(HashSet::<String>::new())),
sender: msg_s,
},
msg_r,
@@ -54,7 +54,7 @@ impl ConnectionManager {
return Err(MethodErr::no_arg());
}
let conn = Connection::new(settings)?;
let conn = Connection::new(settings, self.conns.clone())?;
// It would be nice to have a single main loop, but thread-per-conn is
// is easy enough for me to understand in Rust at the moment.