ServerConsole.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using H.Pipes;
  2. using InABox.Wpf.Console;
  3. using PRSServices;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Timers;
  10. using System.Windows;
  11. using System.Windows.Media;
  12. using System.Windows.Threading;
  13. using Comal.Classes;
  14. using H.Formatters;
  15. using InABox.Core;
  16. using InABox.Formatters;
  17. namespace PRSLicensing;
  18. public class LicensingConsole : InABox.Wpf.Console.Console
  19. {
  20. private PipeClient<string>? _client;
  21. private PRSService? _service;
  22. private readonly bool _monitoronly = true;
  23. public string ServiceName { get; private set; }
  24. private Timer? RefreshTimer;
  25. public LicensingConsole(string servicename, string description, bool monitoronly = true): base(description)
  26. {
  27. ServiceName = servicename;
  28. _monitoronly = monitoronly;
  29. }
  30. protected override void OnLoaded()
  31. {
  32. base.OnLoaded();
  33. _client = new PipeClient<string>(ServiceName, new MemoryPackFormatter<string>());
  34. _client.MessageReceived += (o, args) =>
  35. {
  36. Dispatcher.BeginInvoke(() =>
  37. {
  38. ConsoleControl.LoadLogEntry(args.Message ?? "");
  39. });
  40. };
  41. _client.Connected += (o, args) =>
  42. {
  43. Dispatcher.BeginInvoke(() => { ConsoleControl.Enabled = true; });
  44. };
  45. _client.Disconnected += (o, args) =>
  46. {
  47. Dispatcher.BeginInvoke(() => { ConsoleControl.Enabled = false; });
  48. if (RefreshTimer == null)
  49. {
  50. RefreshTimer = new Timer(1000);
  51. RefreshTimer.Elapsed += RefreshTimer_Elapsed;
  52. }
  53. RefreshTimer.Start();
  54. };
  55. _client.ExceptionOccurred += (o, args) =>
  56. {
  57. };
  58. if (!_client.IsConnecting)
  59. {
  60. _client.ConnectAsync();
  61. }
  62. if (!_monitoronly)
  63. {
  64. var timer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 3) };
  65. timer.Tick += (o, args) =>
  66. {
  67. timer.IsEnabled = false;
  68. _service = new PRSLicensingService(ServiceName);
  69. _service.Run(ServiceName);
  70. };
  71. timer.IsEnabled = true;
  72. }
  73. }
  74. private void RefreshTimer_Elapsed(object? sender, ElapsedEventArgs e)
  75. {
  76. if (_client is null) return;
  77. if (!_client.IsConnected)
  78. {
  79. if (!_client.IsConnecting)
  80. {
  81. _client.ConnectAsync();
  82. }
  83. }
  84. else
  85. {
  86. RefreshTimer?.Stop();
  87. }
  88. }
  89. protected override void OnClosing()
  90. {
  91. base.OnClosing();
  92. if (_monitoronly)
  93. {
  94. _client?.DisposeAsync().AsTask().Wait();
  95. RefreshTimer?.Stop();
  96. }
  97. else
  98. _service?.Halt();
  99. }
  100. protected override string GetLogDirectory()
  101. {
  102. return LicensingEngine.GetPath(ServiceName);
  103. }
  104. }