jrouter/router/zones.go

144 lines
2.7 KiB
Go
Raw Normal View History

2024-04-14 18:29:29 +10:00
/*
Copyright 2024 Josh Deprez
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2024-04-19 14:57:25 +10:00
package router
2024-04-14 18:29:29 +10:00
import (
2024-04-14 21:36:35 +10:00
"slices"
2024-04-14 18:29:29 +10:00
"sort"
"sync"
"time"
"github.com/sfiera/multitalk/pkg/ddp"
)
2024-04-15 17:08:53 +10:00
//const maxZoneAge = 10 * time.Minute // TODO: confirm
2024-04-14 18:29:29 +10:00
type Zone struct {
Network ddp.Network
Name string
Local bool
LastSeen time.Time
}
type zoneKey struct {
network ddp.Network
name string
}
type ZoneTable struct {
mu sync.Mutex
zones map[zoneKey]*Zone
}
func NewZoneTable() *ZoneTable {
return &ZoneTable{
zones: make(map[zoneKey]*Zone),
}
}
func (zt *ZoneTable) Upsert(network ddp.Network, name string, local bool) {
zt.mu.Lock()
defer zt.mu.Unlock()
key := zoneKey{network, name}
z := zt.zones[key]
if z != nil {
z.Local = local
z.LastSeen = time.Now()
return
}
zt.zones[key] = &Zone{
Network: network,
Name: name,
Local: local,
LastSeen: time.Now(),
}
}
2024-04-14 21:36:35 +10:00
func (zt *ZoneTable) Query(ns []ddp.Network) map[ddp.Network][]string {
slices.Sort(ns)
zs := make(map[ddp.Network][]string)
zt.mu.Lock()
defer zt.mu.Unlock()
for _, z := range zt.zones {
2024-04-15 17:08:53 +10:00
// if time.Since(z.LastSeen) > maxZoneAge {
// continue
// }
2024-04-14 21:36:35 +10:00
if _, ok := slices.BinarySearch(ns, z.Network); ok {
zs[z.Network] = append(zs[z.Network], z.Name)
}
}
return zs
}
2024-04-15 18:08:45 +10:00
func (zt *ZoneTable) LookupName(name string) []*Zone {
zt.mu.Lock()
defer zt.mu.Unlock()
var zs []*Zone
for _, z := range zt.zones {
if z.Name == name {
zs = append(zs, z)
}
}
return zs
}
2024-04-14 18:29:29 +10:00
func (zt *ZoneTable) LocalNames() []string {
zt.mu.Lock()
seen := make(map[string]struct{})
zs := make([]string, 0, len(zt.zones))
for _, z := range zt.zones {
2024-04-15 17:08:53 +10:00
// if time.Since(z.LastSeen) > maxZoneAge {
// continue
// }
2024-04-14 18:29:29 +10:00
if !z.Local {
continue
}
if _, s := seen[z.Name]; s {
continue
}
seen[z.Name] = struct{}{}
zs = append(zs, z.Name)
}
zt.mu.Unlock()
sort.Strings(zs)
return zs
}
func (zt *ZoneTable) AllNames() []string {
zt.mu.Lock()
seen := make(map[string]struct{})
zs := make([]string, 0, len(zt.zones))
for _, z := range zt.zones {
2024-04-15 17:08:53 +10:00
// if time.Since(z.LastSeen) > maxZoneAge {
// continue
// }
2024-04-14 18:29:29 +10:00
if _, s := seen[z.Name]; s {
continue
}
seen[z.Name] = struct{}{}
zs = append(zs, z.Name)
}
zt.mu.Unlock()
sort.Strings(zs)
return zs
}