Esempio n. 1
0
        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);
        }
Esempio n. 2
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);
    }
Esempio n. 3
0
    /// <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);
    }
Esempio n. 4
0
    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);
        }
    }
Esempio n. 5
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);
    }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
    public async AsyncReply <bool> Connect(string hostname, ushort port)
    {
        var rt = new AsyncReply <bool>();

        this.hostname = hostname;
        this.server   = false;

        state = SocketState.Connecting;
        await sock.ConnectAsync(hostname, port);


        try
        {
            await BeginAsync();

            state = SocketState.Established;
            //OnConnect?.Invoke();
            Receiver?.NetworkConnect(this);
        }
        catch (Exception ex)
        {
            state = SocketState.Closed;// .Terminated;
            Close();
            Global.Log(ex);
        }

        return(true);
    }
Esempio n. 8
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);
        }
Esempio n. 9
0
    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);
    }
Esempio n. 10
0
        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);
            }
        }
Esempio n. 11
0
    //public AsyncQueue<T> Then(Action<T> callback)
    //{
    //  base.Then(new Action<object>(o => callback((T)o)));

    //return this;
    //}

    public void Add(AsyncReply <T> reply)
    {
        lock (queueLock)
            list.Add(reply);

        resultReady = false;
        reply.Then(processQueue);
    }
Esempio n. 12
0
    /*
     * private static IResource[] QureyIn(string[] path, int index, IEnumerable<IResource> resources)// AutoList<IResource, Instance> resources)
     * {
     *  var rt = new List<IResource>();
     *
     *  if (index == path.Length - 1)
     *  {
     *      if (path[index] == "")
     *          foreach (IResource child in resources)
     *            rt.Add(child);
     *       else
     *          foreach (IResource child in resources)
     *              if (child.Instance.Name == path[index])
     *                  rt.Add(child);
     *  }
     *  else
     *      foreach (IResource child in resources)
     *          if (child.Instance.Name == path[index])
     *              rt.AddRange(QureyIn(path, index+1, child.Instance.Children<IResource>()));
     *
     *  return rt.ToArray();
     * }
     *
     * public static AsyncReply<IResource[]> Query(string path)
     * {
     *  if (path == null || path == "")
     *  {
     *      var roots = stores.Where(s => s.Instance.Parents<IResource>().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 async AsyncReply <IResource[]> Query(string path)
    {
        var rt = new AsyncReply <IResource[]>();

        var       p = path.Trim().Split('/');
        IResource resource;

        foreach (var store in stores.Keys)
        {
            if (p[0] == store.Instance.Name)
            {
                if (p.Length == 1)
                {
                    return new IResource[] { store }
                }
                ;

                var res = await store.Get(String.Join("/", p.Skip(1).ToArray()));

                if (res != null)
                {
                    return new IResource[] { res }
                }
                ;


                resource = store;
                for (var i = 1; i < p.Length; i++)
                {
                    var children = await resource.Instance.Children <IResource>(p[i]);

                    if (children.Length > 0)
                    {
                        if (i == p.Length - 1)
                        {
                            return(children);
                        }
                        else
                        {
                            resource = children[0];
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                return(null);
            }
        }



        return(null);
    }
        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);
        }
Esempio n. 14
0
        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);
        }
Esempio n. 15
0
        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);
        }
Esempio n. 16
0
        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);
        }
Esempio n. 17
0
    public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
    {
        var ft = Instance.Template.GetFunctionTemplateByName(binder.Name);

        var reply = new AsyncReply <object>();

        if (attached && ft != null)
        {
            var indexedArgs = new Map <byte, object>();

            if (args.Length == 1)
            {
                // Detect anonymous types
                var type = args[0].GetType();


                if (Codec.IsAnonymous(type))
                {
                    var pis = type.GetProperties();

                    for (byte i = 0; i < ft.Arguments.Length; i++)
                    {
                        var pi = pis.FirstOrDefault(x => x.Name == ft.Arguments[i].Name);
                        if (pi != null)
                        {
                            indexedArgs.Add(i, pi.GetValue(args[0]));
                        }
                    }

                    result = _Invoke(ft.Index, indexedArgs);
                }
                else
                {
                    indexedArgs.Add((byte)0, args[0]);
                    result = _Invoke(ft.Index, indexedArgs);
                }
            }
            else
            {
                for (byte i = 0; i < args.Length; i++)
                {
                    indexedArgs.Add(i, args[i]);
                }

                result = _Invoke(ft.Index, indexedArgs);
            }
            return(true);
        }
        else
        {
            result = null;
            return(false);
        }
    }
Esempio n. 18
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);
        }
Esempio n. 19
0
 public AsyncAwaiter(AsyncReply <T> reply)
 {
     reply.Then(x =>
     {
         this.IsCompleted = true;
         this.result      = (T)x;
         this.callback?.Invoke();
     }).Error(x =>
     {
         exception        = x;
         this.IsCompleted = true;
         this.callback?.Invoke();
     });
 }
Esempio n. 20
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);
    }
Esempio n. 21
0
    public AsyncReply <bool> SendAsync(byte[] message, int offset, int length)
    {
        if (state == SocketState.Closed)// || state == SocketState.Terminated)
        {
            return(new AsyncReply <bool>(false));
        }

        var msg = message.Clip((uint)offset, (uint)length);

        lock (sendLock)
        {
            if (state == SocketState.Closed)// || state == SocketState.Terminated)
            {
                return(new AsyncReply <bool>(false));
            }

            if (!sock.Connected)
            {
                return(new AsyncReply <bool>(false));
            }

            var rt = new AsyncReply <bool>();

            if (asyncSending || held)
            {
                sendBufferQueue.Enqueue(new KeyValuePair <AsyncReply <bool>, byte[]>(rt, msg));
            }
            else
            {
                asyncSending = true;
                try
                {
                    currentReply = rt;
                    sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, sendCallback, this);// null);
                }
                catch (Exception ex)
                {
                    rt.TriggerError(ex);
                    asyncSending = false;
                    //state = SocketState.Terminated;
                    Close();
                }
                //sock.SendAsync(new ArraySegment<byte>(msg), SocketFlags.None).ContinueWith(DataSent);
            }

            return(rt);
        }
    }
Esempio n. 22
0
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            var ft = Instance.Template.GetFunctionTemplate(binder.Name);

            var reply = new AsyncReply();

            if (isAttached && ft != null)
            {
                result = _Invoke(ft.Index, args);
                return(true);
            }
            else
            {
                result = null;
                return(false);
            }
        }
Esempio n. 23
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));
            }
        }
Esempio n. 24
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);
    }
Esempio n. 25
0
        /// <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);
        }
Esempio n. 26
0
    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);
    }
Esempio n. 27
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);
        }
Esempio n. 28
0
    public AsyncReply <bool> SendAsync(byte[] message, int offset, int length)
    {
        var msg = message.Clip((uint)offset, (uint)length);

        lock (sendLock)
        {
            if (!sock.Connected)
            {
                return(new AsyncReply <bool>(false));
            }

            var rt = new AsyncReply <bool>();

            if (asyncSending || held)
            {
                sendBufferQueue.Enqueue(new KeyValuePair <AsyncReply <bool>, byte[]>(rt, msg));
            }
            else
            {
                asyncSending = true;
                try
                {
                    ssl.BeginWrite(msg, 0, msg.Length, SendCallback, rt);// null);
                }
                catch (Exception ex)
                {
                    rt.TriggerError(ex);
                    asyncSending = false;
                    //state = SocketState.Terminated;
                    Close();
                }
            }

            return(rt);
        }
    }
Esempio n. 29
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);
        }
Esempio n. 30
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));
            }
        }