Optimized persisting of retained messages to avoid useless writes to the filesystem

This commit is contained in:
Christian Kratky
2017-11-08 14:30:13 +01:00
parent 7e48746944
commit d2e481719c
8 changed files with 155 additions and 28 deletions
@@ -88,10 +88,13 @@ namespace MQTTnet.TestApp.UniversalWindows
if (_mqttClient != null)
{
await _mqttClient.DisconnectAsync();
_mqttClient.ApplicationMessageReceived -= OnApplicationMessageReceived;
}
var factory = new MqttFactory();
_mqttClient = factory.CreateMqttClient();
_mqttClient.ApplicationMessageReceived += OnApplicationMessageReceived;
await _mqttClient.ConnectAsync(options);
}
catch (Exception exception)
@@ -100,6 +103,17 @@ namespace MQTTnet.TestApp.UniversalWindows
}
}
private async void OnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs eventArgs)
{
var item = $"Timestamp: {DateTime.Now:O} | Topic: {eventArgs.ApplicationMessage.Topic} | Payload: {Encoding.UTF8.GetString(eventArgs.ApplicationMessage.Payload)} | QoS: {eventArgs.ApplicationMessage.QualityOfServiceLevel}";
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
ReceivedMessages.Items.Add(item);
});
}
private async void Publish(object sender, RoutedEventArgs e)
{
if (_mqttClient == null)
@@ -332,9 +346,21 @@ namespace MQTTnet.TestApp.UniversalWindows
return;
}
JsonServerStorage storage = null;
if (ServerPersistRetainedMessages.IsChecked == true)
{
storage = new JsonServerStorage();
if (ServerClearRetainedMessages.IsChecked == true)
{
storage.Clear();
}
}
_mqttServer = new MqttFactory().CreateMqttServer(o =>
{
o.DefaultEndpointOptions.Port = int.Parse(ServerPort.Text);
o.Storage = storage;
});
await _mqttServer.StartAsync();
@@ -350,5 +376,10 @@ namespace MQTTnet.TestApp.UniversalWindows
await _mqttServer.StopAsync();
_mqttServer = null;
}
private void ClearReceivedMessages(object sender, RoutedEventArgs e)
{
ReceivedMessages.Items.Clear();
}
}
}