1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2024 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::error::{Error, Result};

use serde::{Deserialize, Serialize};
use sn_protocol::storage::{ChunkAddress, RegisterAddress};
use sn_transfers::UniquePubkey;
use tokio::sync::broadcast;

const NODE_EVENT_CHANNEL_SIZE: usize = 500;

/// Channel where users of the public API can listen to events broadcasted by the node.
#[derive(Clone)]
pub struct NodeEventsChannel(broadcast::Sender<NodeEvent>);

/// Type of channel receiver where events are broadcasted to by the node.
pub type NodeEventsReceiver = broadcast::Receiver<NodeEvent>;

impl Default for NodeEventsChannel {
    fn default() -> Self {
        Self(broadcast::channel(NODE_EVENT_CHANNEL_SIZE).0)
    }
}

impl NodeEventsChannel {
    /// Returns a new receiver to listen to the channel.
    /// Multiple receivers can be actively listening.
    pub fn subscribe(&self) -> broadcast::Receiver<NodeEvent> {
        self.0.subscribe()
    }

    // Broadcast a new event, meant to be a helper only used by the sn_node's internals.
    pub(crate) fn broadcast(&self, event: NodeEvent) {
        let event_string = format!("{event:?}");
        if let Err(err) = self.0.send(event) {
            trace!(
                "Error occurred when trying to broadcast a node event ({event_string:?}): {err}"
            );
        }
    }

    /// Returns the number of active receivers
    pub fn receiver_count(&self) -> usize {
        self.0.receiver_count()
    }
}

/// Type of events broadcasted by the node to the public API.
#[derive(Clone, Serialize, custom_debug::Debug, Deserialize)]
pub enum NodeEvent {
    /// The node has been connected to the network
    ConnectedToNetwork,
    /// A Chunk has been stored in local storage
    ChunkStored(ChunkAddress),
    /// A Register has been created in local storage
    RegisterCreated(RegisterAddress),
    /// A Register edit operation has been applied in local storage
    RegisterEdited(RegisterAddress),
    /// A CashNote Spend has been stored in local storage
    SpendStored(UniquePubkey),
    /// One of the sub event channel closed and unrecoverable.
    ChannelClosed,
    /// Terminates the node
    TerminateNode(String),
}

impl NodeEvent {
    /// Convert NodeEvent to bytes
    pub fn to_bytes(&self) -> Result<Vec<u8>> {
        rmp_serde::to_vec(&self).map_err(|_| Error::NodeEventParsingFailed)
    }

    /// Get NodeEvent from bytes
    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
        rmp_serde::from_slice(bytes).map_err(|_| Error::NodeEventParsingFailed)
    }
}