Example #1
0
 def read_and_dispatch(self, timeout=None, thread=True, condition=None):
     """
         Read one message from socket (with timeout specified by the optional 
         argument *timeout*) and dispatches that message.
         
         Parameters:
         
         **timeout** = None
             Timeout in seconds of the read operation. If it is None 
             (or ommitted) then the read will wait 
             until new data is available.
             
         **(return value)**
             True, in case of the operation has suceeded and **one** message
             has been dispatched. False, if no data or malformed data has 
             been received.
             
     """
     self.read_lock.acquire()
     self.reading_event.set()
     try:
         if condition:
             if condition() == False:
                 return False
         if thread:
             dispatch_item = self.dispatch_item_threaded
         else:
             dispatch_item = self.dispatch_item_single
         
         if self._http:
             r = HTTPRequest(self)
             data = r.read()
         else:
             data = self.read(timeout=timeout)
         if not data: 
             return False 
         try:
             item = json.loads(data, self)
             if type(item) is list: # batch call
                 for i in item: 
                     dispatch_item(i)
             elif type(item) is dict: # std call
                 if 'result' in item or 'error' in item:
                     self.dispatch_item_single(item)
                 else:
                     dispatch_item(item)
             else: # Unknown format :-(
                 _log.debug("Received message with unknown format type: %s" , type(item))
                 return False
         except Exception:
             _log.debug(traceback.format_exc())
             return False
         return True
     finally:
         self.reading_event.clear()
         self.read_lock.release()
Example #2
0
    def read_and_dispatch(self, timeout=None, thread=True, condition=None):
        """
            Read one message from socket (with timeout specified by the optional
            argument *timeout*) and dispatches that message.

            Parameters:

            **timeout** = None
                Timeout in seconds of the read operation. If it is None
                (or ommitted) then the read will wait
                until new data is available.

            **(return value)**
                True, in case of the operation has suceeded and **one** message
                has been dispatched. False, if no data or malformed data has
                been received.

        """
        self.read_lock.acquire()
        self.reading_event.set()
        try:
            if condition:
                if condition() is False:
                    return False
            if thread:
                dispatch_item = self.dispatch_item_threaded
            else:
                dispatch_item = self.dispatch_item_single

            data = self.read(timeout=timeout)
            if not data:
                return False
            try:
                item = json.loads(data, self)
                if type(item) is list:  # batch call
                    for i in item:
                        dispatch_item(i)
                elif type(item) is dict:  # std call
                    if 'result' in item:
                        self.dispatch_item_single(item)
                    else:
                        dispatch_item(item)
                else:  # Unknown format :-(
                    _log.debug("Received message with unknown format type: %s",
                               type(item))
                    return False
            except Exception:
                _log.debug(traceback.format_exc())
                return False
            return True
        finally:
            self.reading_event.clear()
            self.read_lock.release()