You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.6 KiB
Go

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
*System
Ip string
Port int
Err chan error
Hb time.Duration
Pc chan int
*pb.UnimplementedManagementServer
}
func NewTUIManager(c *Client, ip string, ch chan *Client,sys *System, pc chan int, err chan error) {
sig := make(chan bool)
m := NewManager(c, ch, sig, err)
hb := time.Duration(5)
t := &TUIManager{Hb: hb,Err: err}
t.Manager = m
t.System = sys
t.Ip = ip
t.Pc = pc
go t.Listen(sig)
}
func (t *TUIManager) Listen(sig chan bool) {
for {
c := <-sig
if c {
t.Start()
}
}
}
func (t *TUIManager) Start() {
//
go t.Register() // begin tui server to respond to tui client reqs
//go t.Monitor(conn)
}
func (t *TUIManager) Register() {
lis, err := net.Listen("tcp", fmt.Sprintf("%v:0",t.Ip))
if err != nil {
log.Fatal(err)
}
t.Port = lis.Addr().(*net.TCPAddr).Port
grpcServer := grpc.NewServer()
pb.RegisterManagementServer(grpcServer,t)
go grpcServer.Serve(lis)
log.Printf("TUI %v Endpoint active on %v:%v\n",t.Id, t.Ip, t.Port)
t.Pc <-t.Port
}
func (t *TUIManager) GetReactors(ctx context.Context, req *pb.GetReactorsRequest) (*pb.GetReactorsResponse, error) {
//
reactors := []*pb.Reactor{}
resp := &pb.GetReactorsResponse{ClientId:t.Id,Reactors:reactors}
r := t.System.GetReactors()
for _,v := range r {
resp.Reactors = append(resp.Reactors, &pb.Reactor{Id:v.Id,Status:v.Status})
}
return resp, nil
}
func (t *TUIManager) GetReactorDevices(ctx context.Context, req *pb.GetReactorDevicesRequest) (*pb.GetReactorDevicesResponse, error) {
rid := req.GetReactorId()
devs := t.System.GetReactorDevices(rid)
devices := []*pb.Dev{}
resp := &pb.GetReactorDevicesResponse{ClientId:t.Id,ReactorId:rid,Devices:devices}
for _, v := range devs {
resp.Devices = append(resp.Devices, &pb.Dev{Addr:int32(v.Addr),Type:v.Type,Status:v.Status,Data:v.Data})
}
return resp, nil
}
func (t *TUIManager) DeleteReactors(ctx context.Context, req *pb.DeleteReactorRequest) (*pb.DeleteReactorResponse, error) {
//
return &pb.DeleteReactorResponse{}, nil
}
func (t *TUIManager) DeleteReactorDevice(ctx context.Context, req *pb.DeleteReactorDeviceRequest) (*pb.DeleteReactorDeviceResponse, error) {
//
return &pb.DeleteReactorDeviceResponse{}, nil
}