| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | namespace IceDiscovery; |
| | 4 | |
|
| | 5 | | internal class LocatorRegistryI : Ice.LocatorRegistryDisp_ |
| | 6 | | { |
| | 7 | | public |
| | 8 | | LocatorRegistryI(Ice.Communicator com) => |
| | 9 | | _wellKnownProxy = Ice.ObjectPrxHelper.createProxy(com, "p").ice_locator(null) |
| | 10 | | .ice_router(null).ice_collocationOptimized(true); |
| | 11 | |
|
| | 12 | | public override Task |
| | 13 | | setAdapterDirectProxyAsync(string adapterId, Ice.ObjectPrx proxy, Ice.Current current) |
| | 14 | | { |
| | 15 | | lock (_mutex) |
| | 16 | | { |
| | 17 | | if (proxy != null) |
| | 18 | | { |
| | 19 | | _adapters[adapterId] = proxy; |
| | 20 | | } |
| | 21 | | else |
| | 22 | | { |
| | 23 | | _adapters.Remove(adapterId); |
| | 24 | | } |
| | 25 | | } |
| | 26 | | return Task.CompletedTask; |
| | 27 | | } |
| | 28 | |
|
| | 29 | | public override Task |
| | 30 | | setReplicatedAdapterDirectProxyAsync( |
| | 31 | | string adapterId, |
| | 32 | | string replicaGroupId, |
| | 33 | | Ice.ObjectPrx proxy, |
| | 34 | | Ice.Current current) |
| | 35 | | { |
| | 36 | | lock (_mutex) |
| | 37 | | { |
| | 38 | | if (proxy != null) |
| | 39 | | { |
| | 40 | | _adapters[adapterId] = proxy; |
| | 41 | | if (!_replicaGroups.TryGetValue(replicaGroupId, out HashSet<string> adapterIds)) |
| | 42 | | { |
| | 43 | | adapterIds = new HashSet<string>(); |
| | 44 | | _replicaGroups.Add(replicaGroupId, adapterIds); |
| | 45 | | } |
| | 46 | | adapterIds.Add(adapterId); |
| | 47 | | } |
| | 48 | | else |
| | 49 | | { |
| | 50 | | _adapters.Remove(adapterId); |
| | 51 | | if (_replicaGroups.TryGetValue(replicaGroupId, out HashSet<string> adapterIds)) |
| | 52 | | { |
| | 53 | | adapterIds.Remove(adapterId); |
| | 54 | | if (adapterIds.Count == 0) |
| | 55 | | { |
| | 56 | | _replicaGroups.Remove(replicaGroupId); |
| | 57 | | } |
| | 58 | | } |
| | 59 | | } |
| | 60 | | } |
| | 61 | | return Task.CompletedTask; |
| | 62 | | } |
| | 63 | |
|
| | 64 | | public override Task |
| | 65 | | setServerProcessProxyAsync(string id, Ice.ProcessPrx process, Ice.Current current) => Task.CompletedTask; |
| | 66 | |
|
| | 67 | | internal Ice.ObjectPrx findObject(Ice.Identity id) |
| | 68 | | { |
| | 69 | | lock (_mutex) |
| | 70 | | { |
| | 71 | | if (id.name.Length == 0) |
| | 72 | | { |
| | 73 | | return null; |
| | 74 | | } |
| | 75 | |
|
| | 76 | | Ice.ObjectPrx prx = _wellKnownProxy.ice_identity(id); |
| | 77 | |
|
| | 78 | | var adapterIds = new List<string>(); |
| | 79 | | foreach (KeyValuePair<string, HashSet<string>> entry in _replicaGroups) |
| | 80 | | { |
| | 81 | | try |
| | 82 | | { |
| | 83 | | prx.ice_adapterId(entry.Key).ice_ping(); |
| | 84 | | adapterIds.Add(entry.Key); |
| | 85 | | } |
| | 86 | | catch (Ice.Exception) |
| | 87 | | { |
| | 88 | | } |
| | 89 | | } |
| | 90 | | if (adapterIds.Count == 0) |
| | 91 | | { |
| | 92 | | foreach (KeyValuePair<string, Ice.ObjectPrx> entry in _adapters) |
| | 93 | | { |
| | 94 | | try |
| | 95 | | { |
| | 96 | | prx.ice_adapterId(entry.Key).ice_ping(); |
| | 97 | | adapterIds.Add(entry.Key); |
| | 98 | | } |
| | 99 | | catch (Ice.Exception) |
| | 100 | | { |
| | 101 | | } |
| | 102 | | } |
| | 103 | | } |
| | 104 | |
|
| | 105 | | if (adapterIds.Count == 0) |
| | 106 | | { |
| | 107 | | return null; |
| | 108 | | } |
| | 109 | | Ice.UtilInternal.Collections.Shuffle(ref adapterIds); |
| | 110 | | return prx.ice_adapterId(adapterIds[0]); |
| | 111 | | } |
| | 112 | | } |
| | 113 | |
|
| | 114 | | internal Ice.ObjectPrx findAdapter(string adapterId, out bool isReplicaGroup) |
| | 115 | | { |
| | 116 | | lock (_mutex) |
| | 117 | | { |
| | 118 | | if (_adapters.TryGetValue(adapterId, out Ice.ObjectPrx result)) |
| | 119 | | { |
| | 120 | | isReplicaGroup = false; |
| | 121 | | return result; |
| | 122 | | } |
| | 123 | |
|
| | 124 | | if (_replicaGroups.TryGetValue(adapterId, out HashSet<string> adapterIds)) |
| | 125 | | { |
| | 126 | | var endpoints = new List<Ice.Endpoint>(); |
| | 127 | | foreach (string a in adapterIds) |
| | 128 | | { |
| | 129 | | if (!_adapters.TryGetValue(a, out Ice.ObjectPrx proxy)) |
| | 130 | | { |
| | 131 | | continue; // TODO: Inconsistency |
| | 132 | | } |
| | 133 | |
|
| | 134 | | result ??= proxy; |
| | 135 | |
|
| | 136 | | endpoints.AddRange(proxy.ice_getEndpoints()); |
| | 137 | | } |
| | 138 | |
|
| | 139 | | if (result != null) |
| | 140 | | { |
| | 141 | | isReplicaGroup = true; |
| | 142 | | return result.ice_endpoints(endpoints.ToArray()); |
| | 143 | | } |
| | 144 | | } |
| | 145 | |
|
| | 146 | | isReplicaGroup = false; |
| | 147 | | return null; |
| | 148 | | } |
| | 149 | | } |
| | 150 | |
|
| | 151 | | private readonly Ice.ObjectPrx _wellKnownProxy; |
| | 152 | | private readonly Dictionary<string, Ice.ObjectPrx> _adapters = new Dictionary<string, Ice.ObjectPrx>(); |
| | 153 | | private readonly Dictionary<string, HashSet<string>> _replicaGroups = new Dictionary<string, HashSet<string>>(); |
| | 154 | | private readonly object _mutex = new(); |
| | 155 | | } |
| | 156 | |
|
| | 157 | | internal class LocatorI : Ice.LocatorDisp_ |
| | 158 | | { |
| 1 | 159 | | public LocatorI(LookupI lookup, Ice.LocatorRegistryPrx registry) |
| | 160 | | { |
| 1 | 161 | | _lookup = lookup; |
| 1 | 162 | | _registry = registry; |
| 1 | 163 | | } |
| | 164 | |
|
| | 165 | | public override Task<Ice.ObjectPrx> |
| 1 | 166 | | findObjectByIdAsync(Ice.Identity id, Ice.Current current) => _lookup.findObject(id); |
| | 167 | |
|
| | 168 | | public override Task<Ice.ObjectPrx> |
| 1 | 169 | | findAdapterByIdAsync(string adapterId, Ice.Current current) => _lookup.findAdapter(adapterId); |
| | 170 | |
|
| 1 | 171 | | public override Ice.LocatorRegistryPrx getRegistry(Ice.Current current) => _registry; |
| | 172 | |
|
| | 173 | | private readonly LookupI _lookup; |
| | 174 | | private readonly Ice.LocatorRegistryPrx _registry; |
| | 175 | | } |