VCloudServerConnectionTestV2.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using JsonRpcLite.Network;
  2. using JsonRpcLite.Rpc;
  3. using System;
  4. using Vinno.IUS.Common.Log;
  5. using Vinno.vCloud.Common.FIS.Helper;
  6. using WingInterfaceLibrary.Interface;
  7. namespace Vinno.vCloud.Common.FIS.Test
  8. {
  9. public class VCloudServerConnectionTestV2 : VCloudTestItem
  10. {
  11. private readonly string _vCloudServerConnectionTestError1 = "vCloudServerConnectionTestError1";
  12. private readonly string _vCloudServerConnectionTestError2 = "vCloudServerConnectionTestError2";
  13. private readonly string _prefix;
  14. private JsonRpcHttpClientEngine _clientEngine;
  15. private IVinnoServerService _vinnoServerService;
  16. public JsonRpcClient Client { get; private set; }
  17. public VCloudServerConnectionTestV2(VCloudServiceTest vCloudServiceTest, bool isUseHttps) : base("vCloudServerConnection", vCloudServiceTest)
  18. {
  19. _prefix = "http://";
  20. if (isUseHttps)
  21. {
  22. _prefix = "https://";
  23. }
  24. }
  25. public override VCloudServiceTestResult Execute()
  26. {
  27. //Check it can conncect to vCloud server
  28. var result = TimeoutExecute(CheckvCloudServerConnected);
  29. if (result == VCloudSubItemTestStatus.Timeout)
  30. {
  31. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, TimeoutError, "(001)");//ErrorCode=001
  32. }
  33. if (result == VCloudSubItemTestStatus.Failed)
  34. {
  35. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _vCloudServerConnectionTestError1, VCloudServiceTest.VCloudPort);
  36. }
  37. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestDone);
  38. }
  39. private VCloudSubItemTestStatus CheckvCloudServerConnected()
  40. {
  41. try
  42. {
  43. Client = new JsonRpcClient();
  44. _clientEngine = new JsonRpcHttpClientEngine($"{_prefix}{VCloudServiceTest.VCloudHost}:{VCloudServiceTest.VCloudPort}");
  45. Client.UseEngine(_clientEngine);
  46. _vinnoServerService = Client.CreateProxy<IVinnoServerService>(6000);
  47. var result = JsonRpcHelper.Echo(_vinnoServerService);
  48. if (result == null || result.Code != 0)
  49. {
  50. return VCloudSubItemTestStatus.Failed;
  51. }
  52. else
  53. {
  54. return VCloudSubItemTestStatus.Successful;
  55. }
  56. }
  57. catch (Exception ex)
  58. {
  59. Logger.WriteLineInfo($"vCloudServiceTest - CheckvCloudServerConnected Error :{ex}");
  60. return VCloudSubItemTestStatus.Failed;
  61. }
  62. }
  63. public void Close()
  64. {
  65. if (Client != null)
  66. {
  67. Client.Dispose();
  68. Client = null;
  69. _clientEngine = null;
  70. _vinnoServerService = null;
  71. }
  72. }
  73. }
  74. }