summaryrefslogtreecommitdiff
path: root/java/src/IceInternal/CommunicatorBatchOutgoingAsync.java
blob: 8d2d2be737ca481452c85a983d78480026f41416 (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
// **********************************************************************
//
// Copyright (c) 2003-2012 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;

public class CommunicatorBatchOutgoingAsync extends BatchOutgoingAsync
{
    public CommunicatorBatchOutgoingAsync(Ice.Communicator communicator, Instance instance, String operation,
                                          CallbackBase callback)
    {
        super(communicator, instance, operation, callback);

        //
        // _useCount is initialized to 1 to prevent premature callbacks.
        // The caller must invoke ready() after all flush requests have
        // been initiated.
        //
        _useCount = 1;

        //
        // Assume all connections are flushed synchronously.
        //
        _sentSynchronously = true;
    }

    public void flushConnection(Ice.Connection con)
    {
        synchronized(_monitor)
        {
            ++_useCount;
        }
        con.begin_flushBatchRequests(_cb);
    }

    public void ready()
    {
        check(null, null, true);
    }

    private void completed(Ice.AsyncResult r)
    {
        Ice.Connection con = r.getConnection();
        assert(con != null);

        try
        {
            con.end_flushBatchRequests(r);
            assert(false); // completed() should only be called when an exception occurs.
        }
        catch(Ice.LocalException ex)
        {
            check(r, ex, false);
        }
    }

    private void sent(Ice.AsyncResult r)
    {
        check(r, null, r.sentSynchronously());
    }

    private void check(Ice.AsyncResult r, Ice.LocalException ex, boolean userThread)
    {
        boolean done = false;

        synchronized(_monitor)
        {
            assert(_useCount > 0);
            --_useCount;

            //
            // We report that the communicator flush request was sent synchronously
            // if all of the connection flush requests are sent synchronously.
            //
            if((r != null && !r.sentSynchronously()) || ex != null)
            {
                _sentSynchronously = false;
            }

            if(_useCount == 0)
            {
                done = true;
                _state |= Done | OK | Sent;
                _monitor.notifyAll();
            }
        }

        if(done)
        {
            //
            // sentSynchronously_ is immutable here.
            //
            if(!_sentSynchronously && userThread)
            {
                __sentAsync();
            }
            else
            {
                assert(_sentSynchronously == userThread); // sentSynchronously && !userThread is impossible.
                __sent();
            }
        }
    }

    private int _useCount;

    private Ice.Callback _cb = new Ice.Callback()
    {
        @Override
        public void completed(Ice.AsyncResult r)
        {
            CommunicatorBatchOutgoingAsync.this.completed(r);
        }

        @Override
        public void sent(Ice.AsyncResult r)
        {
            CommunicatorBatchOutgoingAsync.this.sent(r);
        }
    };
}