LicensingEngine.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using Comal.Classes;
  2. using Comal.Stores;
  3. using InABox.Clients;
  4. using InABox.Configuration;
  5. using InABox.Core;
  6. using InABox.Rpc;
  7. using InABox.Wpf.Reports;
  8. using PRS.Shared;
  9. using PRSServer;
  10. using PRSServices;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. namespace PRSLicensing;
  18. public class LicensingEngine : Engine<LicensingEngineProperties>
  19. {
  20. public override void Run()
  21. {
  22. Logger.Send(LogType.Information, "", "Starting..");
  23. if (string.IsNullOrWhiteSpace(Properties.Server))
  24. {
  25. Logger.Send(LogType.Error, "", "Server is blank!");
  26. return;
  27. }
  28. var transport = new RpcClientPipeTransport(DatabaseServerProperties.GetPipeName(Properties.Server, true));
  29. ClientFactory.SetClientType(typeof(RpcClient<>), Platform.WebEngine, Version, transport);
  30. CheckConnection();
  31. Logger.Send(LogType.Information, "", "Registering Classes");
  32. StoreUtils.RegisterClasses();
  33. CoreUtils.RegisterClasses();
  34. ComalUtils.RegisterClasses();
  35. PRSSharedUtils.RegisterClasses();
  36. ReportUtils.RegisterClasses();
  37. ConfigurationUtils.RegisterClasses();
  38. Logger.Send(LogType.Information, "", "Starting Listener on port " + Properties.ListenPort);
  39. }
  40. private void CheckConnection()
  41. {
  42. // Wait for server connection
  43. while (!Client.Ping())
  44. {
  45. Logger.Send(LogType.Error, "", "Database server unavailable. Trying again in 30 seconds...");
  46. Task.Delay(30_000).Wait();
  47. Logger.Send(LogType.Information, "", "Retrying connection...");
  48. }
  49. ClientFactory.SetBypass();
  50. }
  51. public override void Stop()
  52. {
  53. Logger.Send(LogType.Information, "", "Stopping");
  54. }
  55. }
  56. public class LicensingEngineProperties : ServerProperties
  57. {
  58. [ComboLookupEditor(typeof(LicensingDatabaseServerLookupGenerator))]
  59. [EditorSequence(1)]
  60. public string Server { get; set; }
  61. [IntegerEditor]
  62. [EditorSequence(2)]
  63. public int ListenPort { get; set; }
  64. public override ServerType Type()
  65. {
  66. return ServerType.Other;
  67. }
  68. }
  69. public class LicensingDatabaseServerLookupGenerator : LookupGenerator<LicensingEngineProperties>
  70. {
  71. public LicensingDatabaseServerLookupGenerator(LicensingEngineProperties[] items) : base(items)
  72. {
  73. }
  74. protected override void DoGenerateLookups()
  75. {
  76. var config = new LocalConfiguration<ServerSettings>(CoreUtils.GetCommonAppData("PRSServer"), "");
  77. var servers = config.LoadAll();
  78. foreach (var server in servers.Select(x => x.Value.CreateServer(x.Key)))
  79. {
  80. if (server.Type == ServerType.Database)
  81. {
  82. AddValue(server.Key, server.Name);
  83. }
  84. }
  85. }
  86. }