Explorar o código

Overhauled Remote Connection to combine URL and Port, and allow fallback URL settings
Fixed inconsistent LocalConfigurationSettings and UserConfigurationSettings naming conventions
Removed unused DeviceID from DbFactory.Start()

Frank van den Bos %!s(int64=2) %!d(string=hai) anos
pai
achega
d7a8d0413d

+ 7 - 9
InABox.Client.Local/LocalClient.cs

@@ -43,9 +43,7 @@ namespace InABox.Clients
 
     public class LocalClient<TEntity> : BaseClient<TEntity> where TEntity : Entity, new()
     {
-        private IStore<TEntity> store;
-
-        public LocalClient(string parameters)
+        public LocalClient()
         {
             Notify.Notifier = new LocalNotifier();
             Notify.Notifier.Poll(ClientFactory.SessionID);
@@ -108,7 +106,7 @@ namespace InABox.Clients
 
         protected override CoreTable DoQuery(Filter<TEntity> filter, Columns<TEntity> columns, SortOrder<TEntity> sort = null)
         {
-            store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
+            var store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
             var result = store.Query(filter, columns, sort);
             return result;
         }
@@ -119,7 +117,7 @@ namespace InABox.Clients
 
         protected override TEntity[] DoLoad(Filter<TEntity> filter = null, SortOrder<TEntity> sort = null)
         {
-            store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
+            var store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
             var result = store.Load(filter, sort);
             return result;
         }
@@ -176,14 +174,14 @@ namespace InABox.Clients
 
         protected override void DoSave(TEntity entity, string auditnote)
         {
-            store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
+            var store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
             store.Save(entity, auditnote);
             entity.CommitChanges();
         }
 
         protected override void DoSave(IEnumerable<TEntity> entities, string auditnote)
         {
-            store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
+            var store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
             store.Save(entities, auditnote);
             foreach(var entity in entities)
             {
@@ -197,13 +195,13 @@ namespace InABox.Clients
 
         protected override void DoDelete(TEntity entity, string auditnote)
         {
-            store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
+            var store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
             store.Delete(entity, auditnote);
         }
 
         protected override void DoDelete(IList<TEntity> entities, string auditnote)
         {
-            store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
+            var store = DbFactory.FindStore<TEntity>(ClientFactory.UserGuid, ClientFactory.UserID, ClientFactory.Platform, ClientFactory.Version);
             store.Delete(entities, auditnote);
         }
 

+ 30 - 6
InABox.Client.Remote.Json/JsonClient.cs

@@ -5,7 +5,9 @@ using System.IO.Compression;
 using System.Linq;
 using System.Net;
 using System.Net.Http;
+using System.Runtime.CompilerServices;
 using System.Text;
+using System.Threading.Tasks;
 using InABox.Core;
 using InABox.Remote.Shared;
 using RestSharp;
@@ -36,20 +38,42 @@ namespace InABox.Clients
 
         private BinarySerializationSettings BinarySerializationSettings;
 
-        public JsonClient(string url, int port, bool useSimpleEncryption, bool compression, BinarySerializationSettings binarySerializationSettings) : base(url, port, useSimpleEncryption)
+        public JsonClient(string url, bool useSimpleEncryption, bool compression, BinarySerializationSettings binarySerializationSettings) : base(url, useSimpleEncryption)
         {
             _compression = compression;
             BinarySerializationSettings = binarySerializationSettings;
         }
 
-        public JsonClient(string url, int port, bool useSimpleEncryption) : this(url, port, useSimpleEncryption, true, BinarySerializationSettings.Latest)
+        public JsonClient(string url, bool useSimpleEncryption) : this(url, useSimpleEncryption, true, BinarySerializationSettings.Latest)
         {
         }
 
-        public JsonClient(string url, int port) : this(url, port, false, true, BinarySerializationSettings.Latest)
+        public JsonClient(string url) : this(url, false, true, BinarySerializationSettings.Latest)
         {
         }
 
+        public static string Ping(String[] urls, out DatabaseInfo info)
+        {
+            String result = "";
+            info = new DatabaseInfo();
+            List<Task<Tuple<String,DatabaseInfo>>> pings = urls.Select(x => Task.Run(
+                () => new Tuple<String,DatabaseInfo>(x,new JsonClient<User>(x).Info())
+            )).ToList();
+            while (pings.Count > 0)
+            {
+                var ping = Task.WhenAny(pings).Result;
+                if (ping.Status == TaskStatus.RanToCompletion && !String.IsNullOrWhiteSpace(ping.Result.Item2.Version))
+                {
+                    result = ping.Result.Item1;
+                    info = ping.Result.Item2;
+                    break;
+                }
+                else
+                    pings.Remove(ping);
+            }
+            return result;
+        }
+
 
         //private static void CheckSecuritySettings()
         //{
@@ -88,7 +112,7 @@ namespace InABox.Clients
             //Log("  * {0}{1}() Serializing data took {2}ms ({3} bytes)", Action, typeof(TEntity).Name, sw.ElapsedMilliseconds, json.Length);
             //sw.Restart();
 
-            var uri = new Uri(string.Format("{0}:{1}", URL, Port));
+            var uri = new Uri(URL);
             var cli = new RestClient(uri);
             var cmd = string.Format(
                 "{0}{1}?format={2}&responseFormat={3}&serializationVersion={4}", 
@@ -251,7 +275,7 @@ namespace InABox.Clients
         public override IEnumerable<string> SupportedTypes()
         {
             var result = new List<string>();
-            var uri = new Uri(string.Format("{0}:{1}", URL, Port));
+            var uri = new Uri(URL);
             var cli = new RestClient(uri);
             var req = new RestRequest("/classes", Method.GET) { Timeout = 20000 };
 
@@ -285,7 +309,7 @@ namespace InABox.Clients
 
         public override DatabaseInfo Info()
         {
-            var uri = new Uri(string.Format("{0}:{1}", URL, Port));
+            var uri = new Uri(URL);
             var cli = new RestClient(uri);
             var req = new RestRequest("/info", Method.GET) { Timeout = 20000 };
 

+ 9 - 19
InABox.Client.Remote.Shared/RemoteClient.cs

@@ -19,10 +19,6 @@ namespace InABox.Clients
 
     public static class URLCache
     {
-        private static string? Domain { get; set; }
-
-        private static int? Port { get; set; }
-
         private static string URL { get; set; } = "";
 
         private static bool isHTTPS;
@@ -31,31 +27,27 @@ namespace InABox.Clients
 
         public static void Clear()
         {
-            Domain = null;
-            Port = null;
             URL = "";
             isHTTPS = false;
         }
 
-        public static string GetURL(string domain, int port)
+        public static string GetURL(string url)
         {
-            domain = domain.Split(new[] { "://" }, StringSplitOptions.RemoveEmptyEntries).Last();
-            if (domain != Domain || port != Port)
+            url = url.Split(new[] { "://" }, StringSplitOptions.RemoveEmptyEntries).Last();
+            if (!String.Equals(url,URL))
             {
-                var client = new HttpClient { BaseAddress = new Uri($"https://{domain}:{port}") };
+                var client = new HttpClient { BaseAddress = new Uri($"https://{url}") };
                 try
                 {
                     client.GetAsync("operations").Wait();
-                    URL = $"https://{domain}";
+                    URL = $"https://{url}";
                     isHTTPS = true;
                 }
                 catch (Exception)
                 {
-                    URL = $"http://{domain}";
+                    URL = $"http://{url}";
                     isHTTPS = false;
                 }
-                Port = port;
-                Domain = domain;
             }
             return URL;
         }
@@ -78,15 +70,13 @@ namespace InABox.Clients
 
     public abstract class RemoteClient<TEntity> : BaseClient<TEntity> where TEntity : Entity, new()
     {
-        public RemoteClient(string url, int port, bool useSimpleEncryption = false)
+        public RemoteClient(string url, bool useSimpleEncryption = false)
         {
-            URL = URLCache.GetURL(url, port);
-            Port = port;
+            URL = URLCache.GetURL(url);
             UseSimpleEncryption = useSimpleEncryption;
         }
 
         public string URL { get; set; }
-        public int Port { get; set; }
 
         public bool UseSimpleEncryption { get; set; }
 
@@ -451,7 +441,7 @@ namespace InABox.Clients
 
         protected override bool DoPing()
         {
-            var uri = new Uri(string.Format("{0}:{1}", URL, Port));
+            var uri = new Uri(URL);
             var cli = new RestClient(uri);
             var req = new RestRequest("/classes", Method.GET) { Timeout = 20000 };
 

+ 0 - 18
InABox.Configuration/ConnectionSettings.cs

@@ -1,18 +0,0 @@
-using System.Collections.Generic;
-using InABox.Clients;
-
-namespace InABox.Configuration
-{
-    public class ConnectionSettings : LocalConfigurationSettings
-    {
-        public string URL { get; set; }
-        public int Port { get; set; }
-
-        public SerializerProtocol Protocol { get; set; }
-
-        public string UserID { get; set; }
-        public string Password { get; set; }
-
-        public Dictionary<int, int> ButtonFavourites { get; set; }
-    }
-}

+ 1 - 1
InABox.Configuration/GlobalConfiguration.cs

@@ -34,7 +34,7 @@ namespace InABox.Configuration
         private GlobalSettings GetSettings()
         {
             GlobalSettings result = null;
-            if (ClientFactory.GetClientType() != null)
+            if (ClientFactory.ClientType != null)
             {
                 var client = new Client<GlobalSettings>();
                 result = client.Load(new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name).And(x => x.Key).IsEqualTo(Section))

+ 3 - 3
InABox.Configuration/LocalConfiguration.cs

@@ -7,11 +7,11 @@ using InABox.Core;
 
 namespace InABox.Configuration
 {
-    public interface LocalConfigurationSettings : IConfigurationSettings
+    public interface ILocalConfigurationSettings : IConfigurationSettings
     {
     }
 
-    public class LocalConfiguration<T> : Configuration<T> where T : LocalConfigurationSettings, new()
+    public class LocalConfiguration<T> : Configuration<T> where T : ILocalConfigurationSettings, new()
     {
         public LocalConfiguration(string section = "") : base(section)
         {
@@ -176,7 +176,7 @@ namespace InABox.Configuration
     }
 
     public class EncryptedLocalConfiguration<T> : LocalConfiguration<T>
-        where T : LocalConfigurationSettings, new()
+        where T : ILocalConfigurationSettings, new()
     {
         private byte[] Key;
 

+ 3 - 3
InABox.Configuration/UserConfiguration.cs

@@ -6,7 +6,7 @@ using InABox.Core;
 
 namespace InABox.Configuration
 {
-    public interface UserConfigurationSettings : IConfigurationSettings
+    public interface IUserConfigurationSettings : IConfigurationSettings
     {
     }
 
@@ -22,7 +22,7 @@ namespace InABox.Configuration
         public string Contents { get; set; }
     }
 
-    public class UserConfiguration<T> : Configuration<T> where T : UserConfigurationSettings, new()
+    public class UserConfiguration<T> : Configuration<T> where T : IUserConfigurationSettings, new()
     {
         public UserConfiguration(string section = "") : base(section)
         {
@@ -36,7 +36,7 @@ namespace InABox.Configuration
         private UserSettings GetSettings()
         {
             UserSettings result = null;
-            if (ClientFactory.GetClientType() != null)
+            if (ClientFactory.ClientType != null)
             {
                 var client = new Client<UserSettings>();
 

+ 25 - 27
InABox.Core/Client/ClientFactory.cs

@@ -24,19 +24,11 @@ namespace InABox.Clients
 
     public static class ClientFactory
     {
-        private static Type? _type;
-        private static object[] _params;
-
-        private static IEnumerable<string>? SupportedTypes;
-
-        private static Type ClientType => _type ?? throw new Exception("Client type has not been set!");
-
+        
+        
         public static Dictionary<EmailType, Type> MailerTypes = new Dictionary<EmailType, Type>();
-
-        //public static Type _mailer;
-
-        //private static User CurrentUser = null;
-
+        
+        
         public static Guid UserGuid { get; private set; }
 
         public static string UserID { get; private set; }
@@ -44,11 +36,19 @@ namespace InABox.Clients
         public static Guid UserSecurityID { get; private set; }
 
         public static Guid SessionID { get; set; }
+        
 
-        public static string Platform { get; private set; }
+        public static string? Platform { get; private set; }
 
-        public static string Version { get; private set; }
+        public static string? Version { get; private set; }
+        
+        public static Type? ClientType { get; private set; }
+        
+        public static object[]? Parameters { get; private set; }
 
+        public static IEnumerable<string>? SupportedTypes { get; private set; }
+        
+        
         public static string? Recipient2FA { get; private set; }
 
         public static DateTime PasswordExpiration { get; private set; }
@@ -126,23 +126,21 @@ namespace InABox.Clients
             //return result;
         }
 
-        public static void SetClientType(Type type, string platform, string version, params object[] parameters)
+        public static void SetClientType(Type type, string? platform, string? version, params object[]? parameters)
         {
-            _type = type;
-            Platform = platform == null ? Platform : platform;
-            Version = version == null ? Version : version;
-            _params = parameters == null ? _params : parameters;
+            ClientType = type;
+            Platform = String.IsNullOrEmpty(platform) ? Platform : platform;
+            Version = String.IsNullOrEmpty(version) ? Version : version;
+            Parameters = parameters == null ? Parameters : parameters;
             SupportedTypes = parameters == null ? SupportedTypes : null;
         }
 
         public static void ClearClientType()
         {
-            _type = null;
-        }
-
-        public static Type? GetClientType()
-        {
-            return _type;
+            ClientType = null;
+            Platform = "";
+            Version = "";
+            Parameters = null;
         }
 
         // override the need to provide credentials when configuring the database
@@ -337,7 +335,7 @@ namespace InABox.Clients
         public static IClient<TEntity> CreateClient<TEntity>() where TEntity : Entity, new()
         {
             var type = ClientType.MakeGenericType(typeof(TEntity));
-            var client = (IClient<TEntity>)Activator.CreateInstance(type, _params);
+            var client = (IClient<TEntity>)Activator.CreateInstance(type, Parameters);
             //client.UserID = UserID;
             //client.Password = Password;
             //client.Platform = Platform;
@@ -349,7 +347,7 @@ namespace InABox.Clients
         public static IClient CreateClient(Type t)
         {
             var type = ClientType.MakeGenericType(t);
-            var client = (IClient)Activator.CreateInstance(type, _params);
+            var client = (IClient)Activator.CreateInstance(type, Parameters);
             //client.UserID = UserID;
             //client.Password = Password;
             //client.Platform = Platform;

+ 4 - 8
InABox.Database/DbFactory.cs

@@ -13,9 +13,7 @@ namespace InABox.Database
     public static class DbFactory
     {
         public static Dictionary<string, ScriptDocument> LoadedScripts = new();
-
-        private static string _deviceid = "";
-
+        
         private static IProvider? _provider;
         public static IProvider Provider
         {
@@ -41,12 +39,10 @@ namespace InABox.Database
 
         public static DateTime Expiry { get; set; }
 
-        public static void Start(string deviceid)
+        public static void Start()
         {
             CoreUtils.CheckLicensing();
-
-            _deviceid = deviceid;
-
+            
             var status = ValidateSchema();
 
             if (status.Equals(SchemaStatus.New))
@@ -368,7 +364,7 @@ namespace InABox.Database
 
         #region Supported Types
 
-        private class ModuleConfiguration : Dictionary<string, bool>, LocalConfigurationSettings
+        private class ModuleConfiguration : Dictionary<string, bool>, ILocalConfigurationSettings
         {
         }
 

+ 2 - 2
inabox.client.ipc/IPCClient.cs

@@ -101,14 +101,14 @@ namespace InABox.Client.IPC
 
         private void Client_Connected(object? sender, H.Pipes.Args.ConnectionEventArgs<PipeRequest> e)
         {
-            Logger.Send(LogType.Information, "", "Connected to server");
+            Logger.Send(LogType.Information, "", $"Connected to Pipe: {e.Connection.PipeName}");
             Disconnected = false;
             OnConnect?.Invoke();
         }
 
         private void Client_Disconnected(object? sender, H.Pipes.Args.ConnectionEventArgs<PipeRequest> e)
         {
-            Logger.Send(LogType.Information, "", "Disconnected from server");
+            Logger.Send(LogType.Information, "", $"Disconnected from Pipe: {e.Connection.PipeName}");
             foreach (var ev in Events)
             {
                 Responses.TryAdd(ev.Key, PipeRequest.Error(RequestError.DISCONNECTED));

+ 1 - 1
inabox.wpf/DynamicGrid/DynamicGridColumns.cs

@@ -7,7 +7,7 @@ using InABox.Core;
 
 namespace InABox.DynamicGrid
 {
-    public class DynamicGridColumns : List<DynamicGridColumn>, IGlobalConfigurationSettings, UserConfigurationSettings
+    public class DynamicGridColumns : List<DynamicGridColumn>, IGlobalConfigurationSettings, IUserConfigurationSettings
 
     {
         public string GridName { get; set; }

+ 1 - 1
inabox.wpf/DynamicGrid/DynamicGridStyle.cs

@@ -198,7 +198,7 @@ namespace InABox.DynamicGrid
 
             defaultstyle = CreateStyle();
 
-            var stylescript = ClientFactory.GetClientType() == null
+            var stylescript = ClientFactory.ClientType == null
                 ? null
                 : new Client<Script>()
                     .Load(new Filter<Script>(x => x.Section).IsEqualTo(typeof(T).EntityName()).And(x => x.ScriptType).IsEqualTo(ScriptType.RowStyle))