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

Commit 7d8d281

Browse files
Scaffolding for event handling in future protocols
1 parent d91037d commit 7d8d281

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

src/events.rs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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::collections::VecDeque;
17+
use std::sync::{Condvar, Mutex};
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 enqueue(&self, event: Event) {
27+
{
28+
let mut queue = self.queue.lock().unwrap();
29+
queue.push_back(event);
30+
}
31+
32+
self.condvar.notify_one();
33+
}
34+
35+
pub fn wait_next_event(&self) -> Event {
36+
let mut queue =
37+
self.condvar.wait_while(self.queue.lock().unwrap(), |queue| queue.is_empty()).unwrap();
38+
39+
let event = queue.pop_front().expect("non empty queue");
40+
let should_notify = !queue.is_empty();
41+
42+
drop(queue);
43+
44+
if should_notify {
45+
self.condvar.notify_one();
46+
}
47+
48+
event
49+
}
50+
51+
pub fn get_and_clear_pending_events(&self) -> Vec<Event> {
52+
self.queue.lock().unwrap().drain(..).collect()
53+
}
54+
}
55+
56+
/// Event which you should probably take some action in response to.
57+
#[derive(Debug, Clone, PartialEq, Eq)]
58+
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 wait_next_event(&self) -> Event {
81+
self.pending_events.wait_next_event()
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)