def test_all_regular_signals_auto_start(self):
     listener = MockListener(self)
     with gateway_example_app_process(None):
         server_started.connect(listener.started)
         gateway = JavaGateway(
             gateway_parameters=GatewayParameters(),
             callback_server_parameters=CallbackServerParameters())
         server_stopped.connect(
             listener.stopped, sender=gateway.get_callback_server())
         server_connection_started.connect(
             listener.connection_started,
             sender=gateway.get_callback_server())
         server_connection_stopped.connect(
             listener.connection_stopped,
             sender=gateway.get_callback_server())
         pre_server_shutdown.connect(
             listener.pre_shutdown, sender=gateway.get_callback_server())
         post_server_shutdown.connect(
             listener.post_shutdown, sender=gateway.get_callback_server())
         example = gateway.entry_point.getNewExample()
         impl = IHelloImpl()
         self.assertEqual("This is Hello!", example.callHello(impl))
         gateway.shutdown()
     self.assertEqual(1, listener.received["started"])
     self.assertEqual(1, listener.received["stopped"])
     self.assertEqual(1, listener.received["pre_shutdown"])
     self.assertEqual(1, listener.received["post_shutdown"])
     self.assertEqual(1, listener.received["connection_started"])
     self.assertEqual(1, listener.received["connection_stopped"])
    def test_all_regular_signals(self):
        listener = MockListener(self)

        server_started.connect(listener.started)

        hello_state = HelloState()
        client_server = ClientServer(
            JavaParameters(), PythonParameters(), hello_state)
        server_stopped.connect(
            listener.stopped, sender=client_server.get_callback_server())
        server_connection_started.connect(
            listener.connection_started,
            sender=client_server.get_callback_server())
        server_connection_stopped.connect(
            listener.connection_stopped,
            sender=client_server.get_callback_server())
        pre_server_shutdown.connect(
            listener.pre_shutdown, sender=client_server.get_callback_server())
        post_server_shutdown.connect(
            listener.post_shutdown, sender=client_server.get_callback_server())
        with clientserver_example_app_process(True):
            client_server.shutdown()

        self.assertEqual(1, listener.received["started"])
        self.assertEqual(1, listener.received["stopped"])
        self.assertEqual(1, listener.received["pre_shutdown"])
        self.assertEqual(1, listener.received["post_shutdown"])
        self.assertEqual(1, listener.received["connection_started"])
        self.assertEqual(1, listener.received["connection_stopped"])
    def test_signals_started_from_python(self):
        listener = MockListener(self)

        with clientserver_example_app_process():
            server_started.connect(listener.started)

            client_server = ClientServer(
                JavaParameters(), PythonParameters())
            example = client_server.entry_point.getNewExample()
            impl = IHelloImpl()
            self.assertEqual("This is Hello!", example.callHello(impl))

            server_stopped.connect(
                listener.stopped, sender=client_server.get_callback_server())
            server_connection_started.connect(
                listener.connection_started,
                sender=client_server.get_callback_server())
            server_connection_stopped.connect(
                listener.connection_stopped,
                sender=client_server.get_callback_server())
            pre_server_shutdown.connect(
                listener.pre_shutdown,
                sender=client_server.get_callback_server())
            post_server_shutdown.connect(
                listener.post_shutdown,
                sender=client_server.get_callback_server())
            client_server.shutdown()

        self.assertEqual(1, listener.received["started"])
        self.assertEqual(1, listener.received["stopped"])
        self.assertEqual(1, listener.received["pre_shutdown"])
        self.assertEqual(1, listener.received["post_shutdown"])
        # Connection initiated from JavaClient, so no signal sent.
        self.assertEqual(0, listener.received["connection_started"])
        self.assertEqual(0, listener.received["connection_stopped"])
    def test_all_regular_signals(self):
        listener = MockListener(self)

        server_started.connect(listener.started)

        hello_state = HelloState()
        client_server = ClientServer(JavaParameters(), PythonParameters(),
                                     hello_state)
        server_stopped.connect(listener.stopped,
                               sender=client_server.get_callback_server())
        server_connection_started.connect(
            listener.connection_started,
            sender=client_server.get_callback_server())
        server_connection_stopped.connect(
            listener.connection_stopped,
            sender=client_server.get_callback_server())
        pre_server_shutdown.connect(listener.pre_shutdown,
                                    sender=client_server.get_callback_server())
        post_server_shutdown.connect(
            listener.post_shutdown, sender=client_server.get_callback_server())
        with clientserver_example_app_process(True):
            client_server.shutdown()

        self.assertEqual(1, listener.received["started"])
        self.assertEqual(1, listener.received["stopped"])
        self.assertEqual(1, listener.received["pre_shutdown"])
        self.assertEqual(1, listener.received["post_shutdown"])
        self.assertEqual(1, listener.received["connection_started"])
        self.assertEqual(1, listener.received["connection_stopped"])
 def test_all_regular_signals_auto_start(self):
     listener = MockListener(self)
     with gateway_example_app_process(None):
         server_started.connect(listener.started)
         gateway = JavaGateway(
             gateway_parameters=GatewayParameters(),
             callback_server_parameters=CallbackServerParameters())
         server_stopped.connect(listener.stopped,
                                sender=gateway.get_callback_server())
         server_connection_started.connect(
             listener.connection_started,
             sender=gateway.get_callback_server())
         server_connection_stopped.connect(
             listener.connection_stopped,
             sender=gateway.get_callback_server())
         pre_server_shutdown.connect(listener.pre_shutdown,
                                     sender=gateway.get_callback_server())
         post_server_shutdown.connect(listener.post_shutdown,
                                      sender=gateway.get_callback_server())
         example = gateway.entry_point.getNewExample()
         impl = IHelloImpl()
         self.assertEqual("This is Hello!", example.callHello(impl))
         gateway.shutdown()
     self.assertEqual(1, listener.received["started"])
     self.assertEqual(1, listener.received["stopped"])
     self.assertEqual(1, listener.received["pre_shutdown"])
     self.assertEqual(1, listener.received["post_shutdown"])
     self.assertEqual(1, listener.received["connection_started"])
     self.assertEqual(1, listener.received["connection_stopped"])
    def test_signals_started_from_python(self):
        listener = MockListener(self)

        with clientserver_example_app_process():
            server_started.connect(listener.started)

            client_server = ClientServer(JavaParameters(), PythonParameters())
            example = client_server.entry_point.getNewExample()
            impl = IHelloImpl()
            self.assertEqual("This is Hello!", example.callHello(impl))

            server_stopped.connect(listener.stopped,
                                   sender=client_server.get_callback_server())
            server_connection_started.connect(
                listener.connection_started,
                sender=client_server.get_callback_server())
            server_connection_stopped.connect(
                listener.connection_stopped,
                sender=client_server.get_callback_server())
            pre_server_shutdown.connect(
                listener.pre_shutdown,
                sender=client_server.get_callback_server())
            post_server_shutdown.connect(
                listener.post_shutdown,
                sender=client_server.get_callback_server())
            client_server.shutdown()

        self.assertEqual(1, listener.received["started"])
        self.assertEqual(1, listener.received["stopped"])
        self.assertEqual(1, listener.received["pre_shutdown"])
        self.assertEqual(1, listener.received["post_shutdown"])
        # Connection initiated from JavaClient, so no signal sent.
        self.assertEqual(0, listener.received["connection_started"])
        self.assertEqual(0, listener.received["connection_stopped"])
Esempio n. 7
0
    def connect(self,gateway_parameters=None,callback_server_parameters=None,path_hook=None):
        '''
        Connect gateway to Java side
        :param gateway_parameters an overriding GatewayParameters instance.
        If None then self._gateway_parameters is used.  If that is None, then new GatewayParameters (defaults) is used
        :param callback_server_parameters an overriding CallbackServerParameters instance.
        If None then self._callback_server_parameters is used.  If that is None, then new CallbackServerParameters (defaults) is used
        '''
        if callback_server_parameters:
            self._callback_server_parameters = callback_server_parameters
        else:
            self._callback_server_parameters = self._callback_server_parameters if self._callback_server_parameters else CallbackServerParameters()
        
        if gateway_parameters:
            self._gateway_parameters = gateway_parameters
        else:
            self._gateway_parameters = self._gateway_parameters if self._gateway_parameters else GatewayParameters()
            
        with self._lock:
            if not self._gateway is None:
                raise OSError('already connected to java gateway')
            server_started.connect(self._started)
            self._gateway = JavaGateway(gateway_parameters=self._gateway_parameters,callback_server_parameters=self._callback_server_parameters)
            cbserver = self._gateway.get_callback_server()
            server_stopped.connect(
                self._stopped, sender=cbserver)
            server_connection_started.connect(
                self._connection_started,
                sender=cbserver)
            server_connection_stopped.connect(
                self._connection_stopped,
                sender=cbserver)
            pre_server_shutdown.connect(
                self._pre_shutdown, sender=cbserver)
            post_server_shutdown.connect(
                self._post_shutdown, sender=cbserver)
            
            class JavaRemoteServiceDiscoverer(object):
                def __init__(self, bridge):
                    self._bridge = bridge
                    
                def _external_discoverService(self,proxy,props):
                    self._bridge._import_service_from_java(proxy,props)
                    
                def _external_updateDiscoveredService(self,props):
                    self._bridge._modify_service_from_java(props)
                    
                def _external_undiscoverService(self,props):
                    self._bridge._unimport_service_from_java(props)
                    
                def _call_endpoint(self,rsId,methodName,serializedArgs):
                    endpoint = self._bridge.get_export_endpoint_for_rsid(rsId)
                    if endpoint:
                        return endpoint._raw_bytes_from_java(methodName,serializedArgs)
                    else:
                        msg = 'No endpoint for rsId=%s methodName=%s serializedArgs=%s' % (rsId,methodName,serializedArgs)
                        _logger.error(msg)
                        raise Exception(msg)
                    
                class Java:
                    implements = [JAVA_DIRECT_ENDPOINT_CLASS, PY4J_CALL_BY_VALUE_CLASS]

            if not path_hook:
                path_hook = JavaPathHook(self)
            self._bridge = JavaRemoteServiceDiscoverer(self)
            '''Call _getExternalDirectDiscovery first, so that we are ready to export'''
            self._consumer = self._gateway.entry_point._getExternalDirectDiscovery()
            '''Then call _setDirectBridge so that java side can now call us
            to notify about exported services'''
            path_list = self._gateway.entry_point._setDirectBridge(path_hook,self._bridge, self._bridge, self.get_id())
            path_hook.initialize_path(path_list)