def check(id = nil, max_ping_timeouts = MAX_PING_TIMEOUTS)
      unless @terminating || @ping_timer || (id && !@sender.agent.client.connected?(id))
        @ping_id = id
        @ping_timer = EM::Timer.new(PING_TIMEOUT) do
          if @ping_id
            begin
              @ping_stats.update("timeout")
              @ping_timer = nil
              @ping_timeouts[@ping_id] = (@ping_timeouts[@ping_id] || 0) + 1
              if @ping_timeouts[@ping_id] >= max_ping_timeouts
                ErrorTracker.log(self, "Mapper ping via broker #{@ping_id} timed out after #{PING_TIMEOUT} seconds and now " +
                                       "reached maximum of #{max_ping_timeouts} timeout#{max_ping_timeouts > 1 ? 's' : ''}, " +
                                       "attempting to reconnect")
                host, port, index, priority = @sender.client.identity_parts(@ping_id)
                @sender.agent.connect(host, port, index, priority, force = true)
              else
                Log.warning("Mapper ping via broker #{@ping_id} timed out after #{PING_TIMEOUT} seconds")
              end
            rescue Exception => e
              ErrorTracker.log(self, "Failed to reconnect to broker #{@ping_id}", e)
            end
          else
            @ping_timer = nil
          end
        end

        handler = lambda do |_|
          begin
            if @ping_timer
              @ping_stats.update("success")
              @ping_timer.cancel
              @ping_timer = nil
              @ping_timeouts[@ping_id] = 0
              @ping_id = nil
            end
          rescue Exception => e
            ErrorTracker.log(self, "Failed to cancel router ping", e)
          end
        end
        request = Request.new("/router/ping", nil, {:from => @sender.identity, :token => AgentIdentity.generate})
        @sender.pending_requests[request.token] = PendingRequest.new(Request, Time.now, handler)
        ids = [@ping_id] if @ping_id
        @ping_id = @sender.send(:publish, request, ids).first
      end
      true
    end
def check(id = nil, max_ping_timeouts = MAX_PING_TIMEOUTS)
      unless @terminating || @ping_timer || (id && !@sender.agent.client.connected?(id))
        @ping_id = id
        @ping_timer = EM::Timer.new(PING_TIMEOUT) do
          if @ping_id
            begin
              @ping_stats.update("timeout")
              @ping_timer = nil
              @ping_timeouts[@ping_id] = (@ping_timeouts[@ping_id] || 0) + 1
              if @ping_timeouts[@ping_id] >= max_ping_timeouts
                ErrorTracker.log(self, "Mapper ping via broker #{@ping_id} timed out after #{PING_TIMEOUT} seconds and now " +
                                       "reached maximum of #{max_ping_timeouts} timeout#{max_ping_timeouts > 1 ? 's' : ''}, " +
                                       "attempting to reconnect")
                host, port, index, priority = @sender.client.identity_parts(@ping_id)
                @sender.agent.connect(host, port, index, priority, force = true)
              else
                Log.warning("Mapper ping via broker #{@ping_id} timed out after #{PING_TIMEOUT} seconds")
              end
            rescue Exception => e
              ErrorTracker.log(self, "Failed to reconnect to broker #{@ping_id}", e)
            end
          else
            @ping_timer = nil
          end
        end

        handler = lambda do |_|
          begin
            if @ping_timer
              @ping_stats.update("success")
              @ping_timer.cancel
              @ping_timer = nil
              @ping_timeouts[@ping_id] = 0
              @ping_id = nil
            end
          rescue Exception => e
            ErrorTracker.log(self, "Failed to cancel router ping", e)
          end
        end
        request = Request.new("/router/ping", nil, {:from => @sender.identity, :token => AgentIdentity.generate})
        @sender.pending_requests[request.token] = PendingRequest.new(Request, Time.now, handler)
        ids = [@ping_id] if @ping_id
        @ping_id = @sender.send(:publish, request, ids).first
      end
      true
    end
Check whether broker connection is usable by pinging a router via that broker
 Attempt to reconnect if ping does not respond in PING_TIMEOUT seconds and
 if have reached timeout limit
 Ignore request if already checking a connection

 === Parameters
 id(String):: Identity of specific broker to use to send ping, defaults to any
   currently connected broker
 max_ping_timeouts(Integer):: Maximum number of ping timeouts before attempt
   to reconnect, defaults to MAX_PING_TIMEOUTS

 === Return
 true:: Always return true