vCloudServerSelector.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using JsonRpcLite.Network;
  2. using JsonRpcLite.Rpc;
  3. using System;
  4. using System.Diagnostics;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Vinno.IUS.Common.Log;
  8. using Vinno.IUS.Common.Network.Leaf;
  9. using Vinno.IUS.Common.Network.Tcp;
  10. using Vinno.vCloud.Common.FIS.Helper;
  11. using WingInterfaceLibrary.Interface;
  12. namespace Vinno.vCloud.Common.FIS
  13. {
  14. public enum ServerTestResult
  15. {
  16. NotSure,
  17. OldServer,
  18. NewServerWithHttp,
  19. NewServerWithHttps
  20. }
  21. public class vCloudServerSelector : IDisposable
  22. {
  23. private readonly ManualResetEvent _manualResetEvent = new ManualResetEvent(false);
  24. private readonly string _prefix;
  25. private readonly string _host;
  26. private readonly int _port;
  27. private ClientLeaf _leaf;
  28. private JsonRpcClient _clientForHttp;
  29. private JsonRpcClient _clientForHttps;
  30. private ServerTestResult _testResult;
  31. private CancellationTokenSource _cts;
  32. public vCloudServerSelector(string host, int port)
  33. {
  34. _host = host;
  35. _port = port;
  36. }
  37. public ServerTestResult Select()
  38. {
  39. _testResult = ServerTestResult.NotSure;
  40. _cts = new CancellationTokenSource();
  41. _manualResetEvent.Reset();
  42. try
  43. {
  44. Task.Run(CheckLeaf);
  45. Task.Run(() => CheckJsonRpc(false));
  46. Task.Run(() => CheckJsonRpc(true));
  47. if (_manualResetEvent.WaitOne(5000))
  48. {
  49. Logger.WriteLineInfo($"vCloudServerSelector TestResult is {_testResult}");
  50. }
  51. else
  52. {
  53. Logger.WriteLineError($"vCloudServerSelector Test Timeout.The TestResult is {_testResult}");
  54. }
  55. _cts.Cancel();
  56. return _testResult;
  57. }
  58. catch (Exception ex)
  59. {
  60. Logger.WriteLineError($"vCloudServerSelector Test error:{ex}");
  61. _cts.Cancel();
  62. return _testResult;
  63. }
  64. }
  65. private void CheckLeaf()
  66. {
  67. var stopwatch = new Stopwatch();
  68. stopwatch.Start();
  69. var result = LeafTest();
  70. stopwatch.Stop();
  71. Logger.WriteLineInfo($"LeafTest 1st Time Cost {stopwatch.ElapsedMilliseconds}ms,result is {result}");
  72. if (_cts.IsCancellationRequested)
  73. {
  74. Logger.WriteLineInfo($"LeafTest 2nd Time Cancelled");
  75. return;
  76. }
  77. else if (!result)
  78. {
  79. stopwatch.Restart();
  80. result = LeafTest();
  81. stopwatch.Stop();
  82. Logger.WriteLineInfo($"LeafTest 2nd Time Cost {stopwatch.ElapsedMilliseconds}ms,result is {result}");
  83. }
  84. }
  85. private bool LeafTest()
  86. {
  87. try
  88. {
  89. _leaf?.Close();
  90. _leaf = null;
  91. var url = $"{_host}:{_port}";
  92. _leaf = new ClientLeaf(new LeafIdContext(), LeafMode.Dual, new TcpCreator(url));
  93. if (_cts.IsCancellationRequested)
  94. {
  95. _leaf?.Close();
  96. _leaf = null;
  97. return false;
  98. }
  99. else
  100. {
  101. if (_leaf.Online)
  102. {
  103. _testResult = ServerTestResult.OldServer;
  104. _manualResetEvent.Set();
  105. return true;
  106. }
  107. return false;
  108. }
  109. }
  110. catch
  111. {
  112. return false;
  113. }
  114. }
  115. private void CheckJsonRpc(bool isUseHttps)
  116. {
  117. var stopwatch = new Stopwatch();
  118. stopwatch.Start();
  119. var result = isUseHttps ? JsonRpcTestWithHttps() : JsonRpcTestWithHttp();
  120. stopwatch.Stop();
  121. if (isUseHttps)
  122. {
  123. Logger.WriteLineInfo($"JsonRpcTest For Https 1st Time Cost {stopwatch.ElapsedMilliseconds}ms,result is {result}");
  124. }
  125. else
  126. {
  127. Logger.WriteLineInfo($"JsonRpcTest For Http 1st Time Cost {stopwatch.ElapsedMilliseconds}ms,result is {result}");
  128. }
  129. if (_cts.IsCancellationRequested)
  130. {
  131. if (isUseHttps)
  132. {
  133. Logger.WriteLineInfo($"JsonRpcTest For Https 2nd Time Cancelled");
  134. return;
  135. }
  136. else
  137. {
  138. Logger.WriteLineInfo($"JsonRpcTest For Http 2nd Time Cancelled");
  139. return;
  140. }
  141. }
  142. else if (!result)
  143. {
  144. stopwatch.Restart();
  145. result = isUseHttps ? JsonRpcTestWithHttps() : JsonRpcTestWithHttp();
  146. stopwatch.Stop();
  147. if (isUseHttps)
  148. {
  149. Logger.WriteLineInfo($"JsonRpcTest For Https 2nd Time Cost {stopwatch.ElapsedMilliseconds}ms,result is {result}");
  150. }
  151. else
  152. {
  153. Logger.WriteLineInfo($"JsonRpcTest For Http 2nd Time Cost {stopwatch.ElapsedMilliseconds}ms,result is {result}");
  154. }
  155. }
  156. }
  157. private bool JsonRpcTestWithHttp()
  158. {
  159. try
  160. {
  161. _clientForHttp?.Dispose();
  162. _clientForHttp = null;
  163. var url = $"http://{_host}:{_port}";
  164. _clientForHttp = new JsonRpcClient();
  165. var clientEngine = new JsonRpcHttpClientEngine(url);
  166. _clientForHttp.UseEngine(clientEngine);
  167. var vinnoServerService = _clientForHttp.CreateProxy<IVinnoServerService>(5000);
  168. var echoResult = JsonRpcHelper.Echo(vinnoServerService);
  169. if (_cts.IsCancellationRequested)
  170. {
  171. _clientForHttp?.Dispose();
  172. _clientForHttp = null;
  173. return false;
  174. }
  175. else
  176. {
  177. if (echoResult != null && echoResult.Code == 0)
  178. {
  179. _testResult = ServerTestResult.NewServerWithHttp;
  180. _manualResetEvent.Set();
  181. return true;
  182. }
  183. return false;
  184. }
  185. }
  186. catch
  187. {
  188. return false;
  189. }
  190. }
  191. private bool JsonRpcTestWithHttps()
  192. {
  193. try
  194. {
  195. _clientForHttps?.Dispose();
  196. _clientForHttps = null;
  197. var url = $"https://{_host}:{_port}";
  198. _clientForHttps = new JsonRpcClient();
  199. var clientEngine = new JsonRpcHttpClientEngine(url);
  200. _clientForHttps.UseEngine(clientEngine);
  201. var vinnoServerService = _clientForHttps.CreateProxy<IVinnoServerService>(5000);
  202. var echoResult = JsonRpcHelper.Echo(vinnoServerService);
  203. if (_cts.IsCancellationRequested)
  204. {
  205. _clientForHttps?.Dispose();
  206. _clientForHttps = null;
  207. return false;
  208. }
  209. else
  210. {
  211. if (echoResult != null && echoResult.Code == 0)
  212. {
  213. _testResult = ServerTestResult.NewServerWithHttps;
  214. _manualResetEvent.Set();
  215. return true;
  216. }
  217. return false;
  218. }
  219. }
  220. catch
  221. {
  222. return false;
  223. }
  224. }
  225. public void Dispose()
  226. {
  227. try
  228. {
  229. _manualResetEvent.Set();
  230. _cts?.Cancel();
  231. _clientForHttp?.Dispose();
  232. _clientForHttp = null;
  233. _clientForHttps?.Dispose();
  234. _clientForHttps = null;
  235. _leaf?.Close();
  236. _leaf = null;
  237. }
  238. catch (Exception ex)
  239. {
  240. Logger.WriteLineError($"vCloudServerSelector Dispose Error:{ex}");
  241. }
  242. }
  243. }
  244. }