]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | ||
20 | package org.apache.thrift; | |
21 | ||
22 | import org.apache.thrift.protocol.TMessage; | |
23 | import org.apache.thrift.protocol.TMessageType; | |
24 | import org.apache.thrift.protocol.TProtocol; | |
25 | ||
26 | /** | |
27 | * A TServiceClient is used to communicate with a TService implementation | |
28 | * across protocols and transports. | |
29 | */ | |
30 | public abstract class TServiceClient { | |
31 | public TServiceClient(TProtocol prot) { | |
32 | this(prot, prot); | |
33 | } | |
34 | ||
35 | public TServiceClient(TProtocol iprot, TProtocol oprot) { | |
36 | iprot_ = iprot; | |
37 | oprot_ = oprot; | |
38 | } | |
39 | ||
40 | protected TProtocol iprot_; | |
41 | protected TProtocol oprot_; | |
42 | ||
43 | protected int seqid_; | |
44 | ||
45 | /** | |
46 | * Get the TProtocol being used as the input (read) protocol. | |
47 | * @return the TProtocol being used as the input (read) protocol. | |
48 | */ | |
49 | public TProtocol getInputProtocol() { | |
50 | return this.iprot_; | |
51 | } | |
52 | ||
53 | /** | |
54 | * Get the TProtocol being used as the output (write) protocol. | |
55 | * @return the TProtocol being used as the output (write) protocol. | |
56 | */ | |
57 | public TProtocol getOutputProtocol() { | |
58 | return this.oprot_; | |
59 | } | |
60 | ||
61 | protected void sendBase(String methodName, TBase<?,?> args) throws TException { | |
62 | sendBase(methodName, args, TMessageType.CALL); | |
63 | } | |
64 | ||
65 | protected void sendBaseOneway(String methodName, TBase<?,?> args) throws TException { | |
66 | sendBase(methodName, args, TMessageType.ONEWAY); | |
67 | } | |
68 | ||
69 | private void sendBase(String methodName, TBase<?,?> args, byte type) throws TException { | |
70 | oprot_.writeMessageBegin(new TMessage(methodName, type, ++seqid_)); | |
71 | args.write(oprot_); | |
72 | oprot_.writeMessageEnd(); | |
73 | oprot_.getTransport().flush(); | |
74 | } | |
75 | ||
76 | protected void receiveBase(TBase<?,?> result, String methodName) throws TException { | |
77 | TMessage msg = iprot_.readMessageBegin(); | |
78 | if (msg.type == TMessageType.EXCEPTION) { | |
79 | TApplicationException x = new TApplicationException(); | |
80 | x.read(iprot_); | |
81 | iprot_.readMessageEnd(); | |
82 | throw x; | |
83 | } | |
84 | if (msg.seqid != seqid_) { | |
85 | throw new TApplicationException(TApplicationException.BAD_SEQUENCE_ID, | |
86 | String.format("%s failed: out of sequence response: expected %d but got %d", methodName, seqid_, msg.seqid)); | |
87 | } | |
88 | result.read(iprot_); | |
89 | iprot_.readMessageEnd(); | |
90 | } | |
91 | } |