Ejemplo n.º 1
0
    async def message_pump(self):
        pnconfig = PNConfiguration()
        pnconfig.subscribe_key = config['cardsubkey']
        # Why aren't these the default settings?
        pnconfig.ssl = True
        pnconfig.reconnect_policy = PNReconnectionPolicy.EXPONENTIAL

        pubnub = PubNubAsyncio(pnconfig)

        listener = SubscribeListener()
        pubnub.add_listener(listener)

        pubnub.subscribe().channels(config['cardviewerchannel']).execute()
        await listener.wait_for_connect()
        log.info("Connected to PubNub")

        message_future = asyncio.ensure_future(
            listener.wait_for_message_on(config['cardviewerchannel']))

        while True:
            await asyncio.wait([self.stop_future, message_future],
                               return_when=asyncio.FIRST_COMPLETED)
            if message_future.done():
                message = message_future.result().message
                log.info("Message from PubNub: %r", message)

                card_id = self._extract(message)
                if card_id is not None:
                    await self._card(card_id)

                message_future = asyncio.ensure_future(
                    listener.wait_for_message_on(config['cardviewerchannel']))
            if self.stop_future.done():
                break
        if not message_future.done():
            message_future.cancel()

        pubnub.unsubscribe().channels(config['cardviewerchannel']).execute()
        await listener.wait_for_disconnect()
        pubnub.stop()
        log.info("Disconnected from PubNub")
Ejemplo n.º 2
0
	async def message_pump(self):
		pnconfig = PNConfiguration()
		pnconfig.subscribe_key = config['cardsubkey']
		# Why aren't these the default settings?
		pnconfig.ssl = True
		pnconfig.reconnect_policy = PNReconnectionPolicy.EXPONENTIAL

		pubnub = PubNubAsyncio(pnconfig)

		listener = SubscribeListener()
		pubnub.add_listener(listener)

		pubnub.subscribe().channels(config['cardviewerchannel']).execute()
		await listener.wait_for_connect()
		log.info("Connected to PubNub")

		message_future = asyncio.ensure_future(listener.wait_for_message_on(config['cardviewerchannel']))

		while True:
			await asyncio.wait([self.stop_future, message_future], return_when=asyncio.FIRST_COMPLETED)
			if message_future.done():
				message = message_future.result().message
				log.info("Message from PubNub: %r", message)

				card_id = self._extract(message)
				if card_id is not None:
					await self._card(card_id)

				message_future = asyncio.ensure_future(listener.wait_for_message_on(config['cardviewerchannel']))
			if self.stop_future.done():
				break
		if not message_future.done():
			message_future.cancel()

		pubnub.unsubscribe().channels(config['cardviewerchannel']).execute()
		await listener.wait_for_disconnect()
		pubnub.stop()
		log.info("Disconnected from PubNub")
Ejemplo n.º 3
0
def test_subscribe_publish_unsubscribe(event_loop):
    pubnub_sub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop)
    pubnub_pub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop)
    pubnub_sub.config.uuid = 'test-subscribe-asyncio-uuid-sub'
    pubnub_pub.config.uuid = 'test-subscribe-asyncio-uuid-pub'

    callback = SubscribeListener()
    channel = "test-subscribe-asyncio-ch"
    message = "hey"
    pubnub_sub.add_listener(callback)
    pubnub_sub.subscribe().channels(channel).execute()

    yield from callback.wait_for_connect()

    publish_future = asyncio.ensure_future(pubnub_pub.publish().channel(channel).message(message).future())
    subscribe_message_future = asyncio.ensure_future(callback.wait_for_message_on(channel))

    yield from asyncio.wait([
        publish_future,
        subscribe_message_future
    ])

    publish_envelope = publish_future.result()
    subscribe_envelope = subscribe_message_future.result()

    assert isinstance(subscribe_envelope, PNMessageResult)
    assert subscribe_envelope.channel == channel
    assert subscribe_envelope.subscription is None
    assert subscribe_envelope.message == message
    assert subscribe_envelope.timetoken > 0

    assert isinstance(publish_envelope, AsyncioEnvelope)
    assert publish_envelope.result.timetoken > 0
    assert publish_envelope.status.original_response[0] == 1

    pubnub_sub.unsubscribe().channels(channel).execute()
    yield from callback.wait_for_disconnect()

    pubnub_pub.stop()
    pubnub_sub.stop()
Ejemplo n.º 4
0
def test_cg_subscribe_publish_unsubscribe(event_loop, sleeper=asyncio.sleep):
    ch = "test-subscribe-asyncio-channel"
    gr = "test-subscribe-asyncio-group"
    message = "hey"

    pubnub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop)

    envelope = yield from pubnub.add_channel_to_channel_group().channel_group(gr).channels(ch).future()
    assert envelope.status.original_response['status'] == 200

    yield from sleeper(1)

    callback_messages = SubscribeListener()
    pubnub.add_listener(callback_messages)
    pubnub.subscribe().channel_groups(gr).execute()
    yield from callback_messages.wait_for_connect()

    subscribe_future = asyncio.ensure_future(callback_messages.wait_for_message_on(ch))
    publish_future = asyncio.ensure_future(pubnub.publish().channel(ch).message(message).future())
    yield from asyncio.wait([subscribe_future, publish_future])

    sub_envelope = subscribe_future.result()
    pub_envelope = publish_future.result()

    assert pub_envelope.status.original_response[0] == 1
    assert pub_envelope.status.original_response[1] == 'Sent'

    assert sub_envelope.channel == ch
    assert sub_envelope.subscription == gr
    assert sub_envelope.message == message

    pubnub.unsubscribe().channel_groups(gr).execute()
    yield from callback_messages.wait_for_disconnect()

    envelope = yield from pubnub.remove_channel_from_channel_group().channel_group(gr).channels(ch).future()
    assert envelope.status.original_response['status'] == 200

    pubnub.stop()
async def test_subscribe_publish_unsubscribe(event_loop):
    pubnub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop)

    callback = SubscribeListener()
    channel = helper.gen_channel("test-sub-pub-unsub")
    message = "hey"
    pubnub.add_listener(callback)
    pubnub.subscribe().channels(channel).execute()

    await callback.wait_for_connect()

    publish_future = asyncio.ensure_future(pubnub.publish().channel(channel).message(message).future())
    subscribe_message_future = asyncio.ensure_future(callback.wait_for_message_on(channel))

    await asyncio.wait([
        publish_future,
        subscribe_message_future
    ])

    publish_envelope = publish_future.result()
    subscribe_envelope = subscribe_message_future.result()

    assert isinstance(subscribe_envelope, PNMessageResult)
    assert subscribe_envelope.channel == channel
    assert subscribe_envelope.subscription is None
    assert subscribe_envelope.message == message
    assert subscribe_envelope.timetoken > 0

    assert isinstance(publish_envelope, AsyncioEnvelope)
    assert publish_envelope.result.timetoken > 0
    assert publish_envelope.status.original_response[0] == 1

    pubnub.unsubscribe().channels(channel).execute()
    await callback.wait_for_disconnect()

    pubnub.stop()