mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
add the concept of Id to a module
This commit is contained in:
@@ -7,17 +7,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Module interface {
|
type Module interface {
|
||||||
|
Id() string
|
||||||
|
Type() string
|
||||||
Run(context.Context) error
|
Run(context.Context) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type ModuleConfig struct {
|
type ModuleConfig struct {
|
||||||
|
Id string `json:"id"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Params map[string]any `json:"params"`
|
Params map[string]any `json:"params"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ModuleRegistration struct {
|
type ModuleRegistration struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
New func(map[string]any) (Module, error)
|
New func(ModuleConfig) (Module, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterModule(mod ModuleRegistration) {
|
func RegisterModule(mod ModuleRegistration) {
|
||||||
|
|||||||
19
router.go
19
router.go
@@ -34,12 +34,23 @@ func NewRouter(ctx context.Context, config Config) (*Router, error) {
|
|||||||
return nil, fmt.Errorf("problem loading module registration for module type: %s", moduleDecl.Type)
|
return nil, fmt.Errorf("problem loading module registration for module type: %s", moduleDecl.Type)
|
||||||
}
|
}
|
||||||
|
|
||||||
moduleInstance, err := moduleInfo.New(moduleDecl.Params)
|
moduleInstanceExists := false
|
||||||
if err != nil {
|
for _, moduleInstance := range router.ModuleInstances {
|
||||||
return nil, err
|
if moduleInstance.Id() == moduleDecl.Id {
|
||||||
|
moduleInstanceExists = true
|
||||||
|
slog.Warn("module id conflict", "id", moduleDecl.Id, "type", moduleDecl.Type)
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
router.ModuleInstances = append(router.ModuleInstances, moduleInstance)
|
if !moduleInstanceExists {
|
||||||
|
moduleInstance, err := moduleInfo.New(moduleDecl)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
router.ModuleInstances = append(router.ModuleInstances, moduleInstance)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,15 +9,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type TCPClient struct {
|
type TCPClient struct {
|
||||||
Host string
|
config ModuleConfig
|
||||||
Port uint16
|
Host string
|
||||||
|
Port uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.tcp.client",
|
Type: "net.tcp.client",
|
||||||
New: func(params map[string]any) (Module, error) {
|
New: func(config ModuleConfig) (Module, error) {
|
||||||
|
params := config.Params
|
||||||
host, ok := params["host"]
|
host, ok := params["host"]
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -41,14 +42,22 @@ func init() {
|
|||||||
return nil, fmt.Errorf("tcp client port must be uint16")
|
return nil, fmt.Errorf("tcp client port must be uint16")
|
||||||
}
|
}
|
||||||
|
|
||||||
return TCPClient{Host: hostString, Port: uint16(portNum)}, nil
|
return TCPClient{Host: hostString, Port: uint16(portNum), config: config}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ts TCPClient) Run(ctx context.Context) error {
|
func (tc TCPClient) Id() string {
|
||||||
|
return tc.config.Id
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tc TCPClient) Type() string {
|
||||||
|
return tc.config.Type
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tc TCPClient) Run(ctx context.Context) error {
|
||||||
for {
|
for {
|
||||||
client, err := net.Dial("tcp", fmt.Sprintf(":%d", ts.Port))
|
client, err := net.Dial("tcp", fmt.Sprintf(":%d", tc.Port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error(err.Error())
|
slog.Error(err.Error())
|
||||||
time.Sleep(time.Second * 2)
|
time.Sleep(time.Second * 2)
|
||||||
|
|||||||
@@ -8,13 +8,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type TCPServer struct {
|
type TCPServer struct {
|
||||||
Port uint16
|
config ModuleConfig
|
||||||
|
Port uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.tcp.server",
|
Type: "net.tcp.server",
|
||||||
New: func(params map[string]any) (Module, error) {
|
New: func(config ModuleConfig) (Module, error) {
|
||||||
|
params := config.Params
|
||||||
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")
|
||||||
@@ -26,11 +28,19 @@ func init() {
|
|||||||
return nil, fmt.Errorf("tcp server port must be uint16")
|
return nil, fmt.Errorf("tcp server port must be uint16")
|
||||||
}
|
}
|
||||||
|
|
||||||
return TCPServer{Port: uint16(portNum)}, nil
|
return TCPServer{Port: uint16(portNum), config: config}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ts TCPServer) Id() string {
|
||||||
|
return ts.config.Id
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ts TCPServer) Type() string {
|
||||||
|
return ts.config.Type
|
||||||
|
}
|
||||||
|
|
||||||
func (ts TCPServer) HandleClient(ctx context.Context, client net.Conn) {
|
func (ts TCPServer) HandleClient(ctx context.Context, client net.Conn) {
|
||||||
slog.Info("handling connection", "remoteAddr", client.RemoteAddr().String())
|
slog.Info("handling connection", "remoteAddr", client.RemoteAddr().String())
|
||||||
|
|
||||||
|
|||||||
@@ -9,13 +9,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type UDPServer struct {
|
type UDPServer struct {
|
||||||
Port uint16
|
Port uint16
|
||||||
|
config ModuleConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.udp.server",
|
Type: "net.udp.server",
|
||||||
New: func(params map[string]any) (Module, error) {
|
New: func(config ModuleConfig) (Module, error) {
|
||||||
|
params := config.Params
|
||||||
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")
|
||||||
@@ -27,11 +29,19 @@ func init() {
|
|||||||
return nil, fmt.Errorf("udp server port must be uint16")
|
return nil, fmt.Errorf("udp server port must be uint16")
|
||||||
}
|
}
|
||||||
|
|
||||||
return UDPServer{Port: uint16(portNum)}, nil
|
return UDPServer{Port: uint16(portNum), config: config}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (us UDPServer) Id() string {
|
||||||
|
return us.config.Id
|
||||||
|
}
|
||||||
|
|
||||||
|
func (us UDPServer) Type() string {
|
||||||
|
return us.config.Id
|
||||||
|
}
|
||||||
|
|
||||||
func (us UDPServer) Run(ctx context.Context) error {
|
func (us UDPServer) Run(ctx context.Context) error {
|
||||||
|
|
||||||
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", us.Port))
|
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", us.Port))
|
||||||
|
|||||||
Reference in New Issue
Block a user