]>
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 | package org.apache.thrift.async; | |
20 | ||
21 | import org.apache.thrift.protocol.TProtocolFactory; | |
22 | import org.apache.thrift.transport.TNonblockingTransport; | |
23 | ||
24 | public abstract class TAsyncClient { | |
25 | protected final TProtocolFactory ___protocolFactory; | |
26 | protected final TNonblockingTransport ___transport; | |
27 | protected final TAsyncClientManager ___manager; | |
28 | protected TAsyncMethodCall ___currentMethod; | |
29 | private Exception ___error; | |
30 | private long ___timeout; | |
31 | ||
32 | public TAsyncClient(TProtocolFactory protocolFactory, TAsyncClientManager manager, TNonblockingTransport transport) { | |
33 | this(protocolFactory, manager, transport, 0); | |
34 | } | |
35 | ||
36 | public TAsyncClient(TProtocolFactory protocolFactory, TAsyncClientManager manager, TNonblockingTransport transport, long timeout) { | |
37 | this.___protocolFactory = protocolFactory; | |
38 | this.___manager = manager; | |
39 | this.___transport = transport; | |
40 | this.___timeout = timeout; | |
41 | } | |
42 | ||
43 | public TProtocolFactory getProtocolFactory() { | |
44 | return ___protocolFactory; | |
45 | } | |
46 | ||
47 | public long getTimeout() { | |
48 | return ___timeout; | |
49 | } | |
50 | ||
51 | public boolean hasTimeout() { | |
52 | return ___timeout > 0; | |
53 | } | |
54 | ||
55 | public void setTimeout(long timeout) { | |
56 | this.___timeout = timeout; | |
57 | } | |
58 | ||
59 | /** | |
60 | * Is the client in an error state? | |
61 | * @return If client in an error state? | |
62 | */ | |
63 | public boolean hasError() { | |
64 | return ___error != null; | |
65 | } | |
66 | ||
67 | /** | |
68 | * Get the client's error - returns null if no error | |
69 | * @return Get the client's error. <p> returns null if no error | |
70 | */ | |
71 | public Exception getError() { | |
72 | return ___error; | |
73 | } | |
74 | ||
75 | protected void checkReady() { | |
76 | // Ensure we are not currently executing a method | |
77 | if (___currentMethod != null) { | |
78 | throw new IllegalStateException("Client is currently executing another method: " + ___currentMethod.getClass().getName()); | |
79 | } | |
80 | ||
81 | // Ensure we're not in an error state | |
82 | if (___error != null) { | |
83 | throw new IllegalStateException("Client has an error!", ___error); | |
84 | } | |
85 | } | |
86 | ||
87 | /** | |
88 | * Called by delegate method when finished | |
89 | */ | |
90 | protected void onComplete() { | |
91 | ___currentMethod = null; | |
92 | } | |
93 | ||
94 | /** | |
95 | * Called by delegate method on error | |
96 | */ | |
97 | protected void onError(Exception exception) { | |
98 | ___transport.close(); | |
99 | ___currentMethod = null; | |
100 | ___error = exception; | |
101 | } | |
102 | } |