< Summary

Information
Class: IceDiscovery.PluginFactory
Assembly: IceDiscovery
File(s): /home/runner/work/ice/ice/csharp/src/IceDiscovery/PluginI.cs
Tag: 71_18251537082
Line coverage
100%
Covered lines: 5
Uncovered lines: 0
Coverable lines: 5
Total lines: 139
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage
100%
Covered methods: 2
Total methods: 2
Method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_pluginName()100%11100%
create(...)100%22100%

File(s)

/home/runner/work/ice/ice/csharp/src/IceDiscovery/PluginI.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using System.Text;
 4
 5namespace IceDiscovery;
 6
 7public sealed class PluginFactory : Ice.PluginFactory
 8{
 19    public string pluginName => "IceDiscovery";
 10
 11    public Ice.Plugin create(Ice.Communicator communicator, string name, string[] args)
 12    {
 113        if (name != pluginName)
 14        {
 115            throw new Ice.PluginInitializationException(
 116                $"The Discovery plug-in must be named '{pluginName}'.");
 17        }
 118        return new PluginI(communicator);
 19    }
 20}
 21
 22public sealed class PluginI : Ice.Plugin
 23{
 24    public
 25    PluginI(Ice.Communicator communicator) => _communicator = communicator;
 26
 27    public void initialize()
 28    {
 29        Ice.Properties properties = _communicator.getProperties();
 30
 31        bool ipv4 = properties.getIcePropertyAsInt("Ice.IPv4") > 0;
 32        bool preferIPv6 = properties.getIcePropertyAsInt("Ice.PreferIPv6Address") > 0;
 33        string address = properties.getIceProperty("IceDiscovery.Address");
 34        if (address.Length == 0)
 35        {
 36            address = ipv4 && !preferIPv6 ? "239.255.0.1" : "ff15::1";
 37        }
 38        int port = properties.getIcePropertyAsInt("IceDiscovery.Port");
 39        string intf = properties.getIceProperty("IceDiscovery.Interface");
 40
 41        if (properties.getIceProperty("IceDiscovery.Multicast.Endpoints").Length == 0)
 42        {
 43            var s = new StringBuilder();
 44            s.Append("udp -h \"").Append(address).Append("\" -p ").Append(port);
 45            if (intf.Length != 0)
 46            {
 47                s.Append(" --interface \"").Append(intf).Append('"');
 48            }
 49            properties.setProperty("IceDiscovery.Multicast.Endpoints", s.ToString());
 50        }
 51
 52        string lookupEndpoints = properties.getIceProperty("IceDiscovery.Lookup");
 53        if (lookupEndpoints.Length == 0)
 54        {
 55            int protocol = ipv4 && !preferIPv6 ? Ice.Internal.Network.EnableIPv4 : Ice.Internal.Network.EnableIPv6;
 56            List<string> interfaces = Ice.Internal.Network.getInterfacesForMulticast(intf, protocol);
 57            foreach (string p in interfaces)
 58            {
 59                if (p != interfaces[0])
 60                {
 61                    lookupEndpoints += ":";
 62                }
 63                lookupEndpoints += "udp -h \"" + address + "\" -p " + port + " --interface \"" + p + "\"";
 64            }
 65        }
 66
 67        if (properties.getIceProperty("IceDiscovery.Reply.Endpoints").Length == 0)
 68        {
 69            properties.setProperty(
 70                "IceDiscovery.Reply.Endpoints",
 71                "udp -h " + (intf.Length == 0 ? "*" : "\"" + intf + "\""));
 72        }
 73
 74        if (properties.getIceProperty("IceDiscovery.Locator.Endpoints").Length == 0)
 75        {
 76            properties.setProperty("IceDiscovery.Locator.AdapterId", Guid.NewGuid().ToString());
 77        }
 78
 79        _multicastAdapter = _communicator.createObjectAdapter("IceDiscovery.Multicast");
 80        _replyAdapter = _communicator.createObjectAdapter("IceDiscovery.Reply");
 81        _locatorAdapter = _communicator.createObjectAdapter("IceDiscovery.Locator");
 82
 83        //
 84        // Setup locator registry.
 85        //
 86        var locatorRegistry = new LocatorRegistryI(_communicator);
 87        Ice.LocatorRegistryPrx locatorRegistryPrx = Ice.LocatorRegistryPrxHelper.uncheckedCast(
 88            _locatorAdapter.addWithUUID(locatorRegistry));
 89
 90        Ice.ObjectPrx lookupPrx = Ice.ObjectPrxHelper.createProxy(
 91            _communicator,
 92            "IceDiscovery/Lookup -d:" + lookupEndpoints);
 93
 94        lookupPrx = lookupPrx.ice_router(null);
 95
 96        //
 97        // Add lookup and lookup reply Ice objects
 98        //
 99        var lookup = new LookupI(locatorRegistry, LookupPrxHelper.uncheckedCast(lookupPrx), properties);
 100        _multicastAdapter.add(lookup, Ice.Util.stringToIdentity("IceDiscovery/Lookup"));
 101
 102        _replyAdapter.addDefaultServant(new LookupReplyI(lookup), "");
 103        var id = new Ice.Identity("dummy", "");
 104        lookup.setLookupReply(LookupReplyPrxHelper.uncheckedCast(_replyAdapter.createProxy(id).ice_datagram()));
 105
 106        //
 107        // Setup locator on the communicator.
 108        //
 109        Ice.ObjectPrx loc;
 110        loc = _locatorAdapter.addWithUUID(
 111            new LocatorI(lookup, Ice.LocatorRegistryPrxHelper.uncheckedCast(locatorRegistryPrx)));
 112        _defaultLocator = _communicator.getDefaultLocator();
 113        _locator = Ice.LocatorPrxHelper.uncheckedCast(loc);
 114        _communicator.setDefaultLocator(_locator);
 115
 116        _multicastAdapter.activate();
 117        _replyAdapter.activate();
 118        _locatorAdapter.activate();
 119    }
 120
 121    public void destroy()
 122    {
 123        _multicastAdapter?.destroy();
 124        _replyAdapter?.destroy();
 125        _locatorAdapter?.destroy();
 126        if (_communicator.getDefaultLocator().Equals(_locator))
 127        {
 128            // Restore original default locator proxy, if the user didn't change it in the meantime
 129            _communicator.setDefaultLocator(_defaultLocator);
 130        }
 131    }
 132
 133    private readonly Ice.Communicator _communicator;
 134    private Ice.ObjectAdapter _multicastAdapter;
 135    private Ice.ObjectAdapter _replyAdapter;
 136    private Ice.ObjectAdapter _locatorAdapter;
 137    private Ice.LocatorPrx _locator;
 138    private Ice.LocatorPrx _defaultLocator;
 139}