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