示例#1
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()
示例#2
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()
示例#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]).async(here_now_listener.callback)

        if here_now_listener.await() is False:
            self.fail("HereNow operation timeout")
示例#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).async(here_now_listener.callback)

        if here_now_listener.await() is False:
            self.fail("HereNow operation timeout")
示例#5
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]) \
            .async(here_now_listener.callback)

        if here_now_listener. await () is False:
            self.fail("HereNow operation timeout")
示例#6
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) \
            .async(here_now_listener.callback)

        if here_now_listener. await () is False:
            self.fail("HereNow operation timeout")
示例#7
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()
示例#8
0
pubnub.set_stream_logger('pubnub', logging.DEBUG)

pnconfig = PNConfiguration()

pnconfig.subscribe_key = 'demo'
pnconfig.publish_key = 'demo'

pubnub = PubNub(pnconfig)


def here_now_callback(result, status):
    if status.is_error():
        # handle error
        return

    for channel_data in result.channels:
        print("---")
        print("channel: %s" % channel_data.channel_name)
        print("occupancy: %s" % channel_data.occupancy)

        print("occupants: %s" % channel_data.channel_name)
        for occupant in channel_data.occupants:
            print("uuid: %s, state: %s" % (occupant.uuid, occupant.state))

pubnub.here_now().channels("my_channel", "demo").include_uuids(True).async(here_now_callback)