VCloudServerConnectionTest.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using Vinno.IUS.Common.Log;
  2. using Vinno.IUS.Common.Network.Leaf;
  3. using Vinno.IUS.Common.Network.Tcp;
  4. using Vinno.IUS.Common.Network.Transfer;
  5. using Vinno.vCloud.Protocol.Messages.Terminal;
  6. namespace Vinno.vCloud.Common.FIS.Test
  7. {
  8. public class VCloudServerConnectionTest : VCloudTestItem
  9. {
  10. private readonly string _vCloudServerConnectionTestError1 = "vCloudServerConnectionTestError1";
  11. private readonly string _vCloudServerConnectionTestError2 = "vCloudServerConnectionTestError2";
  12. private readonly string _currentUsVersion;
  13. private string _requriedUsVersion;
  14. public ClientLeaf Leaf { get; private set; }
  15. public VCloudServerConnectionTest(VCloudServiceTest vCloudServiceTest, string currentUsVersion) : base("vCloudServerConnection", vCloudServiceTest)
  16. {
  17. _currentUsVersion = currentUsVersion;
  18. }
  19. public override VCloudServiceTestResult Execute()
  20. {
  21. //Check it can conncect to vCloud server
  22. var result = TimeoutExecute(CheckvCloudServerConnected);
  23. if (result == VCloudSubItemTestStatus.Timeout)
  24. {
  25. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, TimeoutError, "(001)");//ErrorCode=001
  26. }
  27. if (result == VCloudSubItemTestStatus.Failed)
  28. {
  29. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _vCloudServerConnectionTestError1, VCloudServiceTest.VCloudPort);
  30. }
  31. //Check current ultrasoic
  32. result = TimeoutExecute(CheckCurrentUsMachineVersion);
  33. if (result != VCloudSubItemTestStatus.Successful)
  34. {
  35. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _vCloudServerConnectionTestError2, _requriedUsVersion);
  36. }
  37. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestDone);
  38. }
  39. private VCloudSubItemTestStatus CheckCurrentUsMachineVersion()
  40. {
  41. if (string.IsNullOrWhiteSpace(_requriedUsVersion))
  42. {
  43. Logger.WriteLineError($"required US software version is empty");
  44. }
  45. if (string.IsNullOrWhiteSpace(_currentUsVersion))
  46. {
  47. Logger.WriteLineError($"Current US software version is empty");
  48. return VCloudSubItemTestStatus.Failed;
  49. }
  50. var minVersions = _requriedUsVersion.Split('.');
  51. var usVersions = _currentUsVersion.Split('.');
  52. if (minVersions.Length >= usVersions.Length)
  53. {
  54. for (int i = 0; i < minVersions.Length; i++)
  55. {
  56. int usVersion = 0;
  57. int minVersion = 0;
  58. if (usVersions.Length - 1 < i)
  59. {
  60. usVersion = 0;
  61. }
  62. else
  63. {
  64. int.TryParse(usVersions[i], out usVersion);
  65. }
  66. int.TryParse(minVersions[i], out minVersion);
  67. if (usVersion > minVersion)
  68. {
  69. return VCloudSubItemTestStatus.Successful;
  70. }
  71. else if (usVersion < minVersion)
  72. {
  73. return VCloudSubItemTestStatus.Failed;
  74. }
  75. }
  76. return VCloudSubItemTestStatus.Successful;
  77. }
  78. else
  79. {
  80. for (int i = 0; i < usVersions.Length; i++)
  81. {
  82. int usVersion = 0;
  83. int minVersion = 0;
  84. if (minVersions.Length - 1 < i)
  85. {
  86. minVersion = 0;
  87. }
  88. else
  89. {
  90. int.TryParse(minVersions[i], out minVersion);
  91. }
  92. int.TryParse(usVersions[i], out usVersion);
  93. if (usVersion > minVersion)
  94. {
  95. return VCloudSubItemTestStatus.Successful;
  96. }
  97. else if (usVersion < minVersion)
  98. {
  99. return VCloudSubItemTestStatus.Failed;
  100. }
  101. }
  102. return VCloudSubItemTestStatus.Successful;
  103. }
  104. }
  105. private VCloudSubItemTestStatus CheckvCloudServerConnected()
  106. {
  107. _requriedUsVersion = string.Empty;
  108. int port = VCloudServiceTest.VCloudPort;
  109. var serverAddress = VCloudServiceTest.VCloudHost;
  110. Leaf = new ClientLeaf(new LeafIdContext(), LeafMode.Dual, new TcpCreator(serverAddress, port));
  111. if (!Leaf.Online)
  112. {
  113. Leaf.Close();
  114. return VCloudSubItemTestStatus.Failed;
  115. }
  116. using (var request = MessagePool.GetMessage<TerminalConnectionTestRequest>())
  117. {
  118. request.TerminalName = VCloudServiceTest.TerminalName;
  119. var result = Leaf.Send(request);
  120. if (result == null)
  121. {
  122. return VCloudSubItemTestStatus.Failed;
  123. }
  124. var testResult = TerminalConnectionTestResult.Convert(result);
  125. if (testResult == null)
  126. {
  127. return VCloudSubItemTestStatus.Failed;
  128. }
  129. _requriedUsVersion = testResult.RequriedUsVersion;
  130. }
  131. Logger.WriteLineInfo($"vCloudServiceTest - Required US version is {_requriedUsVersion}");
  132. return VCloudSubItemTestStatus.Successful;
  133. }
  134. public void Close()
  135. {
  136. Leaf?.Close();
  137. Leaf = null;
  138. }
  139. }
  140. }