-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgRPCControllerServer.go
67 lines (53 loc) · 2.01 KB
/
gRPCControllerServer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"log"
"net"
"context"
// "time"
"io/ioutil"
"strings"
"google.golang.org/grpc"
// "google.golang.org/grpc/credentials/insecure"
pb "github.com/FlavScheidt/gossipGoSnt/proto"
// proto "github.com/golang/protobuf/proto"
// protoreflect "google.golang.org/protobuf/reflect/protoreflect"
// protoimpl "google.golang.org/protobuf/runtime/protoimpl"
// reflect "reflect"
// sync "sync"
)
// server is used to implement helloworld.GreeterServer.
type server struct {
pb.UnimplementedGossipMessageServer
}
func (s *server) ToLibP2P(ctx context.Context, in *pb.Gossip) (*pb.Control, error) {
nodeName, err := ioutil.ReadFile("/etc/hostname")
if err != nil {
log.Fatal(err)
}
node := strings.TrimSpace(fmt.Sprintf("%s",nodeName))
// Log format is "time | handler | received/sent | orign/destination | data"
log.Printf(" | %s | gRPC-Server | Received | Rippled | %v | %v \n", node, in.GetHash(), in.GetValidator_Key())
//Send message to gossipsub
for i := 0; i<len(publishingTopics); i++ {
publishingTopics[i].Publish(in.GetMessage(), in.GetValidator_Key(), in.GetHash())
log.Println("%s: Message published on topic", node, publishingTopics[i].name)
}
return &pb.Control{Stream: true}, nil
}
func gRPCserver() {
log.Println("Starting gRPC server")
lis, err := net.Listen("tcp", fmt.Sprintf(":%s", gRPCportServer))
if err != nil {
log.Fatalf("failed to listen: %v", err)
log.Println("------------------------------------------------------------------")
}
s := grpc.NewServer()
pb.RegisterGossipMessageServer(s, &server{})
log.Printf("server listening at %v", lis.Addr())
log.Println("------------------------------------------------------------------")
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
log.Println("------------------------------------------------------------------")
}
}