You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

145 lines
4.7 KiB

  1. using MQTTnet.Client.Options;
  2. using MQTTnet.Diagnostics;
  3. using MQTTnet.Server;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Net.Security;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MQTTnet.TestApp.NetCore
  12. {
  13. public static class Program
  14. {
  15. public static void Main()
  16. {
  17. //MqttNetConsoleLogger.ForwardToConsole();
  18. Console.WriteLine($"MQTTnet - TestApp.{TargetFrameworkProvider.TargetFramework}");
  19. Console.WriteLine("1 = Start client");
  20. Console.WriteLine("2 = Start server");
  21. Console.WriteLine("3 = Start performance test");
  22. Console.WriteLine("4 = Start managed client");
  23. Console.WriteLine("5 = Start public broker test");
  24. Console.WriteLine("6 = Start server & client");
  25. Console.WriteLine("7 = Client flow test");
  26. Console.WriteLine("8 = Start performance test (client only)");
  27. Console.WriteLine("9 = Start server (no trace)");
  28. Console.WriteLine("a = Start QoS 2 benchmark");
  29. Console.WriteLine("b = Start QoS 1 benchmark");
  30. Console.WriteLine("c = Start QoS 0 benchmark");
  31. var pressedKey = Console.ReadKey(true);
  32. if (pressedKey.KeyChar == '1')
  33. {
  34. Task.Run(ClientTest.RunAsync);
  35. }
  36. else if (pressedKey.KeyChar == '2')
  37. {
  38. Task.Run(ServerTest.RunAsync);
  39. }
  40. else if (pressedKey.KeyChar == '3')
  41. {
  42. Task.Run(PerformanceTest.RunClientAndServer);
  43. }
  44. else if (pressedKey.KeyChar == '4')
  45. {
  46. Task.Run(ManagedClientTest.RunAsync);
  47. }
  48. else if (pressedKey.KeyChar == '5')
  49. {
  50. Task.Run(PublicBrokerTest.RunAsync);
  51. }
  52. else if (pressedKey.KeyChar == '6')
  53. {
  54. Task.Run(ServerAndClientTest.RunAsync);
  55. }
  56. else if (pressedKey.KeyChar == '7')
  57. {
  58. Task.Run(ClientFlowTest.RunAsync);
  59. }
  60. else if (pressedKey.KeyChar == '8')
  61. {
  62. PerformanceTest.RunClientOnly();
  63. return;
  64. }
  65. else if (pressedKey.KeyChar == '9')
  66. {
  67. ServerTest.RunEmptyServer();
  68. return;
  69. }
  70. else if (pressedKey.KeyChar == 'a')
  71. {
  72. Task.Run(PerformanceTest.RunQoS2Test);
  73. }
  74. else if (pressedKey.KeyChar == 'b')
  75. {
  76. Task.Run(PerformanceTest.RunQoS1Test);
  77. }
  78. else if (pressedKey.KeyChar == 'c')
  79. {
  80. Task.Run(PerformanceTest.RunQoS0Test);
  81. }
  82. Thread.Sleep(Timeout.Infinite);
  83. }
  84. }
  85. public class RetainedMessageHandler : IMqttServerStorage
  86. {
  87. private const string Filename = "C:\\MQTT\\RetainedMessages.json";
  88. public Task SaveRetainedMessagesAsync(IList<MqttApplicationMessage> messages)
  89. {
  90. var directory = Path.GetDirectoryName(Filename);
  91. if (!Directory.Exists(directory))
  92. {
  93. Directory.CreateDirectory(directory);
  94. }
  95. File.WriteAllText(Filename, JsonConvert.SerializeObject(messages));
  96. return Task.FromResult(0);
  97. }
  98. public Task<IList<MqttApplicationMessage>> LoadRetainedMessagesAsync()
  99. {
  100. IList<MqttApplicationMessage> retainedMessages;
  101. if (File.Exists(Filename))
  102. {
  103. var json = File.ReadAllText(Filename);
  104. retainedMessages = JsonConvert.DeserializeObject<List<MqttApplicationMessage>>(json);
  105. }
  106. else
  107. {
  108. retainedMessages = new List<MqttApplicationMessage>();
  109. }
  110. return Task.FromResult(retainedMessages);
  111. }
  112. }
  113. public class WikiCode
  114. {
  115. public void Code()
  116. {
  117. //Validate certificate.
  118. var options = new MqttClientOptionsBuilder()
  119. .WithTls(new MqttClientOptionsBuilderTlsParameters
  120. {
  121. CertificateValidationHandler = context =>
  122. {
  123. // TODO: Check conditions of certificate by using above context.
  124. if (context.SslPolicyErrors == SslPolicyErrors.None)
  125. {
  126. return true;
  127. }
  128. return false;
  129. }
  130. })
  131. .Build();
  132. }
  133. }
  134. }