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.
81 lines
1.6 KiB
Go
81 lines
1.6 KiB
Go
// package System provides a way to listen for incoming connections
|
|
// and manage multiple reactor clients.
|
|
package system
|
|
|
|
import (
|
|
pb "dmac/pkg/grpc"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"sync"
|
|
|
|
"github.com/spf13/viper"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
var (
|
|
ErrMissingPort = errors.New("port not set")
|
|
)
|
|
|
|
// Coordinator is runs on the server and is used to oversee
|
|
// the reactor managers as well as process incoming client connections.
|
|
type Coordinator struct {
|
|
Config *viper.Viper
|
|
listener net.Listener
|
|
grpcServer *grpc.Server
|
|
|
|
DatabasePort int `mapstructure:"database_port"`
|
|
GRPCPort int `mapstructure:"grpc_port"`
|
|
|
|
directory map[int]*ReactorManager
|
|
managerMu sync.RWMutex
|
|
|
|
Err chan error
|
|
|
|
// grpc
|
|
pb.UnimplementedHandshakeServer
|
|
pb.UnimplementedMonitoringServer
|
|
}
|
|
|
|
// NewCentralCoordinator creates a central coordinator with the given global
|
|
// config and error channel.
|
|
func NewCentralCoordinator(config *viper.Viper, ch chan error) *Coordinator {
|
|
|
|
rmap := make(map[int]*ReactorManager)
|
|
|
|
return &Coordinator{
|
|
Err: ch,
|
|
Config: config,
|
|
directory: rmap,
|
|
}
|
|
}
|
|
|
|
// Start loads config, starts network listener and registers grpc handlers.
|
|
// Ready for new clients on return.
|
|
func (c *Coordinator) Start() error {
|
|
|
|
if err := c.Config.Unmarshal(c); err != nil {
|
|
return err
|
|
}
|
|
|
|
// ensure it shows up as missing
|
|
if c.GRPCPort == 0 {
|
|
c.Config.Set("grpc_port", 0)
|
|
c.Config.WriteConfig()
|
|
|
|
return ErrMissingPort
|
|
}
|
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%v", c.GRPCPort))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
grpcServer := grpc.NewServer()
|
|
|
|
c.listener = lis
|
|
c.grpcServer = grpcServer
|
|
|
|
return c.Register()
|
|
}
|