mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 13:25:40 +00:00
change protocol to module
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
package showbridge
|
||||
|
||||
type Config struct {
|
||||
Protocols []ProtocolConfig `json:"protocols"`
|
||||
Modules []ModuleConfig `json:"modules"`
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"protocols": [
|
||||
"modules": [
|
||||
{
|
||||
"type": "tcp.server",
|
||||
"params": {
|
||||
|
||||
44
module.go
Normal file
44
module.go
Normal 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)
|
||||
)
|
||||
44
protocol.go
44
protocol.go
@@ -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)
|
||||
)
|
||||
22
router.go
22
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()
|
||||
}
|
||||
|
||||
@@ -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"]
|
||||
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user