123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- 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,
- NewServer
- }
- public class vCloudServerSelector : IDisposable
- {
- private string _prefix;
- private string _host;
- private int _port;
- private ClientLeaf _leaf;
- private JsonRpcClient _client;
- private ServerTestResult _testResult;
- private ManualResetEvent _manualResetEvent = new ManualResetEvent(false);
- private CancellationTokenSource _cts;
- public ServerTestResult Select(string prefix, string host, int port)
- {
- _prefix = prefix;
- _host = host;
- _port = port;
- _testResult = ServerTestResult.NotSure;
- _cts = new CancellationTokenSource();
- _manualResetEvent.Reset();
- try
- {
- Task.Run(CheckLeaf);
- Task.Run(CheckJsonRpc);
- 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 (!result && !_cts.IsCancellationRequested)
- {
- stopwatch.Restart();
- result = LeafTest();
- stopwatch.Stop();
- Logger.WriteLineInfo($"LeafTest 2nd Time Cost {stopwatch.ElapsedMilliseconds}ms,result is {result}");
- }
- }
- private bool LeafTest()
- {
- try
- {
- if (_leaf != null)
- {
- _leaf.Close();
- _leaf = null;
- }
- var url = $"{_host}:{_port}";
- _leaf = new ClientLeaf(new LeafIdContext(), LeafMode.Dual, new TcpCreator(url));
- if (_leaf.Online)
- {
- _testResult = ServerTestResult.OldServer;
- _manualResetEvent.Set();
- return true;
- }
- return false;
- }
- catch
- {
- return false;
- }
- }
- private void CheckJsonRpc()
- {
- var stopwatch = new Stopwatch();
- stopwatch.Start();
- var result = JsonRpcTest();
- stopwatch.Stop();
- Logger.WriteLineInfo($"JsonRpcTest 1st Time Cost {stopwatch.ElapsedMilliseconds}ms,result is {result}");
- if (!result && !_cts.IsCancellationRequested)
- {
- stopwatch.Restart();
- result = JsonRpcTest();
- stopwatch.Stop();
- Logger.WriteLineInfo($"JsonRpcTest 2nd Time Cost {stopwatch.ElapsedMilliseconds}ms,result is {result}");
- }
- }
- private bool JsonRpcTest()
- {
- try
- {
- if (_client != null)
- {
- _client.Dispose();
- _client = null;
- }
- var url = $"{_prefix}{_host}:{_port}";
- _client = new JsonRpcClient();
- var clientEngine = new JsonRpcHttpClientEngine(url);
- _client.UseEngine(clientEngine);
- var vinnoServerService = _client.CreateProxy<IVinnoServerService>(5000);
- var echoResult = JsonRpcHelper.Echo(vinnoServerService);
- if (echoResult != null && echoResult.Code == 0)
- {
- _testResult = ServerTestResult.NewServer;
- _manualResetEvent.Set();
- return true;
- }
- return false;
- }
- catch
- {
- return false;
- }
- }
- public void Dispose()
- {
- try
- {
- _manualResetEvent.Set();
- _cts?.Cancel();
- if (_leaf != null)
- {
- _leaf.Close();
- _leaf = null;
- }
- if (_client != null)
- {
- _client.Dispose();
- _client = null;
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"vCloudServerSelector Dispose Error:{ex}");
- }
- }
- }
- }
|