示例#1
0
 def _deferred_query_callback(cls, list):
     responses = NotaryResponses([])
     for success, response in list:
         if success:
             responses.append(response)
     return responses
示例#2
0
    def query(self, service, num=0, timeout=10):
        """Query Notaries and return NotaryResponses instance

        For any Notary not responding, a None will be in the array.

        num specifies the number of Notaries to query. If 0, all notaries
        are queried.

        timeout is the timeout in seconds"""
        if num == 0:
            to_query = self
        else:
            if num > len(self):
                raise ValueError("Too many notaries requested (%s > %s)" % (num, len(self)))
            to_query = random.sample(self, num)
        responses = NotaryResponses()
        dispatchers = []
        # Use own map here for thread safety
        map = {}
        for notary in to_query:
            self.logger.debug("Querying %s about %s..." % (notary, service))
            dispatchers.append((notary, notary.get_dispatcher(service, map)))
        self.logger.debug("Calling asyncore.loop()")
        timed_asyncore.loop_with_timeout(timeout=timeout, map=map)
        self.logger.debug("asyncore.loop() done.")
        for notary, dispatcher in dispatchers:
            try:
                self.logger.debug("Parsing response from %s" % notary)
                response = dispatcher.get_response()
                self.logger.debug("Response from %s parsed" % notary)
                responses.append(response)
            except EOFError as e:
                self.logger.error("Failed to get response from %s: %s" % (notary, str(e)))
                responses.append(None)
            except httplib.BadStatusLine as e:
                self.logger.error("Failed to parse response from %s, bad status: %s" % (notary, e))
                responses.append(None)
            except NotaryException as e:
                self.logger.error("Error validating response from %s: %s" % (notary, e))
                responses.append(None)
            except Exception as e:
                self.logger.exception("Unknown error handling response from %s: %s" % (notary, e))
                responses.append(None)
        return responses