summaryrefslogtreecommitdiff
path: root/java/src/IceInternal/ProxyOutgoingAsyncBase.java
blob: 2977907811168be72adf2652f1533e5f2cb0839d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// **********************************************************************
//
// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

package IceInternal;

//
// Base class for proxy based invocations. This class handles the
// retry for proxy invocations. It also ensures the child observer is
// correct notified of failures and make sure the retry task is
// correctly canceled when the invocation completes.
//
public abstract class ProxyOutgoingAsyncBase extends OutgoingAsyncBase
{
    public static ProxyOutgoingAsyncBase check(Ice.AsyncResult r, Ice.ObjectPrx prx, String operation)
    {
        ProxyOutgoingAsyncBase.checkImpl(r, prx, operation);
        try
        {
            return (ProxyOutgoingAsyncBase)r;
        }
        catch(ClassCastException ex)
        {
            throw new IllegalArgumentException("Incorrect AsyncResult object for end_" + operation + " method");
        }
    }

    @Override
    public Ice.ObjectPrx getProxy()
    {
        return _proxy;
    }

    @Override
    public boolean completed(Ice.Exception exc)
    {
        if(_childObserver != null)
        {
            _childObserver.failed(exc.ice_name());
            _childObserver.detach();
            _childObserver = null;
        }
        
        //
        // NOTE: at this point, synchronization isn't needed, no other threads should be
        // calling on the callback.
        //
        try
        {
            _instance.retryQueue().add(this, handleException(exc));
            return false;
        }
        catch(Ice.Exception ex)
        {
            return finished(ex); // No retries, we're done
        }
    }
    
    public void retry()
    {
        invokeImpl(false);
    }
    
    public void cancelable(final CancellationHandler handler)
    {
        if(_proxy.__reference().getInvocationTimeout() == -2 && _cachedConnection != null)
        {
            final int timeout = _cachedConnection.timeout();
            if(timeout > 0)
            {
                _future = _instance.timer().schedule(
                    new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            cancel(new Ice.ConnectionTimeoutException());
                        }
                    }, timeout, java.util.concurrent.TimeUnit.MILLISECONDS);
            }
        }
        super.cancelable(handler);
    }
    
    public void abort(Ice.Exception ex)
    {
        assert(_childObserver == null);
        if(finished(ex))
        {
            invokeCompletedAsync();
        }
        else if(ex instanceof Ice.CommunicatorDestroyedException)
        {
            //
            // If it's a communicator destroyed exception, don't swallow
            // it but instead notify the user thread. Even if no callback
            // was provided.
            //
            throw ex;
        }
    }

    protected ProxyOutgoingAsyncBase(Ice.ObjectPrxHelperBase prx, String op, CallbackBase delegate)
    {
        super(prx.ice_getCommunicator(), prx.__reference().getInstance(), op, delegate);
        _proxy = prx;
        _mode = Ice.OperationMode.Normal;
        _cnt = 0;
        _sent = false;
    }
    
    protected ProxyOutgoingAsyncBase(Ice.ObjectPrxHelperBase prx, String op, CallbackBase delegate, BasicStream os)
    {
        super(prx.ice_getCommunicator(), prx.__reference().getInstance(), op, delegate, os);
        _proxy = prx;
        _mode = Ice.OperationMode.Normal;
        _cnt = 0;
        _sent = false;
    }

    protected static Ice.AsyncResult checkImpl(Ice.AsyncResult r, Ice.ObjectPrx p, String operation)
    {
        check(r, operation);
        if(r.getProxy() != p)
        {
            throw new IllegalArgumentException("Proxy for call to end_" + operation +
                                               " does not match proxy that was used to call corresponding " +
                                               "begin_" + operation + " method");
        }
        return r;
    }
    
    protected void invokeImpl(boolean userThread)
    {
        try
        {
            if(userThread)
            {
                int invocationTimeout = _proxy.__reference().getInvocationTimeout();
                if(invocationTimeout > 0)
                {
                    _future = _instance.timer().schedule(
                        new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                cancel(new Ice.InvocationTimeoutException());
                            }
                        }, invocationTimeout, java.util.concurrent.TimeUnit.MILLISECONDS);
                }
            }
            else // If not called from the user thread, it's called from the retry queue
            {
                if(_observer != null)
                {
                    _observer.retried();
                }
            }

            while(true)
            {
                try
                {
                    _sent = false;
                    _handler = _proxy.__getRequestHandler();
                    int status = _handler.sendAsyncRequest(this);
                    if((status & AsyncStatus.Sent) > 0)
                    {
                        if(userThread)
                        {
                            _sentSynchronously = true;
                            if((status & AsyncStatus.InvokeSentCallback) > 0)
                            {
                                invokeSent(); // Call the sent callback from the user thread.
                            }
                        }
                        else
                        {
                            if((status & AsyncStatus.InvokeSentCallback) > 0)
                            {
                                invokeSentAsync(); // Call the sent callback from a client thread pool thread.
                            }
                        }
                    }
                    return; // We're done!
                }
                catch(RetryException ex)
                {
                    handleRetryException(ex);
                }
                catch(Ice.Exception ex)
                {
                    if(_childObserver != null)
                    {
                        _childObserver.failed(ex.ice_name());
                        _childObserver.detach();
                        _childObserver = null;
                    }
                    final int interval = handleException(ex);
                    if(interval > 0)
                    {
                        _instance.retryQueue().add(this, interval);
                        return;
                    }
                    else if(_observer != null)
                    {
                        _observer.retried();
                    }
                }
            }
        }
        catch(Ice.Exception ex)
        {
            //
            // If called from the user thread we re-throw, the exception
            // will be catch by the caller and abort() will be called.
            // 
            if(userThread) 
            {
                throw ex;
            }
            else if(finished(ex)) // No retries, we're done
            {
                invokeCompletedAsync();
            }
        }
    }

    @Override
    protected boolean sent(boolean done)
    {
        _sent = true;
        if(done)
        {
            if(_future != null)
            {
                _future.cancel(false);
                _future = null;
            }
        }
        return super.sent(done);
    }

    @Override
    protected boolean finished(Ice.Exception ex)
    {
        if(_future != null)
        {
            _future.cancel(false);
            _future = null;
        }
        return super.finished(ex);
    }

    @Override
    protected boolean finished(boolean ok)
    {
        if(_future != null)
        {
            _future.cancel(false);
            _future = null;
        }
        return super.finished(ok);
    }

    protected void handleRetryException(RetryException exc)
    {
        _proxy.__setRequestHandler(_handler, null); // Clear request handler and always retry.
    }
    
    protected int handleException(Ice.Exception exc)
    {
        Ice.Holder<Integer> interval = new Ice.Holder<Integer>();
        _cnt = _proxy.__handleException(exc, _handler, _mode, _sent, interval, _cnt);
        return interval.value;
    }
    
    final protected Ice.ObjectPrxHelperBase _proxy;
    protected RequestHandler _handler;
    protected Ice.OperationMode _mode;

    private java.util.concurrent.Future<?> _future;
    private int _cnt;
    private boolean _sent;
}