mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 05:15:47 +00:00
45 lines
851 B
Go
45 lines
851 B
Go
package showbridge
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync"
|
|
)
|
|
|
|
type Protocol interface {
|
|
Run(context.Context) error
|
|
}
|
|
|
|
type ProtocolConfig struct {
|
|
Type string `json:"type"`
|
|
Params map[string]any `json:"params"`
|
|
}
|
|
|
|
type ProtocolRegistration struct {
|
|
Type string `json:"type"`
|
|
New func(map[string]any) (Protocol, error)
|
|
}
|
|
|
|
func RegisterProtocol(proto ProtocolRegistration) {
|
|
|
|
if proto.Type == "" {
|
|
panic("protocol type is missing")
|
|
}
|
|
if proto.New == nil {
|
|
panic("missing ProtocolInfo.New")
|
|
}
|
|
|
|
protocolRegistryMu.Lock()
|
|
defer protocolRegistryMu.Unlock()
|
|
|
|
if _, ok := protocolRegistry[string(proto.Type)]; ok {
|
|
panic(fmt.Sprintf("protocol already registered: %s", proto.Type))
|
|
}
|
|
protocolRegistry[string(proto.Type)] = proto
|
|
}
|
|
|
|
var (
|
|
protocolRegistryMu sync.RWMutex
|
|
protocolRegistry = make(map[string]ProtocolRegistration)
|
|
)
|