Use core client utils instead of rolling our own code to discover endpoints.

This commit is contained in:
Erich Barnstedt 2017-08-04 12:40:25 +02:00
Родитель f82e731303
Коммит 2cab5b2b67
1 изменённых файлов: 1 добавлений и 71 удалений

Просмотреть файл

@ -226,7 +226,7 @@ namespace Opc.Ua.Publisher
/// </summary>
public static async Task EndpointConnect(Uri endpointUrl)
{
EndpointDescription selectedEndpoint = SelectUaTcpEndpoint(DiscoverEndpoints(m_configuration, endpointUrl, 10));
EndpointDescription selectedEndpoint = CoreClientUtils.SelectEndpoint(endpointUrl.AbsoluteUri, true, 10);
ConfiguredEndpoint configuredEndpoint = new ConfiguredEndpoint(selectedEndpoint.Server, EndpointConfiguration.Create(m_configuration));
configuredEndpoint.Update(selectedEndpoint);
@ -384,76 +384,6 @@ namespace Opc.Ua.Publisher
}
}
/// <summary>
/// Discovers all endpoints provided by an OPC UA server using a discovery client
/// </summary>
private static EndpointDescriptionCollection DiscoverEndpoints(ApplicationConfiguration config, Uri discoveryUrl, int timeout)
{
EndpointConfiguration configuration = EndpointConfiguration.Create(config);
configuration.OperationTimeout = timeout;
using (DiscoveryClient client = DiscoveryClient.Create(
discoveryUrl,
EndpointConfiguration.Create(config)))
{
try
{
EndpointDescriptionCollection endpoints = client.GetEndpoints(null);
return ReplaceLocalHostWithRemoteHost(endpoints, discoveryUrl);
}
catch (Exception e)
{
Trace("Could not fetch endpoints from url: " + discoveryUrl.ToString());
Trace("Reason = " + e.Message);
throw e;
}
}
}
/// <summary>
/// Replaces all instances of "LocalHost" in a collection of endpoint description with the real host name
/// </summary>
private static EndpointDescriptionCollection ReplaceLocalHostWithRemoteHost(EndpointDescriptionCollection endpoints, Uri discoveryUrl)
{
EndpointDescriptionCollection updatedEndpoints = endpoints;
foreach (EndpointDescription endpoint in updatedEndpoints)
{
endpoint.EndpointUrl = Utils.ReplaceLocalhost(endpoint.EndpointUrl, discoveryUrl.DnsSafeHost);
StringCollection updatedDiscoveryUrls = new StringCollection();
foreach (string url in endpoint.Server.DiscoveryUrls)
{
updatedDiscoveryUrls.Add(Utils.ReplaceLocalhost(url, discoveryUrl.DnsSafeHost));
}
endpoint.Server.DiscoveryUrls = updatedDiscoveryUrls;
}
return updatedEndpoints;
}
/// <summary>
/// Selects the UA TCP endpoint from an endpoint collection with the highest security settings offered
/// </summary>
private static EndpointDescription SelectUaTcpEndpoint(EndpointDescriptionCollection endpointCollection)
{
EndpointDescription bestEndpoint = null;
foreach (EndpointDescription endpoint in endpointCollection)
{
if (endpoint.TransportProfileUri == Profiles.UaTcpTransport)
{
if ((bestEndpoint == null) ||
(endpoint.SecurityLevel > bestEndpoint.SecurityLevel))
{
bestEndpoint = endpoint;
}
}
}
return bestEndpoint;
}
/// <summary>
/// Standard certificate validation callback
/// </summary>