Esempio n. 1
0
    def _run(self):
        httpd = self.httpd
        # Cache for quicker while loops
        client = self.client
        thread_stopped = self.thread_stopped
        thread_suspended = self.thread_suspended

        # Start up instances
        request_mgr = httppersist.RequestMgr()
        subscription_manager = subscribers.SubscriptionMgr(request_mgr,
                                                           self.player)
        self.subscription_manager = subscription_manager

        if settings('plexCompanion') == 'true':
            # Start up httpd
            start_count = 0
            while True:
                try:
                    httpd = listener.ThreadedHTTPServer(
                        client,
                        subscription_manager,
                        ('', v.COMPANION_PORT),
                        listener.MyHandler)
                    httpd.timeout = 0.95
                    break
                except:
                    LOG.error("Unable to start PlexCompanion. Traceback:")
                    import traceback
                    LOG.error(traceback.print_exc())
                sleep(3000)
                if start_count == 3:
                    LOG.error("Error: Unable to start web helper.")
                    httpd = False
                    break
                start_count += 1
        else:
            LOG.info('User deactivated Plex Companion')
        client.start_all()
        message_count = 0
        if httpd:
            thread = Thread(target=httpd.handle_request)

        while not thread_stopped():
            # If we are not authorized, sleep
            # Otherwise, we trigger a download which leads to a
            # re-authorizations
            while thread_suspended():
                if thread_stopped():
                    break
                sleep(1000)
            try:
                message_count += 1
                if httpd:
                    if not thread.isAlive():
                        # Use threads cause the method will stall
                        thread = Thread(target=httpd.handle_request)
                        thread.start()

                    if message_count == 3000:
                        message_count = 0
                        if client.check_client_registration():
                            LOG.debug('Client is still registered')
                        else:
                            LOG.debug('Client is no longer registered. Plex '
                                      'Companion still running on port %s',
                                      v.COMPANION_PORT)
                            client.register_as_client()
                # Get and set servers
                if message_count % 30 == 0:
                    subscription_manager.serverlist = client.getServerList()
                    subscription_manager.notify()
                    if not httpd:
                        message_count = 0
            except:
                LOG.warn("Error in loop, continuing anyway. Traceback:")
                import traceback
                LOG.warn(traceback.format_exc())
            # See if there's anything we need to process
            try:
                task = state.COMPANION_QUEUE.get(block=False)
            except Empty:
                pass
            else:
                # Got instructions, process them
                self._process_tasks(task)
                state.COMPANION_QUEUE.task_done()
                # Don't sleep
                continue
            sleep(50)
        subscription_manager.signal_stop()
        client.stop_all()
    def run(self):
        httpd = False
        # Cache for quicker while loops
        client = self.client
        threadStopped = self.threadStopped
        threadSuspended = self.threadSuspended

        # Start up instances
        requestMgr = httppersist.RequestMgr()
        jsonClass = functions.jsonClass(requestMgr, self.settings)
        subscriptionManager = subscribers.SubscriptionManager(
            jsonClass, requestMgr, self.player, self.mgr)

        queue = Queue.Queue(maxsize=100)

        if settings('plexCompanion') == 'true':
            # Start up httpd
            start_count = 0
            while True:
                try:
                    httpd = listener.ThreadedHTTPServer(
                        client, subscriptionManager, jsonClass, self.settings,
                        queue, ('', self.settings['myport']),
                        listener.MyHandler)
                    httpd.timeout = 0.95
                    break
                except:
                    log.error("Unable to start PlexCompanion. Traceback:")
                    import traceback
                    log.error(traceback.print_exc())

                sleep(3000)

                if start_count == 3:
                    log.error("Error: Unable to start web helper.")
                    httpd = False
                    break

                start_count += 1
        else:
            log.info('User deactivated Plex Companion')

        client.start_all()

        message_count = 0
        if httpd:
            t = Thread(target=httpd.handle_request)

        while not threadStopped():
            # If we are not authorized, sleep
            # Otherwise, we trigger a download which leads to a
            # re-authorizations
            while threadSuspended():
                if threadStopped():
                    break
                sleep(1000)
            try:
                message_count += 1
                if httpd:
                    if not t.isAlive():
                        # Use threads cause the method will stall
                        t = Thread(target=httpd.handle_request)
                        t.start()

                    if message_count == 3000:
                        message_count = 0
                        if client.check_client_registration():
                            log.debug("Client is still registered")
                        else:
                            log.info(
                                "Client is no longer registered. "
                                "Plex Companion still running on port %s" %
                                self.settings['myport'])
                # Get and set servers
                if message_count % 30 == 0:
                    subscriptionManager.serverlist = client.getServerList()
                    subscriptionManager.notify()
                    if not httpd:
                        message_count = 0
            except:
                log.warn("Error in loop, continuing anyway. Traceback:")
                import traceback
                log.warn(traceback.format_exc())
            # See if there's anything we need to process
            try:
                task = queue.get(block=False)
            except Queue.Empty:
                pass
            else:
                # Got instructions, process them
                self.processTasks(task)
                queue.task_done()
                # Don't sleep
                continue
            sleep(20)

        client.stop_all()
        if httpd:
            try:
                httpd.socket.shutdown(SHUT_RDWR)
            except:
                pass
            finally:
                httpd.socket.close()
        log.info("----===## Plex Companion stopped ##===----")
Esempio n. 3
0
    def run(self):
        # Cache for quicker while loops
        log = self.logMsg
        client = self.client
        threadStopped = self.threadStopped
        threadSuspended = self.threadSuspended
        start_count = 0

        # Start up instances
        requestMgr = httppersist.RequestMgr()
        jsonClass = functions.jsonClass(requestMgr, self.settings)
        subscriptionManager = subscribers.SubscriptionManager(
            jsonClass, requestMgr)

        # Start up httpd
        while True:
            try:
                httpd = listener.ThreadedHTTPServer(
                    client,
                    subscriptionManager,
                    jsonClass,
                    self.settings,
                    ('', self.settings['myport']),
                    listener.MyHandler)
                httpd.timeout = 0.95
                break
            except:
                log("Unable to start PlexCompanion. Traceback:", -1)
                log(traceback.print_exc(), -1)

            xbmc.sleep(3000)

            if start_count == 3:
                log("Error: Unable to start web helper.", -1)
                httpd = False
                break

            start_count += 1

        if not httpd:
            return

        client.start_all()

        message_count = 0
        while not threadStopped():
            # If we are not authorized, sleep
            # Otherwise, we trigger a download which leads to a
            # re-authorizations
            while threadSuspended():
                if threadStopped():
                    break
                xbmc.sleep(1000)
            try:

                httpd.handle_request()
                message_count += 1

                if message_count > 100:
                    if client.check_client_registration():
                        log("Client is still registered", 1)
                    else:
                        log("Client is no longer registered", 1)
                        log("Plex Companion still running on port %s"
                            % self.settings['myport'], 1)
                    message_count = 0

                # Get and set servers
                subscriptionManager.serverlist = client.getServerList()

                subscriptionManager.notify()
                xbmc.sleep(50)
            except:
                log("Error in loop, continuing anyway", 0)
                log(traceback.format_exc(), 1)
                xbmc.sleep(50)

        client.stop_all()
        try:
            httpd.socket.shutdown(socket.SHUT_RDWR)
        except:
            pass
        finally:
            httpd.socket.close()
        log("----===## Plex Companion stopped ##===----", 0)