2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-09-13 12:43:17 +00:00
This commit is contained in:
2025-08-24 14:51:18 +03:00
parent a1353a03ad
commit e64d96bd0d
5 changed files with 113 additions and 65 deletions

View File

@@ -69,7 +69,7 @@ public class EsiurProxyRewrite : IModelFinalizingConvention
if (Codec.ImplementsInterface(entityType.ClrType, typeof(IResource))) if (Codec.ImplementsInterface(entityType.ClrType, typeof(IResource)))
{ {
// check if the object exists // check if the object exists
var obj = options.Warehouse.CreateInstance(entityType.ClrType) as IResource; var obj = options.Warehouse.Create(entityType.ClrType) as IResource;
options.Store.TypesByType[entityType.ClrType].PrimaryKey.SetValue(obj, id); options.Store.TypesByType[entityType.ClrType].PrimaryKey.SetValue(obj, id);
options.Warehouse.Put(id.ToString(), obj, options.Store, null, 0, manager).Wait(); options.Warehouse.Put(id.ToString(), obj, options.Store, null, 0, manager).Wait();
return obj; return obj;

View File

@@ -430,7 +430,7 @@ public partial class DistributedConnection : NetworkConnection, IStore
if (packet.DataType == null) if (packet.DataType == null)
return offset; return offset;
Console.WriteLine("Incoming: " + packet);
if (packet.Method == IIPPacketMethod.Notification) if (packet.Method == IIPPacketMethod.Notification)
{ {
var dt = packet.DataType.Value; var dt = packet.DataType.Value;

View File

@@ -55,6 +55,18 @@ class IIPPacket : Packet
return base.Compose(); return base.Compose();
} }
public override string ToString()
{
return Method switch
{
IIPPacketMethod.Notification => $"{Method} {Notification}",
IIPPacketMethod.Request => $"{Method} {Request}",
IIPPacketMethod.Reply => $"{Method} {Reply}",
IIPPacketMethod.Extension => $"{Method} {Extension}",
_ => $"{Method}"
};
}
bool NotEnough(uint offset, uint ends, uint needed) bool NotEnough(uint offset, uint ends, uint needed)
{ {
if (offset + needed > ends) if (offset + needed > ends)
@@ -80,11 +92,11 @@ class IIPPacket : Packet
if (Method == IIPPacketMethod.Notification) if (Method == IIPPacketMethod.Notification)
{ {
Notification = (IIPPacketNotification)(data[offset++] & 0x3f); Notification = (IIPPacketNotification)(data[offset++] & 0x1f);
} }
else if (Method == IIPPacketMethod.Request) else if (Method == IIPPacketMethod.Request)
{ {
Request = (IIPPacketRequest)(data[offset++] & 0x3f); Request = (IIPPacketRequest)(data[offset++] & 0x1f);
if (NotEnough(offset, ends, 4)) if (NotEnough(offset, ends, 4))
return -dataLengthNeeded; return -dataLengthNeeded;
@@ -94,7 +106,7 @@ class IIPPacket : Packet
} }
else if (Method == IIPPacketMethod.Reply) else if (Method == IIPPacketMethod.Reply)
{ {
Reply = (IIPPacketReply)(data[offset++] & 0x3f); Reply = (IIPPacketReply)(data[offset++] & 0x1f);
if (NotEnough(offset, ends, 4)) if (NotEnough(offset, ends, 4))
return -dataLengthNeeded; return -dataLengthNeeded;
@@ -104,7 +116,7 @@ class IIPPacket : Packet
} }
else if (Method == IIPPacketMethod.Extension) else if (Method == IIPPacketMethod.Extension)
{ {
Extension = (byte)(data[offset++] & 0x3f); Extension = (byte)(data[offset++] & 0x1f);
} }
if (hasDTU) if (hasDTU)

View File

@@ -361,39 +361,6 @@ public class Warehouse
if (store == null) if (store == null)
throw new Exception("Resource store is not set."); 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<WeakReference<IResource>> list;
// if (stores.TryGetValue(store, out list))
// lock (((ICollection)list).SyncRoot)
// list.Add(resourceReference);
// //stores[store].Add(resourceReference);
// }
// else
// {
// store = parent.Instance.Store;
// List<WeakReference<IResource>> 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); resource.Instance = new Instance(this, resourceCounter++, instanceName, resource, store, customTemplate, age);
@@ -464,34 +431,115 @@ public class Warehouse
if (resource.Instance != null) if (resource.Instance != null)
throw new Exception("Resource has a store."); throw new Exception("Resource has a store.");
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException("Invalid path.");
var location = path.TrimStart('/').Split('/'); var location = path.TrimStart('/').Split('/');
IResource parent = null; IResource parent = null;
IStore store = null;
var instanceName = location.Last(); var instanceName = location.Last();
if (location.Length <= 1 && !(resource is IStore)) if (location.Length == 1)
throw new Exception("Can't find the store.");
else if (location.Length == 1)
return await Put<T>(path, resource, null, customTemplate, age, manager, attributes);
else
{ {
// get parent if (!(resource is IStore))
parent = await Get<IResource>(string.Join("/", location.Take(location.Length - 1))); throw new Exception("Resource is not a store, root level path is not allowed.");
if (parent == null)
throw new Exception("Can't find parent");
return await Put<T>(instanceName, resource, parent.Instance.Store, customTemplate, age, manager, attributes);
} }
if (location.Length == 1 &&
if (location.Length == 1)
return await Put<T>(path, resource, null, customTemplate, age, manager, attributes);
else
{
// get parent
parent = await Get<IResource>(string.Join("/", location.Take(location.Length - 1)));
if (parent == null)
throw new Exception("Can't find parent");
return await Put<T>(instanceName, resource, parent.Instance.Store, customTemplate, age, manager, attributes);
}
var resourceReference = new WeakReference<IResource>(resource);
if (resource is IStore && store == null)
store = (IStore)resource;
if (store == null)
throw new Exception("Resource store is not set.");
resource.Instance = new Instance(this, resourceCounter++, instanceName, resource, store, customTemplate, age);
if (attributes != null)
if (attributes is Map<string, object> attrs)
resource.Instance.SetAttributes(attrs);
else
resource.Instance.SetAttributes(Map<string, object>.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<WeakReference<IResource>>());
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;
} }
public T CreateInstance<T>(object properties = null) public T Create<T>(object properties = null)
{ {
return (T)CreateInstance(typeof(T), properties); return (T)Create(typeof(T), properties);
} }
public IResource CreateInstance(Type type, object properties = null) public IResource Create(Type type, object properties = null)
{ {
type = ResourceProxy.GetProxy(type); type = ResourceProxy.GetProxy(type);

View File

@@ -65,18 +65,6 @@ public class MemoryStore : IStore
return new AsyncReply<bool>(true); return new AsyncReply<bool>(true);
} }
//public AsyncReply<IResource> Retrieve(uint iid)
//{
// if (resources.ContainsKey(iid))
// {
// if (resources.ContainsKey(iid))// .TryGetTarget(out r))
// return new AsyncReply<IResource>(resources[iid]);
// else
// return new AsyncReply<IResource>(null);
// }
// else
// return new AsyncReply<IResource>(null);
//}
public AsyncReply<bool> Trigger(ResourceTrigger trigger) public AsyncReply<bool> Trigger(ResourceTrigger trigger)
{ {