Ejemplo n.º 1
0
    class GeneratedConnection(websocket_connection_factory(auth_class,
                                                           pubsub)):
        def __init__(self, *args, **kwargs):
            self.player = {'username': None, 'picture': None}
            super(GeneratedConnection, self).__init__(*args, **kwargs)

        def command_subscribe(self, args):
            result = super(GeneratedConnection, self).command_subscribe(args)
            if result is not False:
                self.player['username'] = self.request.remote_ip
                self.player['picture'] = 'dunno'
                self.id = self.authenticator.get_identifier()
                game[self.id] = self.player.copy()
                gamelog(self,
                        'added ' + self.player['username'] + ' to the game')
                self.pubsub.publish('mousemoves',
                                    'update',
                                    game,
                                    sender='Server')

        def close_connection(self):
            del game[self.id]
            gamelog(self, 'removed ' + self.player['username'] + ' from game')
            self.pubsub.publish('mousemoves', 'update', game, sender='Server')
            return super(GeneratedConnection, self).close_connection()
Ejemplo n.º 2
0
 class GeneratedConnection(websocket_connection_factory(auth_class,
                                                        pubsub)):
     def close_connection(self):
         self.pubsub.publish('mousemoves',
                             'disconnect',
                             sender=self.authenticator.get_identifier())
         return super(GeneratedConnection, self).close_connection()
Ejemplo n.º 3
0
def test_websocket_connection_factory():
    auth_class = mock.Mock()
    pubsub_instance = mock.Mock()

    conn_class = factories.websocket_connection_factory(
        auth_class, pubsub_instance)

    assert conn_class.authenticator_class == auth_class
    assert conn_class.pubsub == pubsub_instance

    assert hasattr(conn_class, 'open') is True
    assert hasattr(conn_class, 'send') is True
Ejemplo n.º 4
0
def test_websocket_connection_factory():
    auth_class = mock.Mock()
    pubsub_instance = mock.Mock()

    conn_class = factories.websocket_connection_factory(
        auth_class, pubsub_instance)

    assert conn_class.authenticator_class == auth_class
    assert conn_class.pubsub == pubsub_instance

    assert hasattr(conn_class, 'open') is True
    assert hasattr(conn_class, 'send') is True
Ejemplo n.º 5
0
    class BasicConnection(websocket_connection_factory(auth_class, pubsub)):
        def on_message(self, msg):
            super(BasicConnection, self).on_message(msg)

        """
        @return {boolean} publish : publish the message
        """

        def call_handler(self, channel, payload, **kwargs):
            module = ''
            handler = ''
            subchannel = ''

            comma_index = channel.index(",")
            first = channel[0:comma_index]
            last = channel[comma_index + 1:]

            print "First"
            print first
            print "Last"
            print last

            #       parts = first.split(".")
            module = first
            #       if len(parts) < 1:
            #           return True
            #       else:
            #           schema_name = parts[0]
            #           module = ".".join(parts[1:])

            parts = last.split(".")

            print "Parts"
            print parts

            if len(parts) is not 2:
                return True
            else:
                handler = parts[0]
                subchannel = parts[1]

            print "Parts"
            print parts

            #            connection.schema_name = schema_name
            twodots_index = payload.index(":")

            data = simplejson.loads(payload[twodots_index + 1:])
            mod = import_module(module + ".channels")

            handler_class = getattr(mod, handler)

            #       handler_class = getattr(import_module(module), handler)
            print "Handler"
            print handler_class

            handler_instance = handler_class(self)

            action = handler_instance.handle_channel_message(subchannel, data)

            return action

        def on_channel_message(self, channel, payload):
            print "On channel message on %s" % channel

            if (channel in self.subscriber.channels
                    and self.authenticator.can_publish(channel)):
                print "channel"
                print channel

                publish_it = self.call_handler(channel, payload)

                print "Republish it ??"
                print publish_it

                if publish_it == True:
                    self.publish(payload)
Ejemplo n.º 6
0
    class BasicConnection(websocket_connection_factory(auth_class, pubsub)):
        def on_message(self, msg):
            super(BasicConnection, self).on_message(msg)

        """
        Calls the appropriate channel handler following the channel name.
        If no handler is found, the function returns True, which just broadcasts the message to the 
        omnibus channel.
        
        If the right format of channel is used. The function imports {handler} from {module}.channels and
        calls the handler function passing to it {subchannel} as the channel name.
        
        Parameters :
        ------------
        channel - str
            The channel "route".
            {module},{handler}.{subchannel}
        Return 
        ------
        boolean - True to publish right away the message to the omnibus channel; False to do nothing.
        """

        def call_handler(self, channel, payload, **kwargs):
            module = ''
            handler = ''
            subchannel = ''

            comma_index = channel.index(",")
            first = channel[0:comma_index]
            last = channel[comma_index + 1:]

            module = first

            parts = last.split(".")

            if len(parts) is not 2:
                return True
            else:
                handler = parts[0]
                subchannel = parts[1]

            twodots_index = payload.index(":")

            data = simplejson.loads(payload[twodots_index + 1:])
            mod = import_module(module + ".channels")

            handler_class = getattr(mod, handler)

            handler_instance = handler_class(self)

            action = handler_instance.handle_channel_message(subchannel, data)

            return action

        def on_channel_message(self, channel, payload):

            if (channel in self.subscriber.channels
                    and self.authenticator.can_publish(channel)):

                publish_it = self.call_handler(channel, payload)

                if publish_it == True:
                    self.publish(payload)