def wait(self, condition, timeout=False, msg=None): """Call process until condition() is true""" if timeout is False: timeout = self.timeout if timeout is None: while not condition() and not self.disconnected: self.container.process() else: container_timeout = self.container.timeout self.container.timeout = timeout try: deadline = time.time() + timeout while not condition() and not self.disconnected: self.container.process() if deadline < time.time(): txt = "Connection %s timed out" % self.url if msg: txt += ": " + msg raise Timeout(txt) finally: self.container.timeout = container_timeout if self.disconnected or self._is_closed(): self.container.stop() self.conn.handler = None # break cyclical reference if self.disconnected and not self._is_closed(): raise ConnectionException("Connection %s disconnected: %s" % (self.url, self.disconnected))
def connect(self, host, port=None, username=None, password=None, force_sasl=True, ssl_domain=None): if ssl_domain: self.ssl = SSL(self.transport, ssl_domain) self.ssl.peer_hostname = host if username and password: sasl = self.transport.sasl() sasl.plain(username, password) elif force_sasl: sasl = self.transport.sasl() sasl.mechanisms('ANONYMOUS') sasl.client() try: self.socket.connect_ex((host, port or 5672)) except socket.gaierror, e: raise ConnectionException("Cannot resolve '%s': %s" % (host, e))
def on_disconnected(self, event): raise ConnectionException("Connection %s disconnected" % self.url);
def on_transport_closed(self, event): if event.connection.state & Endpoint.LOCAL_ACTIVE: raise ConnectionException("Connection %s disconnected" % self.url);
def on_transport_error(self, event): self.retry_attempts += 1 if self.retry_attempts == 10: raise ConnectionException(event.transport.condition.description) super()