using System; using MQTTnet.Core.Adapter; using MQTTnet.Core.Client; using MQTTnet.Core.Serializer; using Microsoft.Extensions.Logging; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using MQTTnet.Implementations; using MQTTnet.Core.ManagedClient; using MQTTnet.Core.Server; using MQTTnet.Core.Channel; namespace MQTTnet { public class MqttFactory : IMqttCommunicationAdapterFactory, IMqttClientSesssionFactory, IMqttClientFactory, IMqttServerFactory { private readonly IServiceProvider _serviceProvider; public MqttFactory() : this(BuildServiceProvider()) { } public MqttFactory(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } public ILoggerFactory GetLoggerFactory() { return _serviceProvider.GetRequiredService(); } public IMqttCommunicationAdapter CreateClientCommunicationAdapter(IMqttClientOptions options) { var logger = _serviceProvider.GetRequiredService>(); return new MqttChannelCommunicationAdapter(CreateCommunicationChannel(options.ChannelOptions), CreateSerializer(options.ProtocolVersion), logger); } public IMqttCommunicationAdapter CreateServerCommunicationAdapter(IMqttCommunicationChannel channel) { var serializer = _serviceProvider.GetRequiredService(); var logger = _serviceProvider.GetRequiredService>(); return new MqttChannelCommunicationAdapter(channel, serializer, logger); } public IMqttCommunicationChannel CreateCommunicationChannel(IMqttClientChannelOptions options) { if (options == null) throw new ArgumentNullException(nameof(options)); switch (options) { case MqttClientTcpOptions tcpOptions: return CreateTcpChannel(tcpOptions); case MqttClientWebSocketOptions webSocketOptions: return CreateWebSocketChannel(webSocketOptions); default: throw new NotSupportedException(); } } public MqttTcpChannel CreateTcpChannel(MqttClientTcpOptions tcpOptions) { return new MqttTcpChannel(tcpOptions); } public MqttWebSocketChannel CreateWebSocketChannel(MqttClientWebSocketOptions webSocketOptions) { return new MqttWebSocketChannel(webSocketOptions); } public MqttPacketSerializer CreateSerializer(MqttProtocolVersion protocolVersion) { return new MqttPacketSerializer { ProtocolVersion = protocolVersion }; } public MqttClientSession CreateClientSession(string clientId, MqttClientSessionsManager clientSessionsManager) { return new MqttClientSession( clientId, _serviceProvider.GetRequiredService>(), clientSessionsManager, _serviceProvider.GetRequiredService(), _serviceProvider.GetRequiredService>(), _serviceProvider.GetRequiredService>(), _serviceProvider.GetRequiredService() ); } public IMqttClient CreateMqttClient() { return _serviceProvider.GetRequiredService(); } public IManagedMqttClient CreateManagedMqttClient() { return _serviceProvider.GetRequiredService(); } public IMqttServer CreateMqttServer() { return _serviceProvider.GetRequiredService(); } public IMqttServer CreateMqttServer(Action configure) { if (configure == null) throw new ArgumentNullException(nameof(configure)); var options = _serviceProvider.GetRequiredService>(); configure(options.Value); return _serviceProvider.GetRequiredService(); } private static IServiceProvider BuildServiceProvider() { var serviceProvider = new ServiceCollection() .AddMqttClient() .AddMqttServer() .AddLogging() .BuildServiceProvider(); serviceProvider.GetRequiredService() .AddMqttTrace(); return serviceProvider; } } }