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