pub struct SourceCache { /* private fields */ }Expand description
Thread-safe cache for NDI source discovery.
SourceCache eliminates the need for applications to manually cache NDI instances
and discovered sources. Discovery initializes the runtime on a cache miss.
Cached sources own their strings and need no live runtime. Entries remain
cached until explicitly invalidated or cleared.
Equivalent hostname case/final-dot and numeric IP spellings share one entry.
Hostnames, IP addresses, and DNS aliases otherwise remain distinct identities.
§Thread Safety
SourceCache is thread-safe and can be shared across threads using Arc<SourceCache>.
Interior mutability is handled internally with proper synchronization.
§Examples
use grafton_ndi::SourceCache;
use std::time::Duration;
// Create a cache instance
let cache = SourceCache::new();
// Find a source by hostname or IP with automatic caching
let source = cache.find_by_host("192.168.0.107", Duration::from_secs(5))?;
println!("Found source: {}", source);
// Subsequent lookups use the cache
let same_source = cache.find_by_host("192.168.0.107", Duration::from_secs(5))?;
Implementations§
Source§impl SourceCache
impl SourceCache
Sourcepub fn find_by_host(&self, host: &str, timeout: Duration) -> Result<Source>
pub fn find_by_host(&self, host: &str, timeout: Duration) -> Result<Source>
Find a source by IP address or hostname with built-in caching.
This method handles NDI initialization and source discovery internally. If a source matching the host has been previously found, it returns the cached result. Otherwise, it performs NDI discovery and caches the result.
§Arguments
host- The hostname or IP address to search fortimeout- Maximum time to wait for source discovery. Must not exceedcrate::MAX_TIMEOUT(~49.7 days).
§Returns
The discovered source, or an error if no matching source is found or the timeout expires.
§Errors
Error::NoSourcesFoundif no source matching the host is discoveredError::InvalidConfigurationifhostis not a bare hostname or IP, ortimeoutexceedscrate::MAX_TIMEOUT- Other errors if NDI initialization or discovery fails
§Examples
use grafton_ndi::SourceCache;
use std::time::Duration;
let cache = SourceCache::new();
// Find by IP address
let source = cache.find_by_host("192.168.0.107", Duration::from_secs(5))?;
// Find by the machine component of an SDK source name
let source = cache.find_by_host("CAMERA1", Duration::from_secs(5))?;Sourcepub fn invalidate(&self, host: &str)
pub fn invalidate(&self, host: &str)
Invalidate the cache entry for a specific host.
This is useful when a source goes offline or when you want to force a fresh discovery on the next lookup. Discovery started before this call may return to its caller, but cannot repopulate the cache afterward. Any accepted equivalent spelling removes the same host identity. Malformed input removes no entry but still invalidates pending discovery.
§Arguments
host- The hostname or IP address to remove from the cache
§Examples
use grafton_ndi::SourceCache;
use std::time::Duration;
let cache = SourceCache::new();
let source = cache.find_by_host("192.168.0.107", Duration::from_secs(5))?;
// Later, if the source goes offline
cache.invalidate("192.168.0.107");
// Next lookup will perform fresh discoverySourcepub fn clear(&self)
pub fn clear(&self)
Clear all cached sources.
This removes all entries from the cache, forcing fresh discovery for all subsequent lookups. Pending discovery cannot repopulate the cache after this call returns.
§Examples
use grafton_ndi::SourceCache;
use std::time::Duration;
let cache = SourceCache::new();
cache.find_by_host("192.168.0.107", Duration::from_secs(5))?;
cache.find_by_host("192.168.0.108", Duration::from_secs(5))?;
// Clear all cached sources
cache.clear();Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the number of distinct cached host identities.
This can be useful for monitoring cache usage and debugging.
§Examples
use grafton_ndi::SourceCache;
use std::time::Duration;
let cache = SourceCache::new();
assert_eq!(cache.len(), 0);
cache.find_by_host("192.168.0.107", Duration::from_secs(5))?;
assert_eq!(cache.len(), 1);