2024-08-04 00:55:28 -04:00
|
|
|
package nexus
|
|
|
|
|
|
|
|
import (
|
2024-08-06 11:19:30 -04:00
|
|
|
"context"
|
2024-10-21 22:19:31 -04:00
|
|
|
"errors"
|
2024-08-04 00:55:28 -04:00
|
|
|
"io"
|
2024-10-18 09:34:20 -04:00
|
|
|
"runtime"
|
2024-08-04 00:55:28 -04:00
|
|
|
"sync"
|
|
|
|
|
2024-10-22 08:39:15 -04:00
|
|
|
"dynatron.me/x/stillbox/internal/version"
|
2024-08-06 11:19:30 -04:00
|
|
|
"dynatron.me/x/stillbox/pkg/calls"
|
2024-11-03 07:19:03 -05:00
|
|
|
"dynatron.me/x/stillbox/pkg/database"
|
2024-08-04 00:55:28 -04:00
|
|
|
"dynatron.me/x/stillbox/pkg/pb"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"google.golang.org/protobuf/proto"
|
2024-10-19 14:14:15 -04:00
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
2024-08-04 00:55:28 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Client interface {
|
|
|
|
sync.Locker
|
|
|
|
|
|
|
|
Connection
|
|
|
|
|
2024-10-20 12:26:32 -04:00
|
|
|
Hello(context.Context)
|
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
|
|
|
|
}
|
|
|
|
|
2024-10-19 14:14:15 -04:00
|
|
|
type ToClient interface {
|
|
|
|
protoreflect.ProtoMessage
|
|
|
|
}
|
|
|
|
|
2024-10-21 22:19:31 -04:00
|
|
|
var (
|
|
|
|
ErrSentToClosed = errors.New("sent to closed connection")
|
|
|
|
)
|
|
|
|
|
2024-08-04 00:55:28 -04:00
|
|
|
type Connection interface {
|
|
|
|
io.Closer
|
2024-08-04 10:56:46 -04:00
|
|
|
CloseCh()
|
2024-08-04 00:55:28 -04:00
|
|
|
|
2024-10-21 13:49:20 -04:00
|
|
|
Shutdown()
|
|
|
|
|
2024-10-21 22:19:31 -04:00
|
|
|
Send(ToClient) error
|
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
|
|
|
}
|
2024-10-18 09:34:20 -04:00
|
|
|
|
2024-10-20 13:04:42 -04:00
|
|
|
func pbServerInfo(ctx context.Context) *pb.ServerInfo {
|
|
|
|
cts, err := database.FromCtx(ctx).GetDatabaseSize(ctx)
|
2024-10-20 12:26:32 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("get calls table size")
|
|
|
|
cts = "unknown"
|
|
|
|
}
|
|
|
|
|
2024-10-20 13:04:42 -04:00
|
|
|
return &pb.ServerInfo{
|
2024-10-18 09:34:20 -04:00
|
|
|
ServerName: version.Name,
|
2024-10-18 15:21:42 -04:00
|
|
|
Version: version.Version,
|
|
|
|
Built: version.Built,
|
|
|
|
Platform: runtime.GOOS + "-" + runtime.GOARCH,
|
2024-10-20 13:04:42 -04:00
|
|
|
DbSize: cts,
|
2024-10-18 09:34:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-20 12:26:32 -04:00
|
|
|
func (c *client) Hello(ctx context.Context) {
|
2024-10-31 16:50:08 -04:00
|
|
|
_ = c.Send(&pb.Message{
|
2024-10-18 09:34:20 -04:00
|
|
|
ToClientMessage: &pb.Message_Hello{
|
|
|
|
Hello: &pb.Hello{
|
2024-10-20 13:04:42 -04:00
|
|
|
ServerInfo: pbServerInfo(ctx),
|
2024-10-18 09:34:20 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|