|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
//"fmt"
|
|
|
|
"FRMS/internal/pkg/logging"
|
|
|
|
)
|
|
|
|
|
|
|
|
// this package creates coordinators responsible for keeping track of active clients and invoking managers
|
|
|
|
type CreateManager interface {
|
|
|
|
Start()
|
|
|
|
NewManager(*Client, chan error) GeneralManager
|
|
|
|
}
|
|
|
|
|
|
|
|
type GeneralManager interface {
|
|
|
|
Start(*Client)
|
|
|
|
GetPort() int
|
|
|
|
}
|
|
|
|
|
|
|
|
type Coordinator struct {
|
|
|
|
*Managers
|
|
|
|
CreateManager
|
|
|
|
Err chan error
|
|
|
|
Pc chan int
|
|
|
|
}
|
|
|
|
|
|
|
|
type Managers struct {
|
|
|
|
Directory map[uint32]GeneralManager
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
// interface stuff
|
|
|
|
func NewCoordinator(manager CreateManager, err chan error) *Coordinator {
|
|
|
|
d := make(map[uint32]GeneralManager)
|
|
|
|
m := &Managers{Directory:d}
|
|
|
|
c := &Coordinator{Err:err}
|
|
|
|
c.CreateManager = manager
|
|
|
|
c.Managers = m
|
|
|
|
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.GetManager(cl)
|
|
|
|
go m.Start(cl)
|
|
|
|
return m.GetPort()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Coordinator) GetManager(cl *Client) GeneralManager {
|
|
|
|
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.Err)
|
|
|
|
c.Managers.Directory[cl.Id] = m
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
// reactor coordinator
|
|
|
|
type reactorCoordinator struct {
|
|
|
|
//empty unexported for method
|
|
|
|
Sys *SystemViewer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *reactorCoordinator) Start() {
|
|
|
|
logging.Debug(logging.DStart,"RCO 01 Starting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *reactorCoordinator) NewManager(cl *Client, err chan error) GeneralManager {
|
|
|
|
logging.Debug(logging.DClient, "RCO 01 starting manager for %v client %v",cl.Type,cl.Id)
|
|
|
|
return NewReactorManager(cl,r.Sys,err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewReactorCoordinator(sys *SystemViewer, err chan error) *Coordinator {
|
|
|
|
return NewCoordinator(&reactorCoordinator{Sys:sys}, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//tui coordinator
|
|
|
|
type tuiCoordinator struct {
|
|
|
|
//can add fields as needed
|
|
|
|
Ip string
|
|
|
|
Sys *SystemViewer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tuiCoordinator) Start() {
|
|
|
|
logging.Debug(logging.DStart,"TCO 01 Starting!")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tuiCoordinator) NewManager(cl *Client, err chan error) GeneralManager {
|
|
|
|
logging.Debug(logging.DClient, "TCO 01 starting manager for %v client %v",cl.Type,cl.Id)
|
|
|
|
return NewTUIManager(t.Ip,cl,t.Sys,err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTUICoordinator(ip string, sys *SystemViewer, err chan error) *Coordinator {
|
|
|
|
return NewCoordinator(&tuiCoordinator{Ip:ip,Sys:sys}, err)
|
|
|
|
}
|