Exemple #1
0
 def test_socket_nodelay(self):
     transport, proto = yield from connect(virtualhost=self.vhost, loop=self.loop,
         host=self.host,port=self.port, login=self.login,password=self.password)
     sock = transport.get_extra_info('socket')
     opt_val = sock.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY)
     self.assertEqual(opt_val, 1)
     yield from proto.close()
Exemple #2
0
def new_task():
    try:
        transport, protocol = yield from aioamqp.connect('localhost', 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return


    channel = yield from protocol.channel()

    yield from channel.queue('task_queue', durable=True)

    message = ' '.join(sys.argv[1:]) or "Hello World!"

    yield from channel.basic_publish(
        payload=message,
        exchange_name='',
        routing_key='task_queue',
        properties={
            'delivery_mode': 2,
        },
    )
    print(" [x] Sent %r" % message,)

    yield from protocol.close()
    transport.close()
Exemple #3
0
    def test_connect_tuning(self):
        # frame_max should be higher than 131072
        frame_max = 131072
        channel_max = 10
        heartbeat = 100
        _transport, proto = yield from connect(
            host=self.host,
            port=self.port,
            virtualhost=self.vhost,
            loop=self.loop,
            channel_max=channel_max,
            frame_max=frame_max,
            heartbeat=heartbeat,
        )
        self.assertEqual(proto.state, OPEN)
        self.assertIsNotNone(proto.server_properties)

        self.assertDictEqual(proto.connection_tunning, {
            'frame_max': frame_max,
            'channel_max': channel_max,
            'heartbeat': heartbeat
        })

        self.assertEqual(proto.server_channel_max, channel_max)
        self.assertEqual(proto.server_frame_max, frame_max)
        self.assertEqual(proto.server_heartbeat, heartbeat)

        yield from proto.close()
Exemple #4
0
def receive_log():
    try:
        transport, protocol = yield from aioamqp.connect("localhost", 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    channel = yield from protocol.channel()
    exchange_name = "topic_logs"

    yield from channel.exchange(exchange_name, "topic")

    result = yield from channel.queue(queue_name="", durable=False, auto_delete=True)
    queue_name = result["queue"]

    binding_keys = sys.argv[1:]
    if not binding_keys:
        print("Usage: %s [binding_key]..." % (sys.argv[0],))
        sys.exit(1)

    for binding_key in binding_keys:
        yield from channel.queue_bind(exchange_name="topic_logs", queue_name=queue_name, routing_key=binding_key)

    print(" [*] Waiting for logs. To exit press CTRL+C")

    yield from channel.basic_consume(callback, queue_name=queue_name)
Exemple #5
0
def receive_log():
    try:
        protocol = yield from aioamqp.connect('localhost', 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return


    channel = yield from protocol.channel()
    exchange_name = 'logs'
    # TODO let rabbitmq choose the queue name
    queue_name = 'queue-%s' % random.randint(0, 10000)

    yield from channel.exchange(exchange_name, 'fanout')

    yield from asyncio.wait_for(channel.queue(queue_name, durable=False, auto_delete=True), timeout=10)

    yield from asyncio.wait_for(channel.queue_bind(exchange_name=exchange_name, queue_name=queue_name, routing_key=''), timeout=10)

    print(' [*] Waiting for logs. To exit press CTRL+C')

    yield from asyncio.wait_for(channel.basic_consume(queue_name), timeout=10)

    while True:
        consumer_tag, delivery_tag, message = yield from channel.consume()
        print("consumer {} recved {} ({})".format(consumer_tag, message, delivery_tag))
def receive_log():
    try:
        protocol = yield from aioamqp.connect('localhost', 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    channel = yield from protocol.channel()
    exchange_name = 'topic_logs'
    # TODO let rabbitmq choose the queue name
    queue_name = 'queue-%s' % random.randint(0, 10000)

    yield from channel.exchange(exchange_name, 'topic')

    yield from asyncio.wait_for(channel.queue(queue_name, durable=False, auto_delete=True), timeout=10)

    binding_keys = sys.argv[1:]
    if not binding_keys:
        print("Usage: %s [binding_key]..." % (sys.argv[0],))
        sys.exit(1)

    for binding_key in binding_keys:
        yield from asyncio.wait_for(channel.queue_bind(exchange_name='topic_logs',
                                                       queue_name=queue_name,
                                                       routing_key=binding_key), timeout=10)

    print(' [*] Waiting for logs. To exit press CTRL+C')

    yield from asyncio.wait_for(channel.basic_consume(queue_name), timeout=10)

    while True:
        consumer_tag, delivery_tag, message = yield from channel.consume()
        print("consumer {} recved {} ({})".format(consumer_tag, message, delivery_tag))
Exemple #7
0
def receive_log():
    try:
        protocol = yield from aioamqp.connect('localhost', 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return


    channel = yield from protocol.channel()
    exchange_name = 'logs'
    # TODO let rabbitmq choose the queue name
    queue_name = 'queue-%s' % random.randint(0, 10000)

    yield from channel.exchange(exchange_name, 'fanout')

    yield from asyncio.wait_for(channel.queue(queue_name, durable=False, auto_delete=True), timeout=10)

    yield from asyncio.wait_for(channel.queue_bind(exchange_name=exchange_name, queue_name=queue_name, routing_key=''), timeout=10)

    print(' [*] Waiting for logs. To exit press CTRL+C')

    yield from asyncio.wait_for(channel.basic_consume(queue_name), timeout=10)

    while True:
        consumer_tag, delivery_tag, message = yield from channel.consume()
        print("consumer {} recved {} ({})".format(consumer_tag, message, delivery_tag))
    yield from asyncio.sleep(10)
    yield from asyncio.wait_for(protocol.client_close(), timeout=10)
Exemple #8
0
def receive_log():
    try:
        transport, protocol = yield from aioamqp.connect('localhost', 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    channel = yield from protocol.channel()
    exchange_name = 'topic_logs'
    # TODO let rabbitmq choose the queue name
    queue_name = 'queue-%s' % random.randint(0, 10000)

    yield from channel.exchange(exchange_name, 'topic')

    yield from asyncio.wait_for(channel.queue(queue_name, durable=False, auto_delete=True), timeout=10)

    binding_keys = sys.argv[1:]
    if not binding_keys:
        print("Usage: %s [binding_key]..." % (sys.argv[0],))
        sys.exit(1)

    for binding_key in binding_keys:
        yield from asyncio.wait_for(channel.queue_bind(exchange_name='topic_logs',
                                                       queue_name=queue_name,
                                                       routing_key=binding_key), timeout=10)

    print(' [*] Waiting for logs. To exit press CTRL+C')

    yield from asyncio.wait_for(channel.basic_consume(queue_name, callback=callback), timeout=10)
Exemple #9
0
def new_task():
    try:
        transport, protocol = yield from aioamqp.connect('localhost', 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    channel = yield from protocol.channel()

    yield from channel.queue('task_queue', durable=True)

    message = ' '.join(sys.argv[1:]) or "Hello World!"

    yield from channel.basic_publish(
        payload=message,
        exchange_name='',
        routing_key='task_queue',
        properties={
            'delivery_mode': 2,
        },
    )
    print(" [x] Sent %r" % message, )

    yield from protocol.close()
    transport.close()
Exemple #10
0
def receive():
    transport, protocol = yield from aioamqp.connect()
    channel = yield from protocol.channel()

    yield from channel.queue_declare(queue_name='hello')

    yield from channel.basic_consume(callback, queue_name='hello')
Exemple #11
0
    async def _connect_to_amqp_broker(self):
        """Connect to the broker, and creates the exchanges and queues needed by the handlers registered at the moment
        of connection.

        Raises:
            ConnectionError:
        """
        try:
            self._transport, self._protocol = await asyncio.wait_for(
                aioamqp.connect(
                    host=self.conn_parameters["host"],
                    port=self.conn_parameters["port"],
                    login=self.conn_parameters["user"],
                    password=self.conn_parameters["password"],
                    virtualhost=self.conn_parameters["virtual_host"],
                    on_error=self.on_error_callback,
                ),
                CONNECTION_TIMEOUT_SECONDS,
            )
        except (aioamqp.AmqpClosedConnection, TimeoutError,
                builtins.ConnectionError, OSError):
            logger.critical(f"Error connecting to the AMQP broker")
            raise ConnectionError
        else:
            logger.info(
                f"Connected to AMQP host: {self.conn_parameters['host']}")

            self._channel = await self._protocol.channel()
            logger.info("AMQP channel created")
            await self._declare_queues(self.inputs)
            logger.info("AMQP queues declared")
Exemple #12
0
def run_server():
	transport, protocol = yield from aioamqp.connect(host="10.211.55.70",
	                                                 port=5672,
	                                                 login="******",
	                                                 password="******")
	channel = yield from protocol.channel()

	yield from channel.queue_declare(queue_name='hello')

	i = 0

	print("Serving")
	while True:
		yield from channel.basic_publish(
			payload=str(i),
			exchange_name='',
			routing_key='hello'
		)

		i += 1

	yield from protocol.close()

	# ensure the socket is closed.
	transport.close()
def receive_log():
    try:
        transport, protocol = yield from aioamqp.connect('localhost', 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    channel = yield from protocol.channel()
    exchange_name = 'topic_logs'

    yield from channel.exchange(exchange_name, 'topic')

    result = yield from channel.queue(queue_name='',
                                      durable=False,
                                      auto_delete=True)
    queue_name = result['queue']

    binding_keys = sys.argv[1:]
    if not binding_keys:
        print("Usage: %s [binding_key]..." % (sys.argv[0], ))
        sys.exit(1)

    for binding_key in binding_keys:
        yield from channel.queue_bind(exchange_name='topic_logs',
                                      queue_name=queue_name,
                                      routing_key=binding_key)

    print(' [*] Waiting for logs. To exit press CTRL+C')

    yield from channel.basic_consume(callback, queue_name=queue_name)
Exemple #14
0
def lechbot_notif_consume(coroutine):
    try:
        transport, protocol = yield from aioamqp.connect(
            host=RMQ_HOST, login=RMQ_USER, password=RMQ_PASSWORD,
            on_error=rmq_error_callback)
        channel = yield from protocol.channel()
        queue = yield from channel.queue_declare(LECHBOT_NOTIFS_QUEUE)

        @asyncio.coroutine
        def consume(body, envelope, properties):
            yield from channel.basic_client_ack(envelope.delivery_tag)
            try:
                msg = json.loads(body.decode())
                now = datetime.now()
                msgtime = datetime.strptime(
                    msg.get('time', now.strftime(TIMEFMT)), TIMEFMT)
                if (now - msgtime).total_seconds() < 120:
                    yield from coroutine(msg['name'])
            except:
                traceback.print_exc()

        yield from channel.basic_consume(LECHBOT_NOTIFS_QUEUE, callback=consume)
    except aioamqp.AmqpClosedConnection:
        logger.exception("closed connections")
        return
def receive_log(waiter):
    try:
        transport, protocol = yield from aioamqp.connect('localhost', 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    channel = yield from protocol.channel()
    exchange_name = 'direct_logs'

    yield from channel.exchange(exchange_name, 'direct')

    result = yield from channel.queue(queue_name='', durable=False, auto_delete=True)

    queue_name = result['queue']

    severities = sys.argv[1:]
    if not severities:
        print("Usage: %s [info] [warning] [error]" % (sys.argv[0],))
        sys.exit(1)

    for severity in severities:
        yield from channel.queue_bind(
            exchange_name='direct_logs',
            queue_name=queue_name,
            routing_key=severity,
        )

    print(' [*] Waiting for logs. To exit press CTRL+C')

    yield from asyncio.wait_for(channel.basic_consume(callback, queue_name=queue_name), timeout=10)
    yield from waiter.wait()

    yield from protocol.close()
    transport.close()
def receive_log():
    try:
        transport, protocol = yield from aioamqp.connect('localhost', 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    channel = yield from protocol.channel()
    exchange_name = 'direct_logs'
    # TODO let rabbitmq choose the queue name
    queue_name = 'queue-%s' % random.randint(0, 10000)

    yield from channel.exchange(exchange_name, 'direct')

    yield from asyncio.wait_for(channel.queue(queue_name, durable=False, auto_delete=True), timeout=10)

    severities = sys.argv[1:]
    if not severities:
        print("Usage: %s [info] [warning] [error]" % (sys.argv[0],))
        sys.exit(1)

    for severity in severities:
        yield from asyncio.wait_for(channel.queue_bind(exchange_name='direct_logs',
                                    queue_name=queue_name,
                                    routing_key=severity), timeout=10)

    print(' [*] Waiting for logs. To exit press CTRL+C')

    yield from asyncio.wait_for(channel.basic_consume(callback, queue_name=queue_name), timeout=10)
    yield from asyncio.Event().wait()
Exemple #17
0
def receive_log():
    try:
        transport, protocol = yield from aioamqp.connect('localhost', 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    channel = yield from protocol.channel()
    exchange_name = 'logs'

    yield from channel.exchange(exchange_name=exchange_name,
                                type_name='fanout')

    # let RabbitMQ generate a random queue name
    result = yield from channel.queue(queue_name='', exclusive=True)

    queue_name = result['queue']
    yield from channel.queue_bind(exchange_name=exchange_name,
                                  queue_name=queue_name,
                                  routing_key='')

    print(' [*] Waiting for logs. To exit press CTRL+C')

    yield from channel.basic_consume(callback,
                                     queue_name=queue_name,
                                     no_ack=True)
Exemple #18
0
def receive():
    transport, protocol = yield from aioamqp.connect()
    channel = yield from protocol.channel()

    yield from channel.queue_declare(queue_name='b')

    yield from channel.basic_consume(callback, queue_name='b')
Exemple #19
0
def receive_log():
    try:
        transport, protocol = yield from aioamqp.connect(AMQP_HOST, 5672)
    except:
        print("closed connections")
        return

    channel = yield from protocol.channel()
    # exchange_name = 'cloudstack-events'
    exchange_name = 'race-exchange'
    queue_name = 'async-queue-%s' % random.randint(0, 10000)
    yield from channel.exchange(exchange_name, 'topic', auto_delete=False, passive=False, durable=False)
    # yield from channel.basic_qos(prefetch_count=1, prefetch_size=0, connection_global=False)
    yield from asyncio.wait_for(channel.queue(queue_name, durable=False, auto_delete=True), timeout=10)

    binding_keys = [race_routing_key]

    for binding_key in binding_keys:
        print("binding", binding_key)
        yield from asyncio.wait_for(channel.queue_bind(exchange_name=exchange_name,
                                                       queue_name=queue_name,
                                                       routing_key=binding_key), timeout=10)

    logger.info(racer_name)
    logger.info(' waiting for command')
    logger.info(queue_name)
    yield from channel.basic_consume(callback, queue_name=queue_name)
Exemple #20
0
    def test_connect_tuning(self):
        # frame_max should be higher than 131072
        frame_max = 131072
        channel_max = 10
        heartbeat = 100
        transport, proto = yield from connect(
            virtualhost=self.vhost,
            loop=self.loop,
            channel_max=channel_max,
            frame_max=frame_max,
            heartbeat=heartbeat,
        )
        self.assertTrue(proto.is_open)
        self.assertIsNotNone(proto.server_properties)

        self.assertDictEqual(proto.connection_tunning, {
            'frame_max': frame_max,
            'channel_max': channel_max,
            'heartbeat': heartbeat
        })

        self.assertEqual(proto.server_channel_max, channel_max)
        self.assertEqual(proto.server_frame_max, frame_max)
        self.assertEqual(proto.server_heartbeat, heartbeat)

        yield from proto.close()
Exemple #21
0
def check_rabbitmq_server():
    """
    Wait till rabbit_mq server is up
    # TODO: need to make it more robust
    :return:
    """

    flag = False

    while not flag:
        try:
            transport, protocol = yield from aioamqp.connect(AMQP_HOST, 5672)
            flag = True
            logger.info("RabbitMQ server Up")
        except Exception:
            logger.info("RabbitMQ server conection is closed")
            sleep(0.05)

    channel = yield from protocol.channel()
    exchange_name = 'race-exchange'
    message = {"racer_name": racer_name}
    routing_key = 'register'

    yield from channel.exchange(exchange_name, 'topic')

    yield from channel.publish(json.dumps(message), exchange_name=exchange_name, routing_key=routing_key)
    logger.info("Registerd for racer as --> {}".format(racer_name))

    yield from protocol.close()
    transport.close()
Exemple #22
0
 def test_socket_nodelay(self):
     transport, proto = yield from connect(virtualhost=self.vhost,
                                           loop=self.loop)
     sock = transport.get_extra_info('socket')
     opt_val = sock.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY)
     self.assertEqual(opt_val, 1)
     yield from proto.close()
Exemple #23
0
def receive_log():
    try:
        protocol = yield from aioamqp.connect('localhost', 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return


    channel = yield from protocol.channel()
    exchange_name = 'direct_logs'
    # TODO let rabbitmq choose the queue name
    queue_name = 'queue-%s' % random.randint(0, 10000)

    yield from channel.exchange(exchange_name, 'direct')

    yield from asyncio.wait_for(channel.queue(queue_name, durable=False, auto_delete=True), timeout=10)

    severities = sys.argv[1:]
    if not severities:
        print("Usage: %s [info] [warning] [error]" % (sys.argv[0],))
        sys.exit(1)

    for severity in severities:
        yield from asyncio.wait_for(channel.queue_bind(exchange_name='direct_logs',
                                    queue_name=queue_name,
                                    routing_key=severity), timeout=10)

    print(' [*] Waiting for logs. To exit press CTRL+C')

    yield from asyncio.wait_for(channel.basic_consume(queue_name), timeout=10)

    while True:
        consumer_tag, delivery_tag, message = yield from channel.consume()
        print("consumer {} recved {} ({})".format(consumer_tag, message, delivery_tag))
Exemple #24
0
def receive_log(host, port, login, password):
    try:
        transport, protocol = yield from aioamqp.connect(
            host, port, login, password)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    channel = yield from protocol.channel()
    exchange_name = 'topic_logs'

    yield from channel.exchange(exchange_name, 'topic')

    result = yield from channel.queue(queue_name='khb',
                                      durable=False,
                                      auto_delete=True)

    binding_keys = 'khb.*'

    for binding_key in binding_keys:
        yield from channel.queue_bind(exchange_name='topic_logs',
                                      queue_name='khb',
                                      routing_key=binding_key)

    #print(' [*] Waiting for logs. To exit press CTRL+C')
    for i in range(100):
        yield from channel.basic_consume(callback,
                                         queue_name='khb',
                                         no_ack=True)
def receive(app):
    try:
        transport, protocol = yield from aioamqp.connect(
            host=os.environ.get('RABBIT_PORT_5672_TCP_ADDR', '192.168.59.103'),
            port=int(os.environ.get('RABBIT_PORT_5672_TCP_PORT', 5672)),
            login=os.environ.get('RABBIT_ENV_USER', 'admin'),
            password=os.environ.get('RABBIT_ENV_RABBITMQ_PASS', 'password'),
        )
    except Exception as e:
        print("closed amqp connections")
        return

    channel = yield from protocol.channel()
    exchange = yield from channel.exchange_declare(exchange_name='ws_msg.exchange', type_name='direct')
    queue_name = 'ws_msg'

    yield from channel.queue_declare(queue_name)
    yield from channel.queue_bind(queue_name, 'ws_msg.exchange', 'ws_msg')

    # yield from asyncio.wait_for(channel.queue(queue_name, durable=False, auto_delete=False), timeout=10)
    yield from asyncio.wait_for(
        channel.basic_consume(
            queue_name,
            callback=callback_wrapper(channel, app)
        ),
        timeout=10
    )
Exemple #26
0
    def init_amqp(self, queue_name):
        transport, protocol = yield from aioamqp.connect()
        channel = yield from protocol.channel()

        yield from channel.queue_declare(queue_name=queue_name)

        yield from channel.basic_consume(self.amqp_handle,
                                         queue_name=queue_name,
                                         no_ack=True)
def get_rabbitmq_channel():
    """Get rabbitmq channel.
    """
    try:
        transport, protocol = yield from aioamqp.connect('localhost', 5672)
        channel = yield from protocol.channel()
        return channel
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return
Exemple #28
0
 def _connect(self):
     self._transport, self._protocol = yield from aioamqp.connect(
         host=self.app.settings['AMQP_HOST'],
         port=self.app.settings['AMQP_PORT'],
         login=self.app.settings['AMQP_USERNAME'],
         password=self.app.settings['AMQP_PASSWORD'],
         virtualhost=self.app.settings['AMQP_VIRTUAL_HOST'],
         heartbeat=self.app.settings['AMQP_HEARTBEAT_INTERVAL'],
         **self.app.settings['AMQP_CONNECTION_KWARGS'])
     self._channel = yield from self._protocol.channel()
Exemple #29
0
def rpc_server():

    transport, protocol = yield from aioamqp.connect()

    channel = yield from protocol.channel()

    yield from channel.queue_declare(queue_name='rpc_queue')
    yield from channel.basic_qos(prefetch_count=1, prefetch_size=0, connection_global=False)
    yield from channel.basic_consume(on_request, queue_name='rpc_queue')
    print(" [x] Awaiting RPC requests")
Exemple #30
0
def run_client():
	transport, protocol = yield from aioamqp.connect(host="10.211.55.70",
	                                                 port=5672,
	                                                 login="******",
	                                                 password="******")

	channel = yield from protocol.channel()

	yield from channel.queue_declare(queue_name='hello')

	yield from channel.basic_consume(callback, queue_name='hello', no_ack=True)
    def mq_connect(self):
        try:
            self._transport, self._protocol = yield from aioamqp.connect('localhost', 5672)
        except aioamqp.AmqpClosedConnection:
            print("closed connections")
            raise

        self._channel = yield from self._protocol.channel()
        yield from self._channel.exchange_declare(
            exchange_name=config.EXCHANGE_NAME, type_name='fanout'
        )
Exemple #32
0
 def _connect(self):
     self._transport, self._protocol = yield from aioamqp.connect(
         host=self.app.settings['AMQP_HOST'],
         port=self.app.settings['AMQP_PORT'],
         login=self.app.settings['AMQP_USERNAME'],
         password=self.app.settings['AMQP_PASSWORD'],
         virtualhost=self.app.settings['AMQP_VIRTUAL_HOST'],
         heartbeat=self.app.settings['AMQP_HEARTBEAT_INTERVAL'],
         **self.app.settings['AMQP_CONNECTION_KWARGS']
     )
     self._channel = yield from self._protocol.channel()
Exemple #33
0
def worker():
    try:
        transport, protocol = yield from aioamqp.connect("localhost", 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    channel = yield from protocol.channel()

    yield from channel.queue(queue_name="task_queue", durable=True)
    yield from channel.basic_qos(prefetch_count=1, prefetch_size=0, connection_global=False)
    yield from channel.basic_consume(callback, queue_name="task_queue")
Exemple #34
0
def rpc_server():

    transport, protocol = yield from aioamqp.connect()

    channel = yield from protocol.channel()

    yield from channel.queue_declare(queue_name='rpc_queue')
    yield from channel.basic_qos(prefetch_count=1,
                                 prefetch_size=0,
                                 connection_global=False)
    yield from channel.basic_consume(on_request, queue_name='rpc_queue')
    print(" [x] Awaiting RPC requests")
Exemple #35
0
def produce():
    try:
        transport, protocol = yield from aioamqp.connect('localhost', 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    queue_name = 'py2.queue'
    channel = yield from protocol.channel()
    yield from asyncio.wait_for(channel.queue(queue_name, durable=False, auto_delete=True), timeout=10)

    while True:
        yield from channel.publish("py3.message", '', queue_name)
Exemple #36
0
def receive():
    try:
        transport, protocol = yield from aioamqp.connect('localhost', 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    channel = yield from protocol.channel()
    queue_name = 'py2.queue'

    yield from asyncio.wait_for(channel.queue(queue_name, durable=False, auto_delete=True), timeout=10)

    yield from asyncio.wait_for(channel.basic_consume(queue_name, callback=callback), timeout=10)
Exemple #37
0
    def connect(self):
        """ an `__init__` method can't be a coroutine"""
        self.transport, self.protocol = yield from aioamqp.connect()
        self.channel = yield from self.protocol.channel()

        result = yield from self.channel.queue_declare(queue_name='', exclusive=True)
        self.callback_queue = result['queue']

        yield from self.channel.basic_consume(
            self.on_response,
            no_ack=True,
            queue_name=self.callback_queue,
        )
Exemple #38
0
def connect():
    try:
        transport, protocol = yield from aioamqp.connect()  # use default parameters
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    print("connected !")
    yield from asyncio.sleep(1)

    print("close connection")
    yield from protocol.close()
    transport.close()
Exemple #39
0
def send():
    transport, protocol = yield from aioamqp.connect()
    channel = yield from protocol.channel()

    yield from channel.queue_declare(queue_name='hello')

    yield from channel.basic_publish(payload='Hello World!',
                                     exchange_name='',
                                     routing_key='hello')

    print(" [x] Sent 'Hello World!'")
    yield from protocol.close()
    transport.close()
Exemple #40
0
    def connect(self):
        """ an `__init__` method can't be a coroutine"""
        self.transport, self.protocol = yield from aioamqp.connect()
        self.channel = yield from self.protocol.channel()

        result = yield from self.channel.queue_declare(queue_name='',
                                                       exclusive=True)
        self.callback_queue = result['queue']

        yield from self.channel.basic_consume(
            self.on_response,
            no_ack=True,
            queue_name=self.callback_queue,
        )
Exemple #41
0
def lechbot_event(event_name):
    try:
        transport, protocol = yield from aioamqp.connect(
            host=RMQ_HOST, login=RMQ_USER, password=RMQ_PASSWORD,
            on_error=rmq_error_callback)
        channel = yield from protocol.channel()
        yield from channel.queue_declare(LECHBOT_EVENTS_QUEUE)
        msg = {'time': datetime.now().strftime(TIMEFMT), 'name': event_name}
        yield from channel.publish(json.dumps(msg), '', LECHBOT_EVENTS_QUEUE)
        yield from protocol.close()
        transport.close()
    except aioamqp.AmqpClosedConnection:
        logger.exception("closed connections")
        return
Exemple #42
0
def worker():
    try:
        transport, protocol = yield from aioamqp.connect('localhost', 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    channel = yield from protocol.channel()

    yield from channel.queue(queue_name='task_queue', durable=True)
    yield from channel.basic_qos(prefetch_count=1,
                                 prefetch_size=0,
                                 connection_global=False)
    yield from channel.basic_consume(callback, queue_name='task_queue')
    def connect(self):
        """ an `__init__` method can't be a coroutine"""
        self.transport, self.protocol = yield from aioamqp.connect()
        self.channel = yield from self.protocol.channel()

        result = yield from self.channel.queue_declare(queue_name='daemon')
        yield from self.channel.basic_qos(prefetch_count=1, prefetch_size=0, connection_global=False)
        self.callback_queue = result['queue']

        yield from self.channel.basic_consume(
            self.on_response,
            no_ack=True,
            queue_name=self.callback_queue,
        )
Exemple #44
0
def send():
    transport, protocol = yield from aioamqp.connect()
    channel = yield from protocol.channel()

    yield from channel.queue_declare(queue_name='hello')

    yield from channel.basic_publish(
        payload='Hello World!',
        exchange_name='',
        routing_key='hello'
    )

    print(" [x] Sent 'Hello World!'")
    yield from protocol.close()
    transport.close()
Exemple #45
0
def send():
    transport, protocol = yield from aioamqp.connect()
    channel = yield from protocol.channel(return_callback=handle_return)

    yield from channel.queue_declare(queue_name='hello')

    yield from channel.basic_publish(
        payload='Hello World!',
        exchange_name='',
        routing_key='helo',  # typo on purpose, will cause the return
        mandatory=True,
    )

    print(" [x] Sent 'Hello World!'")
    yield from protocol.close()
    transport.close()
Exemple #46
0
def receive():
    ssl_options = {
        'certfile': 'client.pem',
        'keyfile': 'client.key',
        'ca_certs': 'cacert.pem',
    }
    transport, protocol = yield from aioamqp.connect(host='host',
                                                     login="******",
                                                     ssl=True,
                                                     ssl_options=ssl_options,
                                                     login_method='EXTERNAL')
    channel = yield from protocol.channel()

    yield from channel.basic_publish(payload=json.dumps(
        {'xxx': 'Hello World!'}),
                                     exchange_name='honeypots',
                                     routing_key='dionaea')
Exemple #47
0
def produce():
    try:
        protocol = yield from aioamqp.connect('localhost', 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    queue_name = 'py2.queue'
    channel = yield from protocol.channel()
    yield from asyncio.wait_for(channel.queue(queue_name,
                                              durable=False,
                                              auto_delete=True),
                                timeout=10)

    while True:
        yield from channel.publish("py3.message", '', queue_name)
        yield from asyncio.sleep(10)
Exemple #48
0
def receive():
    try:
        protocol = yield from aioamqp.connect('localhost', 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    channel = yield from protocol.channel()
    queue_name = 'py2.queue'

    yield from asyncio.wait_for(channel.queue(queue_name, durable=False, auto_delete=True), timeout=10)

    yield from asyncio.wait_for(channel.basic_consume(queue_name), timeout=10)

    while True:
        consumer_tag, delivery_tag, message = yield from channel.consume()
        print("consumer {} recved {} ({})".format(consumer_tag, message, delivery_tag))
Exemple #49
0
def connect():
    try:
        transport, protocol = yield from aioamqp.connect(
            host='nonexistant.com',
            on_error=error_callback,
            client_properties={
                'program_name': "test",
                'hostname': socket.gethostname(),
            },

        )
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return
    except ConnectionRefusedError:
        print("f*****g hell")
        return
Exemple #50
0
def exchange_routing():
    try:
        protocol = yield from aioamqp.connect('localhost', 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    channel = yield from protocol.channel()
    exchange_name = 'logs'
    message = ' '.join(sys.argv[1:]) or "info: Hello World!"

    yield from channel.exchange(exchange_name, 'fanout')

    yield from channel.publish(message, exchange_name=exchange_name, routing_key='')
    print(" [x] Sent %r" % (message,))

    yield from protocol.close()
Exemple #51
0
def manage_race():
    try:
        transport, protocol = yield from aioamqp.connect(AMQP_HOST, 5672)
    except:
        logger.info("closed connections")
        return

    channel = yield from protocol.channel()
    # exchange_name = 'cloudstack-events'
    exchange_name = 'race-exchange'
    queue_name = 'async-queue-%s' % random.randint(0, 10000)
    yield from channel.exchange(exchange_name,
                                'topic',
                                auto_delete=False,
                                passive=False,
                                durable=False)
    yield from asyncio.wait_for(channel.queue(queue_name,
                                              durable=False,
                                              auto_delete=True),
                                timeout=10)

    binding_keys = ['master']

    for binding_key in binding_keys:
        print("binding", binding_key)
        yield from asyncio.wait_for(channel.queue_bind(
            exchange_name=exchange_name,
            queue_name=queue_name,
            routing_key=binding_key),
                                    timeout=10)

    print(queue_name)
    print(type(channel))

    logging.info("sending start signal, will sleep for 1 second")
    # yield from asyncio.wait_for(exchange_routing_topic(),1)
    race_msg = generate_msg()
    yield from channel.publish(json.dumps(race_msg),
                               exchange_name=exchange_name,
                               routing_key="race")
    logging.info(" [x] Sent %r" % race_msg)

    logger.info(' waiting for command')

    yield from channel.basic_consume(callback, queue_name=queue_name)
Exemple #52
0
def receive():
    try:
        transport, protocol = yield from aioamqp.connect('localhost', 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    channel = yield from protocol.channel()
    queue_name = 'py2.queue'

    yield from asyncio.wait_for(channel.queue(queue_name,
                                              durable=False,
                                              auto_delete=True),
                                timeout=10)

    yield from asyncio.wait_for(channel.basic_consume(queue_name,
                                                      callback=callback),
                                timeout=10)
Exemple #53
0
def exchange_routing():
    try:
        transport, protocol = yield from aioamqp.connect("localhost", 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    channel = yield from protocol.channel()
    exchange_name = "direct_logs"
    severity = sys.argv[1] if len(sys.argv) > 1 else "info"
    message = " ".join(sys.argv[2:]) or "Hello World!"

    yield from channel.exchange(exchange_name, "direct")

    yield from channel.publish(message, exchange_name=exchange_name, routing_key=severity)
    print(" [x] Sent %r" % (message,))

    yield from transport.close()
Exemple #54
0
def exchange_routing():
    try:
        transport, protocol = yield from aioamqp.connect('localhost', 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    channel = yield from protocol.channel()
    exchange_name = 'topic_logs'
    message = ' '.join(sys.argv[2:]) or 'Hello World!'
    routing_key = sys.argv[1] if len(sys.argv) > 1 else 'anonymous.info'

    yield from channel.exchange(exchange_name, 'topic')

    yield from channel.publish(
        message, exchange_name=exchange_name, routing_key=routing_key)
    print(" [x] Sent %r" % (message,))

    yield from transport.close()
Exemple #55
0
def exchange_routing():
    try:
        protocol = yield from aioamqp.connect('localhost', 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    channel = yield from protocol.channel()
    exchange_name = 'direct_logs'
    severity = sys.argv[1] if len(sys.argv) > 1 else 'info'
    message = ' '.join(sys.argv[2:]) or 'Hello World!'

    yield from channel.exchange(exchange_name, 'direct')

    yield from channel.publish(
        message, exchange_name=exchange_name, routing_key=severity)
    print(" [x] Sent %r" % (message,))

    yield from protocol.close()
Exemple #56
0
def socket(request):
    resp = WebSocketResponse()
    yield from resp.prepare(request)

    if 'amqp' not in request.app:
        transport, protocol = yield from aioamqp.connect()
        request.app['amqp'] = protocol

        @asyncio.coroutine
        def cleanup(app):
            yield from protocol.close(timeout=1.0)
            transport.close()

        request.app.register_on_finish(cleanup)

    amqp = request.app['amqp']
    channel = yield from amqp.channel()
    yield from channel.exchange_declare(exchange_name='demo-room',
                                        type_name='fanout')
    result = yield from channel.queue_declare('', exclusive=True)
    yield from channel.queue_bind(result['queue'], 'demo-room', routing_key='')

    # Consume messages from the queue
    @asyncio.coroutine
    def message(channel, body, envelope, properties):
        if not resp.closed:
            resp.send_str(body.decode('utf-8'))
        yield from channel.basic_client_ack(envelope.delivery_tag)

    yield from channel.basic_consume(message, queue_name=result['queue'])

    # Broadcast messages to the queue
    while True:
        msg = yield from resp.receive()
        if msg.tp == MsgType.text:
            yield from channel.publish(msg.data,
                                       exchange_name='demo-room',
                                       routing_key='')
        else:
            break
    # Client requested close
    yield from channel.close()
    return resp
Exemple #57
0
def create_shim(handler_coro, loop=None):
    logger.info('starting amqp server')
    if loop is not None:
        # this is needed as there is no other way to pass a loop to aioamqp
        asyncio.set_event_loop(loop)
    game = yield from handler_coro
    transport, protocol = yield from aioamqp.connect('localhost')

    @asyncio.coroutine
    def on_request(body, envelope, properties):
        action = json.loads(body.decode('utf8'))
        task = asyncio.ensure_future(game.receive_action(action))

        @asyncio.coroutine
        def receive_action_done(response):
            '''Called with the return value of `receive_action`.'''
            yield from channel.basic_publish(json.dumps(response),
                                             '',
                                             routing_key=properties.reply_to,
                                             properties={'correlation_id': properties.correlation_id})
            # XXX: is this ack in the right place?
            yield from channel.basic_client_ack(envelope.delivery_tag)

        task.add_done_callback(lambda fut: asyncio.ensure_future(receive_action_done(fut.result())))

    # amqp rpc queue
    channel = yield from protocol.channel()
    rpc_queue_name = 'rpc_queue'
    yield from channel.queue_declare(rpc_queue_name)

    # amqp updates exchange
    updates_exchange_name = 'updates'
    yield from channel.exchange_declare(updates_exchange_name, 'fanout')

    def on_change(action):
        asyncio.ensure_future(channel.basic_publish(json.dumps(action), updates_exchange_name, routing_key=''))

    game.subscribe(on_change)

    logger.info('amqp: awaiting RPC requests')
    yield from channel.basic_consume(rpc_queue_name, callback=on_request)
    return (transport, protocol)