Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process events from the UI in thread #50

Merged
merged 1 commit into from
May 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 66 additions & 62 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use crate::{

const PEG_IN_TIMEOUT_YEAR: Duration = Duration::from_secs(86400 * 365);

#[derive(Clone)]
struct HarborCore {
network: Network,
mnemonic: Mnemonic,
Expand Down Expand Up @@ -446,81 +447,84 @@ async fn process_core(core_handle: &mut bridge::CoreHandle, core: &HarborCore) {
loop {
let msg = core_handle.recv().await;

if let Some(msg) = msg {
match msg {
UICoreMsg::SendLightning(invoice) => {
log::info!("Got UICoreMsg::Send");
core.msg(CoreUIMsg::Sending).await;
if let Err(e) = core.send_lightning(invoice).await {
error!("Error sending: {e}");
core.msg(CoreUIMsg::SendFailure(e.to_string())).await;
}
}
UICoreMsg::ReceiveLightning(amount) => {
core.msg(CoreUIMsg::ReceiveGenerating).await;
match core.receive_lightning(amount).await {
Err(e) => {
core.msg(CoreUIMsg::ReceiveFailed(e.to_string())).await;
let core = core.clone();
tokio::spawn(async move {
if let Some(msg) = msg {
match msg {
UICoreMsg::SendLightning(invoice) => {
log::info!("Got UICoreMsg::Send");
core.msg(CoreUIMsg::Sending).await;
if let Err(e) = core.send_lightning(invoice).await {
error!("Error sending: {e}");
core.msg(CoreUIMsg::SendFailure(e.to_string())).await;
}
Ok(invoice) => {
core.msg(CoreUIMsg::ReceiveInvoiceGenerated(invoice)).await;
}
UICoreMsg::ReceiveLightning(amount) => {
core.msg(CoreUIMsg::ReceiveGenerating).await;
match core.receive_lightning(amount).await {
Err(e) => {
core.msg(CoreUIMsg::ReceiveFailed(e.to_string())).await;
}
Ok(invoice) => {
core.msg(CoreUIMsg::ReceiveInvoiceGenerated(invoice)).await;
}
}
}
}
UICoreMsg::SendOnChain {
address,
amount_sats,
} => {
log::info!("Got UICoreMsg::SendOnChain");
core.msg(CoreUIMsg::Sending).await;
if let Err(e) = core.send_onchain(address, amount_sats).await {
error!("Error sending: {e}");
core.msg(CoreUIMsg::SendFailure(e.to_string())).await;
UICoreMsg::SendOnChain {
address,
amount_sats,
} => {
log::info!("Got UICoreMsg::SendOnChain");
core.msg(CoreUIMsg::Sending).await;
if let Err(e) = core.send_onchain(address, amount_sats).await {
error!("Error sending: {e}");
core.msg(CoreUIMsg::SendFailure(e.to_string())).await;
}
}
}
UICoreMsg::ReceiveOnChain => {
core.msg(CoreUIMsg::ReceiveGenerating).await;
match core.receive_onchain().await {
Err(e) => {
core.msg(CoreUIMsg::ReceiveFailed(e.to_string())).await;
UICoreMsg::ReceiveOnChain => {
core.msg(CoreUIMsg::ReceiveGenerating).await;
match core.receive_onchain().await {
Err(e) => {
core.msg(CoreUIMsg::ReceiveFailed(e.to_string())).await;
}
Ok(address) => {
core.msg(CoreUIMsg::ReceiveAddressGenerated(address)).await;
}
}
Ok(address) => {
core.msg(CoreUIMsg::ReceiveAddressGenerated(address)).await;
}
UICoreMsg::GetFederationInfo(invite_code) => {
match core.get_federation_info(invite_code).await {
Err(e) => {
error!("Error getting federation info: {e}");
core.msg(CoreUIMsg::AddFederationFailed(e.to_string()))
.await;
}
Ok(config) => {
core.msg(CoreUIMsg::FederationInfo(config)).await;
}
}
}
}
UICoreMsg::GetFederationInfo(invite_code) => {
match core.get_federation_info(invite_code).await {
Err(e) => {
error!("Error getting federation info: {e}");
UICoreMsg::AddFederation(invite_code) => {
if let Err(e) = core.add_federation(invite_code).await {
error!("Error adding federation: {e}");
core.msg(CoreUIMsg::AddFederationFailed(e.to_string()))
.await;
}
Ok(config) => {
core.msg(CoreUIMsg::FederationInfo(config)).await;
} else {
core.msg(CoreUIMsg::AddFederationSuccess).await;
let new_federation_list = core.get_federation_items().await;
core.msg(CoreUIMsg::FederationListUpdated(new_federation_list))
.await;
}
}
}
UICoreMsg::AddFederation(invite_code) => {
if let Err(e) = core.add_federation(invite_code).await {
error!("Error adding federation: {e}");
core.msg(CoreUIMsg::AddFederationFailed(e.to_string()))
.await;
} else {
core.msg(CoreUIMsg::AddFederationSuccess).await;
let new_federation_list = core.get_federation_items().await;
core.msg(CoreUIMsg::FederationListUpdated(new_federation_list))
.await;
UICoreMsg::Unlock(_password) => {
unreachable!("should already be unlocked")
}
UICoreMsg::GetSeedWords => {
let seed_words = core.get_seed_words().await;
core.msg(CoreUIMsg::SeedWords(seed_words)).await;
}
}
UICoreMsg::Unlock(_password) => {
unreachable!("should already be unlocked")
}
UICoreMsg::GetSeedWords => {
let seed_words = core.get_seed_words().await;
core.msg(CoreUIMsg::SeedWords(seed_words)).await;
}
}
}
});
}
}