using System;
using System.Threading;
using Vinno.IUS.Common.Network.Leaf;
using Vinno.IUS.Common.Network.Tcp;
using Vinno.IUS.Common.Network.Transfer;
using Vinno.vCloud.Common.Storage.ObjectStorageInfo.Interface;
using Vinno.vCloud.Common.Storage.ObjectStorageInfo.Solid;
namespace Vinno.vCloud.Common.Storage.ObjectStorageInfo.SignatureClient.Connect
{
public class SimpleLeafAgency : ILeafAgency
{
private string _leafUrl = string.Empty;
private ClientLeaf _clientLeaf = null;
///
/// 构造LeafAgency
///
/// 服务地址
public SimpleLeafAgency(string leafUrl)
{
_leafUrl = leafUrl;
}
///
/// 构造LeafAgency
///
/// 服务地址
public SimpleLeafAgency(ClientLeaf clientLeaf)
{
_clientLeaf = clientLeaf;
}
///
/// 请求地址
///
public string Url
{
get
{
return _leafUrl;
}
}
///
/// 发送消息
///
///
///
public Message AgencySend(Message request)
{
return AgencyKeepSend(request);
}
///
/// 发送消息如果是网络异常则重发
///
///
///
///
public Message AgencyKeepSend(Message request, int tryTime = 3)
{
var agencyLeaf = new ClientLeaf(new LeafIdContext(), LeafMode.Single, new TcpCreator(_leafUrl));
try
{
if (!agencyLeaf.Online)
{
throw new NetWorkException($"AgencyLeaf is offline");
}
var token = agencyLeaf.ApplyToken();
var result = agencyLeaf.Send(token, request);
return result;
}
catch (Exception exception)
{
switch (exception.GetType().Name)
{
case "NetWorkException":
case "CanNotConnectException":
case "NotConnectedException":
if (tryTime == 0)
{
return null;
}
Thread.Sleep(3000);
return AgencyKeepSend(request, --tryTime);
default:
return null;
}
}
finally
{
agencyLeaf?.Close();
}
}
public void Dispose()
{
}
}
}