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.
122 lines
2.9 KiB
Go
122 lines
2.9 KiB
Go
2 years ago
|
package server
|
||
|
|
||
|
import (
|
||
|
"FRMS/internal/pkg/logging"
|
||
|
"FRMS/internal/pkg/manager"
|
||
|
"errors"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
ErrNoReactorManager = errors.New("no reactor manager found")
|
||
|
)
|
||
|
|
||
|
// ReactorManager can be started/stopped as clients connect/disconnect.
|
||
|
type ReactorManager struct {
|
||
|
Manager // base manager interface
|
||
|
}
|
||
|
|
||
|
// Manager is an interface requiring a structure that can be started
|
||
|
// and stopped as well as provide timeouts in milliseconds.
|
||
|
type Manager interface {
|
||
|
Start() error // status checks
|
||
|
Stop() error
|
||
|
Timeout() (time.Duration, error) // TO Generator
|
||
|
}
|
||
|
|
||
|
// NewManager returns a manager fulfilling the Manager interface as well as
|
||
|
// any potential errors.
|
||
|
func NewManager(max int) (Manager, error) {
|
||
|
return manager.New(max)
|
||
|
}
|
||
|
|
||
|
// GetReactorManager returns a reactor manager for passed id.
|
||
|
// Throws error if manager not found for id.
|
||
|
func (c *Coordinator) LoadReactorManager(id int) (*ReactorManager, error) {
|
||
|
|
||
|
c.managerMu.RLock()
|
||
|
defer c.managerMu.RUnlock()
|
||
|
|
||
|
rm, exists := c.directory[id]
|
||
|
|
||
|
if !exists {
|
||
|
logging.Debug(
|
||
|
logging.DClient,
|
||
|
"RCO 00 creating manager for %v",
|
||
|
id,
|
||
|
)
|
||
|
|
||
|
m, err := NewManager(0)
|
||
|
|
||
|
rm = &ReactorManager{
|
||
|
Manager: m,
|
||
|
}
|
||
|
|
||
|
if err = rm.Start(); err != nil {
|
||
|
return rm, err
|
||
|
}
|
||
|
|
||
|
c.directory[id] = rm
|
||
|
}
|
||
|
|
||
|
return rm, nil
|
||
|
}
|
||
|
|
||
|
// // NewReactorManager takes in a client, config and channel to pass errors on.
|
||
|
// // Returns a new reactor manager as well as any errors that occured during
|
||
|
// // creation.
|
||
|
// // Uses MaxConnectionAttempts which defaults to 10 to prevent
|
||
|
// // unnessecary network load and/or timeout lengths.
|
||
|
// func NewReactorManager(
|
||
|
// ) (*ReactorManager, error) {
|
||
|
|
||
|
// m, err := NewManager(MaxConnectionAttempts)
|
||
|
|
||
|
// if err != nil {
|
||
|
// return &ReactorManager{}, err
|
||
|
// }
|
||
|
|
||
|
// return r, err
|
||
|
// }
|
||
|
|
||
|
// Start logs the start and calls start on the embedded manager.
|
||
|
func (r *ReactorManager) Start() error {
|
||
|
// logging.Debug(logging.DStart, "RMA starting", r.Id)
|
||
|
return r.Manager.Start()
|
||
|
}
|
||
|
|
||
|
// Stop logs the stop and calls stop on the embedded manager.
|
||
|
func (r *ReactorManager) Stop() error {
|
||
|
// logging.Debug(logging.DExit, "RMA %v stopping", r.Id)
|
||
|
return r.Manager.Stop()
|
||
|
}
|
||
|
|
||
|
// UpdateClient is used to change the underlying manager client if there
|
||
|
// changes to its data.
|
||
|
//
|
||
|
// BUG(Keegan): Client is not protected by a lock and may lead to races
|
||
|
// func (r *ReactorManager) UpdateClient(cl *Client) error {
|
||
|
// logging.Debug(logging.DClient, "RMA %v updating client", r.Id)
|
||
|
// r.Client = cl
|
||
|
// return nil
|
||
|
// }
|
||
|
|
||
|
// // ReactorDeviceHandler processes incoming device information and
|
||
|
// // updates the manager accordingly.
|
||
|
// func (r *ReactorManager) ReactorDeviceHandler(devs []*pb.Device) error {
|
||
|
|
||
|
// logging.Debug(logging.DClient, "CCO recieved ping from %v", r.Id)
|
||
|
|
||
|
// for _, dev := range devs {
|
||
|
// logging.Debug(
|
||
|
// logging.DClient,
|
||
|
// "CCO %v device %v is %v",
|
||
|
// r.Id,
|
||
|
// dev.GetAddr(),
|
||
|
// dev.GetStatus().String(),
|
||
|
// )
|
||
|
// }
|
||
|
|
||
|
// return nil
|
||
|
// }
|