removed sloppy backup

main
KeeganForelight 2 years ago
parent 5cd34e89b6
commit 79880d6cd7

@ -1,95 +0,0 @@
package server
import (
"fmt"
"sync"
"net"
"context"
"FRMS/internal/pkg/system"
"google.golang.org/grpc"
pb "FRMS/internal/pkg/grpc"
)
// the goal here is to set up a gRPC handler to respond to reactor pings with their IP and to establish a new coordinator for that specific reactor
type ReactorListener struct { // exporting for easy use in the short term
Reactor map[uint32]*ReactorManager
Ip string
Port int
Err chan error
mu sync.Mutex
pb.UnimplementedHandshakeServer
}
func GetIp(e string) (string, error) {
return system.GetIp(e)
}
func GetPort() (int, error) {
return system.GetPort()
}
func NewReactorListener(ifconfig string,ch chan error) (*ReactorListener, error) {
m := make(map[uint32]*ReactorManager)
var ip string
var port int
var err error
if ip, err = GetIp(ifconfig); err != nil {
return &ReactorListener{}, err
}
if port, err = GetPort(); err != nil {
return &ReactorListener{}, err
}
return &ReactorListener{Ip:ip,Port:port,Err:ch, Reactor:m}, nil
}
func (r *ReactorListener) GetIp() string {
r.mu.Lock()
defer r.mu.Unlock()
return r.Ip
}
func (r *ReactorListener) GetPort() int {
r.mu.Lock()
defer r.mu.Unlock()
return r.Port
}
func (r *ReactorListener) Start() {
// start grpc server and implement reciever
if err := r.Register(); err != nil {
r.Err <- err
}
// reactor listener started and grpc library will execute listener
fmt.Printf("Started listener on %v:%v\n",r.Ip,r.Port)
}
func (r *ReactorListener) Register() error {
// creates a gRPC service and binds it to our handler
lis, err := net.Listen("tcp", fmt.Sprintf("%v:%v",r.Ip,r.Port))
if err != nil {
return err
}
r.Port = lis.Addr().(*net.TCPAddr).Port
grpcServer := grpc.NewServer()
pb.RegisterHandshakeServer(grpcServer, r)
go grpcServer.Serve(lis)
return nil
}
func (r *ReactorListener) ReactorDiscoveryHandler(ctx context.Context, ping *pb.ReactorDiscoveryRequest) (*pb.ReactorDiscoveryResponse, error) {
// incoming reactor ping need to spawn coord
r.mu.Lock()
rm, ok := r.Reactor[ping.GetId()]
fmt.Printf("Reactor %v has connected from %v:%v\n",ping.GetId(),ping.GetIp(),ping.GetPort())
// if reactor exists
if ok {
rm.Start()
} else {
rm := NewReactorManager(r,ping.GetIp(),ping.GetModel(),int(ping.GetPort()),ping.GetId())
go rm.Start()
r.Reactor[ping.GetId()] = rm
}
return &pb.ReactorDiscoveryResponse{Success:true}, nil
}
Loading…
Cancel
Save