summaryrefslogtreecommitdiff
path: root/java/src/IceInternal/RouterInfo.java
blob: d9e6c6f683960583a3b53d10c061b734cb26ece0 (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
// **********************************************************************
//
// Copyright (c) 2003-2007 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 final class RouterInfo
{
    RouterInfo(Ice.RouterPrx router)
    {
        _router = router;

        assert(_router != null);
    }

    synchronized public void
    destroy()
    {
        _clientEndpoints = new EndpointI[0];
        _serverEndpoints = new EndpointI[0];
        _adapter = null;
        _identities.clear();
    }

    public boolean
    equals(java.lang.Object obj)
    {
        if(this == obj)
        {
            return true;
        }

        if(obj instanceof RouterInfo)
        {
            return _router.equals(((RouterInfo)obj)._router);
        }

        return false;
    }

    public Ice.RouterPrx
    getRouter()
    {
        //
        // No mutex lock necessary, _router is immutable.
        //
        return _router;
    }

    public synchronized EndpointI[]
    getClientEndpoints()
    {
        if(_clientEndpoints == null) // Lazy initialization.
        {
            Ice.ObjectPrx clientProxy = _router.getClientProxy();
            if(clientProxy == null)
            {
                //
                // If getClientProxy() return nil, use router endpoints.
                //
                _clientEndpoints = ((Ice.ObjectPrxHelperBase)_router).__reference().getEndpoints();
            }
            else
            {
                clientProxy = clientProxy.ice_router(null); // The client proxy cannot be routed.
            
                //
                // In order to avoid creating a new connection to the
                // router, we must use the same timeout as the already
                // existing connection.
                //
                try
                {
                    clientProxy = clientProxy.ice_timeout(_router.ice_getConnection().timeout());
                }
                catch(Ice.CollocationOptimizationException ex)
                {
                    // Ignore - collocated router.
                }

                _clientEndpoints = ((Ice.ObjectPrxHelperBase)clientProxy).__reference().getEndpoints();
            }
        }

        return _clientEndpoints;
    }

    public synchronized EndpointI[]
    getServerEndpoints()
    {
        if(_serverEndpoints == null) // Lazy initialization.
        {
            Ice.ObjectPrx serverProxy = _router.getServerProxy();
            if(serverProxy == null)
            {
                throw new Ice.NoEndpointException();
            }

            serverProxy = serverProxy.ice_router(null); // The server proxy cannot be routed.
            _serverEndpoints = ((Ice.ObjectPrxHelperBase)serverProxy).__reference().getEndpoints();
        }

        return _serverEndpoints;
    }

    public synchronized void
    addProxy(Ice.ObjectPrx proxy)
    {
        assert(proxy != null);

        if(!_identities.contains(proxy.ice_getIdentity()))
        {
            //
            // Only add the proxy to the router if it's not already in our local map.
            //
            Ice.ObjectPrx[] proxies = new Ice.ObjectPrx[1];
            proxies[0] = proxy;
            Ice.ObjectPrx[] evictedProxies = _router.addProxies(proxies);

            //
            // If we successfully added the proxy to the router, we add it to our local map.
            //
            _identities.add(proxy.ice_getIdentity());

            //
            // We also must remove whatever proxies the router evicted.
            //
            for(int i = 0; i < evictedProxies.length; ++i)
            {
                _identities.remove(evictedProxies[i].ice_getIdentity());
            }
        }
    }

    public synchronized void
    setAdapter(Ice.ObjectAdapter adapter)
    {
        _adapter = adapter;
    }

    public synchronized Ice.ObjectAdapter
    getAdapter()
    {
        return _adapter;
    }

    private final Ice.RouterPrx _router;
    private EndpointI[] _clientEndpoints;
    private EndpointI[] _serverEndpoints;
    private Ice.ObjectAdapter _adapter;
    private java.util.HashSet _identities = new java.util.HashSet();
}