From a7996530a1e190b57ce5d75cae193a5180502872 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Tue, 18 Nov 2025 18:14:11 -0600 Subject: [PATCH] change protocol to module --- config.go | 2 +- config.json | 2 +- module.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ protocol.go | 44 -------------------------------------------- router.go | 22 +++++++++++----------- tcp-client.go | 4 ++-- tcp-server.go | 4 ++-- udp-server.go | 4 ++-- 8 files changed, 63 insertions(+), 63 deletions(-) create mode 100644 module.go delete mode 100644 protocol.go diff --git a/config.go b/config.go index 5438350..4680026 100644 --- a/config.go +++ b/config.go @@ -1,5 +1,5 @@ package showbridge type Config struct { - Protocols []ProtocolConfig `json:"protocols"` + Modules []ModuleConfig `json:"modules"` } diff --git a/config.json b/config.json index 0aa1242..12a20dd 100644 --- a/config.json +++ b/config.json @@ -1,5 +1,5 @@ { - "protocols": [ + "modules": [ { "type": "tcp.server", "params": { diff --git a/module.go b/module.go new file mode 100644 index 0000000..cedf854 --- /dev/null +++ b/module.go @@ -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) +) diff --git a/protocol.go b/protocol.go deleted file mode 100644 index ca39094..0000000 --- a/protocol.go +++ /dev/null @@ -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) -) diff --git a/router.go b/router.go index f082217..65e562e 100644 --- a/router.go +++ b/router.go @@ -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() } diff --git a/tcp-client.go b/tcp-client.go index e2b0566..4db5728 100644 --- a/tcp-client.go +++ b/tcp-client.go @@ -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"] diff --git a/tcp-server.go b/tcp-server.go index 518c20a..03aab93 100644 --- a/tcp-server.go +++ b/tcp-server.go @@ -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") diff --git a/udp-server.go b/udp-server.go index f21f79f..2f5d0ba 100644 --- a/udp-server.go +++ b/udp-server.go @@ -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")