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