Skip to content

Commit

Permalink
fix: do not fatal if failed to connect
Browse files Browse the repository at this point in the history
  • Loading branch information
monoxane committed Nov 27, 2024
1 parent e3ef178 commit 6920534
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 10 deletions.
Binary file added .DS_Store
Binary file not shown.
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ module github.com/monoxane/nk

go 1.19

require github.com/pkg/errors v0.9.1
require (
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.33.0
)

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
golang.org/x/sys v0.12.0 // indirect
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
21 changes: 21 additions & 0 deletions logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package nk

import (
"os"
"time"

"github.com/rs/zerolog"
)

var Log zerolog.Logger

func init() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}

Log = zerolog.New(output).With().Timestamp().Caller().Logger()
}

func SetLogger(logger zerolog.Logger) {
Log = logger
}
Binary file added pkg/.DS_Store
Binary file not shown.
6 changes: 3 additions & 3 deletions pkg/tbus/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package tbus
import (
"bytes"
"encoding/binary"
"log"

"github.com/monoxane/nk"
"github.com/monoxane/nk/pkg/tbus/crc"
)

Expand Down Expand Up @@ -33,7 +33,7 @@ func (xpt *CrosspointRequest) Packet() ([]byte, error) {
payloadBuffer := new(bytes.Buffer)
err := binary.Write(payloadBuffer, binary.BigEndian, payload)
if err != nil {
log.Println("TBusPacketPayload binary.Write failed:", err)
nk.Log.Error().Err(err).Msg("unable pack tbus message into packet")
}

packet := nkRoutePacket{
Expand All @@ -46,7 +46,7 @@ func (xpt *CrosspointRequest) Packet() ([]byte, error) {
packetBuffer := new(bytes.Buffer)
err = binary.Write(packetBuffer, binary.BigEndian, packet)
if err != nil {
log.Println("TBustPacket binary.Write failed:", err)
nk.Log.Error().Err(err).Msg("unable to write packet to tbus gateway")
}

return packetBuffer.Bytes(), nil
Expand Down
13 changes: 7 additions & 6 deletions pkg/tbus/tbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"encoding/binary"
"fmt"
"io"
"log"
"net"
"time"

"github.com/pkg/errors"

"github.com/monoxane/nk"
)

var (
Expand Down Expand Up @@ -54,13 +55,13 @@ func NewGateway(ip net.IP, onRouteUpdate func(RouteUpdate), onStatusUpdate func(
func (tbus *TBusGateway) Connect() error {
conn, err := net.Dial("tcp", tbus.IP.String()+":5000")
if err != nil {
log.Fatalln(err)
nk.Log.Error().Err(err).Msg("unable to connect to tbus gateway")
}
tbus.conn = conn
defer tbus.conn.Close()

if _, err = tbus.conn.Write(NK2_CONNECT_REQ); err != nil {
log.Printf("failed to send the client request: %v\n", err)
nk.Log.Error().Err(err).Msg("unable to write to tbus gateway socket")
}

tbus.onConnect(StatusUpdate{
Expand Down Expand Up @@ -99,15 +100,15 @@ func (tbus *TBusGateway) Disconnect() {

func (tbus *TBusGateway) processNKMessage(buffer []byte, length int) {
msg := buffer[:length]
log.Printf("Processing message of len %d: %x", length, msg)
nk.Log.Debug().Int("len", length).Bytes("message", msg).Msg("received tbus message")

if length == len(NK2_CONNECT_RESP) && bytes.Equal(msg, NK2_CONNECT_RESP) {
log.Printf("Successfully Connected")
nk.Log.Info().Msg("connected to tbus gateway")
tbus.conn.Write(NK_MULTI_STATUS_REQ)
}

if length > 3 && bytes.Equal(msg[:3], NK2_HEADER) {
log.Printf("NK Command or Response, CMD: %x", msg[5:7])
nk.Log.Debug().Bytes("message", msg[5:7]).Msg("processing tbus cmd or response")
if bytes.Equal(msg[5:7], NK_STATUS_RESP) {
tbus.parseSingleUpdateMessage(msg)
}
Expand Down

0 comments on commit 6920534

Please sign in to comment.