stillbox/pkg/gordio/nexus/client.go

61 lines
907 B
Go
Raw Normal View History

2024-08-04 00:55:28 -04:00
package nexus
import (
2024-08-06 11:19:30 -04:00
"context"
2024-08-04 00:55:28 -04:00
"io"
"sync"
2024-08-06 11:19:30 -04:00
"dynatron.me/x/stillbox/pkg/calls"
2024-08-04 00:55:28 -04:00
"dynatron.me/x/stillbox/pkg/pb"
"github.com/rs/zerolog/log"
"google.golang.org/protobuf/proto"
)
type Client interface {
sync.Locker
Connection
2024-08-06 11:19:30 -04:00
HandleCommand(context.Context, *pb.Command)
HandleMessage(context.Context, []byte)
2024-08-04 00:55:28 -04:00
}
type client struct {
sync.RWMutex
Connection
2024-08-06 11:19:30 -04:00
liveState pb.LiveState
filter *calls.TalkgroupFilter
2024-08-04 00:55:28 -04:00
nexus *Nexus
}
type Connection interface {
io.Closer
2024-08-04 10:56:46 -04:00
CloseCh()
2024-08-04 00:55:28 -04:00
2024-08-04 14:55:15 -04:00
Send(*pb.Message) (closed bool)
2024-08-04 00:55:28 -04:00
}
func (n *Nexus) NewClient(conn Connection) Client {
sess := &client{
Connection: conn,
nexus: n,
}
return sess
}
2024-08-06 11:19:30 -04:00
func (c *client) HandleMessage(ctx context.Context, mesgBytes []byte) {
2024-08-04 00:55:28 -04:00
var msg pb.Command
err := proto.Unmarshal(mesgBytes, &msg)
if err != nil {
log.Error().Err(err).Msg("command unmarshal")
return
}
2024-08-06 11:19:30 -04:00
c.HandleCommand(ctx, &msg)
2024-08-04 00:55:28 -04:00
}