File tree 4 files changed +39
-2
lines changed
4 files changed +39
-2
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9
9
10
10
### Added
11
11
12
+ - Node API to subscribe to peer connection events [ #625 ] ( https://github.com/p2panda/aquadoggo/pull/625 )
12
13
- Support private network secured by pre-shared key [ #635 ] ( https://github.com/p2panda/aquadoggo/pull/635 )
13
14
14
15
### Changed
Original file line number Diff line number Diff line change 1
1
// SPDX-License-Identifier: AGPL-3.0-or-later
2
2
3
3
use anyhow:: { bail, Result } ;
4
+ use tokio:: sync:: mpsc:: Receiver ;
4
5
5
6
use crate :: api:: { migrate, LockFile } ;
6
7
use crate :: bus:: { ServiceMessage , ServiceSender } ;
7
8
use crate :: context:: Context ;
8
9
10
+ #[ derive( Debug , Clone ) ]
11
+ pub enum NodeEvent {
12
+ PeerConnected ,
13
+ PeerDisconnected ,
14
+ }
15
+
9
16
/// Interface to interact with the node in a programmatic, "low-level" way.
10
17
#[ derive( Debug ) ]
11
18
pub struct NodeInterface {
@@ -42,4 +49,26 @@ impl NodeInterface {
42
49
43
50
Ok ( did_migration_happen)
44
51
}
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
+ }
45
74
}
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ mod config_file;
6
6
mod lock_file;
7
7
mod migration;
8
8
9
- pub use api:: NodeInterface ;
9
+ pub use api:: { NodeEvent , NodeInterface } ;
10
10
pub use config_file:: ConfigFile ;
11
11
pub use lock_file:: LockFile ;
12
12
pub use migration:: migrate;
Original file line number Diff line number Diff line change 2
2
3
3
use anyhow:: Result ;
4
4
use p2panda_rs:: identity:: KeyPair ;
5
+ use tokio:: sync:: mpsc:: Receiver ;
5
6
6
- use crate :: api:: NodeInterface ;
7
+ use crate :: api:: { NodeEvent , NodeInterface } ;
7
8
use crate :: bus:: ServiceMessage ;
8
9
use crate :: config:: Configuration ;
9
10
use crate :: context:: Context ;
@@ -131,4 +132,10 @@ impl Node {
131
132
pub async fn migrate ( & self , lock_file : LockFile ) -> Result < bool > {
132
133
self . api . migrate ( lock_file) . await
133
134
}
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
+ }
134
141
}
You can’t perform that action at this time.
0 commit comments