blob: 4722be0403953ccce72cb3db90a460706972e40b [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001// Copyright 2017 The etcd Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package adapter
16
17import (
18 "context"
19
20 "go.etcd.io/etcd/etcdserver/api/v3election/v3electionpb"
21
22 "google.golang.org/grpc"
23)
24
25type es2ec struct{ es v3electionpb.ElectionServer }
26
27func ElectionServerToElectionClient(es v3electionpb.ElectionServer) v3electionpb.ElectionClient {
28 return &es2ec{es}
29}
30
31func (s *es2ec) Campaign(ctx context.Context, r *v3electionpb.CampaignRequest, opts ...grpc.CallOption) (*v3electionpb.CampaignResponse, error) {
32 return s.es.Campaign(ctx, r)
33}
34
35func (s *es2ec) Proclaim(ctx context.Context, r *v3electionpb.ProclaimRequest, opts ...grpc.CallOption) (*v3electionpb.ProclaimResponse, error) {
36 return s.es.Proclaim(ctx, r)
37}
38
39func (s *es2ec) Leader(ctx context.Context, r *v3electionpb.LeaderRequest, opts ...grpc.CallOption) (*v3electionpb.LeaderResponse, error) {
40 return s.es.Leader(ctx, r)
41}
42
43func (s *es2ec) Resign(ctx context.Context, r *v3electionpb.ResignRequest, opts ...grpc.CallOption) (*v3electionpb.ResignResponse, error) {
44 return s.es.Resign(ctx, r)
45}
46
47func (s *es2ec) Observe(ctx context.Context, in *v3electionpb.LeaderRequest, opts ...grpc.CallOption) (v3electionpb.Election_ObserveClient, error) {
48 cs := newPipeStream(ctx, func(ss chanServerStream) error {
49 return s.es.Observe(in, &es2ecServerStream{ss})
50 })
51 return &es2ecClientStream{cs}, nil
52}
53
54// es2ecClientStream implements Election_ObserveClient
55type es2ecClientStream struct{ chanClientStream }
56
57// es2ecServerStream implements Election_ObserveServer
58type es2ecServerStream struct{ chanServerStream }
59
60func (s *es2ecClientStream) Send(rr *v3electionpb.LeaderRequest) error {
61 return s.SendMsg(rr)
62}
63func (s *es2ecClientStream) Recv() (*v3electionpb.LeaderResponse, error) {
64 var v interface{}
65 if err := s.RecvMsg(&v); err != nil {
66 return nil, err
67 }
68 return v.(*v3electionpb.LeaderResponse), nil
69}
70
71func (s *es2ecServerStream) Send(rr *v3electionpb.LeaderResponse) error {
72 return s.SendMsg(rr)
73}
74func (s *es2ecServerStream) Recv() (*v3electionpb.LeaderRequest, error) {
75 var v interface{}
76 if err := s.RecvMsg(&v); err != nil {
77 return nil, err
78 }
79 return v.(*v3electionpb.LeaderRequest), nil
80}