'Periodically sends a ping and checks if we should close the connection
due to the other side timing out.'

def send_ping(self):

    now = self.clock.time_msec()
    if self.time_we_closed:
        if ((now - self.time_we_closed) > PING_TIMEOUT_MS):
            logger.info('[%s]    Failed    to    close    connection    gracefully,    aborting', self.id())
            self.transport.abortConnection()
    else:
        if ((now - self.last_sent_command) >= PING_TIME):
            self.send_command(PingCommand(now))
        if (self.received_ping and ((now - self.last_received_command) > PING_TIMEOUT_MS)):
            logger.info("[%s]    Connection    hasn't    received    command    in    %r    ms.    Closing.", self.id(), (now - self.last_received_command))
            self.send_error('ping    timeout')
