Files
MARTe_IO_Components/Client/WebUI/sources.go
T
Martino Ferrari b15e637f14 Add TCP control + UDP multicast mode to UDPStreamer
DataSource (C++):
- New optional config: MulticastGroup (e.g. "239.0.0.1") and DataPort
  (default Port+1); when set, enables multicast mode; absent = unicast
- Control plane: BasicTCPSocket listener on Port; CONNECT received over
  TCP triggers HandleTCPConnect() which sends CONFIG via the same TCP
  connection
- Data plane: BasicUDPSocket aimed at MulticastGroup:DataPort; all DATA
  fragments sent via dataSocket.Write() so any joined client receives them
- useMulticast is the single branch point; every unicast code path is
  unchanged when MulticastGroup is absent
- New methods: HandleTCPConnect(), IsMulticast()
- 5 new unit tests (ports 44710-44729); all 38 tests passing

WebUI hub (Go):
- SourceConfig gains MulticastGroup and DataPort fields (JSON, optional)
- UDPClient gains multicastGroup/dataPort; Run() routes to new
  runMulticastSession() when multicastGroup is non-empty
- runMulticastSession(): TCP dial for CONNECT/CONFIG, net.ListenMulticastUDP
  for DATA, background goroutine watches TCP for DISCONNECT/close
- All existing sm.Add() call sites updated (unicast callers pass "", 0)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-26 22:56:27 +02:00

144 lines
3.6 KiB
Go

package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"sync"
"sync/atomic"
)
// SourceConfig is the serialisable description of one data source (for file save/load).
type SourceConfig struct {
Label string `json:"label"`
Addr string `json:"addr"`
MulticastGroup string `json:"multicastGroup,omitempty"` // "" = unicast mode
DataPort int `json:"dataPort,omitempty"` // 0 = Addr port+1
}
// managedSource is the SourceManager's view of one running source.
type managedSource struct {
id string
label string
addr string
multicastGroup string
dataPort int
client *UDPClient
}
// SourceManager owns the lifecycle of all active data sources.
// It is the only place where UDPClient instances are created and stopped.
type SourceManager struct {
mu sync.RWMutex
sources map[string]*managedSource
hub *Hub
filePath string // empty = save not configured
nextID atomic.Int32
}
// NewSourceManager creates a SourceManager bound to the given hub.
// filePath is the default path for Save(); may be empty.
func NewSourceManager(hub *Hub, filePath string) *SourceManager {
return &SourceManager{
sources: make(map[string]*managedSource),
hub: hub,
filePath: filePath,
}
}
func (sm *SourceManager) genID() string {
return fmt.Sprintf("s%d", sm.nextID.Add(1))
}
// Add creates a new source, registers it in the hub, and starts connecting.
// multicastGroup: multicast IP (e.g. "239.0.0.1") or "" for unicast mode.
// dataPort: UDP data port in multicast mode; 0 = Addr port+1.
// Returns the source ID.
func (sm *SourceManager) Add(label, addr, multicastGroup string, dataPort int) string {
if label == "" {
label = addr
}
id := sm.genID()
c := NewUDPClient(addr, id, sm.hub, multicastGroup, dataPort)
ms := &managedSource{
id: id, label: label, addr: addr,
multicastGroup: multicastGroup, dataPort: dataPort,
client: c,
}
sm.mu.Lock()
sm.sources[id] = ms
sm.mu.Unlock()
// Register in hub (creates hub-side state, broadcasts sources list).
sm.hub.AddSource(id, label, addr)
go c.Run()
return id
}
// Remove stops the source's client and removes it from the hub.
func (sm *SourceManager) Remove(id string) {
sm.mu.Lock()
ms, ok := sm.sources[id]
if ok {
delete(sm.sources, id)
}
sm.mu.Unlock()
if ok {
ms.client.Stop()
sm.hub.RemoveSource(id)
}
}
// Save writes the current source list to filePath.
func (sm *SourceManager) Save() error {
if sm.filePath == "" {
return fmt.Errorf("no sources-file configured (start with --sources-file path)")
}
sm.mu.RLock()
cfgs := make([]SourceConfig, 0, len(sm.sources))
for _, ms := range sm.sources {
cfgs = append(cfgs, SourceConfig{
Label: ms.label,
Addr: ms.addr,
MulticastGroup: ms.multicastGroup,
DataPort: ms.dataPort,
})
}
sm.mu.RUnlock()
data, err := json.MarshalIndent(cfgs, "", " ")
if err != nil {
return err
}
return os.WriteFile(sm.filePath, data, 0644)
}
// Load reads sources from path and adds them. Sets filePath for future saves.
func (sm *SourceManager) Load(path string) error {
data, err := os.ReadFile(path)
if err != nil {
return err
}
var cfgs []SourceConfig
if err := json.Unmarshal(data, &cfgs); err != nil {
return err
}
sm.filePath = path
for _, cfg := range cfgs {
sm.Add(cfg.Label, cfg.Addr, cfg.MulticastGroup, cfg.DataPort)
}
return nil
}
// ParseSourceArg parses "label@host:port" or "host:port".
func ParseSourceArg(s string) (label, addr string) {
s = strings.TrimSpace(s)
if idx := strings.Index(s, "@"); idx >= 0 {
return strings.TrimSpace(s[:idx]), strings.TrimSpace(s[idx+1:])
}
return "", s
}