Add interfaces for factories

This commit is contained in:
Christian Kratky
2017-09-19 23:04:42 +02:00
parent 51e7cd725e
commit 651f0aa15c
10 changed files with 84 additions and 12 deletions
@@ -7,7 +7,7 @@ using MQTTnet.Implementations;
namespace MQTTnet
{
public class MqttClientFactory
public class MqttClientFactory : IMqttClientFactory
{
public IMqttClient CreateMqttClient(MqttClientOptions options)
{
@@ -21,9 +21,9 @@ namespace MQTTnet
switch (options.ConnectionType)
{
case MqttConnectionType.Tcp:
return new MqttTcpChannel();
return new MqttTcpChannel();
case MqttConnectionType.Ws:
return new MqttWebSocketChannel();
return new MqttWebSocketChannel();
default:
throw new NotSupportedException();
@@ -6,7 +6,7 @@ using MQTTnet.Implementations;
namespace MQTTnet
{
public class MqttServerFactory
public class MqttServerFactory : IMqttServerFactory
{
public IMqttServer CreateMqttServer(MqttServerOptions options)
{
@@ -1,13 +1,13 @@
using System;
using MQTTnet.Core.Adapter;
using MQTTnet.Core.Channel;
using MQTTnet.Core.Client;
using MQTTnet.Core.Serializer;
using MQTTnet.Implementations;
using MQTTnet.Core.Channel;
namespace MQTTnet
{
public class MqttClientFactory
public class MqttClientFactory : IMqttClientFactory
{
public IMqttClient CreateMqttClient(MqttClientOptions options)
{
@@ -30,4 +30,4 @@ namespace MQTTnet
}
}
}
}
}
@@ -6,7 +6,7 @@ using MQTTnet.Implementations;
namespace MQTTnet
{
public class MqttServerFactory
public class MqttServerFactory : IMqttServerFactory
{
public IMqttServer CreateMqttServer(MqttServerOptions options)
{
@@ -113,8 +113,8 @@
<Compile Include="Implementations\MqttWebSocketChannel.cs" />
<Compile Include="MqttClientFactory.cs" />
<Compile Include="Implementations\MqttServerAdapter.cs" />
<Compile Include="MqttServerFactory.cs" />
<Compile Include="Implementations\MqttTcpChannel.cs" />
<Compile Include="MqttServerFactory.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Implementations\WebSocketStream.cs" />
<EmbeddedResource Include="Properties\MQTTnet.Universal.rd.xml" />
@@ -127,7 +127,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
<Version>5.3.3</Version>
<Version>5.4.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
@@ -7,7 +7,7 @@ using MQTTnet.Implementations;
namespace MQTTnet
{
public class MqttClientFactory
public class MqttClientFactory : IMqttClientFactory
{
public IMqttClient CreateMqttClient(MqttClientOptions options)
{
@@ -6,7 +6,7 @@ using MQTTnet.Implementations;
namespace MQTTnet
{
public class MqttServerFactory
public class MqttServerFactory : IMqttServerFactory
{
public IMqttServer CreateMqttServer(MqttServerOptions options)
{
@@ -0,0 +1,7 @@
namespace MQTTnet.Core.Client
{
public interface IMqttClientFactory
{
IMqttClient CreateMqttClient(MqttClientOptions options);
}
}
@@ -0,0 +1,58 @@
using System;
using System.Globalization;
using System.IO;
using System.Text;
using MQTTnet.Core.Protocol;
namespace MQTTnet.Core
{
public class MqttApplicationMessageFactory
{
public MqttApplicationMessage CreateApplicationMessage(string topic, MqttQualityOfServiceLevel qualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce, bool retain = false)
{
if (topic == null) throw new ArgumentNullException(nameof(topic));
return CreateApplicationMessage(topic, new byte[0], qualityOfServiceLevel, retain);
}
public MqttApplicationMessage CreateApplicationMessage(string topic, byte[] payload, MqttQualityOfServiceLevel qualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce, bool retain = false)
{
if (topic == null) throw new ArgumentNullException(nameof(topic));
if (payload == null)
{
payload = new byte[0];
}
return new MqttApplicationMessage(topic, payload, qualityOfServiceLevel, retain);
}
public MqttApplicationMessage CreateApplicationMessage<TPayload>(string topic, TPayload payload, MqttQualityOfServiceLevel qualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce, bool retain = false)
{
if (topic == null) throw new ArgumentNullException(nameof(topic));
var payloadString = Convert.ToString(payload, CultureInfo.InvariantCulture);
var payloadBuffer = string.IsNullOrEmpty(payloadString) ? new byte[0] : Encoding.UTF8.GetBytes(payloadString);
return CreateApplicationMessage(topic, payloadBuffer, qualityOfServiceLevel, retain);
}
public MqttApplicationMessage CreateApplicationMessage(string topic, Stream payload, MqttQualityOfServiceLevel qualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce, bool retain = false)
{
if (topic == null) throw new ArgumentNullException(nameof(topic));
byte[] payloadBuffer;
if (payload == null || payload.Length == 0)
{
payloadBuffer = new byte[0];
}
else
{
payloadBuffer = new byte[payload.Length - payload.Position];
payload.Read(payloadBuffer, 0, payloadBuffer.Length);
}
return CreateApplicationMessage(topic, payloadBuffer, qualityOfServiceLevel, retain);
}
}
}
@@ -0,0 +1,7 @@
namespace MQTTnet.Core.Server
{
public interface IMqttServerFactory
{
IMqttServer CreateMqttServer(MqttServerOptions options);
}
}