1// Copyright 2016 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	"errors"
20
21	pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
22	"google.golang.org/grpc"
23)
24
25var errAlreadySentHeader = errors.New("adapter: already sent header")
26
27type ws2wc struct{ wserv pb.WatchServer }
28
29func WatchServerToWatchClient(wserv pb.WatchServer) pb.WatchClient {
30	return &ws2wc{wserv}
31}
32
33func (s *ws2wc) Watch(ctx context.Context, opts ...grpc.CallOption) (pb.Watch_WatchClient, error) {
34	cs := newPipeStream(ctx, func(ss chanServerStream) error {
35		return s.wserv.Watch(&ws2wcServerStream{ss})
36	})
37	return &ws2wcClientStream{cs}, nil
38}
39
40// ws2wcClientStream implements Watch_WatchClient
41type ws2wcClientStream struct{ chanClientStream }
42
43// ws2wcServerStream implements Watch_WatchServer
44type ws2wcServerStream struct{ chanServerStream }
45
46func (s *ws2wcClientStream) Send(wr *pb.WatchRequest) error {
47	return s.SendMsg(wr)
48}
49func (s *ws2wcClientStream) Recv() (*pb.WatchResponse, error) {
50	var v interface{}
51	if err := s.RecvMsg(&v); err != nil {
52		return nil, err
53	}
54	return v.(*pb.WatchResponse), nil
55}
56
57func (s *ws2wcServerStream) Send(wr *pb.WatchResponse) error {
58	return s.SendMsg(wr)
59}
60func (s *ws2wcServerStream) Recv() (*pb.WatchRequest, error) {
61	var v interface{}
62	if err := s.RecvMsg(&v); err != nil {
63		return nil, err
64	}
65	return v.(*pb.WatchRequest), nil
66}
67