Beispiel #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)
Beispiel #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)
Beispiel #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()
Beispiel #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()
Beispiel #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()
Beispiel #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()
Beispiel #7
0
class TestConfigurationManager:
    def __init__(self):
        self.base_path = utilities.path / 'tests' / 'test_config'
        self.output_path = self.base_path / 'test_outputs' / 'out.json'
        self.trivial_config_path = self.base_path / 'trivial_config.json'
        self.complex_config_path = self.base_path / 'complex_config.json'
        self.merged_path = self.base_path / 'complex_merged_config.json'
        self.config_with_utf_8_path = self.base_path / 'unicode_config.json'
        self.config = None

    def setup(self):
        self.config = ConfigurationManager()

    def test_config_manager_loads_correct_path(self):
        with open(str(str(self.trivial_config_path))) as f:
            raw = f.read()
        self.config.load_config(str(self.trivial_config_path))
        assert_equals(raw, self.config._raw_config)

    def test_config_manager_path_set_on_load(self):
        self.config.load_config(self.trivial_config_path)
        assert_equals(self.config._path, self.trivial_config_path)

    def test_config_manager_load_complex_with_default(self):
        with open(str(self.merged_path)) as f:
            merged = json.load(f)
        self.config.load_config(str(self.complex_config_path), default=True)
        assert_equals(merged, self.config.config)

    def test_config_manager_dot_notation(self):
        complex_config_path = str((self.base_path / 'complex_config.json'))
        self.config.load_config(str(complex_config_path))
        assert_equals(self.config.config.test1.test5.test7.test8,
                      ["test9", "test10"])
        with assert_raises(AttributeError):
            self.config.config.borp

    def test_config_manager_add_dictionary_with_dot_notation(self):
        complex_config_path = str((self.base_path / 'complex_config.json'))
        self.config.load_config(str(complex_config_path))
        self.config.config.testx = {"testy": "testz"}
        assert_equals(self.config.config.testx.testy, "testz")

    def test_config_manager_save_to_path(self):
        self.config.load_config(str(self.complex_config_path))
        self.config.save_config(path=str(self.output_path))
        assert_true(self.output_path.exists(),
                    msg="Output file does not exist.")

    def test_config_manager_save_utf_8(self):
        self.config.load_config(str(self.config_with_utf_8_path))
        self.config.save_config(path=str(self.output_path))
        with self.config_with_utf_8_path.open() as f:
            original = f.read()
        with self.output_path.open() as f:
            saved = f.read()
        assert_equals(original, saved)

    def test_config_manager_with_path_object(self):
        self.config.load_config(self.trivial_config_path)