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

108 行
4.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using MQTTnet.Client.Publishing;
  7. using MQTTnet.Client.Receiving;
  8. using MQTTnet.Server;
  9. using MQTTnet.Server.Status;
  10. namespace MQTTnet.Tests.Mockups
  11. {
  12. public class TestServerWrapper : IMqttServer
  13. {
  14. public TestServerWrapper(IMqttServer implementation, TestContext testContext, TestEnvironment testEnvironment)
  15. {
  16. Implementation = implementation;
  17. TestContext = testContext;
  18. TestEnvironment = testEnvironment;
  19. }
  20. public IMqttServer Implementation { get; }
  21. public TestContext TestContext { get; }
  22. public TestEnvironment TestEnvironment { get; }
  23. public IMqttServerStartedHandler StartedHandler { get => Implementation.StartedHandler; set => Implementation.StartedHandler = value; }
  24. public IMqttServerStoppedHandler StoppedHandler { get => Implementation.StoppedHandler; set => Implementation.StoppedHandler = value; }
  25. public IMqttServerClientConnectedHandler ClientConnectedHandler { get => Implementation.ClientConnectedHandler; set => Implementation.ClientConnectedHandler = value; }
  26. public IMqttServerClientDisconnectedHandler ClientDisconnectedHandler { get => Implementation.ClientDisconnectedHandler; set => Implementation.ClientDisconnectedHandler = value; }
  27. public IMqttServerClientSubscribedTopicHandler ClientSubscribedTopicHandler { get => Implementation.ClientSubscribedTopicHandler; set => Implementation.ClientSubscribedTopicHandler = value; }
  28. public IMqttServerClientUnsubscribedTopicHandler ClientUnsubscribedTopicHandler { get => Implementation.ClientUnsubscribedTopicHandler; set => Implementation.ClientUnsubscribedTopicHandler = value; }
  29. public IMqttServerOptions Options => Implementation.Options;
  30. public IMqttApplicationMessageReceivedHandler ApplicationMessageReceivedHandler { get => Implementation.ApplicationMessageReceivedHandler; set => Implementation.ApplicationMessageReceivedHandler = value; }
  31. public Task ClearRetainedApplicationMessagesAsync()
  32. {
  33. return Implementation.ClearRetainedApplicationMessagesAsync();
  34. }
  35. public Task<IList<IMqttClientStatus>> GetClientStatusAsync()
  36. {
  37. return Implementation.GetClientStatusAsync();
  38. }
  39. public Task<IList<MqttApplicationMessage>> GetRetainedApplicationMessagesAsync()
  40. {
  41. return Implementation.GetRetainedApplicationMessagesAsync();
  42. }
  43. public Task<IList<IMqttSessionStatus>> GetSessionStatusAsync()
  44. {
  45. return Implementation.GetSessionStatusAsync();
  46. }
  47. public Task<MqttClientPublishResult> PublishAsync(MqttApplicationMessage applicationMessage, CancellationToken cancellationToken)
  48. {
  49. return Implementation.PublishAsync(applicationMessage, cancellationToken);
  50. }
  51. public Task StartAsync(IMqttServerOptions options)
  52. {
  53. switch (options)
  54. {
  55. case MqttServerOptionsBuilder builder:
  56. if (builder.Build().ConnectionValidator == null)
  57. {
  58. builder.WithConnectionValidator(ConnectionValidator);
  59. }
  60. break;
  61. case MqttServerOptions op:
  62. if (op.ConnectionValidator == null)
  63. {
  64. op.ConnectionValidator = new MqttServerConnectionValidatorDelegate(ConnectionValidator);
  65. }
  66. break;
  67. default:
  68. break;
  69. }
  70. return Implementation.StartAsync(options);
  71. }
  72. public void ConnectionValidator(MqttConnectionValidatorContext ctx)
  73. {
  74. if (!ctx.ClientId.StartsWith(TestContext.TestName))
  75. {
  76. TestEnvironment.TrackException(new InvalidOperationException($"invalid client connected '{ctx.ClientId}'"));
  77. ctx.ReasonCode = Protocol.MqttConnectReasonCode.ClientIdentifierNotValid;
  78. }
  79. }
  80. public Task StopAsync()
  81. {
  82. return Implementation.StopAsync();
  83. }
  84. public Task SubscribeAsync(string clientId, ICollection<TopicFilter> topicFilters)
  85. {
  86. return Implementation.SubscribeAsync(clientId, topicFilters);
  87. }
  88. public Task UnsubscribeAsync(string clientId, ICollection<string> topicFilters)
  89. {
  90. return Implementation.UnsubscribeAsync(clientId, topicFilters);
  91. }
  92. }
  93. }