-
Notifications
You must be signed in to change notification settings - Fork 0
/
appsender_server.go
67 lines (55 loc) · 1.93 KB
/
appsender_server.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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package appsender
import (
"context"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/lasthyphen/beacongo/ids"
"github.com/lasthyphen/beacongo/snow/engine/common"
appsenderpb "github.com/lasthyphen/beacongo/proto/pb/appsender"
)
var _ appsenderpb.AppSenderServer = &Server{}
type Server struct {
appsenderpb.UnsafeAppSenderServer
appSender common.AppSender
}
// NewServer returns a messenger connected to a remote channel
func NewServer(appSender common.AppSender) *Server {
return &Server{appSender: appSender}
}
func (s *Server) SendAppRequest(_ context.Context, req *appsenderpb.SendAppRequestMsg) (*emptypb.Empty, error) {
nodeIDs := ids.NewNodeIDSet(len(req.NodeIds))
for _, nodeIDBytes := range req.NodeIds {
nodeID, err := ids.ToNodeID(nodeIDBytes)
if err != nil {
return nil, err
}
nodeIDs.Add(nodeID)
}
err := s.appSender.SendAppRequest(nodeIDs, req.RequestId, req.Request)
return &emptypb.Empty{}, err
}
func (s *Server) SendAppResponse(_ context.Context, req *appsenderpb.SendAppResponseMsg) (*emptypb.Empty, error) {
nodeID, err := ids.ToNodeID(req.NodeId)
if err != nil {
return nil, err
}
err = s.appSender.SendAppResponse(nodeID, req.RequestId, req.Response)
return &emptypb.Empty{}, err
}
func (s *Server) SendAppGossip(_ context.Context, req *appsenderpb.SendAppGossipMsg) (*emptypb.Empty, error) {
err := s.appSender.SendAppGossip(req.Msg)
return &emptypb.Empty{}, err
}
func (s *Server) SendAppGossipSpecific(_ context.Context, req *appsenderpb.SendAppGossipSpecificMsg) (*emptypb.Empty, error) {
nodeIDs := ids.NewNodeIDSet(len(req.NodeIds))
for _, nodeIDBytes := range req.NodeIds {
nodeID, err := ids.ToNodeID(nodeIDBytes)
if err != nil {
return nil, err
}
nodeIDs.Add(nodeID)
}
err := s.appSender.SendAppGossipSpecific(nodeIDs, req.Msg)
return &emptypb.Empty{}, err
}