darkfi/dht/
event.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2026 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use std::fmt::Debug;
20
21use crate::{dht::DhtNode, net::ChannelPtr, Result};
22
23type K = blake3::Hash;
24
25#[derive(Clone, Debug)]
26pub enum DhtEvent<N: DhtNode, V: Clone + Debug> {
27    BootstrapStarted,
28    BootstrapCompleted,
29    PingReceived { from: ChannelPtr, result: Result<K> },
30    PingSent { to: ChannelPtr, result: Result<()> },
31    ValueFound { key: K, value: V },
32    NodesFound { key: K, nodes: Vec<N> },
33    ValueLookupStarted { key: K },
34    NodesLookupStarted { key: K },
35    ValueLookupCompleted { key: K, nodes: Vec<N>, values: Vec<V> },
36    NodesLookupCompleted { key: K, nodes: Vec<N> },
37}
38
39impl<N: DhtNode, V: Clone + Debug> DhtEvent<N, V> {
40    pub fn key(&self) -> Option<&blake3::Hash> {
41        match self {
42            DhtEvent::BootstrapStarted => None,
43            DhtEvent::BootstrapCompleted => None,
44            DhtEvent::PingReceived { .. } => None,
45            DhtEvent::PingSent { .. } => None,
46            DhtEvent::ValueFound { key, .. } => Some(key),
47            DhtEvent::NodesFound { key, .. } => Some(key),
48            DhtEvent::ValueLookupStarted { key } => Some(key),
49            DhtEvent::NodesLookupStarted { key } => Some(key),
50            DhtEvent::ValueLookupCompleted { key, .. } => Some(key),
51            DhtEvent::NodesLookupCompleted { key, .. } => Some(key),
52        }
53    }
54
55    pub fn into_value(self) -> Option<V> {
56        match self {
57            DhtEvent::ValueFound { value, .. } => Some(value),
58            _ => None,
59        }
60    }
61}