mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 05:15:47 +00:00
initial commit
This commit is contained in:
44
protocol.go
Normal file
44
protocol.go
Normal file
@@ -0,0 +1,44 @@
|
||||
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)
|
||||
)
|
||||
Reference in New Issue
Block a user