Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

resolve.py 3.2KB

1 year ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- test-case-name: twisted.names.test.test_resolve -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Lookup a name using multiple resolvers.
  6. Future Plans: This needs someway to specify which resolver answered
  7. the query, or someway to specify (authority|ttl|cache behavior|more?)
  8. """
  9. from zope.interface import implementer
  10. from twisted.internet import defer, interfaces
  11. from twisted.names import common, dns, error
  12. class FailureHandler:
  13. def __init__(self, resolver, query, timeout):
  14. self.resolver = resolver
  15. self.query = query
  16. self.timeout = timeout
  17. def __call__(self, failure):
  18. # AuthoritativeDomainErrors should halt resolution attempts
  19. failure.trap(dns.DomainError, defer.TimeoutError, NotImplementedError)
  20. return self.resolver(self.query, self.timeout)
  21. @implementer(interfaces.IResolver)
  22. class ResolverChain(common.ResolverBase):
  23. """
  24. Lookup an address using multiple L{IResolver}s
  25. """
  26. def __init__(self, resolvers):
  27. """
  28. @type resolvers: L{list}
  29. @param resolvers: A L{list} of L{IResolver} providers.
  30. """
  31. common.ResolverBase.__init__(self)
  32. self.resolvers = resolvers
  33. def _lookup(self, name, cls, type, timeout):
  34. """
  35. Build a L{dns.Query} for the given parameters and dispatch it
  36. to each L{IResolver} in C{self.resolvers} until an answer or
  37. L{error.AuthoritativeDomainError} is returned.
  38. @type name: C{str}
  39. @param name: DNS name to resolve.
  40. @type type: C{int}
  41. @param type: DNS record type.
  42. @type cls: C{int}
  43. @param cls: DNS record class.
  44. @type timeout: Sequence of C{int}
  45. @param timeout: Number of seconds after which to reissue the query.
  46. When the last timeout expires, the query is considered failed.
  47. @rtype: L{Deferred}
  48. @return: A L{Deferred} which fires with a three-tuple of lists of
  49. L{twisted.names.dns.RRHeader} instances. The first element of the
  50. tuple gives answers. The second element of the tuple gives
  51. authorities. The third element of the tuple gives additional
  52. information. The L{Deferred} may instead fail with one of the
  53. exceptions defined in L{twisted.names.error} or with
  54. C{NotImplementedError}.
  55. """
  56. if not self.resolvers:
  57. return defer.fail(error.DomainError())
  58. q = dns.Query(name, type, cls)
  59. d = self.resolvers[0].query(q, timeout)
  60. for r in self.resolvers[1:]:
  61. d = d.addErrback(FailureHandler(r.query, q, timeout))
  62. return d
  63. def lookupAllRecords(self, name, timeout=None):
  64. # XXX: Why is this necessary? dns.ALL_RECORDS queries should
  65. # be handled just the same as any other type by _lookup
  66. # above. If I remove this method all names tests still
  67. # pass. See #6604 -rwall
  68. if not self.resolvers:
  69. return defer.fail(error.DomainError())
  70. d = self.resolvers[0].lookupAllRecords(name, timeout)
  71. for r in self.resolvers[1:]:
  72. d = d.addErrback(FailureHandler(r.lookupAllRecords, name, timeout))
  73. return d