/* Copyright (c) 2017 Ahmed Kh. Zamil Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ using Esiur.Core; using Esiur.Data; using Esiur.Misc; using Esiur.Net.IIP; using Esiur.Net.Packets; using Esiur.Proxy; using Esiur.Resource.Template; using Esiur.Security.Permissions; using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace Esiur.Resource; // Central Resources Manager public class Warehouse { public static Warehouse Default = new Warehouse(); //static byte prefixCounter; //static AutoList stores = new AutoList(null); ConcurrentDictionary> resources = new ConcurrentDictionary>(); ConcurrentDictionary>> stores = new ConcurrentDictionary>>(); uint resourceCounter = 0; KeyList> templates = new KeyList>() { //[TemplateType.Unspecified] = new KeyList(), [TemplateType.Resource] = new KeyList(), [TemplateType.Record] = new KeyList(), //[TemplateType.Wrapper] = new KeyList(), [TemplateType.Enum] = new KeyList(), }; bool warehouseIsOpen = false; public delegate void StoreEvent(IStore store); public event StoreEvent StoreConnected; public event StoreEvent StoreDisconnected; public delegate AsyncReply ProtocolInstance(string name, object properties); public KeyList Protocols { get; } = new KeyList(); private Regex urlRegex = new Regex(@"^(?:([\S]*)://([^/]*)/?)"); public Warehouse() { Protocols.Add("iip", async (name, attributes) => await New(name, null, attributes)); new TypeTemplate(typeof(IIPAuthPacketIAuthHeader), this); new TypeTemplate(typeof(IIPAuthPacketIAuthDestination), this); new TypeTemplate(typeof(IIPAuthPacketIAuthFormat), this); } /// /// Get a store by its name. /// /// Store instance name /// public IStore GetStore(string name) { foreach (var s in stores) if (s.Key.Instance.Name == name) return s.Key; return null; } public WeakReference[] Resources => resources.Values.ToArray(); /// /// Get a resource by instance Id. /// /// Instance Id /// public AsyncReply GetById(uint id) { if (resources.ContainsKey(id)) { IResource r; if (resources[id].TryGetTarget(out r)) return new AsyncReply(r); else return new AsyncReply(null); } else return new AsyncReply(null); } void LoadGenerated() { foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { var generatedType = assembly.GetType("Esiur.Generated"); if (generatedType != null) { var resourceTypes = (Type[])generatedType.GetProperty("Resources").GetValue(null); foreach (var t in resourceTypes) { PutTemplate(new TypeTemplate(t)); } var recordTypes = (Type[])generatedType.GetProperty("Records").GetValue(null); foreach (var t in recordTypes) { PutTemplate(new TypeTemplate(t)); } var enumsTypes = (Type[])generatedType.GetProperty("Enums").GetValue(null); foreach (var t in enumsTypes) { PutTemplate(new TypeTemplate(t)); } } } } /// /// Open the warehouse. /// This function issues the initialize trigger to all stores and resources. /// /// True, if no problem occurred. public async AsyncReply Open() { if (warehouseIsOpen) return false; // Load generated models LoadGenerated(); warehouseIsOpen = true; var resSnap = resources.Select(x => { IResource r; if (x.Value.TryGetTarget(out r)) return r; else return null; }).Where(r => r != null).ToArray(); foreach (var r in resSnap) { //IResource r; //if (rk.Value.TryGetTarget(out r)) //{ var rt = await r.Trigger(ResourceTrigger.Initialize); //if (!rt) // return false; if (!rt) { Global.Log("Warehouse", LogType.Warning, $"Resource failed at Initialize {r.Instance.Name} [{r.Instance.Template.ClassName}]"); } //} } foreach (var r in resSnap) { var rt = await r.Trigger(ResourceTrigger.SystemInitialized); if (!rt) { Global.Log("Warehouse", LogType.Warning, $"Resource failed at SystemInitialized {r.Instance.Name} [{r.Instance.Template.ClassName}]"); } } return true; } /// /// Close the warehouse. /// This function issues terminate trigger to all resources and stores. /// /// True, if no problem occurred. public AsyncReply Close() { var bag = new AsyncBag(); foreach (var resource in resources.Values) { IResource r; if (resource.TryGetTarget(out r)) { if (!(r is IStore)) bag.Add(r.Trigger(ResourceTrigger.Terminate)); } } foreach (var store in stores) bag.Add(store.Key.Trigger(ResourceTrigger.Terminate)); foreach (var resource in resources.Values) { IResource r; if (resource.TryGetTarget(out r)) { if (!(r is IStore)) bag.Add(r.Trigger(ResourceTrigger.SystemTerminated)); } } foreach (var store in stores) bag.Add(store.Key.Trigger(ResourceTrigger.SystemTerminated)); bag.Seal(); var rt = new AsyncReply(); bag.Then((x) => { foreach (var b in x) if (!b) { rt.Trigger(false); return; } rt.Trigger(true); }); return rt; } public async AsyncReply Query(string path) { var p = path.Trim().TrimStart('/').Split('/'); foreach (var store in stores.Keys) { if (p[0] == store.Instance.Name) { if (p.Length == 1) return store; var res = await store.Get(String.Join("/", p.Skip(1).ToArray())); if (res != null) return res; return null; } } return null; } /// /// Get a resource by its path. /// Resource path is sperated by '/' character, e.g. "system/http". /// /// /// Resource instance. public async AsyncReply Get(string path, object attributes = null, IResource parent = null, IPermissionsManager manager = null) where T : IResource { if (urlRegex.IsMatch(path)) { var url = urlRegex.Split(path); if (Protocols.ContainsKey(url[1])) { if (!warehouseIsOpen) await Open(); var handler = Protocols[url[1]]; var store = await handler(url[2], attributes); try { if (url[3].Length > 0 && url[3] != "") return (T)await store.Get(url[3]); else return (T)store; } catch (Exception ex) { Remove(store); throw ex; } } } var res = await Query(path); if (res == null) return default(T); else return (T)res; } /// /// Put a resource in the warehouse. /// /// Resource name. /// Resource instance. /// IStore that manages the resource. Can be null if the resource is a store. /// Parent resource. if not presented the store becomes the parent for the resource. public async AsyncReply Put(string instanceName, T resource, IStore store, TypeTemplate customTemplate = null, ulong age = 0, IPermissionsManager manager = null, object attributes = null) where T : IResource { if (resource.Instance != null) throw new Exception("Resource has a store."); var resourceReference = new WeakReference(resource); if (resource is IStore && store == null) store = (IStore)resource; if (store == null) throw new Exception("Resource store is not set."); //if (store == null) //{ // // assign parent's store as a store // if (parent != null) // { // // assign parent as a store // if (parent is IStore) // { // store = (IStore)parent; // List> list; // if (stores.TryGetValue(store, out list)) // lock (((ICollection)list).SyncRoot) // list.Add(resourceReference); // //stores[store].Add(resourceReference); // } // else // { // store = parent.Instance.Store; // List> list; // if (stores.TryGetValue(store, out list)) // lock (((ICollection)list).SyncRoot) // list.Add(resourceReference); // } // } // // assign self as a store (root store) // else if (resource is IStore) // { // store = (IStore)resource; // } // else // throw new Exception("Can't find a store for the resource."); //} resource.Instance = new Instance(this, resourceCounter++, instanceName, resource, store, customTemplate, age); if (attributes != null) if (attributes is Map attrs) resource.Instance.SetAttributes(attrs); else resource.Instance.SetAttributes(Map.FromObject(attributes)); if (manager != null) resource.Instance.Managers.Add(manager); //if (store == parent) // parent = null; try { if (resource is IStore) stores.TryAdd(resource as IStore, new List>()); if (!await store.Put(resource)) throw new Exception("Store failed to put the resource"); //return default(T); //if (parent != null) //{ // await parent.Instance.Store.AddChild(parent, resource); // await store.AddParent(resource, parent); //} var t = resource.GetType(); Global.Counters["T-" + t.Namespace + "." + t.Name]++; resources.TryAdd(resource.Instance.Id, resourceReference); if (warehouseIsOpen) { await resource.Trigger(ResourceTrigger.Initialize); if (resource is IStore) await resource.Trigger(ResourceTrigger.Open); } if (resource is IStore) StoreConnected?.Invoke(resource as IStore); } catch (Exception ex) { Remove(resource); throw ex; } return resource; } /// /// Put a resource in the warehouse. /// /// Resource name. /// Resource instance. /// IStore that manages the resource. Can be null if the resource is a store. /// Parent resource. if not presented the store becomes the parent for the resource. public async AsyncReply Put(string path, T resource, TypeTemplate customTemplate = null, ulong age = 0, IPermissionsManager manager = null, object attributes = null) where T : IResource { if (resource.Instance != null) throw new Exception("Resource has a store."); var location = path.TrimStart('/').Split('/'); IResource parent = null; var instanceName = location.Last(); if (location.Length <= 1 && !(resource is IStore)) throw new Exception("Can't find the store."); else if (location.Length == 1) return await Put(path, resource, null, customTemplate, age, manager, attributes); else { // get parent parent = await Get(string.Join("/", location.Take(location.Length - 1))); if (parent == null) throw new Exception("Can't find parent"); return await Put(instanceName, resource, parent.Instance.Store, customTemplate, age, manager, attributes); } } public T CreateInstance(object properties = null) { return (T)CreateInstance(typeof(T), properties); } public IResource CreateInstance(Type type, object properties = null) { type = ResourceProxy.GetProxy(type); var res = Activator.CreateInstance(type) as IResource; if (properties != null) { if (properties is Map map) { var template = GetTemplateByType(type); foreach (var kvp in map) template.GetPropertyTemplateByIndex(kvp.Key).PropertyInfo.SetValue(res, kvp.Value); } else { var ps = Map.FromObject(properties); foreach (var p in ps) { var pi = type.GetProperty(p.Key, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); if (pi != null) { if (pi.CanWrite) { try { pi.SetValue(res, p.Value); } catch (Exception ex) { Global.Log(ex); } } } else { var fi = type.GetField(p.Key, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); if (fi != null) { try { fi.SetValue(res, p.Value); } catch (Exception ex) { Global.Log(ex); } } } } } } return res; } public async AsyncReply New(Type type, string path, IPermissionsManager manager = null, object attributes = null, object properties = null) { var res = CreateInstance(type, properties); return await Put(path, res, null, 0, manager, attributes); } public async AsyncReply New(string path, IPermissionsManager manager = null, object attributes = null, object properties = null) where T : IResource { return (T)(await New(typeof(T), path, manager, attributes, properties)); } /// /// Put a resource template in the templates warehouse. /// /// Resource template. public void PutTemplate(TypeTemplate template) { if (templates[template.Type].ContainsKey(template.ClassId)) throw new Exception($"Template with same class Id already exists. {templates[template.Type][template.ClassId].ClassName} -> {template.ClassName}"); templates[template.Type][template.ClassId] = template; } /// /// Get a template by type from the templates warehouse. If not in the warehouse, a new ResourceTemplate is created and added to the warehouse. /// /// .Net type. /// Resource template. public TypeTemplate GetTemplateByType(Type type) { if (!(type.IsClass || type.IsEnum)) return null; var baseType = ResourceProxy.GetBaseType(type); if (baseType == typeof(IResource) || baseType == typeof(IRecord)) return null; TemplateType templateType; if (Codec.ImplementsInterface(type, typeof(IResource))) templateType = TemplateType.Resource; else if (Codec.ImplementsInterface(type, typeof(IRecord))) templateType = TemplateType.Record; else if (type.IsEnum) templateType = TemplateType.Enum; else return null; var template = templates[templateType].Values.FirstOrDefault(x => x.DefinedType == baseType); if (template != null) return template; // create new template for type template = new TypeTemplate(baseType, this); TypeTemplate.GetDependencies(template, this); return template; } /// /// Get a template by class Id from the templates warehouse. If not in the warehouse, a new ResourceTemplate is created and added to the warehouse. /// /// Class Id. /// Resource template. public TypeTemplate GetTemplateByClassId(UUID classId, TemplateType? templateType = null) { if (templateType == null) { // look into resources var template = templates[TemplateType.Resource][classId]; if (template != null) return template; // look into records template = templates[TemplateType.Record][classId]; if (template != null) return template; // look into enums template = templates[TemplateType.Enum][classId]; return template; //if (template != null) //// look in wrappers //template = templates[TemplateType.Wrapper][classId]; //return template; } else return templates[templateType.Value][classId]; } /// /// Get a template by class name from the templates warehouse. If not in the warehouse, a new ResourceTemplate is created and added to the warehouse. /// /// Class name. /// Resource template. public TypeTemplate GetTemplateByClassName(string className, TemplateType? templateType = null) { if (templateType == null) { // look into resources var template = templates[TemplateType.Resource].Values.FirstOrDefault(x => x.ClassName == className); if (template != null) return template; // look into records template = templates[TemplateType.Record].Values.FirstOrDefault(x => x.ClassName == className); if (template != null) return template; // look into enums template = templates[TemplateType.Enum].Values.FirstOrDefault(x => x.ClassName == className); //if (template != null) return template; //// look in wrappers //template = templates[TemplateType.Wrapper].Values.FirstOrDefault(x => x.ClassName == className); //return template; } else { return templates[templateType.Value].Values.FirstOrDefault(x => x.ClassName == className); } } public bool Remove(IResource resource) { if (resource.Instance == null) return false; WeakReference resourceReference; if (resources.ContainsKey(resource.Instance.Id)) resources.TryRemove(resource.Instance.Id, out resourceReference); else return false; if (resource != resource.Instance.Store) { List> list; if (stores.TryGetValue(resource.Instance.Store, out list)) { lock (((ICollection)list).SyncRoot) list.Remove(resourceReference); } } if (resource is IStore) { var store = resource as IStore; List> toBeRemoved; stores.TryRemove(store, out toBeRemoved); foreach (var o in toBeRemoved) { IResource r; if (o.TryGetTarget(out r)) Remove(r); } StoreDisconnected?.Invoke(resource as IStore); } if (resource.Instance.Store != null) resource.Instance.Store.Remove(resource); resource.Destroy(); resource.Instance = null; return true; } }