Skip to main content

hive_discovery/
lib.rs

1// file_path: src/services/discovery/mod.rs
2pub mod error;
3pub mod mdns;
4pub mod types;
5
6pub use error::{HiveDiscoError, Result};
7pub use types::{
8    DiscoveryEvent, DiscoveryServiceDetails, DiscoveryServiceStatus, LocalServiceConfig,
9};
10
11use std::sync::Arc;
12use tokio::sync::broadcast;
13
14/// Service Discovery Trait.
15///
16/// Defines the core functional interface that service discovery components must implement.
17pub trait DiscoveryService: Sync + Send {
18    /// Registers the local service on the network.
19    ///
20    /// This makes the current device discoverable as a service provider.
21    fn register_service(&self) -> Result<()>;
22
23    /// Starts network service discovery.
24    ///
25    /// Begins listening for service broadcasts on the network.
26    fn start_discovery(&self) -> Result<()>;
27
28    /// Subscribes to service discovery events.
29    ///
30    /// Returns a `broadcast::Receiver` to receive various service discovery events.
31    fn subscribe(&self) -> broadcast::Receiver<DiscoveryEvent>;
32
33    /// Adds an instance name filter.
34    ///
35    /// This is used to ignore events from specific service instances.
36    fn add_filter(&self, instance_name: String);
37
38    /// Removes an instance name filter.
39    fn remove_filter(&self, instance_name: &str);
40
41    /// Stops network service discovery.
42    ///
43    /// Stops listening for service broadcasts on the network.
44    fn stop_discovery(&self) -> Result<()>;
45
46    /// Refreshes discovered services.
47    ///
48    /// Re-sends `ServiceFound` events for all currently known services.
49    /// This can be useful for new subscribers to get the current state.
50    fn refresh_services(&self) -> Result<()>;
51
52    /// Shuts down the service discovery component.
53    ///
54    /// Completely stops all service discovery related functions and releases resources.
55    fn shutdown(&self) -> Result<()>;
56
57    /// Gets the current operational status of the service discovery component.
58    fn status(&self) -> DiscoveryServiceStatus;
59}
60
61/// Specifies the underlying implementation for service discovery.
62#[derive(Debug, Clone, Copy, PartialEq)]
63pub enum DiscoveryImplementation {
64    /// mDNS/DNS-SD based service discovery.
65    Mdns,
66    /// UDP multicast based service discovery (placeholder).
67    Multicast,
68}
69
70/// Creates a service discovery component instance.
71///
72/// # Arguments
73/// * `implementation` - Specifies which service discovery implementation to use.
74/// * `config` - Configuration parameters for the local service.
75///
76/// # Returns
77/// A `Result` containing an `Arc` to a component that implements the `DiscoveryService` trait,
78/// or a `HiveDiscoError` if instantiation fails.
79pub fn create_discovery_service(
80    implementation: DiscoveryImplementation,
81    config: LocalServiceConfig,
82) -> Result<Arc<dyn DiscoveryService>> {
83    match implementation {
84        DiscoveryImplementation::Mdns => {
85            let service = mdns::MdnsDiscoveryService::new(config)?;
86            Ok(Arc::new(service))
87        }
88        DiscoveryImplementation::Multicast => Err(error::HiveDiscoError::ConfigError(
89            "Multicast implementation is not yet complete".to_string(),
90        )),
91    }
92}