Added IMqttRetainedMessagesManager

* Added interface IMqttRetainedMessagesManager to allow for different
retained messages manager implementations. The interface copies almost
exactly the current MqttRetainedMessagesManager implementation.

* Added IMqttRetainedMessagesManager.Start() to configure the provided
IMqttRetainedMessagesManager outside the constructor. This method returns
Task because some implementations that use external storage may be slow on
initialization (e.g., connect to a database).

* Modified MqttRetainedMessagesManager to implement new interface.
This commit is contained in:
Federico Di Gregorio
2019-12-17 11:57:17 +01:00
parent 624fafc662
commit f949d54aef
9 changed files with 64 additions and 33 deletions
@@ -7,20 +7,21 @@ using MQTTnet.Internal;
namespace MQTTnet.Server
{
public class MqttRetainedMessagesManager
public class MqttRetainedMessagesManager : IMqttRetainedMessagesManager
{
private readonly byte[] _emptyArray = new byte[0];
private readonly AsyncLock _messagesLock = new AsyncLock();
private readonly Dictionary<string, MqttApplicationMessage> _messages = new Dictionary<string, MqttApplicationMessage>();
private readonly IMqttNetChildLogger _logger;
private readonly IMqttServerOptions _options;
private IMqttNetChildLogger _logger;
private IMqttServerOptions _options;
public MqttRetainedMessagesManager(IMqttServerOptions options, IMqttNetChildLogger logger)
public Task Start(IMqttServerOptions options, IMqttNetChildLogger logger)
{
if (logger == null) throw new ArgumentNullException(nameof(logger));
_logger = logger.CreateChildLogger(nameof(MqttRetainedMessagesManager));
_options = options ?? throw new ArgumentNullException(nameof(options));
return Task.CompletedTask;
}
public async Task LoadMessagesAsync()
@@ -128,7 +129,7 @@ namespace MQTTnet.Server
break;
}
}
return matchingRetainedMessages;
}