88//! Frames are opaque Opus bytes — the relay never decodes audio.
99//! `try_send` is used throughout: real-time audio tolerates drops, never queues.
1010
11+ use buzz_core:: CommunityId ;
1112use bytes:: Bytes ;
1213use dashmap:: DashMap ;
1314use std:: sync:: Arc ;
@@ -117,8 +118,8 @@ struct AdmissionGuard {
117118 ///
118119 /// Pin is per-`Room`-instance and clears when the manager evicts the
119120 /// Room via [`AudioRoomManager::cleanup_if_empty`] — the next
120- /// `get_or_create` for the same channel id then constructs a fresh
121- /// `Room` with a fresh `AdmissionGuard` (and therefore `None` pin),
121+ /// `get_or_create` for the same community-local channel then constructs a
122+ /// fresh `Room` with a fresh `AdmissionGuard` (and therefore `None` pin),
122123 /// so a new generation of joiners can negotiate a new version.
123124 /// A momentarily-empty-but-not-yet-cleaned-up Room keeps its pin so
124125 /// reconnecting peers don't accidentally renegotiate mid-call. See
@@ -158,6 +159,8 @@ impl AdmissionGuard {
158159
159160/// A single audio room for one channel.
160161pub struct Room {
162+ /// Community this room belongs to.
163+ pub community_id : CommunityId ,
161164 /// Channel UUID this room belongs to.
162165 pub channel_id : Uuid ,
163166 /// Connected peers keyed by peer UUID.
@@ -170,10 +173,11 @@ pub struct Room {
170173}
171174
172175impl Room {
173- /// Create an empty room for the given channel.
174- pub fn new ( channel_id : Uuid ) -> Self {
176+ /// Create an empty room for the given community-local channel.
177+ pub fn new ( community_id : CommunityId , channel_id : Uuid ) -> Self {
175178 let ( roster_tx, _) = broadcast:: channel ( 64 ) ;
176179 Self {
180+ community_id,
177181 channel_id,
178182 peers : DashMap :: new ( ) ,
179183 guard : std:: sync:: Mutex :: new ( AdmissionGuard :: new ( ) ) ,
@@ -487,7 +491,7 @@ impl Room {
487491
488492/// Global registry of active audio rooms.
489493pub struct AudioRoomManager {
490- rooms : DashMap < Uuid , Arc < Room > > ,
494+ rooms : DashMap < ( CommunityId , Uuid ) , Arc < Room > > ,
491495}
492496
493497impl AudioRoomManager {
@@ -499,25 +503,47 @@ impl AudioRoomManager {
499503 }
500504
501505 /// Get an existing room or create a new one.
502- pub fn get_or_create ( & self , channel_id : Uuid ) -> Arc < Room > {
506+ ///
507+ /// Channel UUIDs are only unique inside a community. The room key must
508+ /// carry both labels so two tenants that legitimately reuse the same UUID
509+ /// never share peer lists, protocol pins, or audio frames.
510+ pub fn get_or_create ( & self , community_id : CommunityId , channel_id : Uuid ) -> Arc < Room > {
503511 self . rooms
504- . entry ( channel_id)
505- . or_insert_with ( || Arc :: new ( Room :: new ( channel_id) ) )
512+ . entry ( ( community_id , channel_id) )
513+ . or_insert_with ( || Arc :: new ( Room :: new ( community_id , channel_id) ) )
506514 . clone ( )
507515 }
508516
509- /// Look up an existing room without creating one. Returns `None` when this
510- /// pod hosts no room for the channel — the cross-pod media path uses this
511- /// so an inbound datagram for a session we don't participate in is dropped,
512- /// never materializing an empty room.
513- pub fn get ( & self , channel_id : Uuid ) -> Option < Arc < Room > > {
514- self . rooms . get ( & channel_id) . map ( |r| r. clone ( ) )
517+ /// Look up an existing community-local room without creating one.
518+ pub fn get ( & self , community_id : CommunityId , channel_id : Uuid ) -> Option < Arc < Room > > {
519+ self . rooms
520+ . get ( & ( community_id, channel_id) )
521+ . map ( |room| room. clone ( ) )
522+ }
523+
524+ /// Look up a room for a mesh datagram that carries only a channel UUID.
525+ ///
526+ /// The current mesh media envelope does not carry a community identifier.
527+ /// If two active communities use the same channel UUID, routing would be
528+ /// ambiguous, so fail closed instead of delivering one community's audio
529+ /// to the other. Control-path lookups always use [`Self::get`].
530+ pub fn get_unambiguous_by_channel ( & self , channel_id : Uuid ) -> Option < Arc < Room > > {
531+ let mut matches = self
532+ . rooms
533+ . iter ( )
534+ . filter ( |entry| entry. key ( ) . 1 == channel_id)
535+ . map ( |entry| Arc :: clone ( entry. value ( ) ) ) ;
536+ let room = matches. next ( ) ?;
537+ if matches. next ( ) . is_some ( ) {
538+ return None ;
539+ }
540+ Some ( room)
515541 }
516542
517543 /// Remove the room if it has no peers. Returns `true` if the room was removed.
518- pub fn cleanup_if_empty ( & self , channel_id : Uuid ) -> bool {
544+ pub fn cleanup_if_empty ( & self , community_id : CommunityId , channel_id : Uuid ) -> bool {
519545 self . rooms
520- . remove_if ( & channel_id, |_, room| room. is_empty ( ) )
546+ . remove_if ( & ( community_id , channel_id) , |_, room| room. is_empty ( ) )
521547 . is_some ( )
522548 }
523549}
@@ -533,7 +559,7 @@ mod tests {
533559 use super :: * ;
534560
535561 fn fresh_room ( ) -> Room {
536- Room :: new ( Uuid :: new_v4 ( ) )
562+ Room :: new ( CommunityId :: from_uuid ( Uuid :: new_v4 ( ) ) , Uuid :: new_v4 ( ) )
537563 }
538564
539565 #[ test]
@@ -644,9 +670,10 @@ mod tests {
644670 #[ test]
645671 fn manager_cleanup_resets_version_pin ( ) {
646672 let manager = AudioRoomManager :: new ( ) ;
673+ let community_id = CommunityId :: from_uuid ( Uuid :: new_v4 ( ) ) ;
647674 let channel_id = Uuid :: new_v4 ( ) ;
648675
649- let room1 = manager. get_or_create ( channel_id) ;
676+ let room1 = manager. get_or_create ( community_id , channel_id) ;
650677 let ( peer_id, _, _, _) = room1
651678 . add_peer ( "alice" . to_string ( ) , 2 )
652679 . expect ( "first peer admits" ) ;
@@ -655,16 +682,53 @@ mod tests {
655682 . remove_peer_and_check_ended ( peer_id)
656683 . expect ( "peer existed" ) ;
657684 assert ! ( ended, "single-peer room should end on its last departure" ) ;
658- assert ! ( manager. cleanup_if_empty( channel_id) ) ;
685+ assert ! ( manager. cleanup_if_empty( community_id , channel_id) ) ;
659686
660687 // Next joiner with a different version on the same channel id gets a
661688 // brand-new room (no v=2 pin carried over from the prior generation).
662- let room2 = manager. get_or_create ( channel_id) ;
689+ let room2 = manager. get_or_create ( community_id , channel_id) ;
663690 let _ = room2
664691 . add_peer ( "bob" . to_string ( ) , 1 )
665692 . expect ( "fresh room must accept any version" ) ;
666693 }
667694
695+ #[ test]
696+ fn manager_isolates_same_channel_uuid_across_communities ( ) {
697+ let manager = AudioRoomManager :: new ( ) ;
698+ let channel_id = Uuid :: new_v4 ( ) ;
699+ let community_a = CommunityId :: from_uuid ( Uuid :: new_v4 ( ) ) ;
700+ let community_b = CommunityId :: from_uuid ( Uuid :: new_v4 ( ) ) ;
701+
702+ let room_a = manager. get_or_create ( community_a, channel_id) ;
703+ assert ! ( Arc :: ptr_eq(
704+ & manager
705+ . get_unambiguous_by_channel( channel_id)
706+ . expect( "one matching room is unambiguous" ) ,
707+ & room_a
708+ ) ) ;
709+ let room_b = manager. get_or_create ( community_b, channel_id) ;
710+
711+ assert ! (
712+ !Arc :: ptr_eq( & room_a, & room_b) ,
713+ "same channel UUID in two communities must create distinct rooms"
714+ ) ;
715+ assert_eq ! ( room_a. community_id, community_a) ;
716+ assert_eq ! ( room_b. community_id, community_b) ;
717+ assert ! (
718+ manager. get_unambiguous_by_channel( channel_id) . is_none( ) ,
719+ "community-free mesh lookup must fail closed on a UUID collision"
720+ ) ;
721+
722+ room_a
723+ . add_peer ( "alice" . to_string ( ) , 1 )
724+ . expect ( "A peer admits" ) ;
725+ assert_eq ! ( room_a. peer_pubkeys( ) , vec![ ( "alice" . to_string( ) , 0 ) ] ) ;
726+ assert ! (
727+ room_b. peer_pubkeys( ) . is_empty( ) ,
728+ "A room peers must not appear in B's same-UUID room"
729+ ) ;
730+ }
731+
668732 /// Peer-index reuse: after a peer leaves, their index is released; a new
669733 /// peer joining the same (still-pinned) room reuses the freed index.
670734 /// Version pin must persist across this reuse — the room generation
0 commit comments