-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding conventional listener and subscriber endpoint configuration to…
… MQTT. Closes GH-598
- Loading branch information
1 parent
35da768
commit 5d9cf21
Showing
2 changed files
with
68 additions
and
3 deletions.
There are no files selected for viewing
67 changes: 66 additions & 1 deletion
67
src/Transports/MQTT/Wolverine.MQTT/MqttTransportExpression.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,78 @@ | ||
using JasperFx.Core.Reflection; | ||
using Microsoft.Extensions.Options; | ||
using Wolverine.Configuration; | ||
using Wolverine.MQTT.Internals; | ||
|
||
namespace Wolverine.MQTT; | ||
|
||
public class MqttTransportExpression | ||
{ | ||
private readonly MqttTransport _transport; | ||
private readonly WolverineOptions _options; | ||
|
||
internal MqttTransportExpression(MqttTransport transport) | ||
internal MqttTransportExpression(MqttTransport transport, WolverineOptions options) | ||
{ | ||
_transport = transport; | ||
_options = options; | ||
} | ||
|
||
/// <summary> | ||
/// Apply a policy to all listening endpoints | ||
/// </summary> | ||
/// <param name="configure"></param> | ||
/// <returns></returns> | ||
public MqttTransportExpression ConfigureListeners(Action<MqttListenerConfiguration> configure) | ||
{ | ||
var policy = new LambdaEndpointPolicy<MqttTopic>((e, _) => | ||
{ | ||
if (e.Role == EndpointRole.System) | ||
{ | ||
return; | ||
} | ||
|
||
if (!e.IsListener) | ||
{ | ||
return; | ||
} | ||
|
||
var configuration = new MqttListenerConfiguration(e); | ||
configure(configuration); | ||
|
||
configuration!.As<IDelayedEndpointConfiguration>().Apply(); | ||
}); | ||
|
||
_options.Policies.Add(policy); | ||
|
||
return this.As<MqttTransportExpression>(); | ||
} | ||
|
||
/// <summary> | ||
/// Apply a policy to all MQTT subscribers | ||
/// </summary> | ||
/// <param name="configure"></param> | ||
/// <returns></returns> | ||
public MqttTransportExpression ConfigureSenders(Action<MqttSubscriberConfiguration> configure) | ||
{ | ||
var policy = new LambdaEndpointPolicy<MqttTopic>((e, _) => | ||
{ | ||
if (e.Role == EndpointRole.System) | ||
{ | ||
return; | ||
} | ||
|
||
if (!e.Subscriptions.Any()) | ||
{ | ||
return; | ||
} | ||
|
||
var configuration = new MqttSubscriberConfiguration(e); | ||
configure(configuration); | ||
|
||
configuration!.As<IDelayedEndpointConfiguration>().Apply(); | ||
}); | ||
|
||
_options.Policies.Add(policy); | ||
|
||
return this.As<MqttTransportExpression>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters