public static AsyncReply <IResource[]> Query(string path) { if (path == null || path == "") { var roots = stores.Where(s => s.Instance.Parents.Count == 0).ToArray(); return(new AsyncReply <IResource[]>(roots)); } else { var rt = new AsyncReply <IResource[]>(); Get(path).Then(x => { var p = path.Split('/'); if (x == null) { rt.Trigger(QureyIn(p, 0, stores)); } else { var ar = QureyIn(p, 0, stores).Where(r => r != x).ToList(); ar.Insert(0, x); rt.Trigger(ar.ToArray()); } }); return(rt); } }
public static AsyncReply TupleParser(byte[] data, uint offset, uint length, DistributedConnection connection, uint[] requestSequence) { var results = new AsyncBag <object>(); var rt = new AsyncReply(); var tupleSize = data[offset++]; length--; var types = new List <Type>(); for (var i = 0; i < tupleSize; i++) { var(cs, rep) = RepresentationType.Parse(data, offset); types.Add(rep.GetRuntimeType()); offset += cs; length -= cs; } while (length > 0) { var(cs, reply) = Codec.Parse(data, offset, connection, requestSequence); results.Add(reply); if (cs > 0) { offset += (uint)cs; length -= (uint)cs; } else { throw new Exception("Error while parsing structured data"); } } results.Seal(); results.Then(ar => { if (ar.Length == 2) { var type = typeof(ValueTuple <,>).MakeGenericType(types.ToArray()); rt.Trigger(Activator.CreateInstance(type, ar[0], ar[1])); } else if (ar.Length == 3) { var type = typeof(ValueTuple <, ,>).MakeGenericType(types.ToArray()); rt.Trigger(Activator.CreateInstance(type, ar[0], ar[1], ar[2])); } else if (ar.Length == 4) { var type = typeof(ValueTuple <, , ,>).MakeGenericType(types.ToArray()); rt.Trigger(Activator.CreateInstance(type, ar[0], ar[1], ar[2], ar[3])); } }); return(rt); }
/// <summary> /// Close the warehouse. /// This function issues terminate trigger to all resources and stores. /// </summary> /// <returns>True, if no problem occurred.</returns> public static AsyncReply <bool> Close() { var bag = new AsyncBag <bool>(); 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 <bool>(); bag.Then((x) => { foreach (var b in x) { if (!b) { rt.Trigger(false); return; } } rt.Trigger(true); }); return(rt); }
public AsyncReply <ISocket> Accept() { var reply = new AsyncReply <ISocket>(); try { sock.AcceptAsync().ContinueWith((x) => { try { reply.Trigger(new SSLSocket(x.Result, cert, true)); } catch { reply.Trigger(null); } }, null); } catch { state = SocketState.Terminated; return(null); } return(reply); }
/// <summary> /// Open the warehouse. /// This function issues the initialize trigger to all stores and resources. /// </summary> /// <returns>True, if no problem occurred.</returns> public static AsyncReply <bool> Open() { var bag = new AsyncBag <bool>(); foreach (var store in stores) { bag.Add(store.Trigger(ResourceTrigger.Initialize)); } bag.Seal(); var rt = new AsyncReply <bool>(); bag.Then((x) => { foreach (var b in x) { if (!b) { rt.Trigger(false); return; } } var rBag = new AsyncBag <bool>(); foreach (var rk in resources) { rBag.Add(rk.Value.Trigger(ResourceTrigger.SystemInitialized)); } rBag.Seal(); rBag.Then(y => { foreach (var b in y) { if (!b) { rt.Trigger(false); return; } } rt.Trigger(true); storeIsOpen = true; }); }); return(rt); }
public AsyncReply <bool> Connect(string hostname, ushort port) { var rt = new AsyncReply <bool>(); try { state = SocketState.Connecting; sock.ConnectAsync(hostname, port).ContinueWith((x) => { if (x.IsFaulted) { rt.TriggerError(x.Exception); } else { state = SocketState.Established; //OnConnect?.Invoke(); Receiver?.NetworkConnect(this); Begin(); rt.Trigger(true); } }); } catch (Exception ex) { rt.TriggerError(ex); } return(rt); }
public static unsafe AsyncReply EnumParser(byte[] data, uint offset, uint length, DistributedConnection connection, uint[] requestSequence) { var classId = data.GetGuid(offset); offset += 16; var index = data[offset++]; var template = Warehouse.GetTemplateByClassId((Guid)classId, TemplateType.Enum); if (template != null) { return(new AsyncReply(template.Constants[index].Value)); } else { var reply = new AsyncReply(); connection.GetTemplate((Guid)classId).Then(tmp => { reply.Trigger(tmp.Constants[index].Value); }).Error(x => reply.TriggerError(x)); return(reply); } }
/// <summary> /// Set property value. /// </summary> /// <param name="index">Zero-based property index.</param> /// <param name="value">Value</param> /// <returns>Indicator when the property is set.</returns> protected internal AsyncReply <object> _Set(byte index, object value) { if (index >= properties.Length) { return(null); } var reply = new AsyncReply <object>(); var parameters = Codec.Compose(value, connection); connection.SendRequest(Packets.IIPPacket.IIPPacketAction.SetProperty) .AddUInt32(instanceId) .AddUInt8(index) .AddUInt8Array(parameters) .Done() .Then((res) => { // not really needed, server will always send property modified, // this only happens if the programmer forgot to emit in property setter properties[index] = value; reply.Trigger(null); }); return(reply); }
public AsyncReply <string> Stream(int count) { var reply = new AsyncReply <string>(); var msg = new object[] { "Have you throught what if a function has multiple returns ?", "So you can return chunks of IO operation that not yet finished.", "Also, what about the progress ?", "This is an example of both.", "Use it anyway you like" }; Timer timer = null; var msgCounter = 0; timer = new Timer((x) => { reply.TriggerProgress(AsyncReply.ProgressType.Execution, count, 22); if (count % 2 == 0 && msgCounter < msg.Length) { reply.TriggerChunk(msg[msgCounter++]); } count--; if (count <= 0) { timer.Dispose(); reply.Trigger("Done"); } }, null, 10, 3000); return(reply); }
public AsyncReply <KeyList <PropertyTemplate, PropertyValue[]> > GetRecord(IResource resource, DateTime fromDate, DateTime toDate) { var properties = resource.Instance.Template.Properties.Where(x => x.Storage == StorageMode.Recordable).ToList(); var reply = new AsyncReply <KeyList <PropertyTemplate, PropertyValue[]> >(); AsyncBag <PropertyValue[]> bag = new AsyncBag <PropertyValue[]>(); foreach (var p in properties) { bag.Add(GetPropertyRecordByDate(resource, p.Name, fromDate, toDate)); } bag.Seal(); bag.Then(x => { var list = new KeyList <PropertyTemplate, PropertyValue[]>(); for (var i = 0; i < x.Length; i++) { list.Add(properties[i], x[i]); } reply.Trigger(list); }); return(reply); }
public AsyncReply <object> Void() { var rt = new AsyncReply <object>(); _InvokeByArrayArguments(0, new object[] { }) .Then(x => rt.Trigger((object)x)) .Error(x => rt.TriggerError(x)) .Chunk(x => rt.TriggerChunk(x)); return(rt); }
public AsyncReply <object> ConnectionOptional(object a1, int a2, string a3) { var rt = new AsyncReply <object>(); _InvokeByArrayArguments(4, new object[] { a1, a2, a3 }) .Then(x => rt.Trigger((object)x)) .Error(x => rt.TriggerError(x)) .Chunk(x => rt.TriggerChunk(x)); return(rt); }
public AsyncReply <object> InvokeEvents(string msg) { var rt = new AsyncReply <object>(); _InvokeByArrayArguments(1, new object[] { msg }) .Then(x => rt.Trigger((object)x)) .Error(x => rt.TriggerError(x)) .Chunk(x => rt.TriggerChunk(x)); return(rt); }
public AsyncReply <int> ChildMethod(string childName) { var rt = new AsyncReply <int>(); _InvokeByArrayArguments(0, new object[] { childName }) .Then(x => rt.Trigger((int)x)) .Error(x => rt.TriggerError(x)) .Chunk(x => rt.TriggerChunk(x)); return(rt); }
public static AsyncReply TypedMapParser(byte[] data, uint offset, uint length, DistributedConnection connection, uint[] requestSequence) { // get key type var(keyCs, keyRepType) = RepresentationType.Parse(data, offset); offset += keyCs; length -= keyCs; var(valueCs, valueRepType) = RepresentationType.Parse(data, offset); offset += valueCs; length -= valueCs; var map = (IMap)Activator.CreateInstance(typeof(Map <,>).MakeGenericType(keyRepType.GetRuntimeType(), valueRepType.GetRuntimeType())); var rt = new AsyncReply(); var results = new AsyncBag <object>(); while (length > 0) { var(cs, reply) = Codec.Parse(data, offset, connection, requestSequence); results.Add(reply); if (cs > 0) { offset += (uint)cs; length -= (uint)cs; } else { throw new Exception("Error while parsing structured data"); } } results.Seal(); results.Then(ar => { for (var i = 0; i < ar.Length; i += 2) { map.Add(ar[i], ar[i + 1]); } rt.Trigger(map); }); return(rt); }
public AsyncReply <bool> Trigger(ResourceTrigger trigger) { if (trigger == ResourceTrigger.Initialize) { var filter = new BsonDocument(); var list = resourcesCollection.Find(filter).ToList(); Console.WriteLine(list.Count); // if (list.Count == 0) // return new AsyncBag<IResource>(new IResource[0]); var bag = new AsyncBag <IResource>(); for (var i = 0; i < list.Count; i++) { Console.WriteLine("Loading {0}/{1}", i, list.Count); bag.Add(Get("id/" + list[i]["_id"].AsObjectId.ToString())); } bag.Seal(); var rt = new AsyncReply <bool>(); bag.Then((x) => { rt.Trigger(true); }); return(rt); } else if (trigger == ResourceTrigger.Terminate) { // save all resources foreach (var resource in resources.Values) { SaveResource(resource); } return(new AsyncReply <bool>(true)); } else { return(new AsyncReply <bool>(true)); } }
public static AsyncReply <KeyList <PropertyTemplate, PropertyValue[]> > HistoryParser(byte[] data, uint offset, uint length, IResource resource, DistributedConnection connection, uint[] requestSequence) { //var count = (int)toAge - (int)fromAge; var list = new KeyList <PropertyTemplate, PropertyValue[]>(); var reply = new AsyncReply <KeyList <PropertyTemplate, PropertyValue[]> >(); var bagOfBags = new AsyncBag <PropertyValue[]>(); var ends = offset + length; while (offset < ends) { var index = data[offset++]; var pt = resource.Instance.Template.GetPropertyTemplateByIndex(index); list.Add(pt, null); var cs = data.GetUInt32(offset, Endian.Little); offset += 4; var(len, pv) = PropertyValueParser(data, offset, connection, requestSequence); bagOfBags.Add(pv);// ParsePropertyValueArray(data, offset, cs, connection)); offset += len; } bagOfBags.Seal(); bagOfBags.Then(x => { for (var i = 0; i < list.Count; i++) { list[list.Keys.ElementAt(i)] = x[i]; } reply.Trigger(list); }); return(reply); }
/// <summary> /// Parse property value. /// </summary> /// <param name="data">Array of bytes.</param> /// <param name="offset">Zero-indexed offset.</param> /// <param name="connection">DistributedConnection is required to fetch resources.</param> /// <param name="cs">Output content size.</param> /// <returns>PropertyValue.</returns> public static AsyncReply <PropertyValue> ParsePropertyValue(byte[] data, uint offset, out uint cs, DistributedConnection connection)//, bool ageIncluded = true) { var reply = new AsyncReply <PropertyValue>(); var age = data.GetUInt64(offset); offset += 8; DateTime date = data.GetDateTime(offset); offset += 8; uint valueSize; Parse(data, offset, out valueSize, connection).Then(value => { reply.Trigger(new PropertyValue(value, age, date)); }); cs = 16 + valueSize; return(reply); }
public static (uint, AsyncReply <PropertyValue>) PropertyValueParser(byte[] data, uint offset, DistributedConnection connection, uint[] requestSequence)//, bool ageIncluded = true) { var reply = new AsyncReply <PropertyValue>(); var age = data.GetUInt64(offset, Endian.Little); offset += 8; DateTime date = data.GetDateTime(offset, Endian.Little); offset += 8; var(valueSize, results) = Codec.Parse(data, offset, connection, requestSequence); results.Then(value => { reply.Trigger(new PropertyValue(value, age, date)); }); return(16 + valueSize, reply); }
public AsyncReply <PropertyValue[]> GetPropertyRecordByDate(IResource resource, string propertyName, DateTime fromDate, DateTime toDate) { var objectId = resource.Instance.Attributes["objectId"].ToString(); var record = this.database.GetCollection <BsonDocument>("record_" + objectId); var builder = Builders <BsonDocument> .Filter; var filter = builder.Gte("date", fromDate) & builder.Lte("date", toDate) & builder.Eq("property", propertyName); var reply = new AsyncReply <PropertyValue[]>(); record.FindAsync(filter).ContinueWith((x) => { var values = ((Task <IAsyncCursor <BsonDocument> >)x).Result.ToList(); var bag = new AsyncBag <object>(); foreach (var v in values) { bag.Add(Parse(v["value"])); } bag.Seal(); bag.Then((results) => { var list = new List <PropertyValue>(); for (var i = 0; i < results.Length; i++) { list.Add(new PropertyValue(results[i], (ulong)values[i]["age"].AsInt64, values[i]["date"].ToUniversalTime())); } reply.Trigger(list.ToArray()); }); }); return(reply); }
public void SetResult() { reply.Trigger(null); }
AsyncReply <IResource> Fetch(string id) { var filter = Builders <BsonDocument> .Filter.Eq("_id", new BsonObjectId(new ObjectId(id))); var list = resourcesCollection.Find(filter).ToList(); if (list.Count == 0) { return(new AsyncReply <IResource>(null)); } var document = list[0]; IResource resource = (IResource)Activator.CreateInstance(Type.GetType(document["classname"].AsString)); resources.Add(document["_id"].AsObjectId.ToString(), resource); Warehouse.Put(resource, document["name"].AsString, this); var parents = document["parents"].AsBsonArray; var children = document["children"].AsBsonArray; //var managers = document["managers"].AsBsonArray; var attributes = Parse(document["attributes"]).Then(x => { resource.Instance.SetAttributes(x as Structure); }); var bag = new AsyncBag <object>(); foreach (var p in parents) { var ap = Warehouse.Get(p.AsString); bag.Add(ap); ap.Then((x) => { if (!resource.Instance.Parents.Contains(x)) { resource.Instance.Parents.Add(x); } }); } foreach (var c in children) { var ac = Warehouse.Get(c.AsString); bag.Add(ac); ac.Then((x) => { if (!resource.Instance.Children.Contains(x)) { resource.Instance.Children.Add(x); } }); } /* * // load managers * foreach(var m in managers) * { * IPermissionsManager pm = (IPermissionsManager)Activator.CreateInstance(Type.GetType(m["classname"].AsString)); * var sr = Parse(m["settings"]); * bag.Add(sr); * sr.Then((x) => * { * pm.Initialize((Structure)x, resource); * resource.Instance.Managers.Add(pm); * }); * } */ // Load values var values = document["values"].AsBsonDocument; foreach (var v in values) { var valueInfo = v.Value as BsonDocument; var av = Parse(valueInfo["value"]); bag.Add(av); av.Then((x) => { resource.Instance.LoadProperty(v.Name, (ulong)valueInfo["age"].AsInt64, valueInfo["modification"].ToUniversalTime(), x); }); } bag.Seal(); var rt = new AsyncReply <IResource>(); bag.Then((x) => { rt.Trigger(resource); }); return(rt); }
public void SetResult(T result) { reply.Trigger(result); }
/// <summary> /// Parse a structure /// </summary> /// <param name="data">Bytes array</param> /// <param name="offset">Zero-indexed offset.</param> /// <param name="length">Number of bytes to parse.</param> /// <param name="connection">DistributedConnection is required in case a structure in the array holds items at the other end.</param> /// <param name="parsedKeys">Array to store keys in.</param> /// <param name="parsedTypes">Array to store DataTypes in.</param> /// <param name="keys">Array of keys, in case the data doesn't include keys</param> /// <param name="types">Array of DataTypes, in case the data doesn't include DataTypes</param> /// <returns>Structure</returns> public static AsyncReply <Structure> ParseStructure(byte[] data, uint offset, uint length, DistributedConnection connection, out Structure.StructureMetadata metadata, string[] keys = null, DataType[] types = null)// out string[] parsedKeys, out DataType[] parsedTypes, string[] keys = null, DataType[] types = null) { var reply = new AsyncReply <Structure>(); var bag = new AsyncBag <object>(); var keylist = new List <string>(); var typelist = new List <DataType>(); if (keys == null) { while (length > 0) { var len = data[offset++]; keylist.Add(data.GetString(offset, len)); offset += len; typelist.Add((DataType)data[offset]); uint rt; bag.Add(Codec.Parse(data, offset, out rt, connection)); length -= rt + len + 1; offset += rt; } } else if (types == null) { keylist.AddRange(keys); while (length > 0) { typelist.Add((DataType)data[offset]); uint rt; bag.Add(Codec.Parse(data, offset, out rt, connection)); length -= rt; offset += rt; } } else { keylist.AddRange(keys); typelist.AddRange(types); var i = 0; while (length > 0) { uint rt; bag.Add(Codec.Parse(data, offset, out rt, connection, types[i])); length -= rt; offset += rt; i++; } } bag.Seal(); bag.Then((res) => { // compose the list var s = new Structure(); for (var i = 0; i < keylist.Count; i++) { s[keylist[i]] = res[i]; } reply.Trigger(s); }); metadata = new Structure.StructureMetadata() { Keys = keylist.ToArray(), Types = typelist.ToArray() }; return(reply); }
AsyncReply Parse(BsonValue value) { if (value.BsonType == BsonType.Document) { var doc = value.AsBsonDocument; if (doc["type"] == 0) { return(Warehouse.Get(doc["link"].AsString)); } // structure else if (doc["type"] == 1) { var bag = new AsyncBag <object>(); var rt = new AsyncReply <Structure>(); var bs = (BsonDocument)doc["values"].AsBsonDocument; var s = new Structure(); foreach (var v in bs) { bag.Add(Parse(v.Value)); } bag.Seal(); bag.Then((x) => { for (var i = 0; i < x.Length; i++) { s[bs.GetElement(i).Name] = x[i]; } rt.Trigger(s); }); return(rt); } else { return(new AsyncReply(null)); } } else if (value.BsonType == BsonType.Array) { var array = value.AsBsonArray; var bag = new AsyncBag <object>(); foreach (var v in array) { bag.Add(Parse(v)); } bag.Seal(); return(bag); } else if (value.BsonType == BsonType.DateTime) { return(new AsyncReply(value.ToUniversalTime())); } else { return(new AsyncReply(value.RawValue)); } }
public static unsafe AsyncReply RecordParser(byte[] data, uint offset, uint length, DistributedConnection connection, uint[] requestSequence) { var reply = new AsyncReply <IRecord>(); var classId = data.GetGuid(offset); offset += 16; length -= 16; var template = Warehouse.GetTemplateByClassId((Guid)classId, TemplateType.Record); var initRecord = (TypeTemplate template) => { ListParser(data, offset, length, connection, requestSequence).Then(r => { var ar = (object[])r; if (template.DefinedType != null) { var record = Activator.CreateInstance(template.DefinedType) as IRecord; for (var i = 0; i < template.Properties.Length; i++) { try { var v = Convert.ChangeType(ar[i], template.Properties[i].PropertyInfo.PropertyType); template.Properties[i].PropertyInfo.SetValue(record, v); } catch (Exception ex) { Console.WriteLine(ex); } } reply.Trigger(record); } else { var record = new Record(); for (var i = 0; i < template.Properties.Length; i++) { record.Add(template.Properties[i].Name, ar[i]); } reply.Trigger(record); } }); }; if (template != null) { initRecord(template); } else { connection.GetTemplate((Guid)classId).Then(tmp => { ListParser(data, offset, length, connection, requestSequence).Then(r => { if (tmp == null) { reply.TriggerError(new AsyncException(ErrorType.Management, (ushort)ExceptionCode.TemplateNotFound, "Template not found for record.")); } else { initRecord(tmp); } }); }).Error(x => reply.TriggerError(x)); } return(reply); }