summaryrefslogtreecommitdiff
path: root/java/src/IceInternal/OutgoingConnectionFactory.java
blob: e2a04ee6ddcacf04c40075164352b182246fb081 (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
// **********************************************************************
//
// Copyright (c) 2001
// Mutable Realms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

package IceInternal;

public class OutgoingConnectionFactory
{
    public synchronized Connection
    create(Endpoint[] endpoints)
    {
        if(_instance == null)
        {
            throw new Ice.CommunicatorDestroyedException();
        }

        assert(endpoints.length > 0);

        //
        // Reap destroyed connections.
        //
        java.util.Iterator p = _connections.values().iterator();
        while(p.hasNext())
        {
            Connection connection = (Connection)p.next();
            if(connection.destroyed())
            {
                p.remove();
            }
        }

        //
        // Search for existing connections.
        //
	DefaultsAndOverrides defaultsAndOverrides = _instance.defaultsAndOverrides();
        for(int i = 0; i < endpoints.length; i++)
        {
	    Endpoint endpoint = endpoints[i];
	    if(defaultsAndOverrides.overrideTimeout)
	    {
		endpoint = endpoint.timeout(defaultsAndOverrides.overrideTimeoutValue);
	    }

            Connection connection = (Connection)_connections.get(endpoint);
            if(connection != null)
            {
                return connection;
            }
        }

        //
        // No connections exist, try to create one
        //
        TraceLevels traceLevels = _instance.traceLevels();
        Ice.Logger logger = _instance.logger();

        Connection connection = null;
        Ice.LocalException exception = null;
        for(int i = 0; i < endpoints.length; i++)
        {
  	    Endpoint endpoint = endpoints[i];
	    if(defaultsAndOverrides.overrideTimeout)
	    {
		endpoint = endpoint.timeout(defaultsAndOverrides.overrideTimeoutValue);
	    }
	    
	    try
            {
                Transceiver transceiver = endpoint.clientTransceiver();
                if(transceiver == null)
                {
                    Connector connector = endpoint.connector();
                    assert(connector != null);
                    transceiver = connector.connect(endpoint.timeout());
                    assert(transceiver != null);
                }
                connection = new Connection(_instance, transceiver, endpoint, null);
		connection.validate();
                connection.activate();
                _connections.put(endpoint, connection);
                break;
            }
            catch(Ice.LocalException ex)
            {
                exception = ex;
            }

            if(traceLevels.retry >= 2)
            {
                StringBuffer s = new StringBuffer();
                s.append("connection to endpoint failed");
                if(i < endpoints.length - 1)
                {
                    s.append(", trying next endpoint\n");
                }
                else
                {
                    s.append(" and no more endpoints to try\n");
                }
                s.append(exception.toString());
                logger.trace(traceLevels.retryCat, s.toString());
            }
        }

        if(connection == null)
        {
            assert(exception != null);
            throw exception;
        }

        return connection;
    }

    public synchronized void
    setRouter(Ice.RouterPrx router)
    {
        if(_instance == null)
        {
            throw new Ice.CommunicatorDestroyedException();
        }

        RouterInfo routerInfo = _instance.routerManager().get(router);
        if(routerInfo != null)
        {
            //
            // Search for connections to the router's client proxy
            // endpoints, and update the object adapter for such
            // connections, so that callbacks from the router can be
            // received over such connections.
            //
            Ice.ObjectPrx proxy = routerInfo.getClientProxy();
            Ice.ObjectAdapter adapter = routerInfo.getAdapter();
	    DefaultsAndOverrides defaultsAndOverrides = _instance.defaultsAndOverrides();
            Endpoint[] endpoints = ((Ice.ObjectPrxHelper)proxy).__reference().endpoints;
            for(int i = 0; i < endpoints.length; i++)
            {
		Endpoint endpoint = endpoints[i];
		if(defaultsAndOverrides.overrideTimeout)
		{
		    endpoint = endpoint.timeout(defaultsAndOverrides.overrideTimeoutValue);
		}

                Connection connection = (Connection)_connections.get(endpoint);
                if(connection != null)
                {
                    connection.setAdapter(adapter);
                }
            }
        }
    }

    public synchronized void
    removeAdapter(Ice.ObjectAdapter adapter)
    {
        if(_instance == null)
        {
            throw new Ice.CommunicatorDestroyedException();
        }

        java.util.Iterator p = _connections.values().iterator();
        while(p.hasNext())
        {
            Connection connection = (Connection)p.next();
            if(connection.getAdapter() == adapter)
            {
                connection.setAdapter(null);
            }
        }
    }

    //
    // Only for use by Instance.
    //
    OutgoingConnectionFactory(Instance instance)
    {
        _instance = instance;
    }

    protected void
    finalize()
        throws Throwable
    {
        assert(_instance == null);

        super.finalize();
    }

    public synchronized void
    destroy()
    {
        if(_instance == null)
        {
            return;
        }

        java.util.Iterator p = _connections.values().iterator();
        while(p.hasNext())
        {
            Connection connection = (Connection)p.next();
            connection.destroy(Connection.CommunicatorDestroyed);
        }
        _connections.clear();
        _instance = null;
    }

    private Instance _instance;
    private java.util.HashMap _connections = new java.util.HashMap();
}