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 *port
Err chan error
*pb.UnimplementedManagementServer
}
type port struct {
Chan chan int
int
}
func NewTUIManager(ip string, c *Client, sys *System, err chan error) GeneralManager {
m := NewManager(err)
t := &TUIManager{Err: err}
t.Port = &port{Chan:make(chan int)}
t.Manager = m
t.System = sys
t.Ip = ip
return t
}
func (t *TUIManager) Start(cl *Client) {
//
t.Manager.Start(cl)
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)
}
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) 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
}