change protocol to module

This commit is contained in:
Joel Wetzell
2025-11-18 18:14:11 -06:00
parent 93c6c05591
commit a7996530a1
8 changed files with 63 additions and 63 deletions

View File

@@ -1,5 +1,5 @@
package showbridge package showbridge
type Config struct { type Config struct {
Protocols []ProtocolConfig `json:"protocols"` Modules []ModuleConfig `json:"modules"`
} }

View File

@@ -1,5 +1,5 @@
{ {
"protocols": [ "modules": [
{ {
"type": "tcp.server", "type": "tcp.server",
"params": { "params": {

44
module.go Normal file
View File

@@ -0,0 +1,44 @@
package showbridge
import (
"context"
"fmt"
"sync"
)
type Module interface {
Run(context.Context) error
}
type ModuleConfig struct {
Type string `json:"type"`
Params map[string]any `json:"params"`
}
type ModuleRegistration struct {
Type string `json:"type"`
New func(map[string]any) (Module, error)
}
func RegisterModule(mod ModuleRegistration) {
if mod.Type == "" {
panic("module type is missing")
}
if mod.New == nil {
panic("missing ModuleInfo.New")
}
moduleRegistryMu.Lock()
defer moduleRegistryMu.Unlock()
if _, ok := moduleRegistry[string(mod.Type)]; ok {
panic(fmt.Sprintf("module already registered: %s", mod.Type))
}
moduleRegistry[string(mod.Type)] = mod
}
var (
moduleRegistryMu sync.RWMutex
moduleRegistry = make(map[string]ModuleRegistration)
)

View File

@@ -1,44 +0,0 @@
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)
)

View File

@@ -6,30 +6,30 @@ import (
) )
type Router struct { type Router struct {
Context context.Context Context context.Context
ProtocolInstances []Protocol ModuleInstances []Module
} }
func NewRouter(ctx context.Context, config Config) (*Router, error) { func NewRouter(ctx context.Context, config Config) (*Router, error) {
router := Router{ router := Router{
Context: ctx, Context: ctx,
ProtocolInstances: []Protocol{}, ModuleInstances: []Module{},
} }
for _, protocolDecl := range config.Protocols { for _, moduleDecl := range config.Modules {
protocolInfo, ok := protocolRegistry[protocolDecl.Type] moduleInfo, ok := moduleRegistry[moduleDecl.Type]
if !ok { if !ok {
return nil, fmt.Errorf("problem loading protocol registration for protocol type: %s", protocolDecl.Type) return nil, fmt.Errorf("problem loading module registration for module type: %s", moduleDecl.Type)
} }
protocolInstance, err := protocolInfo.New(protocolDecl.Params) moduleInstance, err := moduleInfo.New(moduleDecl.Params)
if err != nil { if err != nil {
return nil, err return nil, err
} }
router.ProtocolInstances = append(router.ProtocolInstances, protocolInstance) router.ModuleInstances = append(router.ModuleInstances, moduleInstance)
} }
@@ -37,8 +37,8 @@ func NewRouter(ctx context.Context, config Config) (*Router, error) {
} }
func (r *Router) Run() { func (r *Router) Run() {
for _, protocolInstance := range r.ProtocolInstances { for _, moduleInstance := range r.ModuleInstances {
go protocolInstance.Run(r.Context) go moduleInstance.Run(r.Context)
} }
<-r.Context.Done() <-r.Context.Done()
} }

View File

@@ -13,9 +13,9 @@ type TCPClient struct {
} }
func init() { func init() {
RegisterProtocol(ProtocolRegistration{ RegisterModule(ModuleRegistration{
Type: "tcp.client", Type: "tcp.client",
New: func(params map[string]any) (Protocol, error) { New: func(params map[string]any) (Module, error) {
host, ok := params["host"] host, ok := params["host"]

View File

@@ -11,9 +11,9 @@ type TCPServer struct {
} }
func init() { func init() {
RegisterProtocol(ProtocolRegistration{ RegisterModule(ModuleRegistration{
Type: "tcp.server", Type: "tcp.server",
New: func(params map[string]any) (Protocol, error) { New: func(params map[string]any) (Module, error) {
port, ok := params["port"] port, ok := params["port"]
if !ok { if !ok {
return nil, fmt.Errorf("tcp server requires a port parameter") return nil, fmt.Errorf("tcp server requires a port parameter")

View File

@@ -12,9 +12,9 @@ type UDPServer struct {
} }
func init() { func init() {
RegisterProtocol(ProtocolRegistration{ RegisterModule(ModuleRegistration{
Type: "udp.server", Type: "udp.server",
New: func(params map[string]any) (Protocol, error) { New: func(params map[string]any) (Module, error) {
port, ok := params["port"] port, ok := params["port"]
if !ok { if !ok {
return nil, fmt.Errorf("udp server requires a port parameter") return nil, fmt.Errorf("udp server requires a port parameter")