event.rs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. use std::fmt::Debug;
  19. use crate::{dht::DhtNode, net::ChannelPtr, Result};
  20. type K = blake3::Hash;
  21. #[derive(Clone, Debug)]
  22. pub enum DhtEvent<N: DhtNode, V: Clone + Debug> {
  23. BootstrapStarted,
  24. BootstrapCompleted,
  25. PingReceived { from: ChannelPtr, result: Result<K> },
  26. PingSent { to: ChannelPtr, result: Result<()> },
  27. ValueFound { key: K, value: V },
  28. NodesFound { key: K, nodes: Vec<N> },
  29. ValueLookupStarted { key: K },
  30. NodesLookupStarted { key: K },
  31. ValueLookupCompleted { key: K, nodes: Vec<N>, values: Vec<V> },
  32. NodesLookupCompleted { key: K, nodes: Vec<N> },
  33. }
  34. impl<N: DhtNode, V: Clone + Debug> DhtEvent<N, V> {
  35. pub fn key(&self) -> Option<&blake3::Hash> {
  36. match self {
  37. DhtEvent::BootstrapStarted => None,
  38. DhtEvent::BootstrapCompleted => None,
  39. DhtEvent::PingReceived { .. } => None,
  40. DhtEvent::PingSent { .. } => None,
  41. DhtEvent::ValueFound { key, .. } => Some(key),
  42. DhtEvent::NodesFound { key, .. } => Some(key),
  43. DhtEvent::ValueLookupStarted { key } => Some(key),
  44. DhtEvent::NodesLookupStarted { key } => Some(key),
  45. DhtEvent::ValueLookupCompleted { key, .. } => Some(key),
  46. DhtEvent::NodesLookupCompleted { key, .. } => Some(key),
  47. }
  48. }
  49. pub fn into_value(self) -> Option<V> {
  50. match self {
  51. DhtEvent::ValueFound { value, .. } => Some(value),
  52. _ => None,
  53. }
  54. }
  55. }