/
occclient.go
93 lines (83 loc) · 2.56 KB
/
occclient.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package nopb
import (
"context"
"google.golang.org/grpc"
"github.com/AliceO2Group/Control/executor/protos"
)
// Equivalent to the pb.OccClient interface so we can use protobuf-generated code
type OccClient interface {
EventStream(ctx context.Context, in *pb.EventStreamRequest, opts ...grpc.CallOption) (pb.Occ_EventStreamClient, error)
StateStream(ctx context.Context, in *pb.StateStreamRequest, opts ...grpc.CallOption) (pb.Occ_StateStreamClient, error)
GetState(ctx context.Context, in *pb.GetStateRequest, opts ...grpc.CallOption) (*pb.GetStateReply, error)
Transition(ctx context.Context, in *pb.TransitionRequest, opts ...grpc.CallOption) (*pb.TransitionReply, error)
}
type occClient struct {
cc *grpc.ClientConn
}
func NewOccClient(cc *grpc.ClientConn) OccClient {
return &occClient{cc}
}
type occEventStreamClient struct {
grpc.ClientStream
}
func (x *occEventStreamClient) Recv() (*pb.EventStreamReply, error) {
m := new(pb.EventStreamReply)
if err := x.ClientStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
func (c *occClient) EventStream(ctx context.Context, in *pb.EventStreamRequest, opts ...grpc.CallOption) (pb.Occ_EventStreamClient, error) {
opts = append(opts,
[]grpc.CallOption{
grpc.CallContentSubtype("json"),
}...
)
streamDesc := grpc.StreamDesc{
StreamName: "EventStream",
Handler: nil,
ServerStreams: true,
ClientStreams: false,
}
stream, err := c.cc.NewStream(ctx, &streamDesc, "EventStream", opts...)
if err != nil {
return nil, err
}
x := &occEventStreamClient{stream}
if err := x.ClientStream.SendMsg(in); err != nil {
return nil, err
}
if err := x.ClientStream.CloseSend(); err != nil {
return nil, err
}
return x, nil
}
func (c *occClient) StateStream(ctx context.Context, in *pb.StateStreamRequest, opts ...grpc.CallOption) (pb.Occ_StateStreamClient, error) {
return nil, nil
}
func (c *occClient) GetState(ctx context.Context, in *pb.GetStateRequest, opts ...grpc.CallOption) (*pb.GetStateReply, error) {
out := pb.GetStateReply{}
opts = append(opts,
[]grpc.CallOption{
grpc.CallContentSubtype("json"),
}...
)
err := c.cc.Invoke(ctx, "GetState", in, &out, opts...)
if err != nil {
return nil, err
}
return &out, nil
}
func (c *occClient) Transition(ctx context.Context, in *pb.TransitionRequest, opts ...grpc.CallOption) (*pb.TransitionReply, error) {
out := new(pb.TransitionReply)
opts = append(opts,
[]grpc.CallOption{
grpc.CallContentSubtype("json"),
}...
)
err := c.cc.Invoke(ctx, "Transition", in, &out, opts...)
if err != nil {
return nil, err
}
return out, nil
}