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