Dequeue() public method

public Dequeue ( ) : object
return object
Example #1
0
    override public void Start()
    {
      Channel returns = new Channel();
      returns.EnqueueEvent += delegate(object o, EventArgs ea) {
        while(returns.Count > 0) {
          Hashtable result = null;
          try {
            result = returns.Dequeue() as Hashtable;
          } catch {
            continue;
          }

          byte[] res = result["value"] as byte[];
          if(res != null) {
            Results.Enqueue(MemBlock.Reference(res));
          }
        }
        if(_enqueue != null) {
          _enqueue(this, EventArgs.Empty);
        }
      };

      returns.CloseEvent += delegate(object o, EventArgs ea) {
        Finished();
      };

      Dht dht = new Dht(Node, 3, 20);
      dht.AsyncGet(Key, returns);
    }
Example #2
0
 override public void Start()
 {
   Channel returns = new Channel();
   returns.CloseEvent += delegate(object o, EventArgs ea) {
     try {
       bool success = (bool) returns.Dequeue();
       _result = new DhtPutResult(success, null);
     } catch (Exception e) {
       _result = new DhtPutResult(false, e);
     } finally {
       Finished();
     }
   };
   Dht dht = new Dht(Node, 3, 20);
   dht.AsyncPut(Key, Value, Ttl, returns);
 }
Example #3
0
      public void HandleEC(bool succ, Edge e, Exception x) {
        if(!succ) {
          ECB(false, null, x);
          return;
        } else if(_root) {
          Interlocked.Increment(ref _pel._count);
          e.CloseEvent += _pel.CloseHandler;
          ECB(succ, e, x);
          return;
        }

        /*
         * Got the underlying Edge, now do the path protocol
         */ 
        Channel results = new Channel(1);
        results.CloseEvent += delegate(object q, EventArgs args) {
          try {
            RpcResult res = (RpcResult)results.Dequeue();
            object o = res.Result;
            if(o is Exception) {
              throw (o as Exception);
            }
          } catch(Exception cx) {
            e.Close();
            ECB(false, null, cx);
            return;
          }

          //If we get here, everything looks good:
          PathEdge pe = new PathEdge(_pel._pem, e, LocalPath, RemotePath);
          //Start sending e's packets into pe
          pe.Subscribe();
          pe.CloseEvent += _pel.CloseHandler;
          Interlocked.Increment(ref _pel._count);
          ECB(true, pe, null);
        };

        //Make sure we hear the packets on this edge:
        e.Subscribe(_pel._pem, null);
        //Now make the rpc call:
        _pel._pem.Rpc.Invoke(e, results, "sys:pathing.create", LocalPath, RemotePath ); 
      }
Example #4
0
 /**
  * Call the GetStatus method on the given connection
  */
 protected void CallGetStatus(string type, Connection c) {
   ConnectionTable tab = this.ConnectionTable;
   if( c != null ) {
     StatusMessage req = GetStatus(type, c.Address);
     Channel stat_res = new Channel(1);
     EventHandler handle_result = delegate(object q, EventArgs eargs) {
       try {
         RpcResult r = (RpcResult)stat_res.Dequeue();
         StatusMessage sm = new StatusMessage( (IDictionary)r.Result );
         tab.UpdateStatus(c, sm);
       }
       catch(Exception) {
         //Looks like lc disappeared before we could update it
       }
     };
     stat_res.CloseEvent += handle_result;
     _rpc.Invoke(c.Edge, stat_res, "sys:link.GetStatus", req.ToDictionary() );
   }
 }
Example #5
0
      public void Start()
      {
        Channel returns = new Channel();
        returns.CloseEvent += delegate(object o, EventArgs ea) {
          try {
            _successful = (bool) returns.Dequeue();
          } catch {
          }

          _done = true;
          if(_callback != null) {
            _callback(this, EventArgs.Empty);
          }
        };
        Dht dht = new Dht(_node, 3, 20);
        dht.AsyncPut(_key, _value, _ttl, returns);
      }
Example #6
0
        /**
         * Check all the edges in the ConnectionTable and see if any of them
         * need to be pinged or closed.
         * This method is connected to the heartbeat event.
         */
        virtual protected void CheckEdgesCallback(object node, EventArgs args)
        {
            DateTime now = DateTime.UtcNow;

            //_connection_timeout = ComputeDynamicTimeout();
            _connection_timeout = MAX_CONNECTION_TIMEOUT;

            /*
             * If we haven't heard from any of these people in this time,
             * we ping them, and if we don't get a response, we close them
             */
            foreach (Connection c in _connection_table)
            {
                Edge     e             = c.Edge;
                TimeSpan since_last_in = now - e.LastInPacketDateTime;
                if ((1 == _send_pings) && (since_last_in > _connection_timeout))
                {
                    object       ping_arg = String.Empty;
                    DateTime     start    = DateTime.UtcNow;
                    EventHandler on_close = delegate(object q, EventArgs cargs) {
                        BCon.Channel qu = (BCon.Channel)q;
                        if (qu.Count == 0)
                        {
                            /* we never got a response! */
                            if (!e.IsClosed)
                            {
                                //We are going to close it after waiting:
                                ProtocolLog.WriteIf(ProtocolLog.NodeLog, String.Format(
                                                        "On an edge timeout({1}), closing connection: {0}",
                                                        c, DateTime.UtcNow - start));
                                //Make sure it is indeed closed.
                                e.Close();
                            }
                            else
                            {
                                //The edge was closed somewhere else, so it
                                //didn't timeout.
                            }
                        }
                        else
                        {
                            //We got a response, let's make sure it's not an exception:
                            bool close = false;
                            try {
                                RpcResult r = (RpcResult)qu.Dequeue();
                                object    o = r.Result; //This will throw an exception if there was a problem
                                if (!o.Equals(ping_arg))
                                {
                                    //Something is wrong with the other node:
                                    ProtocolLog.WriteIf(ProtocolLog.NodeLog, String.Format(
                                                            "Ping({0}) != {1} on {2}", ping_arg, o, c));
                                    close = true;
                                }
                            }
                            catch (Exception x) {
                                ProtocolLog.WriteIf(ProtocolLog.Exceptions, String.Format(
                                                        "Ping on {0}: resulted in: {1}", c, x));
                                close = true;
                            }
                            if (close)
                            {
                                e.Close();
                            }
                        }
                    };
                    BCon.Channel tmp_queue = new BCon.Channel(1);
                    tmp_queue.CloseEvent += on_close;
                    //Do the ping
                    try {
                        _rpc.Invoke(e, tmp_queue, "sys:link.Ping", ping_arg);
                    }
                    catch (EdgeClosedException) {
                        //Just ignore closed edges, clearly we can't ping them
                    }
                    catch (EdgeException x) {
                        if (!x.IsTransient)
                        {
                            //Go ahead and close the Edge
                            e.Close();
                        }
                    }
                }
            }
            foreach (Edge e in _connection_table.GetUnconnectedEdges())
            {
                if (now - e.LastInPacketDateTime > _unconnected_timeout)
                {
                    if (ProtocolLog.Connections.Enabled)
                    {
                        ProtocolLog.Write(ProtocolLog.Connections, String.Format(
                                              "Closed an unconnected edge: {0}", e));
                    }
                    e.Close();
                }
            }
        }
Example #7
0
 /// <summary>Starts a test on the specified node.</summary>
 protected void Invoke(Address addr)
 {
   Channel q = new Channel(1);
   q.CloseEvent += delegate(object send, EventArgs ea) {
     RpcResult result = null;
     if(q.Count > 0) {
       result = q.Dequeue() as RpcResult;
     }
     ProcessResults(addr, result);
   };
   AHSender sender = new AHGreedySender(_node, addr);
   _node.Rpc.Invoke(sender, q, "sys:link.GetNeighbors");
 }
Example #8
0
    override protected void SeekTAs(DateTime now)
    {
      if(Interlocked.Exchange(ref _ongoing, 1) == 1) {
        return;
      }

      Channel chan = new Channel();

      EventHandler handler = delegate(object o, EventArgs ea) {
        if(!chan.Closed && chan.Count < 8) {
          return;
        }

        List<TransportAddress> tas = new List<TransportAddress>();
        while(chan.Count > 0) {
          AHAddress addr = null;
          try {
            IDictionary dict = (IDictionary) chan.Dequeue();
            byte[] baddr = (byte[]) dict["value"];
            addr = new AHAddress(MemBlock.Reference(baddr));
          } catch {
            continue;
          }
          tas.Add(new SubringTransportAddress(addr, _shared_namespace));
        }

        if(tas.Count > 0) {
          CheckAndUpdateRemoteTAs(tas);
        }

        if(chan.Closed) {
          Interlocked.Exchange(ref _ongoing, 0);
        }
      };

      if(_steady_state == 0) {
        chan.EnqueueEvent += handler;
      }
      chan.CloseEvent += handler;

      try {
        _dht.AsyncGet(_private_dht_key, chan);
      } catch(DhtException) {
        chan.Close();
      }
    }