Init
This commit is contained in:
@@ -7,49 +7,61 @@ import (
|
||||
mapset "github.com/deckarep/golang-set/v2"
|
||||
)
|
||||
|
||||
// ... existing code ...
|
||||
|
||||
type RoutingTable struct {
|
||||
mu sync.RWMutex
|
||||
routes map[string]mapset.Set[*net.UDPAddr]
|
||||
}
|
||||
|
||||
// NewRoutingTable crée une table vide.
|
||||
func NewRoutingTable() *RoutingTable {
|
||||
return &RoutingTable{
|
||||
routes: make(map[string]mapset.Set[*net.UDPAddr]),
|
||||
}
|
||||
}
|
||||
|
||||
func (rt *RoutingTable) AddClient(channelID string, addr *net.UDPAddr) {
|
||||
// Add enregistre un client dans un channel.
|
||||
func (rt *RoutingTable) Add(channel string, addr *net.UDPAddr) {
|
||||
rt.mu.Lock()
|
||||
defer rt.mu.Unlock()
|
||||
|
||||
if rt.routes[channelID] == nil {
|
||||
rt.routes[channelID] = mapset.NewSet[*net.UDPAddr]()
|
||||
set, ok := rt.routes[channel]
|
||||
if !ok {
|
||||
set = mapset.NewSet[*net.UDPAddr]()
|
||||
rt.routes[channel] = set
|
||||
}
|
||||
rt.routes[channelID].Add(addr)
|
||||
set.Add(addr)
|
||||
}
|
||||
|
||||
func (rt *RoutingTable) RemoveClient(channelID string, addr *net.UDPAddr) {
|
||||
// Remove supprime un client d'un channel.
|
||||
func (rt *RoutingTable) Remove(channel string, addr *net.UDPAddr) {
|
||||
rt.mu.Lock()
|
||||
defer rt.mu.Unlock()
|
||||
|
||||
if clients, exists := rt.routes[channelID]; exists {
|
||||
clients.Remove(addr)
|
||||
if clients.Cardinality() == 0 {
|
||||
delete(rt.routes, channelID)
|
||||
}
|
||||
set, ok := rt.routes[channel]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
set.Remove(addr)
|
||||
if set.Cardinality() == 0 {
|
||||
delete(rt.routes, channel)
|
||||
}
|
||||
}
|
||||
|
||||
// GetClients returns the clients connected to the given channelID
|
||||
// don't modify the returned set!
|
||||
func (rt *RoutingTable) GetClients(channelID string) mapset.Set[*net.UDPAddr] {
|
||||
// GetAddrs renvoie une copie de la liste des clients d'un channel.
|
||||
func (rt *RoutingTable) GetAddrs(channel string) []*net.UDPAddr {
|
||||
rt.mu.RLock()
|
||||
defer rt.mu.RUnlock()
|
||||
|
||||
clients, exists := rt.routes[channelID]
|
||||
if !exists {
|
||||
set, ok := rt.routes[channel]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
return clients
|
||||
addrs := make([]*net.UDPAddr, 0, set.Cardinality())
|
||||
for addr := range set.Iter() {
|
||||
addrs = append(addrs, addr)
|
||||
}
|
||||
return addrs
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user