Example #1
0
class ServerFactory:
    def __init__(self):
        try:
            self.protocols = []
            self.configuration_manager = ConfigurationManager()
            self.configuration_manager.load_config(
                path / 'config' / 'config.json',
                default=True)
            self.plugin_manager = PluginManager(self.configuration_manager)
            self.plugin_manager.load_from_path(
                path / self.configuration_manager.config.plugin_path)
            self.plugin_manager.resolve_dependencies()
            self.plugin_manager.activate_all()
            asyncio.Task(self.plugin_manager.get_overrides())
        except Exception as e:
            print("Exception encountered during server startup.")
            print(e)
            loop.stop()
            sys.exit()


    def remove(self, protocol):
        self.protocols.remove(protocol)

    def __call__(self, reader, writer):
        server = StarryPyServer(reader, writer, factory=self)
        self.protocols.append(server)
        print(self.protocols)
Example #2
0
class ServerFactory:
    def __init__(self):
        try:
            self.protocols = []
            self.configuration_manager = ConfigurationManager()
            self.configuration_manager.load_config(path / 'config' /
                                                   'config.json',
                                                   default=True)
            self.plugin_manager = PluginManager(self.configuration_manager)
            self.plugin_manager.load_from_path(
                path / self.configuration_manager.config.plugin_path)
            self.plugin_manager.resolve_dependencies()
            self.plugin_manager.activate_all()
            asyncio.Task(self.plugin_manager.get_overrides())
        except Exception as e:
            print("Exception encountered during server startup.")
            print(e)
            loop.stop()
            sys.exit()

    def remove(self, protocol):
        self.protocols.remove(protocol)

    def __call__(self, reader, writer):
        server = StarryPyServer(reader, writer, factory=self)
        self.protocols.append(server)
        print(self.protocols)
Example #3
0
class ServerFactory:
    def __init__(self):
        try:
            self.protocols = []
            self.configuration_manager = ConfigurationManager()
            self.configuration_manager.load_config(path / 'config' /
                                                   'config.json',
                                                   default=True)
            self.plugin_manager = PluginManager(self.configuration_manager,
                                                factory=self)
            self.plugin_manager.load_from_path(
                path / self.configuration_manager.config.plugin_path)
            self.plugin_manager.resolve_dependencies()
            self.plugin_manager.activate_all()
            asyncio.Task(self.plugin_manager.get_overrides())
        except Exception as e:
            logger.exception("Error during server startup.", exc_info=True)

            loop.stop()
            sys.exit()

    @asyncio.coroutine
    def broadcast(self,
                  messages,
                  *,
                  world="",
                  name="",
                  channel=0,
                  client_id=0):
        for protocol in self.protocols:
            try:
                yield from protocol.send_message(messages,
                                                 world=world,
                                                 name=name,
                                                 channel=channel,
                                                 client_id=client_id)
            except ConnectionError:
                continue

    def remove(self, protocol):
        self.protocols.remove(protocol)

    def __call__(self, reader, writer):
        server = StarryPyServer(reader, writer, factory=self)
        self.protocols.append(server)

    def kill_all(self):
        for protocol in self.protocols:
            protocol.die()
Example #4
0
class ServerFactory:
    def __init__(self):
        try:
            self.protocols = []
            self.configuration_manager = ConfigurationManager()
            self.configuration_manager.load_config(
                path / 'config' / 'config.json',
                default=True)
            self.plugin_manager = PluginManager(self.configuration_manager,
                                                factory=self)
            self.plugin_manager.load_from_path(
                path / self.configuration_manager.config.plugin_path)
            self.plugin_manager.resolve_dependencies()
            self.plugin_manager.activate_all()
            asyncio.Task(self.plugin_manager.get_overrides())
        except Exception as e:
            logger.exception("Error during server startup.", exc_info=True)

            loop.stop()
            sys.exit()

    @asyncio.coroutine
    def broadcast(self, messages, *, world="", name="", channel=0,
                  client_id=0):
        for protocol in self.protocols:
            try:
                yield from protocol.send_message(messages,
                                                 world=world,
                                                 name=name,
                                                 channel=channel,
                                                 client_id=client_id)
            except ConnectionError:
                continue

    def remove(self, protocol):
        self.protocols.remove(protocol)

    def __call__(self, reader, writer):
        server = StarryPyServer(reader, writer, factory=self)
        self.protocols.append(server)

    def kill_all(self):
        for protocol in self.protocols:
            protocol.die()
Example #5
0
class ServerFactory:
    def __init__(self):
        try:
            self.connections = []
            self.configuration_manager = ConfigurationManager()
            self.configuration_manager.load_config(path / 'config' /
                                                   'config.json',
                                                   default=True)
            self.plugin_manager = PluginManager(self.configuration_manager,
                                                factory=self)
            self.plugin_manager.load_from_path(
                path / self.configuration_manager.config.plugin_path)
            self.plugin_manager.resolve_dependencies()
            self.plugin_manager.activate_all()
            asyncio.ensure_future(self.plugin_manager.get_overrides())
        except Exception as err:
            logger.exception("Error during server startup.", exc_info=True)

            loop.stop()
            sys.exit()

    @asyncio.coroutine
    def broadcast(self,
                  messages,
                  *,
                  mode=ChatReceiveMode.RADIO_MESSAGE,
                  **kwargs):
        """
        Send a message to all connected clients.

        :param messages: Message(s) to be sent.
        :param mode: Mode bit of message.
        :return: Null.
        """
        for connection in self.connections:
            try:
                yield from connection.send_message(messages, mode=mode)
            except Exception as err:
                logger.exception("Error while trying to broadcast.")
                logger.exception(err)
                continue

    def remove(self, connection):
        """
        Remove a single connection.

        :param connection: Connection to be removed.
        :return: Null.
        """
        self.connections.remove(connection)

    def __call__(self, reader, writer):
        """
        Whenever a client connects, ping the server factory to start
        handling it.

        :param reader: Reader transport socket.
        :param writer: Writer transport socket.
        :return: Null.
        """
        server = StarryPyServer(reader,
                                writer,
                                self.configuration_manager,
                                factory=self)
        self.connections.append(server)
        logger.debug("New connection established.")

    def kill_all(self):
        """
        Drop all connections.

        :return: Null.
        """
        logger.debug("Dropping all connections.")
        for connection in self.connections:
            connection.die()
Example #6
0
class ServerFactory:
    def __init__(self):
        try:
            self.connections = []
            self.configuration_manager = ConfigurationManager()
            self.configuration_manager.load_config(
                path / 'config' / 'config.json',
                default=True)
            self.plugin_manager = PluginManager(self.configuration_manager,
                                                factory=self)
            self.plugin_manager.load_from_path(
                path / self.configuration_manager.config.plugin_path)
            self.plugin_manager.resolve_dependencies()
            self.plugin_manager.activate_all()
            asyncio.ensure_future(self.plugin_manager.get_overrides())
        except Exception as err:
            logger.exception("Error during server startup.", exc_info=True)

            loop.stop()
            sys.exit()

    @asyncio.coroutine
    def broadcast(self, messages):
        """
        Send a message to all connected clients.

        :param messages: Message(s) to be sent.
        :return: Null.
        """
        for connection in self.connections:
            try:
                yield from connection.send_message(messages)
            except Exception as err:
                logger.exception("Error while trying to broadcast.")
                logger.exception(err)
                continue

    def remove(self, connection):
        """
        Remove a single connection.

        :param connection: Connection to be removed.
        :return: Null.
        """
        self.connections.remove(connection)

    def __call__(self, reader, writer):
        """
        Whenever a client connects, ping the server factory to start
        handling it.

        :param reader: Reader transport socket.
        :param writer: Writer transport socket.
        :return: Null.
        """
        server = StarryPyServer(reader, writer, self.configuration_manager,
                                factory=self)
        self.connections.append(server)
        logger.debug("New connection established.")

    def kill_all(self):
        """
        Drop all connections.

        :return: Null.
        """
        logger.debug("Dropping all connections.")
        for connection in self.connections:
            connection.die()
class TestPluginManager:
    def __init__(self):
        self.plugin_path = path / 'tests' / 'test_plugins'
        self.good_plugin = self.plugin_path / 'test_plugin_2.py'
        self.good_plugin_package = self.plugin_path / 'test_plugin_package'
        self.bad_plugin = self.plugin_path / 'bad_plugin'
        self.bad_path = self.plugin_path / 'bad_path.py'
        self.dependent_plugins = self.plugin_path / "dependent_plugins"
        self.plugin_manager = PluginManager(None)
        self.loop = None

    def setup(self):
        self.plugin_manager = PluginManager(None)
        self.loop = asyncio.new_event_loop()

    def test_bad_paths(self):
        assert_raises(FileNotFoundError,
                      self.plugin_manager._load_module, self.bad_path)

    def test_load_good_plugins(self):
        self.plugin_manager.load_plugin(self.good_plugin)
        self.plugin_manager.load_plugin(self.good_plugin_package)
        self.plugin_manager.resolve_dependencies()
        assert_in("test_plugin_2",
                  self.plugin_manager.list_plugins().keys())
        assert_in("test_plugin_1",
                  self.plugin_manager.list_plugins().keys())

    def test_load_bad_plugin(self):
        with assert_raises(SyntaxError):
            self.plugin_manager.load_plugin(self.bad_plugin)
            self.plugin_manager.resolve_dependencies()

    def test_load_plugin_dir(self):
        self.plugin_manager.load_from_path(self.plugin_path)
        self.plugin_manager.resolve_dependencies()
        assert_in("test_plugin_2",
                  self.plugin_manager.list_plugins())
        assert_in("test_plugin_1",
                  self.plugin_manager.list_plugins())
        assert_in("bad_plugin",
                  self.plugin_manager.failed)

    def test_the_do_method(self):
        self.plugin_manager.load_plugin(self.good_plugin)
        self.plugin_manager.load_plugin(self.good_plugin_package)
        self.plugin_manager.resolve_dependencies()
        result = self.loop.run_until_complete(
            self.plugin_manager.do("chat_sent", b""))
        assert_equals(result, True)

    def test_dependency_check(self):
        with assert_raises(ImportError):
            self.plugin_manager.load_plugin(self.dependent_plugins / 'b.py')
            self.plugin_manager.resolve_dependencies()

    def test_dependency_resolution(self):
        self.plugin_manager.load_plugins([
            self.dependent_plugins / 'a.py',
            self.dependent_plugins / 'b.py'
        ])

        self.plugin_manager.resolve_dependencies()

    def test_circular_dependency_error(self):
        with assert_raises(ImportError):
            self.plugin_manager.load_plugin(
                self.dependent_plugins / 'circular.py')
            self.plugin_manager.resolve_dependencies()

    def test_empty_overrides(self):
        self.plugin_manager.resolve_dependencies()
        owners = self.loop.run_until_complete(
            self.plugin_manager.get_overrides())
        assert_equal(owners, set())

    def test_override(self):
        self.plugin_manager.load_plugin(
            self.plugin_path / 'test_plugin_package')
        self.plugin_manager.load_plugin(self.plugin_path / 'test_plugin_2.py')
        self.plugin_manager.resolve_dependencies()
        self.plugin_manager.activate_all()
        overrides = self.loop.run_until_complete(
            self.plugin_manager.get_overrides())
        assert_equal(overrides, {'on_chat_sent'})

    def test_override_caching(self):
        self.plugin_manager.load_plugin(self.plugin_path / 'test_plugin_2.py')
        assert_equal(self.plugin_manager._overrides, set())
        assert_equal(self.plugin_manager._override_cache, set())
        self.plugin_manager.activate_all()
        self.loop.run_until_complete(self.plugin_manager.get_overrides())
        assert_is(self.plugin_manager._override_cache,
                  self.plugin_manager._activated_plugins)
        cache = self.plugin_manager._override_cache
        self.loop.run_until_complete(self.plugin_manager.get_overrides())
        assert_is(self.plugin_manager._override_cache, cache)


    def test_activate(self):
        self.plugin_manager.load_plugin(
            self.plugin_path / 'test_plugin_package')
        self.plugin_manager.load_plugin(self.plugin_path / 'test_plugin_2.py')
        self.plugin_manager.resolve_dependencies()
        self.plugin_manager.activate_all()
        assert_equal({x.name for x in self.plugin_manager._activated_plugins},
                     {'test_plugin_1', 'test_plugin_2'})
Example #8
0
class TestPluginManager:
    def __init__(self):
        self.plugin_path = path / 'tests' / 'test_plugins'
        self.good_plugin = self.plugin_path / 'test_plugin_2.py'
        self.good_plugin_package = self.plugin_path / 'test_plugin_package'
        self.bad_plugin = self.plugin_path / 'bad_plugin'
        self.bad_path = self.plugin_path / 'bad_path.py'
        self.dependent_plugins = self.plugin_path / "dependent_plugins"
        self.plugin_manager = PluginManager(None)
        self.loop = None

    def setup(self):
        self.plugin_manager = PluginManager(None)
        self.loop = asyncio.new_event_loop()

    def test_bad_paths(self):
        assert_raises(FileNotFoundError, self.plugin_manager._load_module,
                      self.bad_path)

    def test_load_good_plugins(self):
        self.plugin_manager.load_plugin(self.good_plugin)
        self.plugin_manager.load_plugin(self.good_plugin_package)
        self.plugin_manager.resolve_dependencies()
        assert_in("test_plugin_2", self.plugin_manager.list_plugins().keys())
        assert_in("test_plugin_1", self.plugin_manager.list_plugins().keys())

    def test_load_bad_plugin(self):
        with assert_raises(SyntaxError):
            self.plugin_manager.load_plugin(self.bad_plugin)
            self.plugin_manager.resolve_dependencies()

    def test_load_plugin_dir(self):
        self.plugin_manager.load_from_path(self.plugin_path)
        self.plugin_manager.resolve_dependencies()
        assert_in("test_plugin_2", self.plugin_manager.list_plugins())
        assert_in("test_plugin_1", self.plugin_manager.list_plugins())
        assert_in("bad_plugin", self.plugin_manager.failed)

    def test_the_do_method(self):
        self.plugin_manager.load_plugin(self.good_plugin)
        self.plugin_manager.load_plugin(self.good_plugin_package)
        self.plugin_manager.resolve_dependencies()
        result = self.loop.run_until_complete(
            self.plugin_manager.do("chat_sent", b""))
        assert_equals(result, True)

    def test_dependency_check(self):
        with assert_raises(ImportError):
            self.plugin_manager.load_plugin(self.dependent_plugins / 'b.py')
            self.plugin_manager.resolve_dependencies()

    def test_dependency_resolution(self):
        self.plugin_manager.load_plugins(
            [self.dependent_plugins / 'a.py', self.dependent_plugins / 'b.py'])

        self.plugin_manager.resolve_dependencies()

    def test_circular_dependency_error(self):
        with assert_raises(ImportError):
            self.plugin_manager.load_plugin(self.dependent_plugins /
                                            'circular.py')
            self.plugin_manager.resolve_dependencies()

    def test_empty_overrides(self):
        self.plugin_manager.resolve_dependencies()
        owners = self.loop.run_until_complete(
            self.plugin_manager.get_overrides())
        assert_equal(owners, set())

    def test_override(self):
        self.plugin_manager.load_plugin(self.plugin_path /
                                        'test_plugin_package')
        self.plugin_manager.load_plugin(self.plugin_path / 'test_plugin_2.py')
        self.plugin_manager.resolve_dependencies()
        self.plugin_manager.activate_all()
        overrides = self.loop.run_until_complete(
            self.plugin_manager.get_overrides())
        assert_equal(overrides, {'on_chat_sent'})

    def test_override_caching(self):
        self.plugin_manager.load_plugin(self.plugin_path / 'test_plugin_2.py')
        assert_equal(self.plugin_manager._overrides, set())
        assert_equal(self.plugin_manager._override_cache, set())
        self.plugin_manager.activate_all()
        self.loop.run_until_complete(self.plugin_manager.get_overrides())
        assert_is(self.plugin_manager._override_cache,
                  self.plugin_manager._activated_plugins)
        cache = self.plugin_manager._override_cache
        self.loop.run_until_complete(self.plugin_manager.get_overrides())
        assert_is(self.plugin_manager._override_cache, cache)

    def test_activate(self):
        self.plugin_manager.load_plugin(self.plugin_path /
                                        'test_plugin_package')
        self.plugin_manager.load_plugin(self.plugin_path / 'test_plugin_2.py')
        self.plugin_manager.resolve_dependencies()
        self.plugin_manager.activate_all()
        assert_equal({x.name
                      for x in self.plugin_manager._activated_plugins},
                     {'test_plugin_1', 'test_plugin_2'})