예제 #1
0
    def test_multiple_channels(self):

        pubnub = PubNub(pnconf_sub_copy())
        ch1 = "state-native-sync-ch-1"
        ch2 = "state-native-sync-ch-2"
        pubnub.config.uuid = "state-native-sync-uuid"
        uuid = pubnub.config.uuid

        subscribe_listener = SubscribeListener()
        where_now_listener = NonSubscribeListener()
        pubnub.add_listener(subscribe_listener)
        pubnub.subscribe().channels([ch1, ch2]).execute()

        subscribe_listener.wait_for_connect()

        time.sleep(2)

        pubnub.where_now() \
            .uuid(uuid) \
            .pn_async(where_now_listener.callback)

        if where_now_listener.pn_await() is False:
            self.fail("WhereNow operation timeout")

        result = where_now_listener.result
        channels = result.channels

        assert len(channels) == 2
        assert ch1 in channels
        assert ch2 in channels

        pubnub.unsubscribe().channels([ch1, ch2]).execute()
        subscribe_listener.wait_for_disconnect()

        pubnub.stop()
예제 #2
0
    def test_single_channel(self):
        pubnub = PubNub(pnconf_sub_copy())
        ch = helper.gen_channel("wherenow-asyncio-channel")
        uuid = helper.gen_channel("wherenow-asyncio-uuid")
        pubnub.config.uuid = uuid

        subscribe_listener = SubscribeListener()
        where_now_listener = NonSubscribeListener()
        pubnub.add_listener(subscribe_listener)
        pubnub.subscribe().channels(ch).execute()

        subscribe_listener.wait_for_connect()

        time.sleep(2)

        pubnub.where_now() \
            .uuid(uuid) \
            .pn_async(where_now_listener.callback)

        if where_now_listener.pn_await() is False:
            self.fail("WhereNow operation timeout")

        result = where_now_listener.result
        channels = result.channels

        assert len(channels) == 1
        assert channels[0] == ch

        pubnub.unsubscribe().channels(ch).execute()
        subscribe_listener.wait_for_disconnect()

        pubnub.stop()
예제 #3
0
    def test_multiple_channels(self):
        pubnub = PubNub(pnconf_sub_copy())
        ch1 = helper.gen_channel("here-now-native-sync-ch1")
        ch2 = helper.gen_channel("here-now-native-sync-ch2")
        pubnub.config.uuid = "here-now-native-sync-uuid"

        subscribe_listener = SubscribeListener()
        here_now_listener = NonSubscribeListener()
        pubnub.add_listener(subscribe_listener)
        pubnub.subscribe().channels([ch1, ch2]).execute()

        subscribe_listener.wait_for_connect()

        time.sleep(5)

        pubnub.here_now() \
            .channels([ch1, ch2]) \
            .pn_async(here_now_listener.callback)

        if here_now_listener.pn_await() is False:
            self.fail("HereNow operation timeout")

        result = here_now_listener.result
        channels = result.channels

        assert len(channels) == 2
        assert channels[0].occupancy == 1
        assert channels[0].occupants[0].uuid == pubnub.uuid
        assert channels[1].occupancy == 1
        assert channels[1].occupants[0].uuid == pubnub.uuid

        pubnub.unsubscribe().channels([ch1, ch2]).execute()
        subscribe_listener.wait_for_disconnect()

        pubnub.stop()
예제 #4
0
    def test_single_channel(self):
        pubnub = PubNub(pnconf_sub_copy())
        ch = helper.gen_channel("herenow-asyncio-channel")
        uuid = helper.gen_channel("herenow-asyncio-uuid")
        pubnub.config.uuid = uuid

        subscribe_listener = SubscribeListener()
        here_now_listener = NonSubscribeListener()
        pubnub.add_listener(subscribe_listener)
        pubnub.subscribe().channels(ch).execute()

        subscribe_listener.wait_for_connect()

        time.sleep(2)

        pubnub.here_now() \
            .channels(ch) \
            .include_uuids(True) \
            .pn_async(here_now_listener.callback)

        if here_now_listener.pn_await() is False:
            self.fail("HereNow operation timeout")

        result = here_now_listener.result
        channels = result.channels

        assert len(channels) == 1
        assert channels[0].occupancy == 1
        assert channels[0].occupants[0].uuid == pubnub.uuid

        pubnub.unsubscribe().channels(ch).execute()
        subscribe_listener.wait_for_disconnect()

        pubnub.stop()
예제 #5
0
def subscribe_pub(channel, msg):
    my_listener = SubscribeListener()
    pubnub.add_listener(my_listener)

    pubnub.subscribe().channels(channel).execute()
    my_listener.wait_for_connect()
    print('connected')

    pubnub.publish().channel(channel).message(msg).sync()
    result = my_listener.wait_for_message_on(channel)
    print(result.message)
    # Unsubscribe
    pubnub.unsubscribe().channels(channel).execute()
    my_listener.wait_for_disconnect()

    print('unsubscribed')
def main():
    my_listener = SubscribeListener()
    pubnub.add_listener(my_listener)

    pubnub.subscribe().channels("awesomeChannel").execute()
    yield my_listener.wait_for_connect()
    print("connected")

    yield pubnub.publish().channel("awesomeChannel").message({'fieldA': 'awesome', 'fieldB': 10}).future()
    result = yield my_listener.wait_for_message_on("awesomeChannel")
    print(result.message)

    pubnub.unsubscribe().channels("awesomeChannel").execute()
    yield my_listener.wait_for_disconnect()

    print("unsubscribed")
예제 #7
0
class TestPubNubState(unittest.TestCase):
    def setUp(self):
        self.event = threading.Event()

    def callback(self, response, status):
        self.response = response
        self.status = status
        self.event.set()

    def test_single_channel(self):
        pubnub = PubNub(pnconf_sub_copy())
        ch = helper.gen_channel("herenow-asyncio-channel")
        uuid = helper.gen_channel("herenow-asyncio-uuid")
        pubnub.config.uuid = uuid

        subscribe_listener = SubscribeListener()
        here_now_listener = NonSubscribeListener()
        pubnub.add_listener(subscribe_listener)
        pubnub.subscribe().channels(ch).execute()

        subscribe_listener.wait_for_connect()

        time.sleep(2)

        pubnub.here_now() \
            .channels(ch) \
            .include_uuids(True) \
            .async(here_now_listener.callback)

        if here_now_listener. await () is False:
            self.fail("HereNow operation timeout")

        result = here_now_listener.result
        channels = result.channels

        assert len(channels) == 1
        assert channels[0].occupancy == 1
        assert channels[0].occupants[0].uuid == pubnub.uuid

        pubnub.unsubscribe().channels(ch).execute()
        subscribe_listener.wait_for_disconnect()

        pubnub.stop()
        pubnub.config.uuid = "state-native-sync-uuid"
        uuid = pubnub.config.uuid

        subscribe_listener = SubscribeListener()
        where_now_listener = NonSubscribeListener()
        pubnub.add_listener(subscribe_listener)
        pubnub.subscribe().channels([ch1, ch2]).execute()

        subscribe_listener.wait_for_connect()

        time.sleep(2)

        pubnub.where_now() \
            .uuid(uuid) \
            .async(where_now_listener.callback)

        if where_now_listener.await() is False:
            self.fail("WhereNow operation timeout")

        result = where_now_listener.result
        channels = result.channels

        assert len(channels) == 2
        assert ch1 in channels
        assert ch2 in channels

        pubnub.unsubscribe().channels([ch1, ch2]).execute()
        subscribe_listener.wait_for_disconnect()

        pubnub.stop()
예제 #9
0
        ch2 = helper.gen_channel("here-now-native-sync-ch2")
        pubnub.config.uuid = "here-now-native-sync-uuid"

        subscribe_listener = SubscribeListener()
        here_now_listener = NonSubscribeListener()
        pubnub.add_listener(subscribe_listener)
        pubnub.subscribe().channels([ch1, ch2]).execute()

        subscribe_listener.wait_for_connect()

        time.sleep(5)

        pubnub.here_now().channels([ch1, ch2]).async(here_now_listener.callback)

        if here_now_listener.await() is False:
            self.fail("HereNow operation timeout")

        result = here_now_listener.result
        channels = result.channels

        assert len(channels) == 2
        assert channels[0].occupancy == 1
        assert channels[0].occupants[0].uuid == pubnub.uuid
        assert channels[1].occupancy == 1
        assert channels[1].occupants[0].uuid == pubnub.uuid

        pubnub.unsubscribe().channels([ch1, ch2]).execute()
        subscribe_listener.wait_for_disconnect()

        pubnub.stop()
예제 #10
0
    def presence(self, pubnub, presence):

        if presence.channel != self.admin_channel:
            if presence.event == "join":
                if presence.channel not in self.gateway_channels and presence.uuid != presence.channel and presence.uuid not in self.gateway_uuids:
                    print(
                        '2. UNAUTHORISED USER ({}) HAS JOINED THE UUID CHANNEL ({}).'
                        .format(presence.uuid, presence.channel))
                    self.gd.auth_blacklist(presence.channel, presence.uuid)

                elif presence.channel == "gateway_auth" and presence.uuid not in self.gateway_uuids:

                    print("PRESENCE EVENT: " + str(presence.event))
                    print("[1] UUID JOINED AUTH CHANNEL: {}".format(
                        presence.uuid))

                    pubnub.grant().channels(
                        presence.uuid).read(True).write(True).sync()
                    print("GatewayAuth: Connecting the UUID channel {}".format(
                        presence.uuid))
                    pubnub.subscribe().channels(
                        presence.uuid).with_presence().execute()

                    # Send hashed UUID over the gateway, client will know its UUID so it can hash and compute it.
                    sha3_hash = hashlib.new("sha3_512")

                    encode = sha3_hash.update((presence.uuid).encode("UTF-8"))
                    pubnub.publish().channel('gateway_auth').message(
                        sha3_hash.hexdigest()). async (my_publish_callback)

                    # Check UUID channel info
                    envelope = pubnub.here_now().channels(
                        presence.uuid).include_uuids(True).include_state(
                            True).sync()
                    users_in_channel = envelope.result.total_occupancy

                    # Someone could be spying - at this point only the gateway should be here.
                    if users_in_channel > 1:
                        print("WARNING! Someone may be spying in the channel")
                        uuids_in_channel = []
                        users = envelope.result.channels[0].occupants

                        for occupant in users:  # If there is indeed multiple people in the channel only then we bother to check who.
                            print(
                                occupant.uuid
                            )  # - lists all uuids in channel, if more than one can later 'blacklist' ones not meant to be in the channel -> "do not serve, suspicious"

                            if occupant.uuid not in self.gateway_uuids and occupant.uuid != presence.uuid:  # only blacklist if not gateway or the legit user
                                uuids_in_channel.append(occupant.uuid)
                                self.gd.auth_blacklist(
                                    presence.channel,
                                    occupant.uuid)  # blacklist them

                        pubnub.publish().channel(presence.uuid).message({
                            "error":
                            "Too many occupants in channel, regenerate UUID."
                        }). async (my_publish_callback)

                elif presence.channel not in self.gateway_channels and presence.uuid == presence.channel:  # uuid channel presence
                    print(
                        '[2] REQUIRED USER ({}) HAS JOINED THE UUID CHANNEL ({}).'
                        .format(presence.uuid, presence.channel))
                    users_auth_key = idgen.id_generator()
                    channelName = "SECURE." + idgen.id_generator()

                    # Send auth key to user
                    pubnub.publish().channel(presence.uuid).message({
                        "channel":
                        channelName,
                        "auth_key":
                        users_auth_key,
                        "global_channel":
                        "gateway_global"
                    }). async (
                        my_publish_callback)  # Send data over 1-1 channel

                    # Grant + Subscribe to new private channel.
                    pubnub.grant().channels([channelName]).auth_keys([
                        users_auth_key, self.receiver_auth_key
                    ]).read(True).write(True).manage(True).ttl(0).sync()
                    print(
                        "GatewayReceiver: Connecting to private channel {}..".
                        format(channelName))
                    self.gr.subscribe_channels(
                        channelName
                    )  # Receiver to subscribe to this private channel for function calls.
                    self.gd.gateway_subscriptions(channelName, presence.uuid)

            if presence.event == "timeout" or presence.event == "leave" and presence.uuid not in self.gateway_uuids:
                print(
                    "GatewayAuth: {} event on channel {} by user {}, unsubscribing."
                    .format(presence.event.title(), presence.channel,
                            presence.uuid))
                pubnub.unsubscribe().channels(presence.uuid).execute()
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub, SubscribeListener

pubnub.set_stream_logger('pubnub', logging.DEBUG)

pnconfig = PNConfiguration()

pnconfig.publish_key = "demo"
pnconfig.subscribe_key = "demo"

pubnub = PubNub(pnconfig)

my_listener = SubscribeListener()
pubnub.add_listener(my_listener)

pubnub.subscribe().channels("awesomeChannel").execute()
my_listener.wait_for_connect()
print("connected")

pubnub.publish().channel("awesomeChannel").message({
    'fieldA': 'awesome',
    'fieldB': 10
}).sync()
result = my_listener.wait_for_message_on("awesomeChannel")
print(result.message)

pubnub.unsubscribe().channels("awesomeChannel").execute()
my_listener.wait_for_disconnect()

print("unsubscribed")