From 4cfad2a242635a96f0758d92f0a711c0ac695ace Mon Sep 17 00:00:00 2001 From: Ahmed Zamil Date: Sat, 20 Feb 2021 21:55:11 +0300 Subject: [PATCH] async put --- Esiur.Stores.MongoDB/MongoDBStore.cs | 5 ++- Esiur.Stores.MongoDB/MongoDBStoreGeneric.cs | 4 +- Esiur/Core/ExceptionCode.cs | 1 + Esiur/Net/IIP/DistributedConnection.cs | 24 +++++++---- .../Net/IIP/DistributedConnectionProtocol.cs | 40 ++++++++++++++----- Esiur/Resource/Warehouse.cs | 31 ++++---------- 6 files changed, 58 insertions(+), 47 deletions(-) diff --git a/Esiur.Stores.MongoDB/MongoDBStore.cs b/Esiur.Stores.MongoDB/MongoDBStore.cs index 4bb63b7..04849e3 100644 --- a/Esiur.Stores.MongoDB/MongoDBStore.cs +++ b/Esiur.Stores.MongoDB/MongoDBStore.cs @@ -48,7 +48,7 @@ namespace Esiur.Stores.MongoDB IMongoDatabase database; IMongoCollection resourcesCollection; - Dictionary resources = new Dictionary(); + KeyList resources = new KeyList(); [Public] @@ -158,7 +158,8 @@ namespace Esiur.Stores.MongoDB else resources.Add(id, new WeakReference(resource)); - Warehouse.Put(resource, document["name"].AsString, this); + //@TODO this causes store.put to be invoked, need fix + await Warehouse.Put(resource, document["name"].AsString, this); var parents = document["parents"].AsBsonArray; diff --git a/Esiur.Stores.MongoDB/MongoDBStoreGeneric.cs b/Esiur.Stores.MongoDB/MongoDBStoreGeneric.cs index 0412d1a..11f2e78 100644 --- a/Esiur.Stores.MongoDB/MongoDBStoreGeneric.cs +++ b/Esiur.Stores.MongoDB/MongoDBStoreGeneric.cs @@ -36,9 +36,9 @@ namespace Esiur.Stores.MongoDB public class MongoDBStore : MongoDBStore where T:IResource { [Public] - public T New(string name = null, object properties = null) + public async AsyncReply New(string name = null, object properties = null) { - var resource = Warehouse.New(name, this, null, null, null, properties); + var resource = await Warehouse.New(name, this, null, null, null, properties); resource.Instance.Managers.AddRange(this.Instance.Managers.ToArray()); return resource; } diff --git a/Esiur/Core/ExceptionCode.cs b/Esiur/Core/ExceptionCode.cs index 17944bd..7e75029 100644 --- a/Esiur/Core/ExceptionCode.cs +++ b/Esiur/Core/ExceptionCode.cs @@ -36,5 +36,6 @@ namespace Esiur.Core SetPropertyDenied, ReadOnlyProperty, GeneralFailure, + AddToStoreFailed } } diff --git a/Esiur/Net/IIP/DistributedConnection.cs b/Esiur/Net/IIP/DistributedConnection.cs index 32be72d..9eb8c8a 100644 --- a/Esiur/Net/IIP/DistributedConnection.cs +++ b/Esiur/Net/IIP/DistributedConnection.cs @@ -759,12 +759,18 @@ namespace Esiur.Net.IIP .Done(); ready = true; - Warehouse.Put(this, this.LocalUsername, null, Server); + Warehouse.Put(this, this.LocalUsername, null, Server).Then(x => + { + openReply?.Trigger(true); + OnReady?.Invoke(this); - openReply?.Trigger(true); - OnReady?.Invoke(this); + Server?.Membership.Login(session); + + }).Error(x=> + { + openReply?.TriggerError(x); + }); - Server?.Membership.Login(session); //Global.Log("auth", LogType.Warning, "U:" + RemoteUsername + " IP:" + Socket.RemoteEndPoint.Address.ToString() + " S:AUTH"); @@ -835,10 +841,12 @@ namespace Esiur.Net.IIP ready = true; // put it in the warehouse - Warehouse.Put(this, this.LocalUsername, null, Server); + Warehouse.Put(this, this.LocalUsername, null, Server).Then(x => + { + openReply?.Trigger(true); + OnReady?.Invoke(this); - openReply?.Trigger(true); - OnReady?.Invoke(this); + }).Error(x=> openReply?.TriggerError(x)); } } @@ -945,7 +953,7 @@ namespace Esiur.Net.IIP } } - return new AsyncReply(); + return new AsyncReply(true); } diff --git a/Esiur/Net/IIP/DistributedConnectionProtocol.cs b/Esiur/Net/IIP/DistributedConnectionProtocol.cs index a923929..4a73833 100644 --- a/Esiur/Net/IIP/DistributedConnectionProtocol.cs +++ b/Esiur/Net/IIP/DistributedConnectionProtocol.cs @@ -770,11 +770,16 @@ namespace Esiur.Net.IIP // create the resource var resource = Activator.CreateInstance(type, args) as IResource; - Warehouse.Put(resource, name, store as IStore, parent); + Warehouse.Put(resource, name, store as IStore, parent).Then(ok => + { + SendReply(IIPPacket.IIPPacketAction.CreateResource, callback) + .AddUInt32(resource.Instance.Id) + .Done(); - SendReply(IIPPacket.IIPPacketAction.CreateResource, callback) - .AddUInt32(resource.Instance.Id) - .Done(); + }).Error(x => + { + SendError(ErrorType.Exception, callback, (ushort)ExceptionCode.AddToStoreFailed); + }); }); }); @@ -1959,14 +1964,27 @@ namespace Esiur.Net.IIP { // ClassId, ResourceAge, ResourceLink, Content if (resource == null) - Warehouse.Put(dr, id.ToString(), this, null, tmp); - - Codec.ParsePropertyValueArray((byte[])rt[3], this).Then((ar) => { - dr._Attach(ar); - resourceRequests.Remove(id); - reply.Trigger(dr); - }); + Warehouse.Put(dr, id.ToString(), this, null, tmp).Then((ok) => + { + Codec.ParsePropertyValueArray((byte[])rt[3], this).Then((ar) => + { + dr._Attach(ar); + resourceRequests.Remove(id); + reply.Trigger(dr); + }); + }).Error(ex=>reply.TriggerError(ex)); + } + else + { + Codec.ParsePropertyValueArray((byte[])rt[3], this).Then((ar) => + { + dr._Attach(ar); + resourceRequests.Remove(id); + reply.Trigger(dr); + }).Error(ex=>reply.TriggerError(ex)); + } + }).Error((ex) => { reply.TriggerError(ex); diff --git a/Esiur/Resource/Warehouse.cs b/Esiur/Resource/Warehouse.cs index 7a91a24..c0628c3 100644 --- a/Esiur/Resource/Warehouse.cs +++ b/Esiur/Resource/Warehouse.cs @@ -535,25 +535,11 @@ namespace Esiur.Resource parent = null; - /* - if (parent == null) - { - if (!(resource is IStore)) - store.Instance.Children.Add(resource); - } - else - parent.Instance.Children.Add(resource); - */ - + if (resource is IStore) - { - stores.TryAdd(resource as IStore, new List>()); - StoreConnected?.Invoke(resource as IStore, name); - } - //else - + if (!await store.Put(resource)) return false; @@ -563,16 +549,12 @@ namespace Esiur.Resource { await parent.Instance.Store.AddChild(parent, resource); await store.AddParent(resource, parent); - //store.AddChild(parent, resource); - } var t = resource.GetType(); Global.Counters["T-" + t.Namespace + "." + t.Name]++; - //var wr = new WeakReference(resource); - //lock (resourcesLock) resources.TryAdd(resource.Instance.Id, resourceReference); if (warehouseIsOpen) @@ -580,11 +562,12 @@ namespace Esiur.Resource await resource.Trigger(ResourceTrigger.Initialize); if (resource is IStore) await resource.Trigger(ResourceTrigger.Open); - - return true; } - else - return true; + + if (resource is IStore) + StoreConnected?.Invoke(resource as IStore, name); + + return true; }