def _socket_connect(self): """Create socket and connect to it, using SSL if enabled.""" # Create our socket and set our socket options self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) self.socket.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) # Wrap the SSL socket if we SSL turned on ssl_text = "" if self.parameters.ssl: ssl_text = " with SSL" if self.parameters.ssl_options: # Always overwrite this value self.parameters.ssl_options['do_handshake_on_connect'] = \ self._ssl_handshake self.socket = ssl.wrap_socket(self.socket, **self.parameters.ssl_options) else: self.socket = ssl.wrap_socket(self.socket, do_handshake_on_connect= \ self._ssl_handshake) # Flags for SSL handshake negotiation self._ssl_connecting = True # Try and connect log.info("Connecting fd %d to %s:%i%s", self.socket.fileno(), self.parameters.host, self.parameters.port, ssl_text) self.socket.settimeout(CONNECTION_TIMEOUT) self.socket.connect((self.parameters.host, self.parameters.port)) # Set the socket to non-blocking self.socket.setblocking(0)
def erase_credentials(self): """ Called by Connection when it no longer needs the credentials """ if self.erase_on_connect: log.info("Erasing stored credential values") self.username = None self.password = None
def on_connection_closed(self, conn): t = self.current_delay * ((random.random() * self.jitter) + 1) log.info("%s retrying %r in %r seconds (%r attempts)", self.__class__.__name__, conn.parameters, t, self.attempts_since_last_success) self.current_delay = min(self.max_delay, self.current_delay * self.multiplier) conn.add_timeout(t, conn.reconnect)
def on_connection_closed(self, conn): t = self.current_delay * ((random() * self.jitter) + 1) info("%s retrying %r in %r seconds (%r attempts)", self.__class__.__name__, conn.parameters, t, self.attempts_since_last_success) self.current_delay = min(self.max_delay, self.current_delay * self.multiplier) conn.add_timeout(t, conn._reconnect)
def on_connection_closed(self, conn): """Calculate our reconnection delay, create a timeout to issue a _reconnect""" if not self.is_active: return t = self.new_delay() info("%s retrying %r in %r seconds (%r attempts)", self.__class__.__name__, conn.parameters, t, self.attempts_since_last_success) conn.add_timeout(t, conn._reconnect)
def on_connected(self, connection): pika_log.info("demo_twisted: Connected to RabbitMQ") d = connection.channel() d.addCallback(self.got_channel) d.addCallback(self.declare_exchange) d.addCallback(self.declare_queue) d.addCallback(self.bind_queue) d.addCallback(self.start_consuming) d.addCallback(self.handle_deliveries) d.addErrback(twisted_log.err)
def _on_connection_closed(self, frame, from_adapter=False): """ Let both our RS and Event object know we closed """ # Set that we're actually closed self.closed = True self.closing = False self.open = False # Call any callbacks registered for this self.callbacks.process(0, "_on_connection_closed", self, self) log.info("Disconnected from RabbitMQ at %s:%i", self.parameters.host, self.parameters.port) # Disconnect our transport if it didn't call on_disconnected if not from_adapter: self._adapter_disconnect()
def _socket_connect(self): """Create socket and connect to it, using SSL if enabled""" # Create our socket and set our socket options self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) self.socket.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) # Wrap the SSL socket if we SSL turned on ssl_text = "" if self.parameters.ssl: ssl_text = " with SSL" if self.parameters.ssl_options: self.socket = ssl.wrap_socket(self.socket, **self.parameters.ssl_options) else: self.socket = ssl.wrap_socket(self.socket) # Try and connect log.info("Connecting to %s:%i%s", self.parameters.host, self.parameters.port, ssl_text) self.socket.connect((self.parameters.host, self.parameters.port)) # Set the socket to non-blocking self.socket.setblocking(0)
def declare_queue(self, _): pika_log.info("demo_twisted: Declaring an anonymous queue") return self.channel.queue_declare(queue='', auto_delete=True, durable=False, exclusive=False)
def bind_queue(self, frame): self.queue_name = frame.method.queue pika_log.info("demo_twisted: Queue %s declared" % self.queue_name) return self.channel.queue_bind(queue=self.queue_name, exchange='twisted', routing_key='1')
def got_channel(self, channel): pika_log.info("demo_twisted: Got the channel") self.channel = channel
def declare_exchange(self, _): pika_log.info("demo_twisted: Declaring the exchange") return self.channel.exchange_declare(exchange="twisted", durable=True, type="direct", auto_delete=True)
def on_connected(self, connection): pika_log.info("demo_twisted: Connected to RabbitMQ") self.connection = connection self.connect('', CustomHandler())
def ping(self): pika_log.info("demo_twisted: Still alive")
def handle_payload(self, channel, method_frame, header_frame, body): pika_log.info("demo_twisted: Message received: %s" % body) if body == 'stop-please': self.stop_consuming()
def stopped_consuming(self, _): pika_log.info("demo_twisted: Consuming stopped, queue deleted")
def handle_deliveries(self, queue_and_consumer_tag): pika_log.info("demo_twisted: Consuming started") queue, consumer_tag = queue_and_consumer_tag self.lc = task.LoopingCall(self.consume_from_queue, queue) return self.lc.start(0)
def start_consuming(self, _): pika_log.info("demo_twisted: Queue bound") return self.channel.basic_consume(queue=self.queue_name, no_ack=True)
def handle_payload(self, channel, method_frame, header_frame, body): pika_log.info("demo_twisted: Message received: %s" % body) # here I use the handler attached to the channel channel.handler.handle_event(header_frame, body) if body == 'stop-please': self.stop_consuming()
def got_channel(self, channel, handler): pika_log.info("demo_twisted: Got the channel") self.channel = channel # here I store handler for messages arriving through this channel channel.channel.handler = handler