| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Diagnostics; |
| | 4 | |
|
| | 5 | | namespace Ice.Internal; |
| | 6 | |
|
| | 7 | | public sealed class RouterInfo : IEquatable<RouterInfo> |
| | 8 | | { |
| | 9 | | public interface GetClientEndpointsCallback |
| | 10 | | { |
| | 11 | | void setEndpoints(EndpointI[] endpoints); |
| | 12 | |
|
| | 13 | | void setException(Ice.LocalException ex); |
| | 14 | | } |
| | 15 | |
|
| | 16 | | public interface AddProxyCallback |
| | 17 | | { |
| | 18 | | void addedProxy(); |
| | 19 | |
|
| | 20 | | void setException(Ice.LocalException ex); |
| | 21 | | } |
| | 22 | |
|
| 1 | 23 | | internal RouterInfo(Ice.RouterPrx router) |
| | 24 | | { |
| 1 | 25 | | _router = router; |
| | 26 | |
|
| | 27 | | Debug.Assert(_router != null); |
| 1 | 28 | | } |
| | 29 | |
|
| | 30 | | public void destroy() |
| | 31 | | { |
| 1 | 32 | | lock (_mutex) |
| | 33 | | { |
| 1 | 34 | | _clientEndpoints = []; |
| 1 | 35 | | _adapter = null; |
| 1 | 36 | | _identities.Clear(); |
| 1 | 37 | | } |
| 1 | 38 | | } |
| | 39 | |
|
| 1 | 40 | | public static bool operator ==(RouterInfo lhs, RouterInfo rhs) => lhs is not null ? lhs.Equals(rhs) : rhs is null; |
| | 41 | |
|
| 1 | 42 | | public static bool operator !=(RouterInfo lhs, RouterInfo rhs) => !(lhs == rhs); |
| | 43 | |
|
| | 44 | | public bool Equals(RouterInfo other) => |
| 1 | 45 | | ReferenceEquals(this, other) || (other is not null && _router.Equals(other._router)); |
| | 46 | |
|
| 0 | 47 | | public override bool Equals(object obj) => Equals(obj as RouterInfo); |
| | 48 | |
|
| 0 | 49 | | public override int GetHashCode() => _router.GetHashCode(); |
| | 50 | |
|
| | 51 | | public Ice.RouterPrx getRouter() => |
| | 52 | | // |
| | 53 | | // No mutex lock necessary, _router is immutable. |
| | 54 | | // |
| 1 | 55 | | _router; |
| | 56 | |
|
| | 57 | | public EndpointI[] getClientEndpoints() |
| | 58 | | { |
| 1 | 59 | | lock (_mutex) |
| | 60 | | { |
| 1 | 61 | | if (_clientEndpoints != null) // Lazy initialization. |
| | 62 | | { |
| 1 | 63 | | return _clientEndpoints; |
| | 64 | | } |
| 1 | 65 | | } |
| | 66 | |
|
| 1 | 67 | | ObjectPrx proxy = _router.getClientProxy(out bool? hasRoutingTable); |
| 1 | 68 | | return setClientEndpoints(proxy, hasRoutingTable ?? true); |
| 1 | 69 | | } |
| | 70 | |
|
| | 71 | | public void getClientEndpoints(GetClientEndpointsCallback callback) |
| | 72 | | { |
| 1 | 73 | | EndpointI[] clientEndpoints = null; |
| 1 | 74 | | lock (_mutex) |
| | 75 | | { |
| 1 | 76 | | clientEndpoints = _clientEndpoints; |
| 1 | 77 | | } |
| | 78 | |
|
| 1 | 79 | | if (clientEndpoints != null) // Lazy initialization. |
| | 80 | | { |
| 1 | 81 | | callback.setEndpoints(clientEndpoints); |
| 1 | 82 | | return; |
| | 83 | | } |
| 1 | 84 | | _ = performGetClientProxyAsync(_router); |
| | 85 | |
|
| | 86 | | async Task performGetClientProxyAsync(RouterPrx router) |
| | 87 | | { |
| | 88 | | try |
| | 89 | | { |
| 1 | 90 | | Router_GetClientProxyResult r = await router.getClientProxyAsync().ConfigureAwait(false); |
| 1 | 91 | | callback.setEndpoints(setClientEndpoints(r.returnValue, r.hasRoutingTable ?? true)); |
| 1 | 92 | | } |
| 1 | 93 | | catch (Ice.LocalException ex) |
| | 94 | | { |
| 1 | 95 | | callback.setException(ex); |
| 1 | 96 | | } |
| 0 | 97 | | catch (System.Exception ex) |
| | 98 | | { |
| | 99 | | Debug.Fail($"unexpected exception: {ex}"); |
| 0 | 100 | | throw; |
| | 101 | | } |
| 1 | 102 | | } |
| 1 | 103 | | } |
| | 104 | |
|
| | 105 | | public EndpointI[] getServerEndpoints() |
| | 106 | | { |
| 1 | 107 | | Ice.ObjectPrx serverProxy = _router.getServerProxy() ?? |
| 1 | 108 | | throw new NoEndpointException("Router::getServerProxy returned a null proxy."); |
| 1 | 109 | | serverProxy = serverProxy.ice_router(null); // The server proxy cannot be routed. |
| 1 | 110 | | return ((Ice.ObjectPrxHelperBase)serverProxy).iceReference().getEndpoints(); |
| | 111 | | } |
| | 112 | |
|
| | 113 | | public bool addProxy(Reference reference, AddProxyCallback callback) |
| | 114 | | { |
| 1 | 115 | | Identity identity = reference.getIdentity(); |
| | 116 | |
|
| 1 | 117 | | lock (_mutex) |
| | 118 | | { |
| 1 | 119 | | if (!_hasRoutingTable) |
| | 120 | | { |
| 0 | 121 | | return true; // The router implementation doesn't maintain a routing table. |
| | 122 | | } |
| 1 | 123 | | if (_identities.Contains(identity)) |
| | 124 | | { |
| | 125 | | // |
| | 126 | | // Only add the proxy to the router if it's not already in our local map. |
| | 127 | | // |
| 1 | 128 | | return true; |
| | 129 | | } |
| 1 | 130 | | } |
| 1 | 131 | | _ = performAddProxiesAsync(_router, reference); |
| 1 | 132 | | return false; |
| | 133 | |
|
| | 134 | | async Task performAddProxiesAsync(RouterPrx router, Reference reference) |
| | 135 | | { |
| | 136 | | try |
| | 137 | | { |
| 1 | 138 | | ObjectPrx[] evictedProxies = |
| 1 | 139 | | await router.addProxiesAsync([new ObjectPrxHelper(reference)]).ConfigureAwait(false); |
| 1 | 140 | | addAndEvictProxies(identity, evictedProxies); |
| 1 | 141 | | callback.addedProxy(); |
| 1 | 142 | | } |
| 0 | 143 | | catch (Ice.LocalException ex) |
| | 144 | | { |
| 0 | 145 | | callback.setException(ex); |
| 0 | 146 | | } |
| 0 | 147 | | catch (System.Exception ex) |
| | 148 | | { |
| | 149 | | Debug.Fail($"unexpected exception: {ex}"); |
| 0 | 150 | | throw; |
| | 151 | | } |
| 1 | 152 | | } |
| 1 | 153 | | } |
| | 154 | |
|
| | 155 | | public void setAdapter(Ice.ObjectAdapter adapter) |
| | 156 | | { |
| 1 | 157 | | lock (_mutex) |
| | 158 | | { |
| 1 | 159 | | _adapter = adapter; |
| 1 | 160 | | } |
| 1 | 161 | | } |
| | 162 | |
|
| | 163 | | public Ice.ObjectAdapter getAdapter() |
| | 164 | | { |
| 1 | 165 | | lock (_mutex) |
| | 166 | | { |
| 1 | 167 | | return _adapter; |
| | 168 | | } |
| 1 | 169 | | } |
| | 170 | |
|
| | 171 | | public void clearCache(Reference @ref) |
| | 172 | | { |
| 0 | 173 | | lock (_mutex) |
| | 174 | | { |
| 0 | 175 | | _identities.Remove(@ref.getIdentity()); |
| 0 | 176 | | } |
| 0 | 177 | | } |
| | 178 | |
|
| | 179 | | private EndpointI[] setClientEndpoints(Ice.ObjectPrx clientProxy, bool hasRoutingTable) |
| | 180 | | { |
| 1 | 181 | | lock (_mutex) |
| | 182 | | { |
| 1 | 183 | | if (_clientEndpoints is null) |
| | 184 | | { |
| 1 | 185 | | _hasRoutingTable = hasRoutingTable; |
| 1 | 186 | | _clientEndpoints = clientProxy is null ? |
| 1 | 187 | | ((ObjectPrxHelperBase)_router).iceReference().getEndpoints() : |
| 1 | 188 | | ((ObjectPrxHelperBase)clientProxy).iceReference().getEndpoints(); |
| | 189 | | } |
| 1 | 190 | | return _clientEndpoints; |
| | 191 | | } |
| 1 | 192 | | } |
| | 193 | |
|
| | 194 | | private void addAndEvictProxies(Identity identity, Ice.ObjectPrx[] evictedProxies) |
| | 195 | | { |
| 1 | 196 | | lock (_mutex) |
| | 197 | | { |
| | 198 | | // |
| | 199 | | // Check if the proxy hasn't already been evicted by a |
| | 200 | | // concurrent addProxies call. If it's the case, don't |
| | 201 | | // add it to our local map. |
| | 202 | | // |
| 1 | 203 | | int index = _evictedIdentities.IndexOf(identity); |
| 1 | 204 | | if (index >= 0) |
| | 205 | | { |
| 0 | 206 | | _evictedIdentities.RemoveAt(index); |
| | 207 | | } |
| | 208 | | else |
| | 209 | | { |
| | 210 | | // |
| | 211 | | // If we successfully added the proxy to the router, |
| | 212 | | // we add it to our local map. |
| | 213 | | // |
| 1 | 214 | | _identities.Add(identity); |
| | 215 | | } |
| | 216 | |
|
| | 217 | | // |
| | 218 | | // We also must remove whatever proxies the router evicted. |
| | 219 | | // |
| 1 | 220 | | for (int i = 0; i < evictedProxies.Length; ++i) |
| | 221 | | { |
| 0 | 222 | | if (!_identities.Remove(evictedProxies[i].ice_getIdentity())) |
| | 223 | | { |
| | 224 | | // |
| | 225 | | // It's possible for the proxy to not have been |
| | 226 | | // added yet in the local map if two threads |
| | 227 | | // concurrently call addProxies. |
| | 228 | | // |
| 0 | 229 | | _evictedIdentities.Add(evictedProxies[i].ice_getIdentity()); |
| | 230 | | } |
| | 231 | | } |
| 1 | 232 | | } |
| 1 | 233 | | } |
| | 234 | |
|
| | 235 | | private readonly Ice.RouterPrx _router; |
| | 236 | | private EndpointI[] _clientEndpoints; |
| | 237 | | private Ice.ObjectAdapter _adapter; |
| 1 | 238 | | private readonly HashSet<Ice.Identity> _identities = new HashSet<Ice.Identity>(); |
| 1 | 239 | | private readonly List<Ice.Identity> _evictedIdentities = new List<Ice.Identity>(); |
| | 240 | | private bool _hasRoutingTable; |
| 1 | 241 | | private readonly object _mutex = new(); |
| | 242 | | } |
| | 243 | |
|
| | 244 | | public sealed class RouterManager |
| | 245 | | { |
| | 246 | | internal RouterManager() => _table = new Dictionary<Ice.RouterPrx, RouterInfo>(); |
| | 247 | |
|
| | 248 | | internal void destroy() |
| | 249 | | { |
| | 250 | | lock (_mutex) |
| | 251 | | { |
| | 252 | | foreach (RouterInfo i in _table.Values) |
| | 253 | | { |
| | 254 | | i.destroy(); |
| | 255 | | } |
| | 256 | | _table.Clear(); |
| | 257 | | } |
| | 258 | | } |
| | 259 | |
|
| | 260 | | // Returns router info for a given router. Automatically creates the router info if it doesn't exist yet. |
| | 261 | | internal RouterInfo get(Ice.RouterPrx router) |
| | 262 | | { |
| | 263 | | if (router is null) |
| | 264 | | { |
| | 265 | | return null; |
| | 266 | | } |
| | 267 | |
|
| | 268 | | // Make sure router is not routed. |
| | 269 | | router = RouterPrxHelper.uncheckedCast(router.ice_router(null)); |
| | 270 | |
|
| | 271 | | lock (_mutex) |
| | 272 | | { |
| | 273 | | if (!_table.TryGetValue(router, out RouterInfo info)) |
| | 274 | | { |
| | 275 | | info = new RouterInfo(router); |
| | 276 | | _table.Add(router, info); |
| | 277 | | } |
| | 278 | |
|
| | 279 | | return info; |
| | 280 | | } |
| | 281 | | } |
| | 282 | |
|
| | 283 | | internal void erase(RouterPrx router) |
| | 284 | | { |
| | 285 | | Debug.Assert(router.ice_getRouter() is null); // The router proxy cannot be routed. |
| | 286 | | lock (_mutex) |
| | 287 | | { |
| | 288 | | _table.Remove(router); |
| | 289 | | } |
| | 290 | | } |
| | 291 | |
|
| | 292 | | private readonly Dictionary<Ice.RouterPrx, RouterInfo> _table; |
| | 293 | | private readonly object _mutex = new(); |
| | 294 | | } |