From 9ad60c8193fb3da219141b54efc342ee5a3cfbc2 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Tue, 18 Nov 2025 19:13:14 -0600 Subject: [PATCH] add the concept of Id to a module --- module.go | 5 ++++- router.go | 19 +++++++++++++++---- tcp-client.go | 23 ++++++++++++++++------- tcp-server.go | 16 +++++++++++++--- udp-server.go | 16 +++++++++++++--- 5 files changed, 61 insertions(+), 18 deletions(-) diff --git a/module.go b/module.go index cedf854..491813e 100644 --- a/module.go +++ b/module.go @@ -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) { diff --git a/router.go b/router.go index 447dfda..585db1d 100644 --- a/router.go +++ b/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) - if err != nil { - return nil, err + 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 + } } - 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) + } } diff --git a/tcp-client.go b/tcp-client.go index 1487f9a..c7dde80 100644 --- a/tcp-client.go +++ b/tcp-client.go @@ -9,15 +9,16 @@ import ( ) type TCPClient struct { - Host string - Port uint16 + config ModuleConfig + Host string + Port uint16 } 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) diff --git a/tcp-server.go b/tcp-server.go index de7c7ea..0ee6575 100644 --- a/tcp-server.go +++ b/tcp-server.go @@ -8,13 +8,15 @@ import ( ) type TCPServer struct { - Port uint16 + 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()) diff --git a/udp-server.go b/udp-server.go index 9c40e73..fc2a15c 100644 --- a/udp-server.go +++ b/udp-server.go @@ -9,13 +9,15 @@ import ( ) type UDPServer struct { - Port uint16 + 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))