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.

148 lines
4.3 KiB
Go

package server
import (
"sync"
//"fmt"
"FRMS/internal/pkg/logging"
)
// this package creates coordinators responsible for keeping track of active clients and invoking managers
type SubCoordinator interface {
Start()
NewManager(*Client, chan error) GeneralManager
}
type GeneralManager interface {
Start(*Client)
GetPort() int
}
type Coordinator struct {
Port int // port that we set up gRPC endpoint on
*Managers
SubCoordinator
*SystemViewer
Err chan error
}
type Managers struct {
Directory map[uint32]GeneralManager
sync.RWMutex // potential perf
}
// interface stuff
func NewCoordinator(clientType string, sys *SystemViewer,grpcServer *grpc.Server, err chan error) *Coordinator {
d := make(map[uint32]GeneralManager)
m := &Managers{Directory:d}
c := &Coordinator{Err:err}
c.SubCoordinator = NewSubCoordinator(clientType, err)
c.SystemViewer = sys
c.Managers = m
go c.Register(clientType, grpcServer)
return c
}
func (c *Coordinator) Start() {
// on start we need to create channel listener
// on each new connection we want to check its id against our mapping
c.CreateManager.Start()
}
func (c *Coordinator) ClientHandler(cl *Client) int {
// (creates and) notifies manager of client connection
m := c.LoadManager(cl)
go func (mn GeneralManager, c *client) {
mn.IncomingClient <-c
}(m, cl)
return c.Port
}
func (c *Coordinator) LoadManager(cl *Client) GeneralManager {
// shouldn't happen all that often so should be fine to lock
c.Managers.Lock()
defer c.Managers.Unlock()
var exists bool
var m GeneralManager
if m, exists = c.Managers.Directory[cl.Id]; !exists {
// manager in memory
m = c.NewManager(cl, c.SystemViewer, c.Err)
c.Managers.Directory[cl.Id] = m
go m.Start()
}
return m
}
func (c *Coordinator) GetManager(id uint32) GeneralManager {
// just read locks and reuturns
c.Managers.RLock()
defer c.Managers.RUnlock()
return c.Managers.Directory[id]
}
func NewSubCoordinator(clientType string, err chan error) SubCoordinator {
if clientType == "reactor" {
return &reactorCoordinator{Sys:sys}
} else if clientType == "tui" {
return &tuiCoordinator{Sys:sys}
}
return SubCoordinator{}
}
func (c *Coordinator) Register(clientType string, grpcServer *grpc.Server) {
if clientType == "reactor" {
pb.RegisterManagementServer(grpcServer,c)
logging.Debug(logging.DClient, "RCO ready for client requests")
} else if clientType == "tui" {
pb.RegisterMonitoringServer(grpcServer,c)
logging.Debug(logging.DClient, "TCO ready for client requests")
}
}
// creating sub coordinators for associated gRPC handlers
// reactor coordinator
type reactorCoordinator struct {
pb.UnimplementedMonitoringServer
}
func (r *reactorCoordinator) Start() {
logging.Debug(logging.DStart,"RCO 01 Starting!")
}
func (r *reactorCoordinator) NewManager(cl *Client, sys *SystemViewer, err chan error) GeneralManager {
logging.Debug(logging.DClient, "RCO 01 starting manager for %v client %v",cl.Type,cl.Id)
return NewReactorManager(cl,sys,err)
}
func (c *Coordinator) ReactorStatusHandler(ctx context.Context, req *pb.ReactorStatusRequest) (*pb.ReactorStatusResponse, error) {
m := c.GetManager(req.GetId())
if m == GeneralManager{} {
return &pb.ReactorStatusResponse, errors.New("Manager doesn't exists for that client")
}
return m.ReactorStatusHandler(ctx, req)
}
//tui coordinator
type tuiCoordinator struct {
pb.UnimplementedManagementServer
}
func (t *tuiCoordinator) Start() {
logging.Debug(logging.DStart,"TCO 01 Starting!")
}
func (t *tuiCoordinator) NewManager(cl *Client, sys *SystemViewer, err chan error) GeneralManager {
logging.Debug(logging.DClient, "TCO 01 starting manager for %v client %v",cl.Type,cl.Id)
return NewTUIManager(cl,sys,err)
}
func (c *Coordinator) GetDevices(ctx context.Context, req *pb.GetDevicesRequest) (*pb.GetDevicesResponse, error) {
// grpc handler to fwd to manager
m := c.GetManager(req.GetId())
if m == GeneralManager{} {
// doesnt exist for some reason
return &pb.GetDevicesRequest{}, errors.New("Manager doesn't exists for client")
}
return m.GetDevices(ctx,req)
}