Ejemplo n.º 1
0
    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);
    }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
    /// <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);
    }
Ejemplo n.º 4
0
        /// <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);
        }
Ejemplo n.º 5
0
 public AsyncBagAwaiter(AsyncBag <T> reply)
 {
     reply.Then(x =>
     {
         this.IsCompleted = true;
         this.result      = x;
         this.callback?.Invoke();
     }).Error(x =>
     {
         exception        = x;
         this.IsCompleted = true;
         this.callback?.Invoke();
     });
 }
Ejemplo n.º 6
0
    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);
    }
Ejemplo n.º 7
0
        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));
            }
        }
Ejemplo n.º 8
0
    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);
    }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
        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));
            }
        }
Ejemplo n.º 12
0
        /// <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);
        }