vCloudTerminalTester.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using Vinno.IUS.Common.Log;
  3. using Vinno.IUS.Common.Network.Leaf;
  4. using Vinno.IUS.Common.Network.Tcp;
  5. using Vinno.vCloud.Protocol.Infrastructures;
  6. using Vinno.vCloud.Protocol.Messages.Login;
  7. namespace Vinno.vCloud.Common.FIS.Test
  8. {
  9. public class vCloudTerminalTester
  10. {
  11. /// <summary>
  12. /// Test the connection by given parameters.
  13. /// </summary>
  14. /// <param name="host">The host of the server.</param>
  15. /// <param name="port">The port of the server.</param>
  16. /// <param name="account">The account of the terminal.</param>
  17. /// <param name="password">The password of the terminal.</param>
  18. /// <returns></returns>
  19. public static DeviceTestResult Test(string host, int port, string account, string password)
  20. {
  21. ClientLeaf leaf = null;
  22. try
  23. {
  24. var url = $"{host}:{port}";
  25. leaf = new ClientLeaf(new LeafIdContext(), LeafMode.Dual, new TcpCreator(url));
  26. if (!leaf.Online)
  27. {
  28. leaf.Close();
  29. leaf = null;
  30. return DeviceTestResult.CanNotConnect;
  31. }
  32. var request = new AuthenticateAccountRequest
  33. {
  34. Name = account,
  35. Password = password,
  36. Type = LoginType.Terminal
  37. };
  38. var result = leaf.Send(request, 5000);
  39. var authenticateAccountResult = AuthenticateAccountResult.Convert(result);
  40. if (authenticateAccountResult != null)
  41. {
  42. switch (authenticateAccountResult.Status)
  43. {
  44. case AuthenticateStatus.Success:
  45. return DeviceTestResult.Success;
  46. case AuthenticateStatus.WrongAccount:
  47. case AuthenticateStatus.WrongPassword:
  48. return DeviceTestResult.ErrorAccountOrPassword;
  49. }
  50. }
  51. }
  52. catch (Exception ex)
  53. {
  54. Logger.WriteLineError($"Terminal connection test error:{ex}");
  55. }
  56. finally
  57. {
  58. leaf?.Close();
  59. }
  60. return DeviceTestResult.CanNotConnect;
  61. }
  62. }
  63. }