Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 208d1e2

Browse files
Scaffolding for event handling in future protocols
1 parent d91037d commit 208d1e2

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

src/events.rs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
//! Events are surfaced by the library to indicate some action must be taken
11+
//! by the end-user.
12+
//!
13+
//! Because we don't have a built-in runtime, it's up to the end-user to poll
14+
//! [`crate::LiquidityManager::get_and_clear_pending_events()`] to receive events.
15+
16+
use std::sync::{Mutex, Condvar};
17+
use std::collections::VecDeque;
18+
19+
#[derive(Default)]
20+
pub(crate) struct EventQueue {
21+
queue: Mutex<VecDeque<Event>>,
22+
condvar: Condvar
23+
}
24+
25+
impl EventQueue {
26+
pub fn push(&self, event: Event) {
27+
let mut queue = self.queue.lock().unwrap();
28+
queue.push_back(event);
29+
self.condvar.notify_one();
30+
}
31+
32+
pub fn pop(&self) -> Event {
33+
let mut queue = self.queue.lock().unwrap();
34+
35+
let event = loop {
36+
if let Some(event) = queue.pop_front() {
37+
break event;
38+
} else {
39+
queue = self.condvar.wait(queue).unwrap();
40+
}
41+
};
42+
drop(queue);
43+
event
44+
}
45+
46+
pub fn get_and_clear_pending_events(&self) -> Vec<Event> {
47+
self.queue.lock().unwrap().drain(..).collect()
48+
}
49+
}
50+
51+
/// An Event which you should probably take some action in response to.
52+
#[derive(Debug, Clone, PartialEq, Eq)]
53+
pub enum Event {}

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
2020

2121
mod channel_request;
22+
pub mod events;
2223
mod jit_channel;
2324
mod transport;
2425
mod utils;

src/transport/message_handler.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::events::{Event, EventQueue};
12
use crate::transport::msgs::{LSPSMessage, RawLSPSMessage, LSPS_MESSAGE_TYPE};
23
use crate::transport::protocol::LSPS0MessageHandler;
34

@@ -45,6 +46,7 @@ where
4546
ES::Target: EntropySource,
4647
{
4748
pending_messages: Arc<Mutex<Vec<(PublicKey, LSPSMessage)>>>,
49+
pending_events: Arc<EventQueue>,
4850
request_id_to_method_map: Mutex<HashMap<String, String>>,
4951
lsps0_message_handler: LSPS0MessageHandler<ES>,
5052
provider_config: Option<LiquidityProviderConfig>,
@@ -65,12 +67,27 @@ where
6567

6668
Self {
6769
pending_messages,
70+
pending_events: Arc::new(EventQueue::default()),
6871
request_id_to_method_map: Mutex::new(HashMap::new()),
6972
lsps0_message_handler,
7073
provider_config,
7174
}
7275
}
7376

77+
/// Blocks until next event is ready and returns it
78+
///
79+
/// Typically you would spawn a thread or task that calls this in a loop
80+
pub fn get_next_event(&self) -> Event {
81+
self.pending_events.pop()
82+
}
83+
84+
/// Returns and clears all events without blocking
85+
///
86+
/// Typically you would spawn a thread or task that calls this in a loop
87+
pub fn get_and_clear_pending_events(&self) -> Vec<Event> {
88+
self.pending_events.get_and_clear_pending_events()
89+
}
90+
7491
fn handle_lsps_message(
7592
&self, msg: LSPSMessage, sender_node_id: &PublicKey,
7693
) -> Result<(), lightning::ln::msgs::LightningError> {

0 commit comments

Comments
 (0)