jrouter/router/zones.go

162 lines
3.2 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-26 13:39:30 +10:00
"fmt"
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 {
2024-05-03 16:13:59 +10:00
Network ddp.Network
Name string
LocalPort *EtherTalkPort // nil if remote (local to another router)
LastSeen time.Time
2024-04-14 18:29:29 +10:00
}
2024-04-26 13:39:30 +10:00
func (z Zone) LastSeenAgo() string {
if z.LastSeen.IsZero() {
return "never"
}
return fmt.Sprintf("%v ago", time.Since(z.LastSeen).Truncate(time.Millisecond))
}
2024-04-14 18:29:29 +10:00
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),
}
}
2024-04-26 13:39:30 +10:00
func (zt *ZoneTable) Dump() []Zone {
zt.mu.Lock()
defer zt.mu.Unlock()
zs := make([]Zone, 0, len(zt.zones))
for _, z := range zt.zones {
zs = append(zs, *z)
}
return zs
}
2024-05-03 16:13:59 +10:00
func (zt *ZoneTable) Upsert(network ddp.Network, name string, localPort *EtherTalkPort) {
2024-04-14 18:29:29 +10:00
zt.mu.Lock()
defer zt.mu.Unlock()
key := zoneKey{network, name}
z := zt.zones[key]
if z != nil {
2024-05-03 16:13:59 +10:00
z.LocalPort = localPort
2024-04-14 18:29:29 +10:00
z.LastSeen = time.Now()
return
}
zt.zones[key] = &Zone{
2024-05-03 16:13:59 +10:00
Network: network,
Name: name,
LocalPort: localPort,
LastSeen: time.Now(),
2024-04-14 18:29:29 +10:00
}
}
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-05-03 16:13:59 +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 {
// // if time.Since(z.LastSeen) > maxZoneAge {
// // continue
// // }
// if z.Local != nil {
// 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
// }
2024-04-14 18:29:29 +10:00
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
}