Beispiel #1
0
def start_gui(rpc_address, gui_app=None):

    from golem.rpc.mapping.gui import GUI_EVENT_MAP
    from golem.rpc.session import Client

    gui_app = gui_app or GUIApp(rendering=True)
    events = object_method_map(gui_app.logic, GUI_EVENT_MAP)
    session = Session(rpc_address, events=events)

    reactor = install_qt5_reactor()

    def connect():
        session.connect().addCallbacks(session_ready, start_error)

    def session_ready(*_):
        core_client = Client(session, CORE_METHOD_MAP)
        reactor.callFromThread(gui_app.start, core_client)

    reactor.callWhenRunning(connect)
    reactor.addSystemEventTrigger('before', 'shutdown', session.disconnect)

    try:
        reactor.run()
    finally:
        if gui_app and gui_app.gui and gui_app.gui.app:
            gui_app.gui.app.deleteLater()
Beispiel #2
0
    def test_publish(self):
        session = Session(WebSocketAddress('localhost', 12345, 'golem'))
        publisher = Publisher(session)

        session.publish = Mock()
        session.is_closing = Mock()
        session.is_attached = Mock()
        session.is_attached.return_value = True

        # Not connected, session closing
        session.connected = False
        session.is_closing.return_value = True

        with self.assertNoLogs(logger, level='WARNING'):
            publisher.publish('alias', 1234, kw='arg')
        assert not session.publish.called

        # Not connected, session not closing
        session.connected = False
        session.is_closing.return_value = False

        with self.assertLogs(logger, level='WARNING'):
            publisher.publish('alias', 1234, kw='arg')
        assert not session.publish.called

        # Connected
        session.connected = True
        session.is_closing.return_value = False

        publisher.publish('alias', 1234, kw='arg')
        session.publish.assert_called_with('alias', 1234, kw='arg')
Beispiel #3
0
    def _backend_session_started(self, *_):
        self.state.frontend_session = Session(self.state.router.address,
                                              events=object_method_map(
                                                  self.state.frontend,
                                                  MockService.events))

        self.state.frontend_deferred = self.state.frontend_session.connect()
        self.state.frontend_deferred.addCallbacks(
            self._frontend_session_started, self.state.add_errors)
Beispiel #4
0
    def _start_backend_session(self, *_):
        self.state.backend_session = Session(self.state.router.address,
                                             methods=object_method_map(
                                                 self.state.backend,
                                                 MockService.methods))

        self.state.backend_deferred = self.state.backend_session.connect()
        self.state.backend_deferred.addCallbacks(self._backend_session_started,
                                                 self.state.add_errors)
Beispiel #5
0
    def test_initialization(self):

        address = WebSocketAddress('host', 1234, 'realm')
        session = Session(address)

        assert isinstance(session.ready, Deferred)
        assert isinstance(session.config, types.ComponentConfig)

        assert session.config.realm == u'realm'
        assert not session.ready.called
Beispiel #6
0
    def __init__(self,
                 datadir: str,
                 host: str = CROSSBAR_HOST,
                 port: int = CROSSBAR_PORT,
                 realm: str = CROSSBAR_REALM,
                 ssl: bool = True) -> None:

        address = WebSocketAddress(host, port, realm, ssl)
        cert_manager = CertificateManager(
            os.path.join(get_local_datadir(datadir), CROSSBAR_DIR))
        crsb_user = cert_manager.CrossbarUsers.golemcli
        secret = cert_manager.get_secret(crsb_user)
        self.session = Session(
            address,
            crsb_user=crsb_user,
            crsb_user_secret=secret,
        )
Beispiel #7
0
    def __init__(
            self,
            cli,  # pylint: disable=too-many-arguments
            cert_manager: CertificateManager,
            host: str = CROSSBAR_HOST,
            port: int = CROSSBAR_PORT,
            realm: str = CROSSBAR_REALM,
            ssl: bool = True) -> None:

        address = WebSocketAddress(host, port, realm, ssl)

        self.cli = cli

        crsb_user = cert_manager.CrossbarUsers.golemcli
        self.session = Session(
            address,
            crsb_user=crsb_user,
            crsb_user_secret=cert_manager.get_secret(crsb_user))
Beispiel #8
0
    def _start_session(self) -> Optional[Deferred]:
        if not self.rpc_router:
            self._stop_on_error("rpc", "RPC router is not available")
            return None

        crsb_user = self.rpc_router.cert_manager.CrossbarUsers.golemapp
        self.rpc_session = Session(
            self.rpc_router.address,
            cert_manager=self.rpc_router.cert_manager,
            use_ipv6=self._config_desc.use_ipv6,
            crsb_user=crsb_user,
            crsb_user_secret=self.rpc_router.cert_manager.get_secret(
                crsb_user))
        deferred = self.rpc_session.connect()

        def on_connect(*_):
            methods = rpc_utils.object_method_map(self)
            methods['sys.exposed_procedures'] = \
                self.rpc_session.exposed_procedures
            self.rpc_session.add_procedures(methods)
            self._rpc_publisher = Publisher(self.rpc_session)
            StatusPublisher.set_publisher(self._rpc_publisher)

        return deferred.addCallbacks(on_connect, self._error('rpc session'))
Beispiel #9
0
 def _router_ready(self, *_):
     methods = object_method_map(self.client, CORE_METHOD_MAP)
     self.rpc_session = Session(self.rpc_router.address, methods=methods)
     self.client.configure_rpc(self.rpc_session)
     self.rpc_session.connect().addErrback(self._rpc_error)
Beispiel #10
0
    def __init__(self, cli, host, port, realm, ssl=False):
        address = WebSocketAddress(host, port, realm, ssl)

        self.cli = cli
        self.session = Session(address)
Beispiel #11
0
def start_client(start_ranking, datadir=None,
                 transaction_system=False, client=None,
                 reactor=None, **config_overrides):

    config_logging("client", datadir=datadir)
    logger = logging.getLogger("golem.client")
    install_unhandled_error_logger()

    if not reactor:
        from twisted.internet import reactor

    process_monitor = None

    from golem.core.processmonitor import ProcessMonitor
    from golem.docker.manager import DockerManager
    from golem.rpc.router import CrossbarRouter

    process_monitor = None
    if not client:
        client = Client(datadir=datadir, transaction_system=transaction_system, **config_overrides)

    docker_manager = DockerManager.install(client.config_desc)
    docker_manager.check_environment()
    environments = load_environments()

    client.sync()

    for env in environments:
        client.environments_manager.add_environment(env)
    client.environments_manager.load_config(client.datadir)

    config = client.config_desc
    methods = object_method_map(client, CORE_METHOD_MAP)

    router = CrossbarRouter(
        host=config.rpc_address,
        port=config.rpc_port,
        datadir=client.datadir
    )
    session = Session(router.address, methods=methods)

    def router_ready(*_):
        session.connect().addCallbacks(session_ready, start_error)

    def session_ready(*_):
        global process_monitor

        logger.info('Router session ready. Starting client...')
        try:
            client.configure_rpc(session)
            logger.debug('client.start()')
            client.start()
            logger.debug('after client.start()')
        except SystemExit:
            raise
        except Exception as exc:
            logger.exception("Client process error: {}"
                             .format(exc))

        logger.info('Starting GUI process...')
        gui_process = start_gui(router.address)
        process_monitor = ProcessMonitor(gui_process)
        process_monitor.add_callbacks(stop_reactor)
        logger.info('Starting process monitor...')
        process_monitor.start()

    reactor.addSystemEventTrigger("before", "shutdown", router.stop)
    router.start(reactor, router_ready, start_error)

    if start_ranking:
        client.ranking.run(reactor)

    try:
        reactor.run()
    except ReactorAlreadyRunning:
        logger.debug("Client process: reactor is already running")

    if process_monitor:
        process_monitor.exit()