Bläddra i källkod

Refactoring comal.timesheets code

Frank van den Bos 2 år sedan
förälder
incheckning
e04e0274a9

+ 1 - 1
InABox.Client.RPC/InABox.Client.RPC.csproj

@@ -1,7 +1,7 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
     <PropertyGroup>
-        <TargetFramework>net6.0</TargetFramework>
+        <TargetFramework>netstandard2.1</TargetFramework>
         <ImplicitUsings>enable</ImplicitUsings>
         <Nullable>enable</Nullable>
         <RootNamespace>InABox.RPC.Client</RootNamespace>

+ 7 - 3
InABox.Client.RPC/RPCClient.cs

@@ -1,4 +1,8 @@
+using System;
 using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading;
 using InABox.Clients;
 using InABox.Core;
 
@@ -9,8 +13,8 @@ namespace InABox.Rpc
     {
         private IRpcClientTransport _transport;
 
-        private ConcurrentDictionary<Guid, ManualResetEventSlim> _events = new();
-        private ConcurrentDictionary<Guid, RpcMessage> _responses = new();
+        private ConcurrentDictionary<Guid, ManualResetEventSlim> _events = new ConcurrentDictionary<Guid, ManualResetEventSlim>();
+        private ConcurrentDictionary<Guid, RpcMessage> _responses = new ConcurrentDictionary<Guid, RpcMessage>();
 
         private const int DefaultRequestTimeout = 5 * 60 * 1000; // 5 minutes
         
@@ -113,7 +117,7 @@ namespace InABox.Rpc
         }
 
         private static string[]? _types;
-        public override string[] SupportedTypes()
+        public override IEnumerable<string> SupportedTypes()
         {
             _types ??= CoreUtils.Entities
                 .Where(x => x.GetInterfaces().Contains(typeof(IPersistent)))

+ 1 - 0
InABox.Client.RPC/RPCClientSession.cs

@@ -1,3 +1,4 @@
+using System;
 using InABox.Core;
 
 namespace InABox.Rpc

+ 1 - 0
InABox.Client.RPC/Transports/Pipe/RPCClientPipeTransport.cs

@@ -1,3 +1,4 @@
+using System;
 using H.Pipes;
 using InABox.Core;
 

+ 5 - 2
InABox.Client.RPC/Transports/RPCClientTransport.cs

@@ -1,5 +1,8 @@
+using System;
 using System.Collections.Concurrent;
+using System.Collections.Generic;
 using System.Runtime.CompilerServices;
+using System.Threading;
 using InABox.Clients;
 using InABox.Core;
 using WebSocketSharp;
@@ -10,8 +13,8 @@ namespace InABox.Rpc
     public abstract class RpcClientTransport : IRpcClientTransport
     {
         private RpcClientSession _session = new RpcClientSession();
-        private ConcurrentDictionary<Guid, ManualResetEventSlim> _events = new();
-        private ConcurrentDictionary<Guid, RpcMessage> _responses = new();
+        private ConcurrentDictionary<Guid, ManualResetEventSlim> _events = new ConcurrentDictionary<Guid, ManualResetEventSlim>();
+        private ConcurrentDictionary<Guid, RpcMessage> _responses = new ConcurrentDictionary<Guid, RpcMessage>();
 
         private const int DefaultRequestTimeout = 5 * 60 * 1000; // 5 minutes
 

+ 2 - 0
InABox.Client.RPC/Transports/Socket/RPCClientSocketTransport.cs

@@ -1,3 +1,5 @@
+using System;
+using System.Threading.Tasks;
 using InABox.Clients;
 using InABox.Core;
 using WebSocketSharp;

+ 37 - 43
InABox.Mobile/Shared/MobileUtils.cs

@@ -1,17 +1,12 @@
 using System;
 using System.Linq;
-using System.Net;
-using System.Threading;
 using System.Threading.Tasks;
 using System.Collections.Generic;
-
+using System.Threading;
 using Xamarin.Forms;
 
-using RestSharp;
 using Plugin.Permissions;
 using Plugin.Permissions.Abstractions;
-
-using Syncfusion.Licensing;
 using comal.timesheets;
 using Xamarin.Essentials;
 using PermissionStatus = Plugin.Permissions.Abstractions.PermissionStatus;
@@ -19,6 +14,7 @@ using InABox.Core;
 using InABox.Clients;
 using InABox.Configuration;
 using Comal.Classes;
+using InABox.Rpc;
 
 namespace InABox.Mobile
 {
@@ -61,30 +57,7 @@ namespace InABox.Mobile
         }
 
         public static IAppVersion AppVersion { get { return DependencyService.Get<IAppVersion>(); } }
-
-        public static void SaveToSecureStorage()
-        {
-            try
-            {
-                if (!string.IsNullOrWhiteSpace(App.Settings.UserID))
-                    SecureStorage.SetAsync(GlobalVariables.CacheUserIDString, App.Settings.UserID);
-
-                if (!string.IsNullOrWhiteSpace(App.Settings.Password))
-                    SecureStorage.SetAsync(GlobalVariables.CachePasswordString, App.Settings.Password);
-
-                if (App.DBSettings.URLs.Count() > 0)
-                {
-                    int count = 0;
-                    foreach (string url in App.DBSettings.URLs)
-                    {
-                        SecureStorage.SetAsync(GlobalVariables.CacheSettingsURL + count, url);
-                        count++;
-                    }
-                }
-            }
-            catch { }
-        }
-
+        
         public static void LoadFromLink()
         {
             try
@@ -94,14 +67,16 @@ namespace InABox.Mobile
                 var s = arr.Item2;
                 if (!CheckExpiry(array))
                 {
-                    GlobalVariables.LoadFromLinkString = "Link has expired";
+                    App.Data.LoadFromLinkString = "Link has expired";
                     return;
                 }
 
                 SetDBSettings(array, s);
                 SaveSettings();
                 RemoveSessionID();
-                PingAndSetClient();
+                
+                SetupClient();
+                
             }
             catch (Exception e)
             {
@@ -109,17 +84,40 @@ namespace InABox.Mobile
                     LoadDemoSettings(App.DBSettings);
             }
         }
-
-        private static void PingAndSetClient()
+        
+        public static IRpcClientTransport Transport { get; private set; }
+        
+        public static bool SetupClient()
         {
-            var result = RestClient<User>.Ping(App.DBSettings.URLs, out DatabaseInfo info);
+            if (!App.DBSettings.URLs.Any())
+                return false;
 
-            ClientFactory.SetClientType(typeof(RestClient<>), InABox.Core.Platform.Mobile, MobileUtils.AppVersion.InstalledVersionNumber + App.DeviceString, result, true);
+            List<Task<String>> pings = new List<Task<String?>>();
+            foreach (var url in App.DBSettings.URLs)
+                pings.Add(Task.Run<String?>(() =>
+                {
+                    int iRetry = 0;
+                    while (!new RpcClientSocketTransport(url).Ping() && (iRetry < 10))
+                    {
+                        Thread.Sleep(1000);
+                        iRetry++;
+                    }
+                    return iRetry < 10 ? url : "";
+                }));
+            var index = Task.WaitAny(pings.ToArray<Task>());
+            var result = pings[index].Result;
+            
+            if (String.IsNullOrWhiteSpace(result))
+                return false;
+            
+            Transport = new RpcClientSocketTransport(result);
+            ClientFactory.SetClientType(typeof(RpcClient<>), InABox.Core.Platform.TimeBench, MobileUtils.AppVersion.InstalledVersionNumber + App.Data.DeviceString, Transport);
+            return true;
         }
 
         private static Tuple<string[], string> GetArray()
         {
-            var s = Encryption.Decrypt(GlobalVariables.LoadFromLinkString, "logindetailslink");
+            var s = Encryption.Decrypt(App.Data.LoadFromLinkString, "logindetailslink");
             var index = s.IndexOf("ENDURLS");
             var substring = s.Substring(index + 8, s.Length - index - 8);
             return new Tuple<string[], string>(substring.Split(","), s);
@@ -145,12 +143,8 @@ namespace InABox.Mobile
         private static void SaveSettings()
         {
             ClientFactory.InvalidateUser();
-            new LocalConfiguration<DatabaseSettings>().Delete();
             new LocalConfiguration<DatabaseSettings>().Save(App.DBSettings);
-            App.DBSettings = new LocalConfiguration<DatabaseSettings>().Load();
-            GlobalVariables.ChangeUser = true;
-
-            SaveToSecureStorage();
+            App.Data.ChangeUser = true;
         }
 
         private static void RemoveSessionID()
@@ -302,6 +296,6 @@ namespace InABox.Mobile
 
             return true;
         }
-
+        
     }
 }

+ 1 - 0
InABox.RPC.Shared/Commands/Check2FA/RpcCheck2FAParameters.cs

@@ -1,3 +1,4 @@
+using System;
 using InABox.Core;
 
 namespace InABox.Rpc

+ 1 - 0
InABox.RPC.Shared/Commands/Delete/RpcDeleteParameters.cs

@@ -1,3 +1,4 @@
+using System;
 using InABox.Core;
 
 namespace InABox.Rpc

+ 1 - 0
InABox.RPC.Shared/Commands/Query/RpcQueryDefinition.cs

@@ -1,3 +1,4 @@
+using System;
 using InABox.Core;
 
 namespace InABox.Rpc

+ 1 - 0
InABox.RPC.Shared/Commands/Query/RpcQueryTable.cs

@@ -1,3 +1,4 @@
+using System;
 using InABox.Core;
 
 namespace InABox.Rpc

+ 1 - 0
InABox.RPC.Shared/Commands/Save/RpcSaveItem.cs

@@ -1,3 +1,4 @@
+using System;
 using InABox.Core;
 
 namespace InABox.Rpc

+ 2 - 0
InABox.RPC.Shared/Commands/Save/RpcSaveParameters.cs

@@ -1,3 +1,5 @@
+using System;
+using System.Linq;
 using InABox.Core;
 
 namespace InABox.Rpc

+ 2 - 0
InABox.RPC.Shared/Commands/Save/RpcSaveResult.cs

@@ -1,3 +1,5 @@
+using System;
+using System.Collections.Generic;
 using InABox.Core;
 
 namespace InABox.Rpc

+ 1 - 0
InABox.RPC.Shared/Commands/Validate/RpcValidateParameters.cs

@@ -1,3 +1,4 @@
+using System;
 using InABox.Core;
 
 namespace InABox.Rpc

+ 1 - 0
InABox.RPC.Shared/Commands/Validate/RpcValidateResult.cs

@@ -1,3 +1,4 @@
+using System;
 using InABox.Clients;
 using InABox.Core;
 

+ 1 - 0
InABox.RPC.Shared/IRPCSession.cs

@@ -1,3 +1,4 @@
+using System;
 using InABox.Core;
 
 namespace InABox.Rpc

+ 1 - 1
InABox.RPC.Shared/InABox.RPC.Shared.csproj

@@ -1,7 +1,7 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
     <PropertyGroup>
-        <TargetFramework>net6.0</TargetFramework>
+        <TargetFramework>netstandard2.1</TargetFramework>
         <ImplicitUsings>enable</ImplicitUsings>
         <Nullable>enable</Nullable>
     </PropertyGroup>

+ 1 - 0
InABox.RPC.Shared/RPCMessage.cs

@@ -1,3 +1,4 @@
+using System;
 using InABox.Core;
 
 namespace InABox.Rpc

+ 2 - 0
InABox.RPC.Shared/Transports/RPCTransportExceptionArgs.cs

@@ -1,3 +1,5 @@
+using System;
+
 namespace InABox.Rpc
 {
     public class RpcTransportExceptionArgs

+ 0 - 2
inabox.client.rest/InABox.Client.Rest/InABox.Client.Rest.csproj

@@ -7,9 +7,7 @@
     </PropertyGroup>
 
     <ItemGroup>
-      <ProjectReference Include="..\..\inabox.client.websocket\InABox.Client.WebSocket.csproj" />
       <ProjectReference Include="..\..\InABox.Core\InABox.Core.csproj" />
-      <ProjectReference Include="..\..\inabox.websocket.shared\InABox.WebSocket.Shared.csproj" />
     </ItemGroup>
 
     <ItemGroup>

+ 0 - 1
inabox.client.websocket/InABox.Client.WebSocket.csproj

@@ -11,7 +11,6 @@
 
   <ItemGroup>
     <ProjectReference Include="..\InABox.Core\InABox.Core.csproj" />
-    <ProjectReference Include="..\InABox.WebSocket.Shared\InABox.WebSocket.Shared.csproj" />
   </ItemGroup>
 
 </Project>