|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
"sync"
|
|
|
|
"net"
|
|
|
|
"log"
|
|
|
|
"context"
|
|
|
|
"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
|
|
|
|
*SystemViewer
|
|
|
|
Port *port
|
|
|
|
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.SystemViewer = sys
|
|
|
|
t.Ip = ip
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TUIManager) Start(cl *Client) {
|
|
|
|
//
|
|
|
|
t.PingReset()
|
|
|
|
t.Manager.Start(cl)
|
|
|
|
go t.Timeoutd()
|
|
|
|
go t.Register() // begin tui server to respond to tui client reqs
|
|
|
|
//go t.Monitor(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
// up and running
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TUIManager) GetPort() int {
|
|
|
|
port := <-t.Port.Chan
|
|
|
|
return port
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TUIManager) GetDevices(ctx context.Context, req *pb.GetDevicesRequest) (*pb.GetDevicesResponse, error) {
|
|
|
|
go t.PingReset()
|
|
|
|
rid := req.GetReactorId()
|
|
|
|
devices := []*pb.Dev{}
|
|
|
|
resp := &pb.GetDevicesResponse{ClientId:t.Id,ReactorId:rid,Devices:devices}
|
|
|
|
reactors := t.SystemViewer.GetReactorStatus()
|
|
|
|
for _, v := range reactors {
|
|
|
|
resp.Devices = append(resp.Devices, &pb.Dev{Id:v.Id,Type:v.Type,Status:v.Status,Data:v.Data,Index:v.Index})
|
|
|
|
}
|
|
|
|
|
|
|
|
if rid != 0 {
|
|
|
|
// need reactor devices
|
|
|
|
devs := t.SystemViewer.GetDeviceStatus(rid)
|
|
|
|
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})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|