Skip to content

Commit de0edbf

Browse files
authored
[receive] use cid instead of source name for discovery lookup (#52)
* use cid instead of source name for discovery lookup
1 parent aa52836 commit de0edbf

1 file changed

Lines changed: 32 additions & 15 deletions

File tree

src/receive.rs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ use crate::error::errors::*;
3030
/// This is used for uniquely identifying sources when counting sequence numbers.
3131
use uuid::Uuid;
3232

33-
use std::borrow::Cow;
3433
use std::cmp::{Ordering, max};
3534
use std::collections::HashMap;
3635
use std::fmt;
@@ -203,6 +202,9 @@ pub struct DiscoveredSacnSource {
203202
/// The name of the source, no protocol guarantee this will be unique but if it isn't then universe discovery may not work correctly.
204203
pub name: String,
205204

205+
/// The unique CID of the source. This should be unique across all devices on the network.
206+
pub cid: Uuid,
207+
206208
/// The time at which the discovered source was last updated / a discovery packet was received by the source.
207209
pub last_updated: Instant,
208210

@@ -545,7 +547,7 @@ impl SacnReceiver {
545547
SynchronizationPacket(s) => self.handle_sync_packet(pdu.cid, s)?,
546548
UniverseDiscoveryPacket(u) => {
547549
let discovered_src: Option<String> =
548-
self.handle_universe_discovery_packet(u);
550+
self.handle_universe_discovery_packet(pdu.cid, u);
549551
if let Some(src) = discovered_src
550552
&& self.announce_source_discovery
551553
{
@@ -725,7 +727,7 @@ impl SacnReceiver {
725727
}
726728

727729
if data_pkt.stream_terminated {
728-
self.terminate_stream(cid, data_pkt.source_name, data_pkt.universe);
730+
self.terminate_stream(cid, data_pkt.universe);
729731
if self.announce_stream_termination {
730732
return Err(SacnError::UniverseTerminated(cid, data_pkt.universe));
731733
}
@@ -799,16 +801,14 @@ impl SacnReceiver {
799801
///
800802
/// src_cid: The CID of the source which is terminating a universe.
801803
///
802-
/// source_name: The human readable name of the sACN source to remove the universe from.
803-
///
804804
/// universe: The sACN universe to remove.
805-
fn terminate_stream<'a>(&mut self, src_cid: Uuid, source_name: Cow<'a, str>, universe: u16) {
805+
fn terminate_stream(&mut self, src_cid: Uuid, universe: u16) {
806806
// Will only return an error if the source/universe wasn't found which is acceptable because as it
807807
// comes to the same result.
808808
let _ = self.sequences.remove_seq_numbers(src_cid, universe);
809809

810810
// As with sequence numbers the source might not be found which is acceptable.
811-
if let Some(index) = find_discovered_src(&self.discovered_sources, &source_name.to_string())
811+
if let Some(index) = find_discovered_src(&self.discovered_sources, &src_cid)
812812
{
813813
self.discovered_sources[index].terminate_universe(universe);
814814
}
@@ -916,7 +916,7 @@ impl SacnReceiver {
916916
/// Arguments:
917917
/// src: The DiscoveredSacnSource to update the record of discovered sacn sources with.
918918
fn update_discovered_srcs(&mut self, src: DiscoveredSacnSource) {
919-
if let Some(index) = find_discovered_src(&self.discovered_sources, &src.name) {
919+
if let Some(index) = find_discovered_src(&self.discovered_sources, &src.cid) {
920920
self.discovered_sources.remove(index);
921921
}
922922
self.discovered_sources.push(src);
@@ -930,9 +930,13 @@ impl SacnReceiver {
930930
/// Returns the source name if a source was fully discovered or None if the source was only partially discovered.
931931
///
932932
/// Arguments:
933+
///
934+
/// cid: the source CID.
935+
///
933936
/// discovery_pkt: The universe discovery part of the universe discovery packet to handle.
934937
fn handle_universe_discovery_packet(
935938
&mut self,
939+
cid: Uuid,
936940
discovery_pkt: UniverseDiscoveryPacketFramingLayer,
937941
) -> Option<String> {
938942
let data: UniverseDiscoveryPacketUniverseDiscoveryLayer = discovery_pkt.data;
@@ -950,7 +954,7 @@ impl SacnReceiver {
950954
// See if some pages that belong to the source that this page belongs to have already been received.
951955
match find_discovered_src(
952956
&self.partially_discovered_sources,
953-
&discovery_pkt.source_name.to_string(),
957+
&cid
954958
) {
955959
Some(index) => {
956960
// Some pages have already been received from this source.
@@ -969,6 +973,7 @@ impl SacnReceiver {
969973
// This is the first page received from this source.
970974
let discovered_src: DiscoveredSacnSource = DiscoveredSacnSource {
971975
name: discovery_pkt.source_name.to_string(),
976+
cid,
972977
last_page,
973978
pages: vec![uni_page],
974979
last_updated: Instant::now(),
@@ -1029,10 +1034,12 @@ impl Drop for SacnReceiver {
10291034
/// returns the index of the src in the Vec or None if not found.
10301035
///
10311036
/// Arguments:
1037+
///
10321038
/// srcs: The Vec of DiscoveredSacnSources to search.
1033-
/// name: The human readable name of the source to find.
1034-
fn find_discovered_src(srcs: &[DiscoveredSacnSource], name: &String) -> Option<usize> {
1035-
(0..srcs.len()).find(|&i| srcs[i].name == *name)
1039+
///
1040+
/// cid: The CID (uuid) of the source to find.
1041+
fn find_discovered_src(srcs: &[DiscoveredSacnSource], cid: &Uuid) -> Option<usize> {
1042+
(0..srcs.len()).find(|&i| srcs[i].cid == *cid)
10361043
}
10371044

10381045
/// In general the lower level transport layer is handled by SacnNetworkReceiver (which itself wraps a Socket).
@@ -2158,6 +2165,10 @@ mod test {
21582165
let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();
21592166

21602167
let name = "Test Src 1";
2168+
let src_cid: Uuid = Uuid::from_bytes([
2169+
0xef, 0x07, 0xc8, 0xdd, 0x00, 0x64, 0x44, 0x01, 0xa3, 0xa2, 0x45, 0x9e, 0xf8, 0xe6,
2170+
0x14, 0x3e,
2171+
]);
21612172
let page: u8 = 0;
21622173
let last_page: u8 = 0;
21632174
let universes: Vec<u16> = vec![0, 1, 2, 3, 4, 5];
@@ -2177,14 +2188,15 @@ mod test {
21772188
universes: universes.clone().into(),
21782189
},
21792190
};
2180-
let res: Option<String> = dmx_rcv.handle_universe_discovery_packet(discovery_pkt);
2191+
let res: Option<String> = dmx_rcv.handle_universe_discovery_packet(src_cid, discovery_pkt);
21812192

21822193
assert!(res.is_some());
21832194
assert_eq!(res.unwrap(), name);
21842195

21852196
assert_eq!(dmx_rcv.discovered_sources.len(), 1);
21862197

21872198
assert_eq!(dmx_rcv.discovered_sources[0].name, name);
2199+
assert_eq!(dmx_rcv.discovered_sources[0].cid, src_cid);
21882200
assert_eq!(dmx_rcv.discovered_sources[0].last_page, last_page);
21892201
assert_eq!(dmx_rcv.discovered_sources[0].pages.len(), 1);
21902202
assert_eq!(dmx_rcv.discovered_sources[0].pages[0].page, page);
@@ -2198,6 +2210,10 @@ mod test {
21982210
let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();
21992211

22002212
let name = "Test Src 1";
2213+
let src_cid: Uuid = Uuid::from_bytes([
2214+
0xef, 0x07, 0xc8, 0xdd, 0x00, 0x64, 0x44, 0x01, 0xa3, 0xa2, 0x45, 0x9e, 0xf8, 0xe6,
2215+
0x14, 0x3e,
2216+
]);
22012217
let last_page: u8 = 1;
22022218
let mut universes_page_1: Vec<u16> = Vec::new();
22032219
let mut universes_page_2: Vec<u16> = Vec::new();
@@ -2241,18 +2257,19 @@ mod test {
22412257
universes: universes_page_2.clone().into(),
22422258
},
22432259
};
2244-
let res: Option<String> = dmx_rcv.handle_universe_discovery_packet(discovery_pkt_1);
2260+
let res: Option<String> = dmx_rcv.handle_universe_discovery_packet(src_cid, discovery_pkt_1);
22452261

22462262
assert!(res.is_none()); // Should be none because first packet isn't complete as its only the first page.
22472263

2248-
let res2: Option<String> = dmx_rcv.handle_universe_discovery_packet(discovery_pkt_2);
2264+
let res2: Option<String> = dmx_rcv.handle_universe_discovery_packet(src_cid, discovery_pkt_2);
22492265

22502266
assert!(res2.is_some()); // Source should be discovered because the second and last page is now received.
22512267
assert_eq!(res2.unwrap(), name);
22522268

22532269
assert_eq!(dmx_rcv.discovered_sources.len(), 1);
22542270

22552271
assert_eq!(dmx_rcv.discovered_sources[0].name, name);
2272+
assert_eq!(dmx_rcv.discovered_sources[0].cid, src_cid);
22562273
assert_eq!(dmx_rcv.discovered_sources[0].last_page, last_page);
22572274
assert_eq!(dmx_rcv.discovered_sources[0].pages.len(), 2);
22582275
assert_eq!(dmx_rcv.discovered_sources[0].pages[0].page, 0);

0 commit comments

Comments
 (0)