def wait_for_result(message)
    res = nil
    message_id = message.msg_id
    @pending[message_id] = Time.now
    while res.nil?
      @response_lock.synchronize{
        # Prune messages that timed out
        if @timeout
          now = Time.now
          @pending.delete_if { |_, start_time| (now - start_time) > @timeout }
        end
        pending_ids = @pending.keys
        fail 'Timed out' unless pending_ids.include? message_id

        # Prune invalid responses
        @responses.keep_if { |response| @pending.has_key? response.first }
        res = @responses.find { |response| message.msg_id == response.first }
        if !res.nil?
          @responses.delete(res)
        else
          @response_cv.wait @response_lock, @wait_interval
        end
      }
    end
    return res
  end
def wait_for_result(message)
    res = nil
    message_id = message.msg_id
    @pending[message_id] = Time.now
    while res.nil?
      @response_lock.synchronize{
        # Prune messages that timed out
        if @timeout
          now = Time.now
          @pending.delete_if { |_, start_time| (now - start_time) > @timeout }
        end
        pending_ids = @pending.keys
        fail 'Timed out' unless pending_ids.include? message_id

        # Prune invalid responses
        @responses.keep_if { |response| @pending.has_key? response.first }
        res = @responses.find { |response| message.msg_id == response.first }
        if !res.nil?
          @responses.delete(res)
        else
          @response_cv.wait @response_lock, @wait_interval
        end
      }
    end
    return res
  end
Internal helper, block until response matching message id is received