123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- using JsonRpcLite.Network;
- using JsonRpcLite.Rpc;
- using System;
- using System.Diagnostics;
- using System.Threading;
- using System.Threading.Tasks;
- using Vinno.IUS.Common.Log;
- using Vinno.IUS.Common.Network.Leaf;
- using Vinno.IUS.Common.Network.Tcp;
- using Vinno.vCloud.Common.FIS.Helper;
- using WingInterfaceLibrary.Interface;
- namespace Vinno.vCloud.Common.FIS
- {
- public enum ServerTestResult
- {
- NotSure,
- OldServer,
- NewServerWithHttp,
- NewServerWithHttps
- }
- public class vCloudServerSelector : IDisposable
- {
- private readonly ManualResetEvent _manualResetEvent = new ManualResetEvent(false);
- private readonly string _prefix;
- private readonly string _host;
- private readonly int _port;
- private ClientLeaf _leaf;
- private JsonRpcClient _clientForHttp;
- private JsonRpcClient _clientForHttps;
- private ServerTestResult _testResult;
- private CancellationTokenSource _cts;
- public vCloudServerSelector(string host, int port)
- {
- _host = host;
- _port = port;
- }
- public ServerTestResult Select()
- {
- _testResult = ServerTestResult.NotSure;
- _cts = new CancellationTokenSource();
- _manualResetEvent.Reset();
- try
- {
- Task.Run(CheckLeaf);
- Task.Run(() => CheckJsonRpc(false));
- Task.Run(() => CheckJsonRpc(true));
- if (_manualResetEvent.WaitOne(5000))
- {
- Logger.WriteLineInfo($"vCloudServerSelector TestResult is {_testResult}");
- }
- else
- {
- Logger.WriteLineError($"vCloudServerSelector Test Timeout.The TestResult is {_testResult}");
- }
- _cts.Cancel();
- return _testResult;
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"vCloudServerSelector Test error:{ex}");
- _cts.Cancel();
- return _testResult;
- }
- }
- private void CheckLeaf()
- {
- var stopwatch = new Stopwatch();
- stopwatch.Start();
- var result = LeafTest();
- stopwatch.Stop();
- Logger.WriteLineInfo($"LeafTest 1st Time Cost {stopwatch.ElapsedMilliseconds}ms,result is {result}");
- if (_cts.IsCancellationRequested)
- {
- Logger.WriteLineInfo($"LeafTest 2nd Time Cancelled");
- return;
- }
- else if (!result)
- {
- stopwatch.Restart();
- result = LeafTest();
- stopwatch.Stop();
- Logger.WriteLineInfo($"LeafTest 2nd Time Cost {stopwatch.ElapsedMilliseconds}ms,result is {result}");
- }
- }
- private bool LeafTest()
- {
- try
- {
- _leaf?.Close();
- _leaf = null;
- var url = $"{_host}:{_port}";
- _leaf = new ClientLeaf(new LeafIdContext(), LeafMode.Dual, new TcpCreator(url));
- if (_cts.IsCancellationRequested)
- {
- _leaf?.Close();
- _leaf = null;
- return false;
- }
- else
- {
- if (_leaf.Online)
- {
- _testResult = ServerTestResult.OldServer;
- _manualResetEvent.Set();
- return true;
- }
- return false;
- }
- }
- catch
- {
- return false;
- }
- }
- private void CheckJsonRpc(bool isUseHttps)
- {
- var stopwatch = new Stopwatch();
- stopwatch.Start();
- var result = isUseHttps ? JsonRpcTestWithHttps() : JsonRpcTestWithHttp();
- stopwatch.Stop();
- if (isUseHttps)
- {
- Logger.WriteLineInfo($"JsonRpcTest For Https 1st Time Cost {stopwatch.ElapsedMilliseconds}ms,result is {result}");
- }
- else
- {
- Logger.WriteLineInfo($"JsonRpcTest For Http 1st Time Cost {stopwatch.ElapsedMilliseconds}ms,result is {result}");
- }
- if (_cts.IsCancellationRequested)
- {
- if (isUseHttps)
- {
- Logger.WriteLineInfo($"JsonRpcTest For Https 2nd Time Cancelled");
- return;
- }
- else
- {
- Logger.WriteLineInfo($"JsonRpcTest For Http 2nd Time Cancelled");
- return;
- }
- }
- else if (!result)
- {
- stopwatch.Restart();
- result = isUseHttps ? JsonRpcTestWithHttps() : JsonRpcTestWithHttp();
- stopwatch.Stop();
- if (isUseHttps)
- {
- Logger.WriteLineInfo($"JsonRpcTest For Https 2nd Time Cost {stopwatch.ElapsedMilliseconds}ms,result is {result}");
- }
- else
- {
- Logger.WriteLineInfo($"JsonRpcTest For Http 2nd Time Cost {stopwatch.ElapsedMilliseconds}ms,result is {result}");
- }
- }
- }
- private bool JsonRpcTestWithHttp()
- {
- try
- {
- _clientForHttp?.Dispose();
- _clientForHttp = null;
- var url = $"http://{_host}:{_port}";
- _clientForHttp = new JsonRpcClient();
- var clientEngine = new JsonRpcHttpClientEngine(url);
- _clientForHttp.UseEngine(clientEngine);
- var vinnoServerService = _clientForHttp.CreateProxy<IVinnoServerService>(5000);
- var echoResult = JsonRpcHelper.Echo(vinnoServerService);
- if (_cts.IsCancellationRequested)
- {
- _clientForHttp?.Dispose();
- _clientForHttp = null;
- return false;
- }
- else
- {
- if (echoResult != null && echoResult.Code == 0)
- {
- _testResult = ServerTestResult.NewServerWithHttp;
- _manualResetEvent.Set();
- return true;
- }
- return false;
- }
- }
- catch
- {
- return false;
- }
- }
- private bool JsonRpcTestWithHttps()
- {
- try
- {
- _clientForHttps?.Dispose();
- _clientForHttps = null;
- var url = $"https://{_host}:{_port}";
- _clientForHttps = new JsonRpcClient();
- var clientEngine = new JsonRpcHttpClientEngine(url);
- _clientForHttps.UseEngine(clientEngine);
- var vinnoServerService = _clientForHttps.CreateProxy<IVinnoServerService>(5000);
- var echoResult = JsonRpcHelper.Echo(vinnoServerService);
- if (_cts.IsCancellationRequested)
- {
- _clientForHttps?.Dispose();
- _clientForHttps = null;
- return false;
- }
- else
- {
- if (echoResult != null && echoResult.Code == 0)
- {
- _testResult = ServerTestResult.NewServerWithHttps;
- _manualResetEvent.Set();
- return true;
- }
- return false;
- }
- }
- catch
- {
- return false;
- }
- }
- public void Dispose()
- {
- try
- {
- _manualResetEvent.Set();
- _cts?.Cancel();
- _clientForHttp?.Dispose();
- _clientForHttp = null;
- _clientForHttps?.Dispose();
- _clientForHttps = null;
- _leaf?.Close();
- _leaf = null;
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"vCloudServerSelector Dispose Error:{ex}");
- }
- }
- }
- }
|