Пример #1
0
    def run(self, options):
        """ 
        Beginning monitor thread. 
        Sleep and wait for interrupt. 
        """
        # Start plugin manager
        plugin_manager.load_all_plugins()
        # Start monitor thread
        paths = settings.get('core', 'base').split(',')

        content.set_push_func(plugin_manager.push_queue)
        if options.rebuild:
            content.rebuild()

        self.monitor = Monitor(paths)
        #self.monitor.set_push_queue_func(plugin_manager.push_queue)
        self.monitor.start()

        # Register bonjour
        if settings.get('core', 'aggregation') == 'True':
            self.bonjour = Bonjour()
            self.bonjour.register(socket.gethostname(), '_mediaplat._tcp',
                                  9020)

        self.server.serve_forever()
Пример #2
0
    def run(self, options):
        """ 
        Beginning monitor thread. 
        Sleep and wait for interrupt. 
        """
        # Start plugin manager
        plugin_manager.load_all_plugins()
        # Start monitor thread
        paths = settings.get('core', 'base').split(',')

        content.set_push_func(plugin_manager.push_queue)
        if options.rebuild:
            content.rebuild()

        self.monitor = Monitor(paths)
        #self.monitor.set_push_queue_func(plugin_manager.push_queue)
        self.monitor.start()

        # Register bonjour
        if settings.get('core', 'aggregation') == 'True':
            self.bonjour = Bonjour()
            self.bonjour.register(socket.gethostname(), '_mediaplat._tcp', 9020)

        self.server.serve_forever()
Пример #3
0
class Mediaplat(object):
    """ Daemonize Class """

    def __init__(self):
        """ 
        Constructor of Daemon Class
        """
        # register signal handlers
        signal.signal(signal.SIGTSTP, signal.SIG_IGN)
        signal.signal(signal.SIGTTIN, signal.SIG_IGN)
        signal.signal(signal.SIGTTOU, signal.SIG_IGN)
        signal.signal(signal.SIGINT,  self.__signal_handler_terminate)
        signal.signal(signal.SIGTERM, self.__signal_handler_terminate)

        try:
            self.server = ThreadedUnixSocketServer(SOCKET_FILE, 
                ThreadedUnixSocketRequestHandler)
        except:
            log.error('address already in use')
            sys.exit(1)

    def __signal_handler_terminate(self, signalnum, frame):
        """ 
        Signal handler for terminate signals.

        Signal handler for the "signal.SIGTERM" signal. Raise a "SystemExit"
        exception to do something for that signal
        """
        log.warning("Terminating on signal %(signalnum)r" % locals())
        raise KeyboardInterrupt


    def run(self, options):
        """ 
        Beginning monitor thread. 
        Sleep and wait for interrupt. 
        """
        # Start plugin manager
        plugin_manager.load_all_plugins()
        # Start monitor thread
        paths = settings.get('core', 'base').split(',')

        content.set_push_func(plugin_manager.push_queue)
        if options.rebuild:
            content.rebuild()

        self.monitor = Monitor(paths)
        #self.monitor.set_push_queue_func(plugin_manager.push_queue)
        self.monitor.start()

        # Register bonjour
        if settings.get('core', 'aggregation') == 'True':
            self.bonjour = Bonjour()
            self.bonjour.register(socket.gethostname(), '_mediaplat._tcp', 9020)

        self.server.serve_forever()

    def stop(self):
        """ 
        stop the server and remove socket file.
        Terminate all children threads and wait until terminate. 
        """
        try:
            os.unlink(SOCKET_FILE)
        except:
            pass

        if getattr(self, 'bonjour', None):
            self.bonjour.unregister()

        self.server.shutdown()

        self.monitor.stop()
        plugin_manager.stop() 

    def __enter__(self):
        """ 
        Entry point of Daemon Class for python 'with' statement
        """
        return self


    def __exit__(self, exc_type, exc_value, traceback):
        """ 
        Clean up function of Daemon Class for python 'with' statement
        """
        self.stop()

    @classmethod
    def request(self, send_data, mode = 0):
        """ request daemon function

            @type  send_data: dict
            @param send_data: parameters related with service and action
            @type  mode: int 
            @param mode: request & response type
            @rtype: dict
            @return: {"error": int, "return_date": dict} dictionary
        """
        try:
            if not os.path.exists(SOCKET_FILE):
                raise CommunicateError

            encoded_send_data = json.dumps(send_data)
            total_send_len = len(encoded_send_data)
            send_header = pack(SOCKET_HEADER_FMT, mode, total_send_len)

            sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
            sock.connect(SOCKET_FILE)

            sock.send(send_header)
            if total_send_len <= CHUNK_SIZE:
                sended_len = sock.send(encoded_send_data)
                if total_send_len != sended_len:
                    log.error('comm send data missmatch send_req {0} != sended {1}'.format(total_send_len, sended_len))
                    raise CommunicateError
            else:
                remain_send_len = total_send_len
                current_send_len = 0
                current_sended_len = 0
                current_send_index = 0

                while True:
                    if remain_send_len < CHUNK_SIZE:
                        current_send_len = remain_send_len
                    else:
                        current_send_len = CHUNK_SIZE

                    current_send_data = encoded_send_data[current_send_index:
                        current_send_index + current_send_len]

                    current_sended_len = sock.send(current_send_data)
                    if current_send_len != current_sended_len:
                        log.error('comm send data missmatch send_req {0} != sended {1}'.format(current_send_len, current_send_data))
                        raise CommunicateError

                    remain_send_len -= current_send_len
                    current_send_index += current_send_len
                    if remain_send_len == 0:
                        break

            all_response = ""
            while True:
                response = sock.recv(CHUNK_SIZE)
                if not response:
                    break
                all_response += response

            sock.close()

            return json.loads(all_response)

        except (CommunicateError, socket.error):
            log.exception('request daemon comunication error')
            return {'result': False, 'data': 'request comunication error'}
Пример #4
0
class Mediaplat(object):
    """ Daemonize Class """
    def __init__(self):
        """ 
        Constructor of Daemon Class
        """
        # register signal handlers
        signal.signal(signal.SIGTSTP, signal.SIG_IGN)
        signal.signal(signal.SIGTTIN, signal.SIG_IGN)
        signal.signal(signal.SIGTTOU, signal.SIG_IGN)
        signal.signal(signal.SIGINT, self.__signal_handler_terminate)
        signal.signal(signal.SIGTERM, self.__signal_handler_terminate)

        try:
            self.server = ThreadedUnixSocketServer(
                SOCKET_FILE, ThreadedUnixSocketRequestHandler)
        except:
            log.error('address already in use')
            sys.exit(1)

    def __signal_handler_terminate(self, signalnum, frame):
        """ 
        Signal handler for terminate signals.

        Signal handler for the "signal.SIGTERM" signal. Raise a "SystemExit"
        exception to do something for that signal
        """
        log.warning("Terminating on signal %(signalnum)r" % locals())
        raise KeyboardInterrupt

    def run(self, options):
        """ 
        Beginning monitor thread. 
        Sleep and wait for interrupt. 
        """
        # Start plugin manager
        plugin_manager.load_all_plugins()
        # Start monitor thread
        paths = settings.get('core', 'base').split(',')

        content.set_push_func(plugin_manager.push_queue)
        if options.rebuild:
            content.rebuild()

        self.monitor = Monitor(paths)
        #self.monitor.set_push_queue_func(plugin_manager.push_queue)
        self.monitor.start()

        # Register bonjour
        if settings.get('core', 'aggregation') == 'True':
            self.bonjour = Bonjour()
            self.bonjour.register(socket.gethostname(), '_mediaplat._tcp',
                                  9020)

        self.server.serve_forever()

    def stop(self):
        """ 
        stop the server and remove socket file.
        Terminate all children threads and wait until terminate. 
        """
        try:
            os.unlink(SOCKET_FILE)
        except:
            pass

        if getattr(self, 'bonjour', None):
            self.bonjour.unregister()

        self.server.shutdown()

        self.monitor.stop()
        plugin_manager.stop()

    def __enter__(self):
        """ 
        Entry point of Daemon Class for python 'with' statement
        """
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        """ 
        Clean up function of Daemon Class for python 'with' statement
        """
        self.stop()

    @classmethod
    def request(self, send_data, mode=0):
        """ request daemon function

            @type  send_data: dict
            @param send_data: parameters related with service and action
            @type  mode: int 
            @param mode: request & response type
            @rtype: dict
            @return: {"error": int, "return_date": dict} dictionary
        """
        try:
            if not os.path.exists(SOCKET_FILE):
                raise CommunicateError

            encoded_send_data = json.dumps(send_data)
            total_send_len = len(encoded_send_data)
            send_header = pack(SOCKET_HEADER_FMT, mode, total_send_len)

            sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
            sock.connect(SOCKET_FILE)

            sock.send(send_header)
            if total_send_len <= CHUNK_SIZE:
                sended_len = sock.send(encoded_send_data)
                if total_send_len != sended_len:
                    log.error(
                        'comm send data missmatch send_req {0} != sended {1}'.
                        format(total_send_len, sended_len))
                    raise CommunicateError
            else:
                remain_send_len = total_send_len
                current_send_len = 0
                current_sended_len = 0
                current_send_index = 0

                while True:
                    if remain_send_len < CHUNK_SIZE:
                        current_send_len = remain_send_len
                    else:
                        current_send_len = CHUNK_SIZE

                    current_send_data = encoded_send_data[
                        current_send_index:current_send_index +
                        current_send_len]

                    current_sended_len = sock.send(current_send_data)
                    if current_send_len != current_sended_len:
                        log.error(
                            'comm send data missmatch send_req {0} != sended {1}'
                            .format(current_send_len, current_send_data))
                        raise CommunicateError

                    remain_send_len -= current_send_len
                    current_send_index += current_send_len
                    if remain_send_len == 0:
                        break

            all_response = ""
            while True:
                response = sock.recv(CHUNK_SIZE)
                if not response:
                    break
                all_response += response

            sock.close()

            return json.loads(all_response)

        except (CommunicateError, socket.error):
            log.exception('request daemon comunication error')
            return {'result': False, 'data': 'request comunication error'}