Skip to content

Commit abee607

Browse files
authored
Introduce API to subscribe to peer connection events (#625)
* Introduce API to subscribe to peer connection events * Add entry to CHANGELOG.md
1 parent 04b12ae commit abee607

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- Node API to subscribe to peer connection events [#625](https://github.com/p2panda/aquadoggo/pull/625)
1213
- Support private network secured by pre-shared key [#635](https://github.com/p2panda/aquadoggo/pull/635)
1314

1415
### Changed

aquadoggo/src/api/api.rs

+29
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
// SPDX-License-Identifier: AGPL-3.0-or-later
22

33
use anyhow::{bail, Result};
4+
use tokio::sync::mpsc::Receiver;
45

56
use crate::api::{migrate, LockFile};
67
use crate::bus::{ServiceMessage, ServiceSender};
78
use crate::context::Context;
89

10+
#[derive(Debug, Clone)]
11+
pub enum NodeEvent {
12+
PeerConnected,
13+
PeerDisconnected,
14+
}
15+
916
/// Interface to interact with the node in a programmatic, "low-level" way.
1017
#[derive(Debug)]
1118
pub struct NodeInterface {
@@ -42,4 +49,26 @@ impl NodeInterface {
4249

4350
Ok(did_migration_happen)
4451
}
52+
53+
pub async fn subscribe(&self) -> Receiver<NodeEvent> {
54+
let mut rx = self.tx.subscribe();
55+
let (events_tx, events_rx) = tokio::sync::mpsc::channel::<NodeEvent>(256);
56+
57+
tokio::task::spawn(async move {
58+
loop {
59+
match rx.recv().await {
60+
Ok(ServiceMessage::PeerConnected(_)) => {
61+
let _ = events_tx.send(NodeEvent::PeerConnected).await;
62+
}
63+
Ok(ServiceMessage::PeerDisconnected(_)) => {
64+
let _ = events_tx.send(NodeEvent::PeerDisconnected).await;
65+
}
66+
Ok(_) => continue,
67+
Err(_) => break,
68+
}
69+
}
70+
});
71+
72+
events_rx
73+
}
4574
}

aquadoggo/src/api/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod config_file;
66
mod lock_file;
77
mod migration;
88

9-
pub use api::NodeInterface;
9+
pub use api::{NodeEvent, NodeInterface};
1010
pub use config_file::ConfigFile;
1111
pub use lock_file::LockFile;
1212
pub use migration::migrate;

aquadoggo/src/node.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
use anyhow::Result;
44
use p2panda_rs::identity::KeyPair;
5+
use tokio::sync::mpsc::Receiver;
56

6-
use crate::api::NodeInterface;
7+
use crate::api::{NodeEvent, NodeInterface};
78
use crate::bus::ServiceMessage;
89
use crate::config::Configuration;
910
use crate::context::Context;
@@ -131,4 +132,10 @@ impl Node {
131132
pub async fn migrate(&self, lock_file: LockFile) -> Result<bool> {
132133
self.api.migrate(lock_file).await
133134
}
135+
136+
/// Subscribe to channel reporting on significant node events which can be interesting for
137+
/// clients, for example when peers connect or disconnect.
138+
pub async fn subscribe(&self) -> Receiver<NodeEvent> {
139+
self.api.subscribe().await
140+
}
134141
}

0 commit comments

Comments
 (0)