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

Program.cs 4.6 KiB

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