From 9c03087fa016160025ca2decf688889e59ee2b4e Mon Sep 17 00:00:00 2001 From: TheJoKlLa Date: Sat, 30 Jan 2021 21:15:28 +0100 Subject: [PATCH] Fixed: Connection Bug --- Borepin/Borepin/App.xaml.cs | 32 ++++++-- Borepin/Borepin/PageModel/ServerPageModel.cs | 4 +- Borepin/Borepin/Service/BFFH/BFFHService.cs | 19 +++-- Borepin/Borepin/Service/BFFH/IBFFHService.cs | 4 +- FabAccessAPI_Test/ConnectionTest.cs | 84 ++++++++++++++++++++ FabAccessAPI_Test/FabAccessAPITests.cs | 4 +- 6 files changed, 126 insertions(+), 21 deletions(-) create mode 100644 FabAccessAPI_Test/ConnectionTest.cs diff --git a/Borepin/Borepin/App.xaml.cs b/Borepin/Borepin/App.xaml.cs index 9c7347a..1d91f3f 100644 --- a/Borepin/Borepin/App.xaml.cs +++ b/Borepin/Borepin/App.xaml.cs @@ -7,21 +7,37 @@ using Borepin.DialogModel; using Borepin.Service.Connections; using Borepin.Service.BFFH; using Borepin.Service.Credentials; +using System.Collections.Generic; +using Borepin.Model; namespace Borepin { public partial class App { + private IConnectionService _ConnectionService; + private ICredentialService _CredentialService; + private IBFFHService _BFFHService; + + public App() { - + } protected override async void OnInitialized() { InitializeComponent(); - var result = await NavigationService.NavigateAsync("/MainPage/NavigationPage/ServerListPage"); + Prism.Navigation.INavigationResult result; + List connection_list = await _ConnectionService.GetConnectionList(); + if (connection_list.Count == 0) + { + result = await NavigationService.NavigateAsync("/NavigationPage/HostSelectPage"); + } + else + { + result = await NavigationService.NavigateAsync("/MainPage/NavigationPage/ServerListPage"); + } if (!result.Success) { @@ -48,13 +64,13 @@ namespace Borepin containerRegistry.RegisterDialog(); // Register Service - IConnectionService connectionService = new ConnectionService(); - containerRegistry.RegisterInstance(connectionService); - ICredentialService credentialService = new CredentialService(); - containerRegistry.RegisterInstance(credentialService); + _ConnectionService = new ConnectionService(); + _CredentialService = new CredentialService(); + _BFFHService = new BFFHService(_ConnectionService, _CredentialService); - containerRegistry.RegisterInstance(new BFFHService(connectionService, credentialService)); - containerRegistry.RegisterInstance(new ConnectionService()); + containerRegistry.RegisterInstance(_ConnectionService); + containerRegistry.RegisterInstance(_CredentialService); + containerRegistry.RegisterInstance(_BFFHService); } } } diff --git a/Borepin/Borepin/PageModel/ServerPageModel.cs b/Borepin/Borepin/PageModel/ServerPageModel.cs index b2c700e..ea7ba6f 100644 --- a/Borepin/Borepin/PageModel/ServerPageModel.cs +++ b/Borepin/Borepin/PageModel/ServerPageModel.cs @@ -59,7 +59,7 @@ namespace Borepin.PageModel { if(IsConnected) { - await _BFFHService.Disconnect(); + _BFFHService.Disconnect(); IsConnected = false; } @@ -99,7 +99,7 @@ namespace Borepin.PageModel if(_BFFHService.ActiveConnection != null && connection == _BFFHService.ActiveConnection) { - await _BFFHService.Disconnect(); + _BFFHService.Disconnect(); } await _ConnectionService.RemoveConnection(connection); diff --git a/Borepin/Borepin/Service/BFFH/BFFHService.cs b/Borepin/Borepin/Service/BFFH/BFFHService.cs index d9e2278..1b55544 100644 --- a/Borepin/Borepin/Service/BFFH/BFFHService.cs +++ b/Borepin/Borepin/Service/BFFH/BFFHService.cs @@ -4,6 +4,7 @@ using Borepin.Service.Credentials; using System.Threading.Tasks; using Capnp.Rpc; using System.Collections.Generic; +using System.Net.Sockets; namespace Borepin.Service.BFFH { @@ -24,32 +25,36 @@ namespace Borepin.Service.BFFH public bool IsAuthenticated { get; private set; } - public Task Connect(Model.Connection connection) + public async Task Connect(Model.Connection connection) { if (_Connection != null || ActiveConnection != null) { throw new System.Exception("Still connected"); } - var rpcClient = new TcpRpcClient(); + TcpRpcClient rpcClient = new TcpRpcClient(); rpcClient.Connect(connection.Address.Host, connection.Address.Port); + await Task.Run(async () => + { + while (rpcClient.State == ConnectionState.Initializing) + { + await Task.Delay(100); + } + }); + FabAccessAPI.Connection connection_test = new FabAccessAPI.Connection(rpcClient); _Connection = connection_test; ActiveConnection = connection; - - return Task.FromResult(true); } - public Task Disconnect() + public void Disconnect() { _Connection.RpcClient?.Dispose(); _Connection = null; ActiveConnection = null; - - return Task.FromResult(true); } public bool CanAuthenticate() diff --git a/Borepin/Borepin/Service/BFFH/IBFFHService.cs b/Borepin/Borepin/Service/BFFH/IBFFHService.cs index f70189a..9f5a18a 100644 --- a/Borepin/Borepin/Service/BFFH/IBFFHService.cs +++ b/Borepin/Borepin/Service/BFFH/IBFFHService.cs @@ -25,12 +25,12 @@ namespace Borepin.Service.BFFH /// Connect to BFFH Instance /// /// Connection with address - Task Connect(Connection connection); + Task Connect(Connection connection); /// /// Disconnect from BFFH Instance /// - Task Disconnect(); + void Disconnect(); /// diff --git a/FabAccessAPI_Test/ConnectionTest.cs b/FabAccessAPI_Test/ConnectionTest.cs new file mode 100644 index 0000000..8a4c874 --- /dev/null +++ b/FabAccessAPI_Test/ConnectionTest.cs @@ -0,0 +1,84 @@ +using Capnp.Rpc; +using NUnit.Framework; +using System; + +namespace FabAccessAPI_Test +{ + public class ConnectionTest + { + [Test, Repeat(10)] + public void IPv4() + { + UriBuilder builder = new UriBuilder(); + builder.Host = "127.0.0.1"; + builder.Port = 59661; + + Uri uri = builder.Uri; + + TcpRpcClient rpcClient = new TcpRpcClient(); + + rpcClient.Connect(uri.Host, uri.Port); + + while (rpcClient.State == ConnectionState.Initializing); + + FabAccessAPI.Connection connection = new FabAccessAPI.Connection(rpcClient); + + Assert.AreEqual(ConnectionState.Active, connection.RpcClient.State); + + rpcClient.Dispose(); + Assert.AreEqual(ConnectionState.Down, connection.RpcClient.State); + } + + [Test, Repeat(10)] + public void IPv6() + { + UriBuilder builder = new UriBuilder(); + builder.Host = "[::1]"; + builder.Port = 59661; + + Uri uri = builder.Uri; + + TcpRpcClient rpcClient = new TcpRpcClient(); + + rpcClient.Connect(uri.Host, uri.Port); + + while (rpcClient.State == ConnectionState.Initializing) ; + + FabAccessAPI.Connection connection = new FabAccessAPI.Connection(rpcClient); + + Assert.AreEqual(ConnectionState.Active, connection.RpcClient.State); + + rpcClient.Dispose(); + Assert.AreEqual(ConnectionState.Down, connection.RpcClient.State); + } + + [Test, Repeat(10)] + public void DoubleConnect() + { + TcpRpcClient rpcClient = new TcpRpcClient(); + + rpcClient.Connect("127.0.0.1", 59661); + + while (rpcClient.State == ConnectionState.Initializing) ; + + FabAccessAPI.Connection connection = new FabAccessAPI.Connection(rpcClient); + + Assert.AreEqual(ConnectionState.Active, connection.RpcClient.State); + + rpcClient.Dispose(); + Assert.AreEqual(ConnectionState.Down, connection.RpcClient.State); + + rpcClient = new TcpRpcClient(); + + rpcClient.Connect("127.0.0.1", 59661); + + connection = new FabAccessAPI.Connection(rpcClient); + while (rpcClient.State == ConnectionState.Initializing) ; + + Assert.AreEqual(ConnectionState.Active, connection.RpcClient.State); + + rpcClient.Dispose(); + Assert.AreEqual(ConnectionState.Down, connection.RpcClient.State); + } + } +} diff --git a/FabAccessAPI_Test/FabAccessAPITests.cs b/FabAccessAPI_Test/FabAccessAPITests.cs index 8af2526..6860dca 100644 --- a/FabAccessAPI_Test/FabAccessAPITests.cs +++ b/FabAccessAPI_Test/FabAccessAPITests.cs @@ -32,7 +32,7 @@ namespace FabAccessAPI_Test { [SetUp] public void Setup() { var rpcClient = new TcpRpcClient(); - rpcClient.Connect("::1", 59661); + rpcClient.Connect("[::1]", 59661); _connection = new Connection(rpcClient); } @@ -46,7 +46,7 @@ namespace FabAccessAPI_Test { public void Connect() { Assert.AreEqual(ConnectionState.Active, _connection.RpcClient.State); } - + [Test] public async Task Authenticate() { await _connection.Auth("PLAIN", new Dictionary{{"Username", "Testuser"}, {"Password", "secret"}});