コード例 #1
0
ファイル: RpcDht.cs プロジェクト: pcbing/brunet
        public void HandleRpc(ISender caller, string method, IList args, object rs)
        {
            if (LocalUseOnly)
            {
                try {
                    ReqrepManager.ReplyState _rs = (ReqrepManager.ReplyState)caller;
                    Node node = (Node)_rs.ReturnPath;
                    if (node != _node)
                    {
                        throw new Exception();
                    }
                } catch {
                    AdrException e = new AdrException(-32602, new Exception("Must send from local node!"));
                    _node.Rpc.SendResult(rs, e);
                    return;
                }
            }

            object result = null;

            try {
                switch (method)
                {
                case "Create":
                {
                    // Needs to be Async so we don't deadlock!
                    MemBlock key     = MemBlock.Reference((byte[])args[0]);
                    MemBlock value   = MemBlock.Reference((byte[])args[1]);
                    int      ttl     = (int)args[2];
                    Channel  returns = new Channel(1);
                    returns.CloseEvent += delegate(object o, EventArgs eargs) {
                        _node.Rpc.SendResult(rs, returns.Dequeue());
                    };
                    _dht.AsyncCreate(key, value, ttl, returns);
                    return;
                }

                case "Put":
                {
                    // Needs to be Async so we don't deadlock!
                    MemBlock key     = MemBlock.Reference((byte[])args[0]);
                    MemBlock value   = MemBlock.Reference((byte[])args[1]);
                    int      ttl     = (int)args[2];
                    Channel  returns = new Channel(1);
                    returns.CloseEvent += delegate(object o, EventArgs eargs) {
                        _node.Rpc.SendResult(rs, returns.Dequeue());
                    };
                    _dht.AsyncPut(key, value, ttl, returns);
                    return;
                }

                case "Get":
                {
                    // Needs to be Async so we don't deadlock!
                    MemBlock key     = MemBlock.Reference((byte[])args[0]);
                    Channel  returns = new Channel();
                    returns.CloseEvent += delegate(object o, EventArgs eargs) {
                        Hashtable [] results = new Hashtable[returns.Count];
                        int          pos     = 0;
                        while (returns.Count > 0)
                        {
                            results[pos++] = (Hashtable)returns.Dequeue();
                        }
                        _node.Rpc.SendResult(rs, results);
                    };
                    _dht.AsyncGet(key, returns);
                    return;
                }

                case "BeginGet":
                {
                    MemBlock key = MemBlock.Reference((byte[])args[0]);
                    result = BeginGet(key);
                    break;
                }

                case "ContinueGet":
                {
                    MemBlock token = MemBlock.Reference((byte[])args[0]);
                    ContinueGet(token, rs);
                    return;
                }

                case "EndGet":
                {
                    MemBlock token = MemBlock.Reference((byte[])args[0]);
                    EndGet(token);
                    result = true;
                    break;
                }
                }
            } catch (Exception e) {
                result = new AdrException(-32602, e);
            }
            _node.Rpc.SendResult(rs, result);
        }