Exemplo n.º 1
0
def _stop_reactor():
    if not reactor.running:
        logger.info("reactor not running")
        return
    reactor.callFromThread(reactor.stop)
    reactor.runUntilCurrent()
    logger.info("reactor stopped")
Exemplo n.º 2
0
    def disconnectAll(self):
        reactor.runUntilCurrent()

        def _disconnectAll():
            for subscriber in self.connections_ready:
                yield subscriber.transport.loseConnection()
        task.cooperate(_disconnectAll())
Exemplo n.º 3
0
    def test_clears_lock_if_change_power_state_fails(self):
        class TestException(Exception):
            pass

        pcs = self.patch_autospec(power, 'power_change_starting')
        pcs.return_value = fail(TestException('boom'))

        system_id = factory.make_name('system_id')
        hostname = factory.make_hostname()
        power_driver = random.choice(
            [driver for _, driver in PowerDriverRegistry if driver.queryable])
        power_change = random.choice(['on', 'off', 'cycle'])
        context = sentinel.context

        logger = self.useFixture(TwistedLoggerFixture())

        yield power.maybe_change_power_state(system_id, hostname,
                                             power_driver.name, power_change,
                                             context)
        reactor.runUntilCurrent()  # Run all delayed calls.
        self.assertNotIn(system_id, power.power_action_registry)
        self.assertDocTestMatches(
            """\
            %s: Power %s failed.
            Traceback (most recent call last):
            ...
            %s.TestException: boom
            """ % (hostname, power_change, __name__), logger.dump())
Exemplo n.º 4
0
    def test_tftp_service_rebinds_on_HUP(self):
        # Initial set of interfaces to bind to.
        interfaces = {"1.1.1.1", "2.2.2.2"}
        self.patch(
            tftp_module, "get_all_interface_addresses", lambda: interfaces
        )

        tftp_service = TFTPService(
            resource_root=self.make_dir(),
            client_service=Mock(),
            port=factory.pick_port(),
        )
        tftp_service.updateServers()

        # The child services of tftp_services are named after the
        # interface they bind to.
        self.assertEqual(
            interfaces, {server.name for server in tftp_service.getServers()}
        )

        # Update the set of interfaces to bind to.
        interfaces.add("3.3.3.3")
        interfaces.remove("1.1.1.1")

        # Ask the TFTP service to update its set of servers.
        tftp_service.updateServers()

        # We're in the reactor thread but we want to move the reactor
        # forwards, hence we need to get all explicit about it.
        reactor.runUntilCurrent()

        # The interfaces now bound match the updated interfaces set.
        self.assertEqual(
            interfaces, {server.name for server in tftp_service.getServers()}
        )
Exemplo n.º 5
0
    def _pulse(self):
        # FIXME: This is a hack, remove it when we can avoid
        #        calling dialog.run()
        reactor.doIteration(0.1)
        reactor.runUntilCurrent()

        self.send_progress.pulse()
        return not self.wizard.link_request_done
Exemplo n.º 6
0
    def disconnectAll(self):
        reactor.runUntilCurrent()

        def _disconnectAll():
            for subscriber in self.connections_ready:
                yield subscriber.transport.loseConnection()

        task.cooperate(_disconnectAll())
Exemplo n.º 7
0
    def _pulse(self):
        # FIXME: This is a hack, remove it when we can avoid
        #        calling dialog.run()
        reactor.doIteration(0.1)
        reactor.runUntilCurrent()

        self.send_progress.pulse()
        return not self.wizard.tef_request_done
Exemplo n.º 8
0
    def Receive(self, replytype, chain=None, **kw):
        """This method allows code to act in a synchronous manner, it waits to 
        return until the deferred fires but it doesn't prevent other queued 
        calls from being executed.  Send must be called first, which sets up 
        the chain/factory.  
        
        WARNING: If defer is set to True, must either call Receive
        immediately after Send (ie. no intervening Sends) or pass
        chain in as a paramter.
        
        Parameters:
            replytype -- TypeCode
        KeyWord Parameters:
            chain -- processing chain, optional
            
        """
        chain = chain or self.chain
        d = chain.flow.deferred
        if self.trace:

            def trace(soapdata):
                print("_" * 33,
                      time.ctime(time.time()),
                      "RESPONSE:",
                      file=self.trace)
                print(soapdata, file=self.trace)
                return soapdata

            d.addCallback(trace)

        chain.processResponse(d, replytype, **kw)
        if self.defer:
            return d

        failure = []
        append = failure.append

        def errback(result):
            """Used with Response method to suppress 'Unhandled error in 
            Deferred' messages by adding an errback.
            """
            append(result)
            return None

        d.addErrback(errback)

        # spin reactor
        while not d.called:
            reactor.runUntilCurrent()
            t2 = reactor.timeout()
            t = reactor.running and t2
            reactor.doIteration(t)

        pyobj = d.result
        if len(failure):
            failure[0].raiseException()

        return pyobj
Exemplo n.º 9
0
 def tearDown(self):
     """Kill all connections with fire."""
     if self.transport:
         self.transport.loseConnection()
     super(EmailServerServiceTests, self).tearDown()
     # FIXME: this is definitely not how we're supposed to do this, but it
     # kills the DirtyReactorAggregateErrors.
     reactor.disconnectAll()
     reactor.runUntilCurrent()
Exemplo n.º 10
0
 def tearDown(self):
     """Kill all connections with fire."""
     if self.transport:
         self.transport.loseConnection()
     super(EmailServerServiceTests, self).tearDown()
     # FIXME: this is definitely not how we're supposed to do this, but it
     # kills the DirtyReactorAggregateErrors.
     reactor.disconnectAll()
     reactor.runUntilCurrent()
Exemplo n.º 11
0
 def mainLoop(self, reactor):
     Hub.state = 2
     # Unlike reactor's mainLoop, this function does not catch exceptions.
     # Anything raised goes into the main greenlet (because it is always the
     # parent of this one)
     while reactor.running:
         # Advance simulation time in delayed event processors.
         reactor.runUntilCurrent()
         t2 = reactor.timeout()
         t = reactor.running and t2
         reactor.doIteration(t)
Exemplo n.º 12
0
 def mainLoop(self, reactor):
     Hub.state = 2
     # Unlike reactor's mainLoop, this function does not catch exceptions.
     # Anything raised goes into the main greenlet (because it is always the
     # parent of this one)
     while reactor.running:
         # Advance simulation time in delayed event processors.
         reactor.runUntilCurrent()
         t2 = reactor.timeout()
         t = reactor.running and t2
         reactor.doIteration(t)
Exemplo n.º 13
0
 def _runDefer(self,d, timeout):
     def handleError(x):
         self.error=x
     d.addErrback(handleError)
     count=0 
     while self.result == None and self.error == None and count<timeout:
         reactor.runUntilCurrent()
         reactor.doSelect(0.01)
         count=count+0.01
     if self.error: 
         raise self.error
Exemplo n.º 14
0
    def Receive(self, replytype, chain=None, **kw):
        """This method allows code to act in a synchronous manner, it waits to
        return until the deferred fires but it doesn't prevent other queued
        calls from being executed.  Send must be called first, which sets up
        the chain/factory.

        WARNING: If defer is set to True, must either call Receive
        immediately after Send (ie. no intervening Sends) or pass
        chain in as a paramter.

        Parameters:
            replytype -- TypeCode
        KeyWord Parameters:
            chain -- processing chain, optional

        """
        chain = chain or self.chain
        d = chain.flow.deferred
        if self.trace:
            def trace(soapdata):
                print >>self.trace, "_" * 33, time.ctime(time.time()), "RESPONSE:"
                print >>self.trace, soapdata
                return soapdata

            d.addCallback(trace)

        chain.processResponse(d, replytype, **kw)
        if self.defer:
            return d

        failure = []
        append = failure.append
        def errback(result):
            """Used with Response method to suppress 'Unhandled error in
            Deferred' messages by adding an errback.
            """
            append(result)
            return None

        d.addErrback(errback)

        # spin reactor
        while not d.called:
            reactor.runUntilCurrent()
            t2 = reactor.timeout()
            t = reactor.running and t2
            reactor.doIteration(t)

        pyobj = d.result
        if len(failure):
            failure[0].raiseException()

        return pyobj
Exemplo n.º 15
0
 def run(self):
     while True:
         key = pygame.event.poll()
         if key.type == KEYDOWN:
             if key.key == K_q:
                 break
             elif key.key == K_f:
                 pygame.display.toggle_fullscreen()
         self.draw(screen)
         pygame.display.update()
         reactor.runUntilCurrent()
         reactor.doIteration(0)
Exemplo n.º 16
0
def step(sleep = True, external = True):
    if reactor.running:
        try:
            t = sleep and reactor.running and reactor.timeout()
            reactor.doIteration(t)
            reactor.runUntilCurrent()
        except:
            log.error("problem running reactor - exiting")
            raise SystemExit
        if external:
            dispatch.dispatcher_run()
    else:
        log.info("reactor stopped - exiting")
        raise SystemExit
Exemplo n.º 17
0
def step(sleep=True, external=True):
    if reactor.running:
        try:
            t = sleep and reactor.running and reactor.timeout()
            reactor.doIteration(t)
            reactor.runUntilCurrent()
        except:
            log.error("problem running reactor - exiting")
            raise SystemExit
        if external:
            dispatch.dispatcher_run()
    else:
        log.info("reactor stopped - exiting")
        raise SystemExit
Exemplo n.º 18
0
    def tearDown(self):
        """Kill all connections with fire."""
        if self.transport:
            if not hasattr(self.transport, 'protocol'):
                factory = server.addServer(self.config, self.dist)
                self.transport.protocol = factory.buildProtocol(
                    ('127.0.0.1', 0))

            self.transport.loseConnection()

        super(EmailServerServiceTests, self).tearDown()
        # FIXME: this is definitely not how we're supposed to do this, but it
        # kills the DirtyReactorAggregateErrors.
        reactor.disconnectAll()
        reactor.runUntilCurrent()
Exemplo n.º 19
0
    def tearDown(self):
        """Cleanup method after each ``test_*`` method runs; removes timed out
        connections on the reactor and clears the :ivar:`transport`.

        Basically, kill all connections with fire.
        """
        for delay in reactor.getDelayedCalls():
            try:
                delay.cancel()
            except (AlreadyCalled, AlreadyCancelled):
                pass

        # FIXME: this is definitely not how we're supposed to do this, but it
        # kills the DirtyReactorAggregateErrors.
        reactor.disconnectAll()
        reactor.runUntilCurrent()
Exemplo n.º 20
0
    def tearDown(self):
        """Cleanup method after each ``test_*`` method runs; removes timed out
        connections on the reactor and clears the :ivar:`transport`.

        Basically, kill all connections with fire.
        """
        for delay in reactor.getDelayedCalls():
            try:
                delay.cancel()
            except (AlreadyCalled, AlreadyCancelled):
                pass

        # FIXME: this is definitely not how we're supposed to do this, but it
        # kills the DirtyReactorAggregateErrors.
        reactor.disconnectAll()
        reactor.runUntilCurrent()
Exemplo n.º 21
0
    def test_clears_lock_if_change_power_state_success(self):
        self.patch_methods_using_rpc()

        system_id = factory.make_name('system_id')
        hostname = factory.make_name('hostname')
        power_driver = random.choice(
            [driver for _, driver in PowerDriverRegistry if driver.queryable])
        power_change = random.choice(['on', 'off', 'cycle'])
        context = {
            factory.make_name('context-key'): factory.make_name('context-val')
        }

        yield power.maybe_change_power_state(system_id, hostname,
                                             power_driver.name, power_change,
                                             context)
        reactor.runUntilCurrent()  # Run all delayed calls.
        self.assertNotIn(system_id, power.power_action_registry)
Exemplo n.º 22
0
    def test_checks_missing_packages(self):
        self.patch_methods_using_rpc()

        system_id = factory.make_name('system_id')
        hostname = factory.make_name('hostname')
        power_driver = random.choice(
            [driver for _, driver in PowerDriverRegistry if driver.queryable])
        power_change = random.choice(['on', 'off', 'cycle'])
        context = {
            factory.make_name('context-key'): factory.make_name('context-val')
        }
        yield power.maybe_change_power_state(system_id, hostname,
                                             power_driver.name, power_change,
                                             context)
        reactor.runUntilCurrent()  # Run all delayed calls.
        self.assertThat(power_driver.detect_missing_packages,
                        MockCalledOnceWith())
Exemplo n.º 23
0
    def process(self, timeout=0):
        end = now = time.time()
        final = now + timeout

        while True:
            self._lock.acquire()

            if self._query == 0:
                self._lock.release()
                break

            reactor.runUntilCurrent()
            reactor.doIteration(timeout)

            self._lock.release()

            end = time.time()
            if end >= final:
                break
Exemplo n.º 24
0
    def process(self, timeout=0):
        end = now = time.time()
        final = now + timeout

        while True:
            self._lock.acquire()

            if self._query == 0:
                self._lock.release()
                break

            reactor.runUntilCurrent();
            reactor.doIteration(timeout)

            self._lock.release()

            end = time.time()
            if end >= final:
                break
Exemplo n.º 25
0
    def test__calls_change_power_state_with_timeout(self):
        self.patch_methods_using_rpc()
        defer_with_timeout = self.patch(power, 'deferWithTimeout')

        system_id = factory.make_name('system_id')
        hostname = factory.make_name('hostname')
        power_driver = random.choice(
            [driver for _, driver in PowerDriverRegistry if driver.queryable])
        power_change = random.choice(['on', 'off', 'cycle'])
        context = {
            factory.make_name('context-key'): factory.make_name('context-val')
        }

        yield power.maybe_change_power_state(system_id, hostname,
                                             power_driver.name, power_change,
                                             context)
        reactor.runUntilCurrent()  # Run all delayed calls.
        self.assertThat(
            defer_with_timeout,
            MockCalledOnceWith(power.CHANGE_POWER_STATE_TIMEOUT,
                               power.change_power_state, system_id, hostname,
                               power_driver.name, power_change, context,
                               power.reactor))
Exemplo n.º 26
0
 def _iterateTestReactor(self, debug=False):
     """
     Iterate the reactor.
     """
     reactor.runUntilCurrent()
     if debug:
         # When debug is enabled with iterate using a small delay in steps,
         # to have a much better debug output.
         # Otherwise the debug messages will flood the output.
         print (
             u'delayed: %s\n'
             u'threads: %s\n'
             u'writers: %s\n'
             u'readers: %s\n'
             u'threadpool size: %s\n'
             u'threadpool threads: %s\n'
             u'threadpool working: %s\n'
             u'\n' % (
                 self._reactorQueueToString(),
                 reactor.threadCallQueue,
                 reactor.getWriters(),
                 reactor.getReaders(),
                 self._threadPoolQueueSize(),
                 self._threadPoolThreads(),
                 self._threadPoolWorking(),
                 )
             )
         t2 = reactor.timeout()
         # For testing we want to force to reactor to wake at an
         # interval of at most 1 second.
         if t2 is None or t2 > 1:
             t2 = 0.1
         t = reactor.running and t2
         reactor.doIteration(t)
     else:
         reactor.doIteration(False)
Exemplo n.º 27
0
    def run(self):
        """Override Process run method to provide a custom wrapper for the API.

		This provides a continuous loop for watching the service while keeping
		an ear open to the main process from openContentPlatform, listening for
		any interrupt requests.

		"""
        ## Setup requested log handler
        try:
            ## Twisted imports here to avoid issues with epoll on Linux
            from twisted.internet import reactor, ssl
            from twisted.python.filepath import FilePath
            from twisted.web.server import Site
            from twisted.web.wsgi import WSGIResource
            from twisted.python.threadpool import ThreadPool

            print('Starting {}'.format(self.serviceName))
            self.getLocalLogger()
            self.logger.info('Starting {}'.format(self.serviceName))
            self.logger.info('Setting up the API application...')

            ## Setup shared resources for our WSGIResource instances to use
            self.getSharedLogger()
            self.getSharedDbPool()
            ## Create a PID file for system administration purposes
            pidEntryService(self.serviceName, env, self.pid)
            ## Reference the magic WSGI throwable from our root module using Hug
            application = apiResourceRoot.__hug_wsgi__

            ## Setup the WSGI to be hosted through Twisted's web server
            wsgiThreadPool = ThreadPool()
            wsgiThreadPool.start()
            ## For some reason the system event wasn't working all the time,
            ## so I'm adding an explicit wsgiThreadPool.stop() below as well,
            ## which was needed before reactor.stop() would properly cleanup.
            reactor.addSystemEventTrigger('after', 'shutdown',
                                          wsgiThreadPool.stop)
            resource = WSGIResource(reactor, wsgiThreadPool, application)
            self.logger.info('calling listener on {}:{}.'.format(
                str(self.serviceEndpoint), self.listeningPort))
            if self.useCertificates:
                ## Use TLS to encrypt the communication
                certData = FilePath(
                    os.path.join(
                        env.privateInternalCertPath,
                        self.globalSettings.get(
                            'ocpCertificateCaFile'))).getContent()
                certificate = ssl.PrivateCertificate.loadPEM(certData)
                reactor.listenSSL(self.listeningPort, Site(resource),
                                  certificate.options())
            else:
                ## Plain text communication
                reactor.listenTCP(self.listeningPort,
                                  Site(resource),
                                  interface=self.serviceEndpoint)
            ## Normally we'd just call reactor.run() here and let twisted handle
            ## the wait loop while watching for signals. The problem is that we
            ## need openContentPlatform (parent process) to manage this process.
            ## So this is a bit hacky in that I'm using the reactor code, but I
            ## am manually calling what would be called if I just called run():
            reactor.startRunning()
            ## Start event wait loop
            while reactor._started and not self.shutdownEvent.is_set():
                try:
                    ## Four lines from twisted.internet.main.mainloop:
                    reactor.runUntilCurrent()
                    t2 = reactor.timeout()
                    t = reactor.running and t2
                    reactor.doIteration(t)

                except:
                    exception = traceback.format_exception(
                        sys.exc_info()[0],
                        sys.exc_info()[1],
                        sys.exc_info()[2])
                    self.logger.error('Exception in {}: {}'.format(
                        self.serviceName, str(exception)))
                    break
            if self.shutdownEvent.is_set():
                self.logger.info('Process received shutdownEvent')
            with suppress(Exception):
                wsgiThreadPool.stop()
            with suppress(Exception):
                reactor.stop()

        except:
            exception = traceback.format_exception(sys.exc_info()[0],
                                                   sys.exc_info()[1],
                                                   sys.exc_info()[2])
            self.logger.error('Exception in {}: {}'.format(
                self.serviceName, str(exception)))

        ## Cleanup
        pidRemoveService(self.serviceName, env, self.pid)
        self.logger.info('Stopped {}'.format(self.serviceName))
        print('Stopped {}'.format(self.serviceName))

        ## end run
        return
Exemplo n.º 28
0
 def makeReactorCurrent(self):
     reactor.runUntilCurrent()
Exemplo n.º 29
0
def reactorTick():
    reactor.runUntilCurrent()
    reactor.doIteration(0)
Exemplo n.º 30
0
    def connect(self, event):
        if self.connecting: return
        self.connecting = True
        #Client_GlobalData.serverName = self.serverName.GetLineText(0).encode('utf8')
        if platform.node() != "spoot8-PC" and platform.node(
        ) != "spootdev-virtual-machine":
            Client_GlobalData.serverName = u'www.spootsworld.com'
        else:
            Client_GlobalData.serverName = u'10.0.0.97'
        Client_GlobalData.selfPort = self.portNumberSpinner.GetValue()

        if Client_GlobalData_Config.use_miniupnpc == True and Client_GlobalData.u is not None:
            try:
                #Try to use upnp to map port
                Client_GlobalData.u.discoverdelay = 2000
                print 'Discovering... delay=%ums' % Client_GlobalData.u.discoverdelay
                ndevices = Client_GlobalData.u.discover()
                print ndevices, 'device(s) detected'

                # select an igd
                self.mainFrame.u.selectigd()
                # display information about the IGD and the internet connection
                print 'local ip address :', Client_GlobalData.u.lanaddr
                externalipaddress = Client_GlobalData.u.externalipaddress()
                print 'external ip address :', externalipaddress
                print Client_GlobalData.u.statusinfo(
                ), Client_GlobalData.u.connectiontype()

                port = Client_GlobalData.selfPort

                print 'trying to redirect %s port %u TCP => %s port %u TCP' % (
                    externalipaddress, port, Client_GlobalData.u.lanaddr, port)

                b = Client_GlobalData.u.addportmapping(port, 'TCP', u.lanaddr,
                                                       port, 'HubCade', '')
                if b:
                    print 'Success.'
                else:
                    print 'Failed, hopefully the port is manually mapped.'

                b = Client_GlobalData.u.addportmapping(
                    port, 'UDP', Client_GlobalData.u.lanaddr, port, 'HubCade',
                    '')
                if b:
                    print 'Success.'
                else:
                    print 'Failed, hopefully the port is manually mapped.'
            except:
                print 'miniupnpc port mapping failed.'

        print socket.getaddrinfo(Client_GlobalData.serverName, 5804, 0, 0,
                                 socket.SOL_TCP)
        Client_GlobalData.masterServerIP = socket.getaddrinfo(
            Client_GlobalData.serverName, 5804, 0, 0, socket.SOL_TCP)[0][4][0]

        while Client_GlobalData.masterServerIP == None:
            reactor.runUntilCurrent()
            reactor.doIteration(0)
        #reactor.connectTCP(Client_GlobalData.masterServerIP, 5804, Client_GlobalData.app.clientFactory)
        reactor.connectSSL(Client_GlobalData.masterServerIP, 5804,
                           Client_GlobalData.app.clientFactory,
                           ssl.ClientContextFactory())

        while Client_GlobalData.app.clientFactory.failed==False and ( \
        Client_GlobalData.app.clientFactory.protocol is None or \
        Client_GlobalData.app.clientFactory.protocol.connStatus==ClientProtocol.STARTED or \
        Client_GlobalData.app.clientFactory.protocol.connStatus==ClientProtocol.CHECKING_PORT
        ):
            reactor.runUntilCurrent()
            reactor.doIteration(0)

        if hasattr(Client_GlobalData.app.clientFactory.protocol, 'connStatus'):
            Client_GlobalData.Connected_Status = True
            if Client_GlobalData.type_of_call == "login":
                self.login(self)
            elif Client_GlobalData.type_of_call == "email_password":
                self.emailPassword(self)
            elif Client_GlobalData.type_of_call == "register":
                self.registerAccount(self)

            self.connecting = False
            self.Close(True)
        else:
            Client_GlobalData.Connected_Status = False
            mdial = wx.MessageDialog(
                None,
                'The Hub!Cade server is not accepting connections at this time.',
                'Cannot connect to server', wx.OK | wx.ICON_ERROR)
            mdial.ShowModal()
            mdial.Destroy()
Exemplo n.º 31
0
def update():
		reactor.runUntilCurrent() #@UndefinedVariable
		reactor.doIteration(0) #@UndefinedVariable
		triggerExecQueue()
Exemplo n.º 32
0
    def run(self):
        """Override Process run method to provide a custom wrapper for service.

		Shared by all networked services. This provides a continuous loop for
		watching the child process while keeping an ear open to the main process
		from openContentPlatform, listening for any interrupt requests.

		"""
        try:
            ## Twisted import here to avoid issues with epoll on Linux
            from twisted.internet import reactor, ssl

            ## There are two types of event handlers being used here:
            ##   self.shutdownEvent : main process tells this one to shutdown
            ##                        (e.g. on a Ctrl+C type event)
            ##   self.canceledEvent : this process needs to restart
            serviceEndpoint = self.globalSettings.get('serviceIpAddress')
            useCertificates = self.globalSettings.get('useCertificates', True)

            ## Create a PID file for system administration purposes
            utils.pidEntryService(self.serviceName, env, self.pid)

            ## Network job services use networkService, which is a shared lib
            ## directed by additional input parameters; set args accordingly:
            factoryArgs = None
            if (self.serviceName == 'ContentGatheringService'
                    or self.serviceName == 'UniversalJobService'):
                factoryArgs = (self.serviceName, self.globalSettings,
                               self.canceledEvent, self.shutdownEvent,
                               self.moduleType, self.clientEndpointTable,
                               self.clientResultsTable,
                               self.serviceResultsTable, self.serviceJobTable,
                               self.serviceHealthTable, self.pkgPath,
                               self.serviceSettings, self.serviceLogSetup)
            else:
                factoryArgs = (self.serviceName, self.globalSettings,
                               self.canceledEvent, self.shutdownEvent)

            if useCertificates:
                ## Use TLS to encrypt the communication
                certData = FilePath(
                    os.path.join(
                        env.privateInternalCertPath,
                        self.globalSettings.get(
                            'ocpCertificateCaFile'))).getContent()
                certificate = ssl.PrivateCertificate.loadPEM(certData)
                print('Starting encrypted service: {}'.format(
                    self.serviceName))
                reactor.listenSSL(self.listeningPort,
                                  self.serviceFactory(*factoryArgs),
                                  certificate.options())
            else:
                ## Plain text communication
                print('Starting plain text service: {}'.format(
                    self.serviceName))
                reactor.listenTCP(self.listeningPort,
                                  self.serviceFactory(*factoryArgs),
                                  interface=serviceEndpoint)

            ## The following loop may look hacky at first glance, but solves a
            ## challenge with a mix of Python multi-processing, multi-threading,
            ## then Twisted's reactor and threading.

            ## Python threads that are not daemon types, cannot be forced closed
            ## when the controlling thread closes. So it's important to pass
            ## events all the way through, and have looping code catch/cleanup.

            ## Whenever the main process is being shut down, it must stop all
            ## sub-processes along with their work streams, which includes any
            ## Twisted reactors. We do that by passing shutdownEvent into the
            ## sub-processes. And whenever a service (sub-process) needs to
            ## restart, it notifies the other direction so the main process can
            ## verify it stopped and restart it. We do that by a canceledEvent.

            ## Now to the point of this verbose comment, so the reason we cannot
            ## call reactor.run() here, and instead cut/paste the same code, was
            ## to enhance 'while reactor._started' to watch for our events.

            reactor.startRunning()
            ## Start event wait loop
            while reactor._started and not self.shutdownEvent.is_set(
            ) and not self.canceledEvent.is_set():
                try:
                    ## Four lines from twisted.internet.base.mainloop:
                    reactor.runUntilCurrent()
                    t2 = reactor.timeout()
                    t = reactor.running and t2
                    reactor.doIteration(t)
                except:
                    exception = traceback.format_exception(
                        sys.exc_info()[0],
                        sys.exc_info()[1],
                        sys.exc_info()[2])
                    print('Exception in {}: {}'.format(self.serviceName,
                                                       exception))
                    break
            if self.shutdownEvent.is_set():
                print('Shutdown event received for {}'.format(
                    self.serviceName))
                self.canceledEvent.set()
                with suppress(Exception):
                    time.sleep(2)
                    print('Calling reactor stop for {}'.format(
                        self.serviceName))
                    reactor.stop()
                    time.sleep(.5)
            elif self.canceledEvent.is_set():
                print('Canceled event received for {}'.format(
                    self.serviceName))
                with suppress(Exception):
                    time.sleep(2)
                    reactor.stop()
                    time.sleep(.5)

        except PermissionError:
            exceptionOnly = traceback.format_exception_only(
                sys.exc_info()[0],
                sys.exc_info()[1])
            print('  {}'.format(exceptionOnly))
            print('  Stopping {}'.format(self.serviceName))
        except:
            exception = traceback.format_exception(sys.exc_info()[0],
                                                   sys.exc_info()[1],
                                                   sys.exc_info()[2])
            print('Exception in {}: {}'.format(self.serviceName, exception))

        ## Cleanup
        utils.pidRemoveService(self.serviceName, env, self.pid)
        with suppress(Exception):
            reactor.stop()
        print('Stopped {}'.format(self.serviceName))

        ## end ServiceProcess
        return
Exemplo n.º 33
0
	def run(self):
		"""Override Process run method to provide a custom wrapper for service.

		Shared by all local services. This provides a continuous loop for
		watching the child process while keeping an ear open to the main process
		from openContentPlatform, listening for any interrupt requests.

		"""
		try:
			## Twisted import here to avoid issues with epoll on Linux
			from twisted.internet import reactor

			## There are two types of event handlers being used here:
			##   self.shutdownEvent : main process tells this one to shutdown
			##                        (e.g. on a Ctrl+C type event)
			##   self.canceledEvent : this process needs to restart

			## Create a PID file for system administration purposes
			utils.pidEntryService(self.serviceName, env, self.pid)

			factoryArgs = (self.serviceName, self.globalSettings, self.canceledEvent, self.shutdownEvent)
			print('Starting local service: {}'.format(self.serviceName))

			## The following loop may look hacky at first glance, but solves a
			## challenge with a mix of Python multi-processing, multi-threading,
			## then Twisted's reactor and threading.

			## Python threads that are not daemon types, cannot be forced closed
			## when the controlling thread closes. So it's important to pass
			## events all the way through, and have looping code catch/cleanup.

			## Whenever the main process is being shut down, it must stop all
			## sub-processes along with their work streams, which includes any
			## Twisted reactors. We do that by passing shutdownEvent into the
			## sub-processes. And whenever a service (sub-process) needs to
			## restart, it notifies the other direction so the main process can
			## verify it stopped and restart it. We do that by a canceledEvent.

			## Now to the point of this verbose comment, so the reason we cannot
			## call reactor.run() here, and instead cut/paste the same code, was
			## to enhance 'while reactor._started' to watch for our events.

			reactor.callFromThread(self.serviceClass, *factoryArgs)
			#reactor.callWhenRunning(self.serviceClass *factoryArgs)

			reactor.startRunning()
			## Start event wait loop
			while reactor._started and not self.shutdownEvent.is_set() and not self.canceledEvent.is_set():
				try:
					## Four lines from twisted.internet.base.mainloop:
					reactor.runUntilCurrent()
					t2 = reactor.timeout()
					t = reactor.running and t2
					reactor.doIteration(t)
				except:
					exception = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])
					print('Exception in {}: {}'.format(self.serviceName, exception))
					break
			if self.shutdownEvent.is_set():
				print('Shutdown event received for {}'.format(self.serviceName))
				self.canceledEvent.set()
				with suppress(Exception):
					time.sleep(2)
					print('Calling reactor stop for {}'.format(self.serviceName))
					reactor.stop()
					time.sleep(.5)
			elif self.canceledEvent.is_set():
				print('Canceled event received for {}'.format(self.serviceName))
				with suppress(Exception):
					time.sleep(2)
					reactor.stop()
					time.sleep(.5)

		except PermissionError:
			exceptionOnly = traceback.format_exception_only(sys.exc_info()[0], sys.exc_info()[1])
			print('  {}'.format(exceptionOnly))
			print('  Stopping {}'.format(self.serviceName))
		except:
			exception = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])
			print('Exception in {}: {}'.format(self.serviceName, exception))

		## Cleanup
		utils.pidRemoveService(self.serviceName, env, self.pid)
		## Remove the handler to avoid duplicate lines the next time it runs
		with suppress(Exception):
			reactor.stop()
		print('Stopped {}'.format(self.serviceName))

		## end ServiceProcess
		return
Exemplo n.º 34
0
def ReactorTick():
    if reactor.running:
        reactor.runUntilCurrent()
        reactor.doIteration(0)
Exemplo n.º 35
0
        # Create a new ensemble ElementTree if one was not sepecifed or
        # we could not open it.
        if element_tree is None:
            m.onCreateNewEnsemble()

        gui_width = int(gui_settings.get('gui_layout/width', 785))
        gui_height = int(gui_settings.get('gui_layout/height', 745))
        gui_pos_x = int(gui_settings.get('gui_layout/x', 20))
        gui_pos_y = int(gui_settings.get('gui_layout/y', 55))

        m.show()
        m.setGeometry(gui_pos_x, gui_pos_y, gui_width, gui_height)
        reactor.run()
        reactor.stop()
        reactor.runUntilCurrent()
        logging.shutdown()
        sys.exit(0)
    except SystemExit:
        # Do nothing
        pass
    except Exception, ex:
        global error_str
        error_str = ''

        def appendErr(text):
            global error_str
            error_str += text

        error_file = maestro.util.PseudoFileOut(appendErr)
        traceback.print_exc(file=error_file)
Exemplo n.º 36
0
 def update(self, interval):
     """Update the network via the Twisted Reactor.
     """
     reactor.runUntilCurrent()
     reactor.doSelect(0)
Exemplo n.º 37
0
def update():
	reactor.runUntilCurrent()
	reactor.doIteration(0)
	triggerExecQueue()
Exemplo n.º 38
0
def ReactorTick():
    if reactor.running:
        reactor.runUntilCurrent()
        reactor.doIteration(0)
Exemplo n.º 39
0
 def signal_handler(signal, frame):
     looper.stop()
     reactor.runUntilCurrent()
     reactor.stop()
Exemplo n.º 40
0
 def OnTimer(self, event):
     reactor.runUntilCurrent()
     reactor.doIteration(0)
Exemplo n.º 41
0
def reactorTick():
    reactor.runUntilCurrent()
    reactor.doIteration(0)
Exemplo n.º 42
0
def update(gm,sm):
    reactor.runUntilCurrent()
    reactor.doIteration(0)
Exemplo n.º 43
0
def catch_up():
 reactor.runUntilCurrent()
 reactor.doIteration(0.1)
Exemplo n.º 44
0
 def OnTimer(self,event):
     reactor.runUntilCurrent()
     reactor.doIteration(0)
Exemplo n.º 45
0
 def _pump(self) -> None:
     """Pump the reactor to advance `Linearizer`s."""
     assert isinstance(reactor, ReactorBase)
     while reactor.getDelayedCalls():
         reactor.runUntilCurrent()
Exemplo n.º 46
0
    return f


if __name__ == '__main__':
    from twisted.internet import reactor
    
    @deferargs
    def test(*args, **kwargs):
        return (args, kwargs)
    @deferargs
    def testequal(a, b):
        assert a == b
    testequal(test(1, 2, foo=3), ((1,2),{'foo':3}))
    testequal(test(1, defer.succeed(2), foo=defer.succeed(3)), ((1,2),{'foo':3}))
    
    reactor.runUntilCurrent()
    
    @deferargs
    def testing_catch():
        assert False
    assert_caught = []
    @catch(AssertionError)
    def onAssertError(error):
        assert_caught.append(True)
    @catch()
    def onAnyError(error):
        assert False
    testing_catch()
    
    assert assert_caught
    reactor.runUntilCurrent()
Exemplo n.º 47
0
def update():
	reactor.runUntilCurrent()
	reactor.doIteration(0)
	triggerExecQueue()
Exemplo n.º 48
0
 def update():
     reactor.runUntilCurrent()
     reactor.doSelect(0)
Exemplo n.º 49
0
def catch_up():
    reactor.runUntilCurrent()
    reactor.doIteration(0.1)