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
type Config struct {
Protocols []ProtocolConfig `json:"protocols"`
Modules []ModuleConfig `json:"modules"`
}

View File

@@ -1,5 +1,5 @@
{
"protocols": [
"modules": [
{
"type": "tcp.server",
"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 {
Context context.Context
ProtocolInstances []Protocol
Context context.Context
ModuleInstances []Module
}
func NewRouter(ctx context.Context, config Config) (*Router, error) {
router := Router{
Context: ctx,
ProtocolInstances: []Protocol{},
Context: ctx,
ModuleInstances: []Module{},
}
for _, protocolDecl := range config.Protocols {
for _, moduleDecl := range config.Modules {
protocolInfo, ok := protocolRegistry[protocolDecl.Type]
moduleInfo, ok := moduleRegistry[moduleDecl.Type]
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 {
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() {
for _, protocolInstance := range r.ProtocolInstances {
go protocolInstance.Run(r.Context)
for _, moduleInstance := range r.ModuleInstances {
go moduleInstance.Run(r.Context)
}
<-r.Context.Done()
}

View File

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

View File

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

View File

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