update rust api to match ffi

This commit is contained in:
dignifiedquire
2020-05-22 22:04:20 +02:00
parent 014d2946b2
commit c53a3d5cb4
5 changed files with 32 additions and 33 deletions

View File

@@ -487,9 +487,8 @@ pub unsafe extern "C" fn dc_start_io(context: *mut dc_context_t) {
} }
let ffi_context = &*context; let ffi_context = &*context;
with_inner_async!(ffi_context, ctx, { ctx.run() }).unwrap_or(()) with_inner_async!(ffi_context, ctx, { ctx.start_io() }).unwrap_or(())
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn dc_is_io_running(context: *mut dc_context_t) -> libc::c_int { pub unsafe extern "C" fn dc_is_io_running(context: *mut dc_context_t) -> libc::c_int {
if context.is_null() { if context.is_null() {
@@ -497,7 +496,7 @@ pub unsafe extern "C" fn dc_is_io_running(context: *mut dc_context_t) -> libc::c
} }
let ffi_context = &*context; let ffi_context = &*context;
with_inner_async!(ffi_context, ctx, { ctx.is_running() }).unwrap_or_default() as libc::c_int with_inner_async!(ffi_context, ctx, { ctx.is_io_running() }).unwrap_or_default() as libc::c_int
} }
#[no_mangle] #[no_mangle]
@@ -698,7 +697,7 @@ pub unsafe extern "C" fn dc_stop_io(context: *mut dc_context_t) {
let ffi_context = &*context; let ffi_context = &*context;
with_inner_async!(ffi_context, ctx, async move { with_inner_async!(ffi_context, ctx, async move {
ctx.stop().await; ctx.stop_io().await;
}) })
.unwrap_or(()) .unwrap_or(())
} }

View File

@@ -277,10 +277,8 @@ async fn start(args: Vec<String>) -> Result<(), Error> {
let events = context.get_event_emitter(); let events = context.get_event_emitter();
async_std::task::spawn(async move { async_std::task::spawn(async move {
loop { while let Some(event) = events.recv().await {
if let Some(event) = events.recv().await { receive_event(event);
receive_event(event);
}
} }
}); });
@@ -322,7 +320,7 @@ async fn start(args: Vec<String>) -> Result<(), Error> {
} }
Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => { Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => {
println!("Exiting..."); println!("Exiting...");
context.stop().await; context.stop_io().await;
break; break;
} }
Err(err) => { Err(err) => {
@@ -355,10 +353,10 @@ async fn handle_cmd(
match arg0 { match arg0 {
"connect" => { "connect" => {
ctx.run().await; ctx.start_io().await;
} }
"disconnect" => { "disconnect" => {
ctx.stop().await; ctx.stop_io().await;
} }
"configure" => { "configure" => {
ctx.configure().await?; ctx.configure().await?;
@@ -381,7 +379,7 @@ async fn handle_cmd(
print!("\x1b[1;1H\x1b[2J"); print!("\x1b[1;1H\x1b[2J");
} }
"getqr" | "getbadqr" => { "getqr" | "getbadqr" => {
ctx.run().await; ctx.start_io().await;
if let Some(mut qr) = if let Some(mut qr) =
dc_get_securejoin_qr(&ctx, ChatId::new(arg1.parse().unwrap_or_default())).await dc_get_securejoin_qr(&ctx, ChatId::new(arg1.parse().unwrap_or_default())).await
{ {
@@ -400,7 +398,7 @@ async fn handle_cmd(
} }
} }
"joinqr" => { "joinqr" => {
ctx.run().await; ctx.start_io().await;
if !arg0.is_empty() { if !arg0.is_empty() {
dc_join_securejoin(&ctx, arg1).await; dc_join_securejoin(&ctx, arg1).await;
} }

View File

@@ -39,11 +39,9 @@ async fn main() {
println!("info: {:#?}", info); println!("info: {:#?}", info);
let events = ctx.get_event_emitter(); let events = ctx.get_event_emitter();
async_std::task::spawn(async move { let events_spawn = async_std::task::spawn(async move {
loop { while let Some(event) = events.recv().await {
if let Some(event) = events.recv().await { cb(event);
cb(event);
}
} }
}); });
@@ -62,7 +60,7 @@ async fn main() {
ctx.configure().await.unwrap(); ctx.configure().await.unwrap();
println!("------ RUN ------"); println!("------ RUN ------");
ctx.clone().run().await; ctx.clone().start_io().await;
println!("--- SENDING A MESSAGE ---"); println!("--- SENDING A MESSAGE ---");
let contact_id = Contact::create(&ctx, "dignifiedquire", "dignifiedquire@gmail.com") let contact_id = Contact::create(&ctx, "dignifiedquire", "dignifiedquire@gmail.com")
@@ -89,6 +87,7 @@ async fn main() {
async_std::task::sleep(duration).await; async_std::task::sleep(duration).await;
println!("stopping"); println!("stopping");
ctx.stop().await; ctx.stop_io().await;
println!("closing"); println!("closing");
events_spawn.await;
} }

View File

@@ -135,21 +135,24 @@ impl Context {
Ok(ctx) Ok(ctx)
} }
pub async fn run(&self) { /// Starts the IO scheduler.
assert!(!self.is_running().await, "context is already running"); pub async fn start_io(&self) {
info!(self, "starting IO");
assert!(!self.is_io_running().await, "context is already running");
let l = &mut *self.inner.scheduler.write().await; let l = &mut *self.inner.scheduler.write().await;
l.run(self.clone()).await; l.start(self.clone()).await;
} }
pub async fn is_running(&self) -> bool { /// Returns if the IO scheduler is running.
self.inner.is_running().await pub async fn is_io_running(&self) -> bool {
self.inner.is_io_running().await
} }
pub async fn stop(&self) { /// Stops the IO scheduler.
info!(self, "stopping context"); pub async fn stop_io(&self) {
self.inner.stop().await; info!(self, "stopping IO");
info!(self, "stopped context"); self.inner.stop_io().await;
} }
/// Returns a reference to the underlying SQL instance. /// Returns a reference to the underlying SQL instance.
@@ -491,12 +494,12 @@ impl Context {
} }
impl InnerContext { impl InnerContext {
async fn is_running(&self) -> bool { async fn is_io_running(&self) -> bool {
self.scheduler.read().await.is_running() self.scheduler.read().await.is_running()
} }
async fn stop(&self) { async fn stop_io(&self) {
assert!(self.is_running().await, "context is already stopped"); assert!(self.is_io_running().await, "context is already stopped");
let token = { let token = {
let lock = &*self.scheduler.read().await; let lock = &*self.scheduler.read().await;
lock.pre_stop().await lock.pre_stop().await

View File

@@ -276,7 +276,7 @@ async fn smtp_loop(ctx: Context, started: Sender<()>, smtp_handlers: SmtpConnect
impl Scheduler { impl Scheduler {
/// Start the scheduler, panics if it is already running. /// Start the scheduler, panics if it is already running.
pub async fn run(&mut self, ctx: Context) { pub async fn start(&mut self, ctx: Context) {
let (mvbox, mvbox_handlers) = ImapConnectionState::new(); let (mvbox, mvbox_handlers) = ImapConnectionState::new();
let (sentbox, sentbox_handlers) = ImapConnectionState::new(); let (sentbox, sentbox_handlers) = ImapConnectionState::new();
let (smtp, smtp_handlers) = SmtpConnectionState::new(); let (smtp, smtp_handlers) = SmtpConnectionState::new();