def send_request(req, options={})
      fail "Must pass a request" unless req.request
      req.add_origin_host_and_realm(@local_host, @local_realm)

      peer = options[:peer]

      if peer.nil?
        peer = if req['Destination-Host']
                 peer_identity = req['Destination-Host'].octet_string
                 Diameter.logger.debug("Selecting peer by Destination-Host (#{peer_identity})")
                 @peer_table[peer_identity]
               elsif req['Destination-Realm']
                 realm = req['Destination-Realm'].octet_string
                 Diameter.logger.debug("Selecting peer by Destination-Realm (#{realm})")
                 @peer_table.values.select { |p| p.realm == realm }.sample
               else
                 fail "Request must have Destination-Host or Destination-Realm"
               end
      else
        Diameter.logger.debug("Peer selection forced to #{peer.identity}")
      end

      if peer.nil?
        Diameter.logger.warn("No peer is available to send message - cannot route")
        fail "No acceptable peer"
      elsif peer.state == :UP
        q = Queue.new
        @pending_ete[req.ete] = q
        @tcp_helper.send(req.to_wire, peer.cxn)

=begin
        # Time this request out if no answer is received
        Diameter.logger.debug("Scheduling timeout for #{@answer_timeout}s time")
        Concurrent::timer(@answer_timeout) do
          Diameter.logger.debug("Timing out message with EtE #{req.ete}")
          q = @pending_ete.delete(req.ete)
          if q
            q.push(:timeout)
          end
        end
=end

        p = Concurrent::Promise.execute(executor: @threadpool) {
          Diameter.logger.debug("Waiting for answer to message with EtE #{req.ete}, queue #{q}")
          val = q.pop
          Diameter.logger.debug("Promise fulfilled for message with EtE #{req.ete}")
          val
        }
        return p
      else
        Diameter.logger.warn("Peer #{peer.identity} is in state #{peer.state} - cannot route")
      end
    end
def send_request(req, options={})
      fail "Must pass a request" unless req.request
      req.add_origin_host_and_realm(@local_host, @local_realm)

      peer = options[:peer]

      if peer.nil?
        peer = if req['Destination-Host']
                 peer_identity = req['Destination-Host'].octet_string
                 Diameter.logger.debug("Selecting peer by Destination-Host (#{peer_identity})")
                 @peer_table[peer_identity]
               elsif req['Destination-Realm']
                 realm = req['Destination-Realm'].octet_string
                 Diameter.logger.debug("Selecting peer by Destination-Realm (#{realm})")
                 @peer_table.values.select { |p| p.realm == realm }.sample
               else
                 fail "Request must have Destination-Host or Destination-Realm"
               end
      else
        Diameter.logger.debug("Peer selection forced to #{peer.identity}")
      end

      if peer.nil?
        Diameter.logger.warn("No peer is available to send message - cannot route")
        fail "No acceptable peer"
      elsif peer.state == :UP
        q = Queue.new
        @pending_ete[req.ete] = q
        @tcp_helper.send(req.to_wire, peer.cxn)

=begin
        # Time this request out if no answer is received
        Diameter.logger.debug("Scheduling timeout for #{@answer_timeout}s time")
        Concurrent::timer(@answer_timeout) do
          Diameter.logger.debug("Timing out message with EtE #{req.ete}")
          q = @pending_ete.delete(req.ete)
          if q
            q.push(:timeout)
          end
        end
=end

        p = Concurrent::Promise.execute(executor: @threadpool) {
          Diameter.logger.debug("Waiting for answer to message with EtE #{req.ete}, queue #{q}")
          val = q.pop
          Diameter.logger.debug("Promise fulfilled for message with EtE #{req.ete}")
          val
        }
        return p
      else
        Diameter.logger.warn("Peer #{peer.identity} is in state #{peer.state} - cannot route")
      end
    end
Sends a Diameter request. This is routed to an appropriate peer
 based on the Destination-Host AVP (or, if that is absent, on the
 Destination-Realm AVP).

 This adds this stack's Origin-Host and Origin-Realm AVPs, if
 those AVPs don't already exist.

 @param req [Message] The request to send.
 @param peer [Peer] (Optional) A peer to use as the first hop for the message