Skip to content

Commit

Permalink
Watch for ASB kill
Browse files Browse the repository at this point in the history
  • Loading branch information
pokkst committed Oct 11, 2023
1 parent 4e36f57 commit fcea441
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
19 changes: 19 additions & 0 deletions swap/src/asb/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::collections::HashMap;
use std::convert::{Infallible, TryInto};
use std::fmt::Debug;
use std::sync::Arc;
use std::time::Duration;
use jni::JNIEnv;
use tokio::sync::mpsc;
use uuid::Uuid;
Expand Down Expand Up @@ -171,6 +172,7 @@ where
}

tokio::select! {
biased;
swarm_event = self.swarm.select_next_some() => {
match swarm_event {
SwarmEvent::Behaviour(OutEvent::SwapSetupInitiated { mut send_wallet_snapshot }) => {
Expand Down Expand Up @@ -329,6 +331,11 @@ where
Some(response_channel) = self.inflight_encrypted_signatures.next() => {
let _ = self.swarm.behaviour_mut().encrypted_signature.send_response(response_channel, ());
}
asb_kill = watch_for_asb_kill(&env.unwrap()) => {
let _ = self.monero_wallet.store();
self.monero_wallet.close();
break;
}
}
}
}
Expand Down Expand Up @@ -566,3 +573,15 @@ impl<T> Default for MpscChannels<T> {
MpscChannels { sender, receiver }
}
}

pub async fn watch_for_asb_kill(env: &JNIEnv<'_>) -> Result<()> {
wait_for_asb_client_kill(env)
.await
}

pub async fn wait_for_asb_client_kill(env: &JNIEnv<'_>) -> Result<()> {
while util::get_running_asb(env) {
tokio::time::sleep(Duration::from_secs(5)).await;
}
Ok(())
}
10 changes: 10 additions & 0 deletions swap/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,14 @@ pub fn get_running_swap(env: &JNIEnv) -> bool {
return running;
}
return false;
}

pub fn get_running_asb(env: &JNIEnv) -> bool {
let listener = get_asb_listener(&env);
if let JValue::Object(listener) = listener {
let result = env.call_method(listener, "getRunningAsb", "()Z", &[]);
let running = result.unwrap().z().unwrap();
return running;
}
return false;
}

0 comments on commit fcea441

Please sign in to comment.