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