Files
UA-.NETStandard/Libraries/Opc.Ua.Client/DefaultSessionFactory.cs
T
Ivan van Heerden 1f2a30d7ca Add abstraction 'ISession' for Session object in the Client library (#1702)
Expose an 'ISession' interface as abstraction to ease unit testing. 

Breaking changes are kept to minimal impact, but some changes maybe required to use ISession objects instead of Session.

Note: ISessionClientMethods is hand created, not yet by ModelCompiler
2022-10-07 20:11:53 +02:00

167 lines
6.3 KiB
C#

/* ========================================================================
* Copyright (c) 2005-2022 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Opc.Ua.Client
{
/// <summary>
/// Object that creates instances of an Opc.Ua.Client.Session object.
/// </summary>
public class DefaultSessionFactory : ISessionFactory
{
#region Public Methods
/// <inheritdoc/>
public async Task<ISession> CreateAsync(
ApplicationConfiguration configuration,
ConfiguredEndpoint endpoint,
bool updateBeforeConnect,
string sessionName,
uint sessionTimeout,
IUserIdentity identity,
IList<string> preferredLocales)
{
return await Session.Create(configuration, endpoint, updateBeforeConnect, false,
sessionName, sessionTimeout, identity, preferredLocales).ConfigureAwait(false);
}
/// <inheritdoc/>
public async Task<ISession> CreateAsync(
ApplicationConfiguration configuration,
ConfiguredEndpoint endpoint,
bool updateBeforeConnect,
bool checkDomain,
string sessionName,
uint sessionTimeout,
IUserIdentity identity,
IList<string> preferredLocales)
{
return await Session.Create(configuration, null, endpoint,
updateBeforeConnect, checkDomain, sessionName, sessionTimeout,
identity, preferredLocales).ConfigureAwait(false);
}
/// <inheritdoc/>
public async Task<ISession> CreateAsync(
ApplicationConfiguration configuration,
ITransportWaitingConnection connection,
ConfiguredEndpoint endpoint,
bool updateBeforeConnect,
bool checkDomain,
string sessionName,
uint sessionTimeout,
IUserIdentity identity,
IList<string> preferredLocales)
{
return await Session.Create(configuration, connection, endpoint,
updateBeforeConnect, checkDomain, sessionName, sessionTimeout,
identity, preferredLocales
).ConfigureAwait(false);
}
/// <inheritdoc/>
public async Task<ISession> CreateAsync(
ApplicationConfiguration configuration,
ReverseConnectManager reverseConnectManager,
ConfiguredEndpoint endpoint,
bool updateBeforeConnect,
bool checkDomain,
string sessionName,
uint sessionTimeout,
IUserIdentity userIdentity,
IList<string> preferredLocales,
CancellationToken ct = default
)
{
if (reverseConnectManager == null)
{
return await CreateAsync(configuration, endpoint, updateBeforeConnect,
checkDomain, sessionName, sessionTimeout, userIdentity, preferredLocales).ConfigureAwait(false);
}
ITransportWaitingConnection connection;
do
{
connection = await reverseConnectManager.WaitForConnection(
endpoint.EndpointUrl,
endpoint.ReverseConnect?.ServerUri,
ct).ConfigureAwait(false);
if (updateBeforeConnect)
{
await endpoint.UpdateFromServerAsync(
endpoint.EndpointUrl, connection,
endpoint.Description.SecurityMode,
endpoint.Description.SecurityPolicyUri).ConfigureAwait(false);
updateBeforeConnect = false;
connection = null;
}
} while (connection == null);
return await CreateAsync(
configuration,
connection,
endpoint,
false,
checkDomain,
sessionName,
sessionTimeout,
userIdentity,
preferredLocales).ConfigureAwait(false);
}
/// <inheritdoc/>
public Task<ISession> RecreateAsync(ISession sessionTemplate)
{
if (!(sessionTemplate is Session template))
{
throw new ArgumentOutOfRangeException(nameof(sessionTemplate), "The ISession provided is not of a supported type.");
}
return Task.FromResult((ISession)Session.Recreate(template));
}
/// <inheritdoc/>
public Task<ISession> RecreateAsync(ISession sessionTemplate, ITransportWaitingConnection connection)
{
if (!(sessionTemplate is Session template))
{
throw new ArgumentOutOfRangeException(nameof(sessionTemplate), "The ISession provided is not of a supported type");
}
return Task.FromResult((ISession)Session.Recreate(template, connection));
}
#endregion
}
}