]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/go/thrift/client.go
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / go / thrift / client.go
1 package thrift
2
3 import (
4 "context"
5 "fmt"
6 )
7
8 type TClient interface {
9 Call(ctx context.Context, method string, args, result TStruct) error
10 }
11
12 type TStandardClient struct {
13 seqId int32
14 iprot, oprot TProtocol
15 }
16
17 // TStandardClient implements TClient, and uses the standard message format for Thrift.
18 // It is not safe for concurrent use.
19 func NewTStandardClient(inputProtocol, outputProtocol TProtocol) *TStandardClient {
20 return &TStandardClient{
21 iprot: inputProtocol,
22 oprot: outputProtocol,
23 }
24 }
25
26 func (p *TStandardClient) Send(ctx context.Context, oprot TProtocol, seqId int32, method string, args TStruct) error {
27 // Set headers from context object on THeaderProtocol
28 if headerProt, ok := oprot.(*THeaderProtocol); ok {
29 headerProt.ClearWriteHeaders()
30 for _, key := range GetWriteHeaderList(ctx) {
31 if value, ok := GetHeader(ctx, key); ok {
32 headerProt.SetWriteHeader(key, value)
33 }
34 }
35 }
36
37 if err := oprot.WriteMessageBegin(method, CALL, seqId); err != nil {
38 return err
39 }
40 if err := args.Write(oprot); err != nil {
41 return err
42 }
43 if err := oprot.WriteMessageEnd(); err != nil {
44 return err
45 }
46 return oprot.Flush(ctx)
47 }
48
49 func (p *TStandardClient) Recv(iprot TProtocol, seqId int32, method string, result TStruct) error {
50 rMethod, rTypeId, rSeqId, err := iprot.ReadMessageBegin()
51 if err != nil {
52 return err
53 }
54
55 if method != rMethod {
56 return NewTApplicationException(WRONG_METHOD_NAME, fmt.Sprintf("%s: wrong method name", method))
57 } else if seqId != rSeqId {
58 return NewTApplicationException(BAD_SEQUENCE_ID, fmt.Sprintf("%s: out of order sequence response", method))
59 } else if rTypeId == EXCEPTION {
60 var exception tApplicationException
61 if err := exception.Read(iprot); err != nil {
62 return err
63 }
64
65 if err := iprot.ReadMessageEnd(); err != nil {
66 return err
67 }
68
69 return &exception
70 } else if rTypeId != REPLY {
71 return NewTApplicationException(INVALID_MESSAGE_TYPE_EXCEPTION, fmt.Sprintf("%s: invalid message type", method))
72 }
73
74 if err := result.Read(iprot); err != nil {
75 return err
76 }
77
78 return iprot.ReadMessageEnd()
79 }
80
81 func (p *TStandardClient) Call(ctx context.Context, method string, args, result TStruct) error {
82 p.seqId++
83 seqId := p.seqId
84
85 if err := p.Send(ctx, p.oprot, seqId, method, args); err != nil {
86 return err
87 }
88
89 // method is oneway
90 if result == nil {
91 return nil
92 }
93
94 return p.Recv(p.iprot, seqId, method, result)
95 }