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.
68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
3 years ago
|
package server
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"time"
|
||
|
"context"
|
||
|
"sync"
|
||
|
"google.golang.org/grpc"
|
||
|
"google.golang.org/grpc/status"
|
||
|
pb "FRMS/internal/pkg/grpc"
|
||
|
)
|
||
|
|
||
|
// this package will implement a reactor coordinator and associated go routines
|
||
|
|
||
|
type ReactorManager struct {
|
||
|
*Manager
|
||
|
Devs *Devices
|
||
|
ClientConnections <-chan bool
|
||
|
}
|
||
|
|
||
|
type Devices struct {
|
||
|
mu sync.Mutex
|
||
|
D map[int]Device
|
||
|
}
|
||
|
|
||
|
func NewReactorManager(c *Client,ch chan bool,err chan error) {
|
||
|
d := new(Devices)
|
||
|
r := &ReactorManager{Devs:d,ClientConnections:ch}
|
||
|
r.Manager = NewManager(c, err)
|
||
|
go r.Listen()
|
||
|
}
|
||
|
|
||
|
func (r *ReactorManager) Listen() {
|
||
|
for {
|
||
|
c := <-r.ClientConnections
|
||
|
if c {
|
||
|
r.Start()
|
||
|
} else {
|
||
|
r.Exit()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (r *ReactorManager) Start() {
|
||
|
r.Manager.Start()
|
||
|
conn := r.Connect()
|
||
|
go r.Monitor(conn)
|
||
|
}
|
||
|
|
||
|
func (r *ReactorManager) Monitor(conn *grpc.ClientConn) {
|
||
|
defer conn.Close()
|
||
|
client := pb.NewMonitoringClient(conn)
|
||
|
for r.IsActive() {
|
||
|
req := &pb.ReactorStatusRequest{Id:r.Id}
|
||
|
resp, err := client.GetReactorStatus(context.Background(),req)
|
||
|
code := status.Code(err)
|
||
|
if code != 0 { // if != OK
|
||
|
fmt.Printf("Reactor %v down! ", r.Id)
|
||
|
r.Exit()
|
||
|
}
|
||
|
for _,v := range resp.GetDevices() {
|
||
|
fmt.Printf("%v is %v, ",v.GetType(),v.GetStatus())
|
||
|
}
|
||
|
fmt.Print("\n")
|
||
|
time.Sleep(r.Hb * time.Second) // time between sensor pings
|
||
|
}
|
||
|
}
|