예제 #1
0
 def __init__(self, port=None):
     if port:
         self.port = port
     else:
         self.port = default_port
     self.factory = QuartjesServerFactory()
예제 #2
0
 def __init__(self, port=None):
     if port:
         self.port = port
     else:
         self.port = default_port
     self.factory = QuartjesServerFactory()
예제 #3
0
class ServerConnector(object):
    """
    Server side endpoint of the quartjes connector.

    A server needs at exactly one ServerConnector to be able to accept incoming
    connections. The reactor used by the ServerConnector runs in its own thread
    so you do not need to worry about blocking it.

    Register Service instances with the ServerConnector to allow clients to
    access the services.
    
    Parameters
    ----------
    port : int
        Port number to listen for connections on.
    """
    def __init__(self, port=None):
        if port:
            self.port = port
        else:
            self.port = default_port
        self.factory = QuartjesServerFactory()

    def start(self):
        """
        Start accepting incoming connections. Starts the reactor in a separate thread.
        """
        self._endpoint = TCP4ServerEndpoint(reactor, self.port)
        self._endpoint.listen(self.factory)
        if not reactor.running: #@UndefinedVariable
            self._reactor_thread = ServerConnector.ReactorThread()
            self._reactor_thread.start()

    def stop(self):
        """
        Stop accepting incoming connections. Stops the reactor.
        """
        threads.blockingCallFromThread(reactor, reactor.stop) #@UndefinedVariable

    def register_service(self, service, name):
        """
        Register a new Service instance to be accessible from clients.
        
        Parameters
        ----------
        service
            Service instance to register. It will be available for remote clients.
        name : string
            Name the service will be registered under. Clients must use this to 
            access the service.
        """
        self.factory.register_service(service, name)


    class ReactorThread(Thread):
        """
        Thread for running the reactor loop. Does not run as a daemon, so you
        need to manually stop it when closing the server application.
        """
        def __init__(self):
            Thread.__init__(self, name="ReactorThread")
            self.daemon = False

        def run(self):
            #print("Starting reactor")
            reactor.run(installSignalHandlers=0) #@UndefinedVariable
예제 #4
0
class ServerConnector(object):
    """
    Server side endpoint of the quartjes connector.

    A server needs at exactly one ServerConnector to be able to accept incoming
    connections. The reactor used by the ServerConnector runs in its own thread
    so you do not need to worry about blocking it.

    Register Service instances with the ServerConnector to allow clients to
    access the services.
    
    Parameters
    ----------
    port : int
        Port number to listen for connections on.
    """

    def __init__(self, port=None):
        if port:
            self.port = port
        else:
            self.port = default_port
        self.factory = QuartjesServerFactory()

    def start(self):
        """
        Start accepting incoming connections. Starts the reactor in a separate thread.
        """
        self._endpoint = TCP4ServerEndpoint(reactor, self.port)
        self._endpoint.listen(self.factory)
        if not reactor.running:  # @UndefinedVariable
            self._reactor_thread = ServerConnector.ReactorThread()
            self._reactor_thread.start()

    def stop(self):
        """
        Stop accepting incoming connections. Stops the reactor.
        """
        threads.blockingCallFromThread(reactor, reactor.stop)  # @UndefinedVariable

    def register_service(self, service, name):
        """
        Register a new Service instance to be accessible from clients.
        
        Parameters
        ----------
        service
            Service instance to register. It will be available for remote clients.
        name : string
            Name the service will be registered under. Clients must use this to 
            access the service.
        """
        self.factory.register_service(service, name)

    class ReactorThread(Thread):
        """
        Thread for running the reactor loop. Does not run as a daemon, so you
        need to manually stop it when closing the server application.
        """

        def __init__(self):
            Thread.__init__(self, name="ReactorThread")
            self.daemon = False

        def run(self):
            # print("Starting reactor")
            reactor.run(installSignalHandlers=0)  # @UndefinedVariable