|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
"sync"
|
|
|
|
"net"
|
|
|
|
"log"
|
|
|
|
"context"
|
|
|
|
"FRMS/internal/pkg/logging"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
pb "FRMS/internal/pkg/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// implement tui specific manager to be called for each client conn
|
|
|
|
|
|
|
|
type TUIManager struct {
|
|
|
|
*Manager // embedded manager for access to methods and client
|
|
|
|
Ip string
|
|
|
|
Port *port
|
|
|
|
StatusMon *StatusMonitor // use it for all devs coming in
|
|
|
|
Err chan error
|
|
|
|
*Timeout
|
|
|
|
*pb.UnimplementedManagementServer
|
|
|
|
}
|
|
|
|
|
|
|
|
type Timeout struct {
|
|
|
|
Alert chan bool
|
|
|
|
LastSeen time.Time
|
|
|
|
TO time.Duration
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type port struct {
|
|
|
|
Chan chan int
|
|
|
|
int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTUIManager(ip string, c *Client, sys *SystemViewer, err chan error) GeneralManager {
|
|
|
|
m := NewManager(err)
|
|
|
|
t := &TUIManager{Err: err}
|
|
|
|
t.Port = &port{Chan:make(chan int)}
|
|
|
|
alert := make(chan bool)
|
|
|
|
t.Timeout = &Timeout{Alert:alert,TO:time.Duration(2500*time.Millisecond)} // short time outs are fine because we will just rejoin
|
|
|
|
t.Manager = m
|
|
|
|
t.Ip = ip
|
|
|
|
t.StatusMon = NewStatusMonitor("TUI",t.Id,sys)
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TUIManager) Start(cl *Client) {
|
|
|
|
//
|
|
|
|
t.PingReset()
|
|
|
|
t.Manager.Start(cl)
|
|
|
|
logging.Debug(logging.DStart,"TMA %v starting", t.Id)
|
|
|
|
go t.Timeoutd()
|
|
|
|
go t.Register() // begin tui server to respond to tui client reqs
|
|
|
|
//go t.Monitor(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TUIManager) Exit() {
|
|
|
|
t.Manager.Exit()
|
|
|
|
logging.Debug(logging.DExit,"TMA %v exiting",t.Id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Timeout) PingReset() {
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
t.LastSeen = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TUIManager) Timeoutd() {
|
|
|
|
for t.IsActive() {
|
|
|
|
if sleep, elapsed := t.Elapsed(); elapsed {
|
|
|
|
// timeout elapsed
|
|
|
|
logging.Debug(logging.DClient,"TMA %V client not responding", t.Id)
|
|
|
|
t.Exit()
|
|
|
|
} else {
|
|
|
|
time.Sleep(sleep)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Timeout) Elapsed() (time.Duration, bool) {
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
now := time.Now()
|
|
|
|
if now.After(t.LastSeen.Add(t.TO)) {
|
|
|
|
// timeout expired
|
|
|
|
return 0 * time.Second, true
|
|
|
|
} else {
|
|
|
|
sleep := t.LastSeen.Add(t.TO).Sub(now)
|
|
|
|
return sleep, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TUIManager) Register() {
|
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf("%v:0",t.Ip))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
grpcServer := grpc.NewServer()
|
|
|
|
pb.RegisterManagementServer(grpcServer,t)
|
|
|
|
go grpcServer.Serve(lis)
|
|
|
|
// send port now that server is up
|
|
|
|
t.Port.int = lis.Addr().(*net.TCPAddr).Port
|
|
|
|
go func(ch chan int,p int) {
|
|
|
|
ch <-p
|
|
|
|
}(t.Port.Chan, t.Port.int)
|
|
|
|
logging.Debug(logging.DClient, "TMA %v ready for client conn", t.Id)
|
|
|
|
// up and running
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TUIManager) GetPort() int {
|
|
|
|
port := <-t.Port.Chan
|
|
|
|
return port
|
|
|
|
}
|
|
|
|
|
|
|
|
// tui client requests and logic will be down here
|
|
|
|
|
|
|
|
func (t *TUIManager) GetDevices(ctx context.Context, req *pb.GetDevicesRequest) (*pb.GetDevicesResponse, error) {
|
|
|
|
go t.PingReset()
|
|
|
|
devices := []*pb.Dev{}
|
|
|
|
resp := &pb.GetDevicesResponse{ClientId:t.Id,Devices:devices}
|
|
|
|
if req.GetReactorId() > 0 || req.GetRefresh() {
|
|
|
|
resp.ReactorId = req.GetReactorId()
|
|
|
|
t.StatusMon.UpdateListener(t.Id, req.GetReactorId())
|
|
|
|
}
|
|
|
|
devs := t.StatusMon.GetBuffer() // always empty buffer
|
|
|
|
|
|
|
|
for _, v := range devs {
|
|
|
|
resp.Devices = append(resp.Devices, &pb.Dev{Id:v.Id,Type:v.Type,Status:v.Status,Data:v.Data,Index:v.Index})
|
|
|
|
}
|
|
|
|
logging.Debug(logging.DClient,"TMA %v sending devices to client" ,t.Id)
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TUIManager) DeleteReactors(ctx context.Context, req *pb.DeleteReactorRequest) (*pb.DeleteReactorResponse, error) {
|
|
|
|
go t.PingReset()
|
|
|
|
//
|
|
|
|
return &pb.DeleteReactorResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TUIManager) DeleteReactorDevice(ctx context.Context, req *pb.DeleteReactorDeviceRequest) (*pb.DeleteReactorDeviceResponse, error) {
|
|
|
|
go t.PingReset()
|
|
|
|
//
|
|
|
|
return &pb.DeleteReactorDeviceResponse{}, nil
|
|
|
|
}
|
|
|
|
|