Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

121 rader
3.9 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MQTTnet.Client;
  7. using MQTTnet.Client.Options;
  8. using MQTTnet.Diagnostics;
  9. using MQTTnet.Server;
  10. using Newtonsoft.Json;
  11. namespace MQTTnet.TestApp.NetCore
  12. {
  13. public static class Program
  14. {
  15. public static void Main()
  16. {
  17. Console.WriteLine($"MQTTnet - TestApp.{TargetFrameworkInfoProvider.TargetFramework}");
  18. Console.WriteLine("1 = Start client");
  19. Console.WriteLine("2 = Start server");
  20. Console.WriteLine("3 = Start performance test");
  21. Console.WriteLine("4 = Start managed client");
  22. Console.WriteLine("5 = Start public broker test");
  23. Console.WriteLine("6 = Start server & client");
  24. Console.WriteLine("7 = Client flow test");
  25. Console.WriteLine("8 = Start performance test (client only)");
  26. Console.WriteLine("9 = Start server (no trace)");
  27. Console.WriteLine("a = Start QoS 2 benchmark");
  28. Console.WriteLine("b = Start QoS 1 benchmark");
  29. Console.WriteLine("c = Start QoS 0 benchmark");
  30. var pressedKey = Console.ReadKey(true);
  31. if (pressedKey.KeyChar == '1')
  32. {
  33. Task.Run(ClientTest.RunAsync);
  34. }
  35. else if (pressedKey.KeyChar == '2')
  36. {
  37. Task.Run(ServerTest.RunAsync);
  38. }
  39. else if (pressedKey.KeyChar == '3')
  40. {
  41. PerformanceTest.RunClientAndServer();
  42. return;
  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. }