Esempio n. 1
0
    def __init__(self, name):

        # set name
        self.__name = name

        # logger
        self.logger = Activator().getInstance('LoggerFactory').getLogger('Dispatcher')

        # onStop-delegates
        self.__onStopDelegates = []

        # request-queue
        self.__queueRequest = Queue()

        # request-handler
        self.__requestHandler = RequestHandler(self.logger)

        # running-flag
        self.__running = False

        # request-count
        self.requestCount = 0

        # thread
        self.__thread = Thread(target = self.run)
        self.__thread.setName(self.__name)
        self.__thread.setDaemon(True)
Esempio n. 2
0
class Dispatcher(IActivator):

    # instance
    Instance = None

    # lock
    InstanceLock = Lock()

    """ -------------------------------------------------------------------- """
    """ __new__                                                              """
    """ -------------------------------------------------------------------- """
    def __new__(cls, *p, **k):
        if Dispatcher.Instance is None:
            Dispatcher.Instance = object.__new__(cls, *p, **k)
        return Dispatcher.Instance

    """ -------------------------------------------------------------------- """
    """ __init__                                                             """
    """ -------------------------------------------------------------------- """
    def __init__(self, name):

        # set name
        self.__name = name

        # logger
        self.logger = Activator().getInstance('LoggerFactory').getLogger('Dispatcher')

        # onStop-delegates
        self.__onStopDelegates = []

        # request-queue
        self.__queueRequest = Queue()

        # request-handler
        self.__requestHandler = RequestHandler(self.logger)

        # running-flag
        self.__running = False

        # request-count
        self.requestCount = 0

        # thread
        self.__thread = Thread(target = self.run)
        self.__thread.setName(self.__name)
        self.__thread.setDaemon(True)

    """ -------------------------------------------------------------------- """
    """ getName                                                              """
    """ -------------------------------------------------------------------- """
    def getName(self):
        return self.__name

    """ -------------------------------------------------------------------- """
    """ start                                                              """
    """ -------------------------------------------------------------------- """
    def start(self):

        # log
        self.logger.info('Starting...')

        # start thread
        self.__thread.start()

    """ -------------------------------------------------------------------- """
    """ stop                                                                 """
    """ -------------------------------------------------------------------- """
    def stop(self):

        # running-flag
        self.__running = False

        # join
        self.__thread.join(2.0)

        # shutdown if still alive
        if self.__thread.isAlive():
            self.shutdown()

    """ -------------------------------------------------------------------- """
    """ run                                                                  """
    """ -------------------------------------------------------------------- """
    def run(self):

        # running-flag
        self.__running = True

        # debug
        self.logger.info("up and running")

        # main-loop
        while self.__running:

            # process queue
            try:
                # get and process request
                request = self.__queueRequest.get()
                # handle request
                self.__running = self.__requestHandler.handleRequest(request)
            except Empty, emp:
                self.logger.error("request-queue is empty (%s)" % emp)
            except Exception, e:
                self.logger.error("failed to process request (%s)" % e)