< Summary

Information
Class: IceDiscovery.LocatorRegistryI
Assembly: IceDiscovery
File(s): /_/csharp/src/IceDiscovery/LocatorI.cs
Tag: 91_21789722663
Line coverage
94%
Covered lines: 63
Uncovered lines: 4
Coverable lines: 67
Total lines: 172
Line coverage: 94%
Branch coverage
96%
Covered branches: 31
Total branches: 32
Branch coverage: 96.8%
Method coverage
83%
Covered methods: 5
Total methods: 6
Method coverage: 83.3%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
setAdapterDirectProxy(...)100%22100%
setReplicatedAdapterDirectProxy(...)100%88100%
setServerProcessProxy(...)100%210%
findObject(...)90%10.221086.96%
findAdapter(...)100%1212100%

File(s)

/_/csharp/src/IceDiscovery/LocatorI.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3namespace IceDiscovery;
 4
 5internal class LocatorRegistryI : Ice.LocatorRegistryDisp_
 6{
 17    public
 18    LocatorRegistryI(Ice.Communicator com) =>
 19        _wellKnownProxy = Ice.ObjectPrxHelper.createProxy(com, "p").ice_locator(null)
 110            .ice_router(null).ice_collocationOptimized(true);
 11
 12    public override void setAdapterDirectProxy(string adapterId, Ice.ObjectPrx proxy, Ice.Current current)
 13    {
 114        lock (_mutex)
 15        {
 116            if (proxy != null)
 17            {
 118                _adapters[adapterId] = proxy;
 19            }
 20            else
 21            {
 122                _adapters.Remove(adapterId);
 23            }
 124        }
 125    }
 26
 27    public override void setReplicatedAdapterDirectProxy(
 28        string adapterId,
 29        string replicaGroupId,
 30        Ice.ObjectPrx proxy,
 31        Ice.Current current)
 32    {
 133        lock (_mutex)
 34        {
 135            if (proxy != null)
 36            {
 137                _adapters[adapterId] = proxy;
 138                if (!_replicaGroups.TryGetValue(replicaGroupId, out HashSet<string> adapterIds))
 39                {
 140                    adapterIds = new HashSet<string>();
 141                    _replicaGroups.Add(replicaGroupId, adapterIds);
 42                }
 143                adapterIds.Add(adapterId);
 44            }
 45            else
 46            {
 147                _adapters.Remove(adapterId);
 148                if (_replicaGroups.TryGetValue(replicaGroupId, out HashSet<string> adapterIds))
 49                {
 150                    adapterIds.Remove(adapterId);
 151                    if (adapterIds.Count == 0)
 52                    {
 153                        _replicaGroups.Remove(replicaGroupId);
 54                    }
 55                }
 56            }
 157        }
 158    }
 59
 60    public override void setServerProcessProxy(string id, Ice.ProcessPrx process, Ice.Current current)
 61    {
 062    }
 63
 64    internal Ice.ObjectPrx findObject(Ice.Identity id)
 65    {
 166        lock (_mutex)
 67        {
 168            if (id.name.Length == 0)
 69            {
 070                return null;
 71            }
 72
 173            Ice.ObjectPrx prx = _wellKnownProxy.ice_identity(id);
 74
 175            var adapterIds = new List<string>();
 176            foreach (KeyValuePair<string, HashSet<string>> entry in _replicaGroups)
 77            {
 78                try
 79                {
 180                    prx.ice_adapterId(entry.Key).ice_ping();
 181                    adapterIds.Add(entry.Key);
 182                }
 083                catch (Ice.Exception)
 84                {
 085                }
 86            }
 187            if (adapterIds.Count == 0)
 88            {
 189                foreach (KeyValuePair<string, Ice.ObjectPrx> entry in _adapters)
 90                {
 91                    try
 92                    {
 193                        prx.ice_adapterId(entry.Key).ice_ping();
 194                        adapterIds.Add(entry.Key);
 195                    }
 196                    catch (Ice.Exception)
 97                    {
 198                    }
 99                }
 100            }
 101
 1102            if (adapterIds.Count == 0)
 103            {
 1104                return null;
 105            }
 1106            Ice.UtilInternal.Collections.Shuffle(ref adapterIds);
 1107            return prx.ice_adapterId(adapterIds[0]);
 108        }
 1109    }
 110
 111    internal Ice.ObjectPrx findAdapter(string adapterId, out bool isReplicaGroup)
 112    {
 1113        lock (_mutex)
 114        {
 1115            if (_adapters.TryGetValue(adapterId, out Ice.ObjectPrx result))
 116            {
 1117                isReplicaGroup = false;
 1118                return result;
 119            }
 120
 1121            if (_replicaGroups.TryGetValue(adapterId, out HashSet<string> adapterIds))
 122            {
 1123                var endpoints = new List<Ice.Endpoint>();
 1124                foreach (string a in adapterIds)
 125                {
 1126                    if (!_adapters.TryGetValue(a, out Ice.ObjectPrx proxy))
 127                    {
 128                        continue; // TODO: Inconsistency
 129                    }
 130
 1131                    result ??= proxy;
 132
 1133                    endpoints.AddRange(proxy.ice_getEndpoints());
 134                }
 135
 1136                if (result != null)
 137                {
 1138                    isReplicaGroup = true;
 1139                    return result.ice_endpoints(endpoints.ToArray());
 140                }
 141            }
 142
 1143            isReplicaGroup = false;
 1144            return null;
 145        }
 1146    }
 147
 148    private readonly Ice.ObjectPrx _wellKnownProxy;
 1149    private readonly Dictionary<string, Ice.ObjectPrx> _adapters = new Dictionary<string, Ice.ObjectPrx>();
 1150    private readonly Dictionary<string, HashSet<string>> _replicaGroups = new Dictionary<string, HashSet<string>>();
 1151    private readonly object _mutex = new();
 152}
 153
 154internal class LocatorI : Ice.AsyncLocatorDisp_
 155{
 156    public LocatorI(LookupI lookup, Ice.LocatorRegistryPrx registry)
 157    {
 158        _lookup = lookup;
 159        _registry = registry;
 160    }
 161
 162    public override Task<Ice.ObjectPrx> findObjectByIdAsync(Ice.Identity id, Ice.Current current) =>
 163        _lookup.findObject(id);
 164
 165    public override Task<Ice.ObjectPrx> findAdapterByIdAsync(string adapterId, Ice.Current current) =>
 166        _lookup.findAdapter(adapterId);
 167
 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}