Replace a sleep() with an MPSC channel

This commit is contained in:
2020-05-16 18:09:40 +01:00
parent 640948241a
commit e08ec6b476
3 changed files with 48 additions and 25 deletions

View File

@@ -29,7 +29,7 @@ use dc::Event;
use deltachat as dc;
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex, RwLock};
use std::sync::{mpsc, Arc, Mutex, RwLock};
use std::time::Duration;
pub const CONN_BUS_NAME: &str = "org.freedesktop.Telepathy.Connection.padfoot.delta";
@@ -144,11 +144,12 @@ impl Connection {
})
}
// This is run inside its own thread
// This should be run inside its own thread. It will signal via the channel
// once the main loop is ready
//
// FIXME: running several +process+ loops sure is convenient, but it also
// seems inefficient...
pub fn run(self) {
pub fn run(self, signal: mpsc::Sender<Option<MethodErr>>) {
let bus = self.bus();
let path = self.path();
let state = self.state.clone();
@@ -207,13 +208,20 @@ impl Connection {
match c.request_name(bus.clone(), false, false, true) {
Ok(RequestNameReply::Exists) => {
println!("Another process is already registered on {}", bus);
signal.send(Some(MethodErr::no_arg())).unwrap();
return;
}
Err(e) => {
println!("Failed to register {}: {}", bus, e);
signal.send(Some(MethodErr::no_arg())).unwrap();
return;
}
_ => println!("{} listening on {}", c.unique_name(), bus), // All other responses we can get are a success
_ => {
// All other responses we can get are a success. We are now on
// the message bus, so the caller can proceed
println!("{} listening on {}", c.unique_name(), bus);
signal.send(None).unwrap();
}
};
// Set up delta jobs last in case registering to DBUS fails