diff --git a/internal/framing/framer.go b/internal/framing/framer.go index 36500a5..8ca6d28 100644 --- a/internal/framing/framer.go +++ b/internal/framing/framer.go @@ -1,7 +1,26 @@ package framing +import ( + "fmt" +) + type Framer interface { Decode([]byte) [][]byte Encode([]byte) []byte Clear() } + +func GetFramer(framingType string) (Framer, error) { + switch framingType { + case "CR": + return NewByteSeparatorFramer([]byte{'\r'}), nil + case "LF": + return NewByteSeparatorFramer([]byte{'\n'}), nil + case "CRLF": + return NewByteSeparatorFramer([]byte{'\r', '\n'}), nil + case "SLIP": + return NewSlipFramer(), nil + default: + return nil, fmt.Errorf("unknown framing method: %s", framingType) + } +} diff --git a/serial-client.go b/serial-client.go index 9e77f0a..4ee6ae6 100644 --- a/serial-client.go +++ b/serial-client.go @@ -49,19 +49,10 @@ func init() { return nil, fmt.Errorf("misc.serial.client framing method must be a string") } - var framer framing.Framer + framer, err := framing.GetFramer(framingMethodString) - switch framingMethodString { - case "CR": - framer = framing.NewByteSeparatorFramer([]byte{'\r'}) - case "LF": - framer = framing.NewByteSeparatorFramer([]byte{'\n'}) - case "CRLF": - framer = framing.NewByteSeparatorFramer([]byte{'\r', '\n'}) - case "SLIP": - framer = framing.NewSlipFramer() - default: - return nil, fmt.Errorf("unknown framing method: %s", framingMethodString) + if err != nil { + return nil, err } buadRate, ok := params["baudRate"] diff --git a/tcp-client.go b/tcp-client.go index 3855320..382aa3e 100644 --- a/tcp-client.go +++ b/tcp-client.go @@ -61,19 +61,10 @@ func init() { return nil, fmt.Errorf("net.tcp.client framing method must be a string") } - var framer framing.Framer + framer, err := framing.GetFramer(framingMethodString) - switch framingMethodString { - case "CR": - framer = framing.NewByteSeparatorFramer([]byte{'\r'}) - case "LF": - framer = framing.NewByteSeparatorFramer([]byte{'\n'}) - case "CRLF": - framer = framing.NewByteSeparatorFramer([]byte{'\r', '\n'}) - case "SLIP": - framer = framing.NewSlipFramer() - default: - return nil, fmt.Errorf("unknown framing method: %s", framingMethodString) + if err != nil { + return nil, err } return &TCPClient{framer: framer, Addr: addr, config: config}, nil diff --git a/tcp-server.go b/tcp-server.go index c87800d..098b3f6 100644 --- a/tcp-server.go +++ b/tcp-server.go @@ -16,7 +16,7 @@ import ( type TCPServer struct { config ModuleConfig Addr *net.TCPAddr - FramingMethod string + Framer framing.Framer router *Router quit chan interface{} wg sync.WaitGroup @@ -51,6 +51,12 @@ func init() { return nil, fmt.Errorf("net.tcp.server framing method must be a string") } + framer, err := framing.GetFramer(framingMethodString) + + if err != nil { + return nil, err + } + ipString := "0.0.0.0" ip, ok := params["ip"] @@ -69,7 +75,7 @@ func init() { return nil, err } - return &TCPServer{FramingMethod: framingMethodString, Addr: addr, config: config, quit: make(chan interface{})}, nil + return &TCPServer{Framer: framer, Addr: addr, config: config, quit: make(chan interface{})}, nil }, }) } @@ -92,18 +98,6 @@ func (ts *TCPServer) handleClient(client *net.TCPConn) { ts.connectionsMu.Unlock() slog.Debug("net.tcp.server connection accepted", "id", ts.config.Id, "remoteAddr", client.RemoteAddr().String()) defer client.Close() - var framer framing.Framer - - switch ts.FramingMethod { - case "LF": - framer = framing.NewByteSeparatorFramer([]byte{'\n'}) - case "CR": - framer = framing.NewByteSeparatorFramer([]byte{'\r'}) - case "CRLF": - framer = framing.NewByteSeparatorFramer([]byte{'\r', '\n'}) - case "SLIP": - framer = framing.NewSlipFramer() - } buffer := make([]byte, 1024) ClientRead: @@ -147,9 +141,9 @@ ClientRead: } return } - if framer != nil { + if ts.Framer != nil { if byteCount > 0 { - messages := framer.Decode(buffer[0:byteCount]) + messages := ts.Framer.Decode(buffer[0:byteCount]) for _, message := range messages { if ts.router != nil { ts.router.HandleInput(ts.config.Id, message)