maintenance_client_adapter.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // 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. package adapter
  15. import (
  16. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  17. "golang.org/x/net/context"
  18. "google.golang.org/grpc"
  19. )
  20. type mts2mtc struct{ mts pb.MaintenanceServer }
  21. func MaintenanceServerToMaintenanceClient(mts pb.MaintenanceServer) pb.MaintenanceClient {
  22. return &mts2mtc{mts}
  23. }
  24. func (s *mts2mtc) Alarm(ctx context.Context, r *pb.AlarmRequest, opts ...grpc.CallOption) (*pb.AlarmResponse, error) {
  25. return s.mts.Alarm(ctx, r)
  26. }
  27. func (s *mts2mtc) Status(ctx context.Context, r *pb.StatusRequest, opts ...grpc.CallOption) (*pb.StatusResponse, error) {
  28. return s.mts.Status(ctx, r)
  29. }
  30. func (s *mts2mtc) Defragment(ctx context.Context, dr *pb.DefragmentRequest, opts ...grpc.CallOption) (*pb.DefragmentResponse, error) {
  31. return s.mts.Defragment(ctx, dr)
  32. }
  33. func (s *mts2mtc) Hash(ctx context.Context, r *pb.HashRequest, opts ...grpc.CallOption) (*pb.HashResponse, error) {
  34. return s.mts.Hash(ctx, r)
  35. }
  36. func (s *mts2mtc) Snapshot(ctx context.Context, in *pb.SnapshotRequest, opts ...grpc.CallOption) (pb.Maintenance_SnapshotClient, error) {
  37. cs := newPipeStream(ctx, func(ss chanServerStream) error {
  38. return s.mts.Snapshot(in, &ss2scServerStream{ss})
  39. })
  40. return &ss2scClientStream{cs}, nil
  41. }
  42. // ss2scClientStream implements Maintenance_SnapshotClient
  43. type ss2scClientStream struct{ chanClientStream }
  44. // ss2scServerStream implements Maintenance_SnapshotServer
  45. type ss2scServerStream struct{ chanServerStream }
  46. func (s *ss2scClientStream) Send(rr *pb.SnapshotRequest) error {
  47. return s.SendMsg(rr)
  48. }
  49. func (s *ss2scClientStream) Recv() (*pb.SnapshotResponse, error) {
  50. var v interface{}
  51. if err := s.RecvMsg(&v); err != nil {
  52. return nil, err
  53. }
  54. return v.(*pb.SnapshotResponse), nil
  55. }
  56. func (s *ss2scServerStream) Send(rr *pb.SnapshotResponse) error {
  57. return s.SendMsg(rr)
  58. }
  59. func (s *ss2scServerStream) Recv() (*pb.SnapshotRequest, error) {
  60. var v interface{}
  61. if err := s.RecvMsg(&v); err != nil {
  62. return nil, err
  63. }
  64. return v.(*pb.SnapshotRequest), nil
  65. }