Beispiel #1
0
    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)
Beispiel #2
0
    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)
Beispiel #3
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)
Beispiel #5
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() * 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)
Beispiel #8
0
 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)
Beispiel #9
0
 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)
Beispiel #10
0
    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()
Beispiel #11
0
    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)
Beispiel #12
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)
Beispiel #13
0
 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')
Beispiel #14
0
 def got_channel(self, channel):
     pika_log.info("demo_twisted: Got the channel")
     self.channel = channel
Beispiel #15
0
 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)
Beispiel #16
0
 def on_connected(self, connection):
     pika_log.info("demo_twisted: Connected to RabbitMQ")
     self.connection = connection
     self.connect('', CustomHandler())
Beispiel #17
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)
Beispiel #18
0
 def ping(self):
     pika_log.info("demo_twisted: Still alive")
Beispiel #19
0
 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()
Beispiel #20
0
 def got_channel(self, channel):
     pika_log.info("demo_twisted: Got the channel")
     self.channel = channel
Beispiel #21
0
 def stopped_consuming(self, _):
     pika_log.info("demo_twisted: Consuming stopped, queue deleted")
Beispiel #22
0
 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)
Beispiel #23
0
 def start_consuming(self, _):
     pika_log.info("demo_twisted: Queue bound")
     return self.channel.basic_consume(queue=self.queue_name, no_ack=True)
Beispiel #24
0
 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')
Beispiel #25
0
 def start_consuming(self, _):
     pika_log.info("demo_twisted: Queue bound")
     return self.channel.basic_consume(queue=self.queue_name, no_ack=True)
Beispiel #26
0
 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()
Beispiel #27
0
 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)
Beispiel #28
0
 def ping(self):
     pika_log.info("demo_twisted: Still alive")
Beispiel #29
0
 def stopped_consuming(self, _):
     pika_log.info("demo_twisted: Consuming stopped, queue deleted")
Beispiel #30
0
 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
Beispiel #31
0
 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()
Beispiel #32
0
 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)