sn_node/event.rs
1// Copyright 2024 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
4// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
5// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
6// KIND, either express or implied. Please review the Licences for the specific language governing
7// permissions and limitations relating to use of the SAFE Network Software.
8
9use crate::error::{Error, Result};
10
11use serde::{Deserialize, Serialize};
12use sn_evm::AttoTokens;
13use sn_protocol::{
14 storage::{ChunkAddress, RegisterAddress},
15 NetworkAddress,
16};
17use tokio::sync::broadcast;
18
19const NODE_EVENT_CHANNEL_SIZE: usize = 500;
20
21/// Channel where users of the public API can listen to events broadcasted by the node.
22#[derive(Clone)]
23pub struct NodeEventsChannel(broadcast::Sender<NodeEvent>);
24
25/// Type of channel receiver where events are broadcasted to by the node.
26pub type NodeEventsReceiver = broadcast::Receiver<NodeEvent>;
27
28impl Default for NodeEventsChannel {
29 fn default() -> Self {
30 Self(broadcast::channel(NODE_EVENT_CHANNEL_SIZE).0)
31 }
32}
33
34impl NodeEventsChannel {
35 /// Returns a new receiver to listen to the channel.
36 /// Multiple receivers can be actively listening.
37 pub fn subscribe(&self) -> broadcast::Receiver<NodeEvent> {
38 self.0.subscribe()
39 }
40
41 // Broadcast a new event, meant to be a helper only used by the sn_node's internals.
42 pub(crate) fn broadcast(&self, event: NodeEvent) {
43 let event_string = format!("{event:?}");
44 if let Err(err) = self.0.send(event) {
45 debug!(
46 "Error occurred when trying to broadcast a node event ({event_string:?}): {err}"
47 );
48 }
49 }
50
51 /// Returns the number of active receivers
52 pub fn receiver_count(&self) -> usize {
53 self.0.receiver_count()
54 }
55}
56
57/// Type of events broadcasted by the node to the public API.
58#[derive(Clone, Serialize, custom_debug::Debug, Deserialize)]
59pub enum NodeEvent {
60 /// The node has been connected to the network
61 ConnectedToNetwork,
62 /// A Chunk has been stored in local storage
63 ChunkStored(ChunkAddress),
64 /// A Register has been created in local storage
65 RegisterCreated(RegisterAddress),
66 /// A Register edit operation has been applied in local storage
67 RegisterEdited(RegisterAddress),
68 /// A new reward was received
69 RewardReceived(AttoTokens, NetworkAddress),
70 /// One of the sub event channel closed and unrecoverable.
71 ChannelClosed,
72 /// Terminates the node
73 TerminateNode(String),
74}
75
76impl NodeEvent {
77 /// Convert NodeEvent to bytes
78 pub fn to_bytes(&self) -> Result<Vec<u8>> {
79 rmp_serde::to_vec(&self).map_err(|_| Error::NodeEventParsingFailed)
80 }
81
82 /// Get NodeEvent from bytes
83 pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
84 rmp_serde::from_slice(bytes).map_err(|_| Error::NodeEventParsingFailed)
85 }
86}