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