選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

125 行
4.5 KiB

  1. using System;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using Microsoft.Extensions.Logging;
  4. using Microsoft.Extensions.Options;
  5. using MQTTnet.Core.Adapter;
  6. using MQTTnet.Core.Channel;
  7. using MQTTnet.Core.Client;
  8. using MQTTnet.Core.ManagedClient;
  9. using MQTTnet.Core.Serializer;
  10. using MQTTnet.Core.Server;
  11. using MQTTnet.Implementations;
  12. namespace MQTTnet
  13. {
  14. public class MqttFactory : IMqttCommunicationAdapterFactory, IMqttClientSesssionFactory, IMqttClientFactory, IMqttServerFactory
  15. {
  16. private readonly IServiceProvider _serviceProvider;
  17. private static IServiceProvider BuildServiceProvider()
  18. {
  19. var serviceProvider = new ServiceCollection()
  20. .AddMqttClient()
  21. .AddMqttServer()
  22. .AddLogging()
  23. .BuildServiceProvider();
  24. serviceProvider.GetRequiredService<ILoggerFactory>()
  25. .AddMqttTrace();
  26. return serviceProvider;
  27. }
  28. public MqttFactory()
  29. : this(BuildServiceProvider())
  30. {
  31. }
  32. public MqttFactory(IServiceProvider serviceProvider)
  33. {
  34. _serviceProvider = serviceProvider;
  35. }
  36. public IMqttCommunicationAdapter CreateClientMqttCommunicationAdapter(IMqttClientOptions options)
  37. {
  38. var logger = _serviceProvider.GetRequiredService<ILogger<MqttChannelCommunicationAdapter>>();
  39. return new MqttChannelCommunicationAdapter(CreateMqttCommunicationChannel(options.ChannelOptions), CreateSerializer(options.ProtocolVersion), logger);
  40. }
  41. public IMqttCommunicationAdapter CreateServerMqttCommunicationAdapter(IMqttCommunicationChannel channel)
  42. {
  43. var serializer = _serviceProvider.GetRequiredService<IMqttPacketSerializer>();
  44. var logger = _serviceProvider.GetRequiredService<ILogger<MqttChannelCommunicationAdapter>>();
  45. return new MqttChannelCommunicationAdapter(channel, serializer, logger);
  46. }
  47. public IMqttCommunicationChannel CreateMqttCommunicationChannel(IMqttClientChannelOptions options)
  48. {
  49. if (options == null) throw new ArgumentNullException(nameof(options));
  50. switch (options)
  51. {
  52. case MqttClientTcpOptions tcpOptions:
  53. return CreateTcpChannel(tcpOptions);
  54. case MqttClientWebSocketOptions webSocketOptions:
  55. return CreateWebSocketChannel(webSocketOptions);
  56. default:
  57. throw new NotSupportedException();
  58. }
  59. }
  60. public MqttTcpChannel CreateTcpChannel(MqttClientTcpOptions tcpOptions)
  61. {
  62. return new MqttTcpChannel(tcpOptions);
  63. }
  64. public MqttWebSocketChannel CreateWebSocketChannel(MqttClientWebSocketOptions webSocketOptions)
  65. {
  66. return new MqttWebSocketChannel(webSocketOptions);
  67. }
  68. public MqttPacketSerializer CreateSerializer(MqttProtocolVersion protocolVersion)
  69. {
  70. return new MqttPacketSerializer
  71. {
  72. ProtocolVersion = protocolVersion
  73. };
  74. }
  75. public MqttClientSession CreateClientSession(string clientId, MqttClientSessionsManager clientSessionsManager)
  76. {
  77. return new MqttClientSession(
  78. clientId,
  79. _serviceProvider.GetRequiredService<IOptions<MqttServerOptions>>(),
  80. clientSessionsManager,
  81. _serviceProvider.GetRequiredService<MqttClientSubscriptionsManager>(),
  82. _serviceProvider.GetRequiredService<ILogger<MqttClientSession>>(),
  83. _serviceProvider.GetRequiredService<ILogger<MqttClientPendingMessagesQueue>>());
  84. }
  85. public IMqttClient CreateMqttClient()
  86. {
  87. return _serviceProvider.GetRequiredService<IMqttClient>();
  88. }
  89. public IManagedMqttClient CreateManagedMqttClient()
  90. {
  91. return _serviceProvider.GetRequiredService<ManagedMqttClient>();
  92. }
  93. public IMqttServer CreateMqttServer()
  94. {
  95. return _serviceProvider.GetRequiredService<IMqttServer>();
  96. }
  97. public IMqttServer CreateMqttServer(Action<MqttServerOptions> configure)
  98. {
  99. if (configure == null) throw new ArgumentNullException(nameof(configure));
  100. var options = _serviceProvider.GetRequiredService<IOptions<MqttServerOptions>>();
  101. configure(options.Value);
  102. return _serviceProvider.GetRequiredService<IMqttServer>();
  103. }
  104. }
  105. }