using System;
using Vinno.IUS.Common.Log;
using Vinno.IUS.Common.Network.Leaf;
using Vinno.IUS.Common.Network.Tcp;
using Vinno.vCloud.Protocol.Infrastructures;
using Vinno.vCloud.Protocol.Messages.Login;
namespace Vinno.vCloud.Common.FIS.Test
{
public class vCloudTerminalTester
{
///
/// Test the connection by given parameters.
///
/// The host of the server.
/// The port of the server.
/// The account of the terminal.
/// The password of the terminal.
///
public static DeviceTestResult Test(string host, int port, string account, string password)
{
ClientLeaf leaf = null;
try
{
var url = $"{host}:{port}";
leaf = new ClientLeaf(new LeafIdContext(), LeafMode.Dual, new TcpCreator(url));
if (!leaf.Online)
{
leaf.Close();
leaf = null;
return DeviceTestResult.CanNotConnect;
}
var request = new AuthenticateAccountRequest
{
Name = account,
Password = password,
Type = LoginType.Terminal
};
var result = leaf.Send(request, 5000);
var authenticateAccountResult = AuthenticateAccountResult.Convert(result);
if (authenticateAccountResult != null)
{
switch (authenticateAccountResult.Status)
{
case AuthenticateStatus.Success:
return DeviceTestResult.Success;
case AuthenticateStatus.WrongAccount:
case AuthenticateStatus.WrongPassword:
return DeviceTestResult.ErrorAccountOrPassword;
}
}
}
catch (Exception ex)
{
Logger.WriteLineError($"Terminal connection test error:{ex}");
}
finally
{
leaf?.Close();
}
return DeviceTestResult.CanNotConnect;
}
}
}