Track DMX packet senders and expose via API

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Ian Gulliver
2026-01-30 22:11:34 -08:00
parent d91e5918a4
commit 1ba098ee24
2 changed files with 93 additions and 1 deletions

67
senders/senders.go Normal file
View File

@@ -0,0 +1,67 @@
package senders
import (
"net"
"sync"
"time"
"github.com/gopatchy/artmap/config"
)
type SenderInfo struct {
Universe config.Universe `json:"universe"`
IP string `json:"ip"`
}
type senderKey struct {
protocol config.Protocol
universe uint16
ip string
}
type UniverseSenders struct {
mu sync.Mutex
entries map[senderKey]time.Time
}
func New() *UniverseSenders {
return &UniverseSenders{
entries: map[senderKey]time.Time{},
}
}
func (s *UniverseSenders) Record(u config.Universe, ip net.IP) {
key := senderKey{
protocol: u.Protocol,
universe: u.Number,
ip: ip.String(),
}
s.mu.Lock()
s.entries[key] = time.Now()
s.mu.Unlock()
}
func (s *UniverseSenders) Expire(maxAge time.Duration) {
cutoff := time.Now().Add(-maxAge)
s.mu.Lock()
for k, t := range s.entries {
if t.Before(cutoff) {
delete(s.entries, k)
}
}
s.mu.Unlock()
}
func (s *UniverseSenders) GetAll() []SenderInfo {
s.mu.Lock()
defer s.mu.Unlock()
result := make([]SenderInfo, 0, len(s.entries))
for k := range s.entries {
result = append(result, SenderInfo{
Universe: config.Universe{Protocol: k.protocol, Number: k.universe},
IP: k.ip,
})
}
return result
}