//
// NostaleClientResolver.cs
//
// Copyright (c) František Boháček. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Concurrent;
using Microsoft.Extensions.DependencyInjection;
using NosSmooth.Comms.Data;
using NosSmooth.Core.Client;
namespace NosSmooth.Comms.Core;
///
/// Resolves s into s.
///
///
/// Clients will be connected in case the client is not registered yet.
/// If you wish to register the client yourself, use .
///
public class NostaleClientResolver
{
private readonly IServiceProvider _services;
private readonly ConcurrentDictionary _clients;
///
/// Initializes a new instance of the class.
///
/// The service provider.
public NostaleClientResolver(IServiceProvider services)
{
_services = services;
_clients = new ConcurrentDictionary();
}
///
/// Resolve the connection handler into nostale client.
///
/// The connection.
/// The resolved client.
public INostaleClient Resolve(ConnectionHandler connection)
{
if (!_clients.ContainsKey(connection.Connection))
{
RegisterClient(connection, ActivatorUtilities.CreateInstance(_services, connection));
}
return _clients[connection.Connection];
}
///
/// Register the given client for the given connection.
///
/// The connection handler.
/// The client to register for the given handler.
public void RegisterClient(ConnectionHandler connection, INostaleClient client)
{
_clients[connection.Connection] = client;
}
}