|
|
|
package reactor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
"FRMS/internal/pkg/sensor"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
pb "FRMS/internal/pkg/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Coordinator struct {
|
|
|
|
Id string
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCoordinator(id string, port int) *Coordinator {
|
|
|
|
return &Coordinator{Id:id,Port:port}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Coordinator) Start() error {
|
|
|
|
// start a gRPC server to respond to pings from central server
|
|
|
|
fmt.Printf("Starting gRPC server on %v:%v\n",c.Id,c.Port)
|
|
|
|
lis, err := net.Listen("tcp",fmt.Sprintf("%v:%v",c.Id,c.Port))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
grpcServer := grpc.NewServer()
|
|
|
|
pb.RegisterCoordinatorServer(grpcServer, newServer())
|
|
|
|
|
|
|
|
go grpcServer.Serve(lis)
|
|
|
|
// now setting up sensor managers
|
|
|
|
sm := sensor.NewManager()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sensor discovery abstraction
|
|
|
|
type
|
|
|
|
// grpc stuff
|
|
|
|
func newServer() *coordinatorServer {
|
|
|
|
return &coordinatorServer{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type coordinatorServer struct {
|
|
|
|
pb.UnimplementedCoordinatorServer
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *coordinatorServer) PingHandler(ctx context.Context, ping *pb.PingRequest) (*pb.PingResponse, error){
|
|
|
|
now := time.Now()
|
|
|
|
fmt.Printf("Ping from server recieved at time %v\n", now.Format("15:04:05"))
|
|
|
|
return &pb.PingResponse{Id: s.id}, nil
|
|
|
|
}
|
|
|
|
|