コード例 #1
0
    def test_peer_task(self):
        route_key = conf.PEER_QUEUE_NAME_FORMAT.format(amqp_key=conf.AMQP_KEY)

        service = PeerInnerService(conf.AMQP_TARGET,
                                   route_key,
                                   peer_service=None)
        service._callback_connection_close = lambda conn: None

        stub = PeerInnerStub(conf.AMQP_TARGET, route_key)
        stub._callback_connection_close = lambda conn: None

        async def _run():
            try:
                await service.connect(exclusive=True)
                await stub.connect()

                result = await stub.async_task().hello()
                self.assertEqual(result, 'peer_hello')

                bad_service = PeerInnerService(conf.AMQP_TARGET,
                                               route_key,
                                               peer_service=None)
                with self.assertRaises(ChannelClosed):
                    await bad_service.connect()

                with self.assertRaises(ChannelClosed):
                    await bad_service.connect(exclusive=True)
            finally:
                await service._connection.close()
                await stub._connection.close()

        loop = asyncio.get_event_loop()
        loop.run_until_complete(_run())
コード例 #2
0
        async def _run():
            route_key = conf.PEER_QUEUE_NAME_FORMAT.format(
                amqp_key=conf.AMQP_KEY)
            peer_inner_service = PeerInnerService(conf.AMQP_TARGET,
                                                  route_key,
                                                  peer_service=None)
            peer_inner_service._callback_connection_close = lambda conn: None
            await peer_inner_service.connect()

            route_key = conf.CHANNEL_QUEUE_NAME_FORMAT.format(
                channel_name=conf.LOOPCHAIN_DEFAULT_CHANNEL,
                amqp_key=conf.AMQP_KEY)
            channel_inner_service = ChannelInnerService(conf.AMQP_TARGET,
                                                        route_key,
                                                        channel_service=None)
            channel_inner_service._callback_connection_close = lambda conn: None
            await channel_inner_service.connect()

            StubCollection().amqp_target = conf.AMQP_TARGET
            StubCollection().amqp_key = conf.AMQP_KEY

            await StubCollection().create_peer_stub()
            result = await StubCollection().peer_stub.async_task().hello()
            self.assertEqual(result, 'peer_hello')

            await StubCollection().create_channel_stub(
                conf.LOOPCHAIN_DEFAULT_CHANNEL)
            result = await StubCollection().channel_stubs[
                conf.LOOPCHAIN_DEFAULT_CHANNEL].async_task().hello()
            self.assertEqual(result, 'channel_hello')

            await peer_inner_service._connection.close()
            await channel_inner_service._connection.close()
コード例 #3
0
        async def _run():
            try:
                await service.connect(exclusive=True)
                await stub.connect()

                result = await stub.async_task().hello()
                self.assertEqual(result, 'peer_hello')

                bad_service = PeerInnerService(conf.AMQP_TARGET,
                                               route_key,
                                               peer_service=None)
                try:
                    await bad_service.connect()
                    raise RuntimeError('Peer inner service is not exclusive.')
                except ChannelClosed:
                    pass

                try:
                    await bad_service.connect(exclusive=True)
                    raise RuntimeError('Peer inner service is not exclusive.')
                except ChannelClosed:
                    pass
            finally:
                await service._connection.close()
                await stub._connection.close()
コード例 #4
0
        async def _run():
            try:
                await service.connect(exclusive=True)
                await stub.connect()

                result = await stub.async_task().hello()
                self.assertEqual(result, 'peer_hello')

                bad_service = PeerInnerService(conf.AMQP_TARGET,
                                               route_key,
                                               peer_service=None)
                with self.assertRaises(ChannelClosed):
                    await bad_service.connect()

                with self.assertRaises(ChannelClosed):
                    await bad_service.connect(exclusive=True)
            finally:
                await service._connection.close()
                await stub._connection.close()
コード例 #5
0
    def serve(self,
              port,
              agent_pin: str = None,
              amqp_target: str = None,
              amqp_key: str = None,
              event_for_init: multiprocessing.Event = None):
        """start func of Peer Service ===================================================================

        :param port:
        :param agent_pin: kms agent pin
        :param amqp_target: rabbitmq host target
        :param amqp_key: sharing queue key
        :param event_for_init: set when peer initiates
        """

        amqp_target = amqp_target or conf.AMQP_TARGET
        amqp_key = amqp_key or conf.AMQP_KEY

        stopwatch_start = timeit.default_timer()

        self.__init_kms_helper(agent_pin)
        self.__init_port(port)
        self.__init_level_db()
        self.__init_key_by_channel()

        StubCollection().amqp_target = amqp_target
        StubCollection().amqp_key = amqp_key

        peer_queue_name = conf.PEER_QUEUE_NAME_FORMAT.format(amqp_key=amqp_key)
        self.__outer_service = PeerOuterService()
        self.__inner_service = PeerInnerService(amqp_target,
                                                peer_queue_name,
                                                conf.AMQP_USERNAME,
                                                conf.AMQP_PASSWORD,
                                                peer_service=self)

        self.__channel_infos = self.__get_channel_infos()
        if not self.__channel_infos:
            util.exit_and_msg(
                "There is no peer_list, initial network is not allowed without RS!"
            )

        self.__run_rest_services(port)
        self.run_common_service()

        self.__close_kms_helper()

        stopwatch_duration = timeit.default_timer() - stopwatch_start
        logging.info(
            f"Start Peer Service at port: {port} start duration({stopwatch_duration})"
        )

        async def _serve():
            await self.ready_tasks()
            await self.__inner_service.connect(conf.AMQP_CONNECTION_ATTEMPS,
                                               conf.AMQP_RETRY_DELAY,
                                               exclusive=True)

            if conf.CHANNEL_BUILTIN:
                await self.serve_channels()

            if event_for_init is not None:
                event_for_init.set()

            logging.info(f'peer_service: init complete peer: {self.peer_id}')

        loop = self.__inner_service.loop
        loop.create_task(_serve())
        loop.add_signal_handler(signal.SIGINT, self.close)
        loop.add_signal_handler(signal.SIGTERM, self.close)

        try:
            loop.run_forever()
        finally:
            loop.run_until_complete(loop.shutdown_asyncgens())
            loop.close()

        self.__common_service.wait()

        # process monitor must stop monitoring before any subprocess stop
        # Monitor().stop()

        logging.info("Peer Service Ended.")
        if self.__rest_service is not None:
            self.__rest_service.stop()

        if self.__rest_proxy_server is not None:
            self.__rest_proxy_server.stop()
コード例 #6
0
    def serve(self,
              port,
              agent_pin: str = None,
              amqp_target: str = None,
              amqp_key: str = None,
              event_for_init: multiprocessing.Event = None):
        """start func of Peer Service ===================================================================

        :param port:
        :param agent_pin: kms agent pin
        :param amqp_target: rabbitmq host target
        :param amqp_key: sharing queue key
        :param event_for_init: set when peer initiates
        """

        amqp_target = amqp_target or conf.AMQP_TARGET
        amqp_key = amqp_key or conf.AMQP_KEY

        stopwatch_start = timeit.default_timer()

        self._init_kms_helper(agent_pin)
        self._init_port(port)
        self._init_node_key()

        StubCollection().amqp_target = amqp_target
        StubCollection().amqp_key = amqp_key

        peer_queue_name = conf.PEER_QUEUE_NAME_FORMAT.format(amqp_key=amqp_key)
        self._outer_service = PeerOuterService(self)
        self._inner_service = PeerInnerService(amqp_target,
                                               peer_queue_name,
                                               conf.AMQP_USERNAME,
                                               conf.AMQP_PASSWORD,
                                               peer_service=self)

        self._channel_infos = conf.CHANNEL_OPTION

        self._run_rest_services(port)

        self._close_kms_helper()

        stopwatch_duration = timeit.default_timer() - stopwatch_start
        logging.info(
            f"Start Peer Service at port: {port} start duration({stopwatch_duration})"
        )

        async def _serve():
            await self.run_p2p_server()
            await self.ready_tasks()
            await self._inner_service.connect(conf.AMQP_CONNECTION_ATTEMPTS,
                                              conf.AMQP_RETRY_DELAY,
                                              exclusive=True)

            if conf.CHANNEL_BUILTIN:
                await self.serve_channels()

            if event_for_init is not None:
                event_for_init.set()

            logging.info(f'init complete peer: {self.peer_id}')

        loop = self._inner_service.loop
        loop.create_task(_serve())
        loop.add_signal_handler(signal.SIGINT, self.close)
        loop.add_signal_handler(signal.SIGTERM, self.close)

        try:
            loop.run_forever()
        finally:
            loop.run_until_complete(loop.shutdown_asyncgens())
            self._cleanup(loop)
            loop.close()

        logging.info("Peer Service Ended.")