/
post_info_service.go
60 lines (48 loc) · 1.64 KB
/
post_info_service.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
package grpcserver
import (
"context"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
pb "github.com/spacemeshos/api/release/go/spacemesh/v1"
"go.uber.org/zap"
"google.golang.org/grpc"
"github.com/spacemeshos/go-spacemesh/common/types"
)
var statusMap map[types.PostState]pb.PostState_State = map[types.PostState]pb.PostState_State{
types.PostStateIdle: pb.PostState_IDLE,
types.PostStateProving: pb.PostState_PROVING,
}
// PostInfoService provides information about connected PostServices.
type PostInfoService struct {
log *zap.Logger
states postState
}
// RegisterService registers this service with a grpc server instance.
func (s *PostInfoService) RegisterService(server *grpc.Server) {
pb.RegisterPostInfoServiceServer(server, s)
}
func (s *PostInfoService) RegisterHandlerService(mux *runtime.ServeMux) error {
return pb.RegisterPostInfoServiceHandlerServer(context.Background(), mux, s)
}
// String returns the name of this service.
func (s *PostInfoService) String() string {
return "PostInfoService"
}
// NewPostInfoService creates a new instance of the post info grpc service.
func NewPostInfoService(log *zap.Logger, states postState) *PostInfoService {
return &PostInfoService{
log: log,
states: states,
}
}
func (s *PostInfoService) PostStates(context.Context, *pb.PostStatesRequest) (*pb.PostStatesResponse, error) {
states := s.states.PostStates()
pbStates := make([]*pb.PostState, 0, len(states))
for id, state := range states {
pbStates = append(pbStates, &pb.PostState{
Id: id.NodeID().Bytes(),
Name: id.Name(),
State: statusMap[state],
})
}
return &pb.PostStatesResponse{States: pbStates}, nil
}