def get_deferred_host_ip(): global _host_ip global _host_ip_callbacks global _host_ip_cachetime if hasattr(reactor, 'ident'): assert reactor.ident == thread.get_ident() if _host_ip is not 'unknown' and _host_ip_cachetime + CACHE_TIME > bttime(): return defer.succeed(_host_ip) if get_route_ip: ip = get_route_ip() if ip: _host_ip = ip _host_ip_cachetime = bttime() return defer.succeed(_host_ip) df = defer.Deferred() if not _host_ip_callbacks: def connect(ip): factory = RecorderFactory() factory.protocol = RecorderProtocol if hasattr(reactor, 'limiter'): reactor.connectTCP(ip, 80, factory, urgent=True) else: reactor.connectTCP(ip, 80, factory) rdf = reactor.resolve("ip.bittorrent.com") rdf.addCallback(connect) rdf.addErrback(lambda e : _got_result(None)) _host_ip_callbacks.append(df) return df
def get_deferred_host_ips(): global _host_ips global _host_ips_cachetime assert reactor.ident == thread.get_ident() if _host_ips is not None and _host_ips_cachetime + CACHE_TIME > bttime(): return defer.succeed(_host_ips) df = get_deferred_host_ip() finaldf = defer.Deferred() df.addCallback(_get_deferred_host_ips2, finaldf) return finaldf
def get_deferred_host_ips(): global _host_ips global _host_ips_cachetime if hasattr(reactor, 'ident'): assert reactor.ident == thread.get_ident() if _host_ips is not None and _host_ips_cachetime + CACHE_TIME > bttime(): return defer.succeed(_host_ips) df = get_deferred_host_ip() finaldf = defer.Deferred() df.addCallback(_get_deferred_host_ips2, finaldf) return finaldf
def get_deferred_host_ip(): global _host_ip global _host_ip_callbacks global _host_ip_cachetime if hasattr(reactor, 'ident'): assert reactor.ident == thread.get_ident() if _host_ip is not 'unknown' and _host_ip_cachetime + CACHE_TIME > bttime( ): return defer.succeed(_host_ip) if get_route_ip: ip = get_route_ip() if ip: _host_ip = ip _host_ip_cachetime = bttime() return defer.succeed(_host_ip) df = defer.Deferred() if not _host_ip_callbacks: def connect(ip): factory = RecorderFactory() factory.protocol = RecorderProtocol if hasattr(reactor, 'limiter'): reactor.connectTCP(ip, 80, factory, urgent=True) else: reactor.connectTCP(ip, 80, factory) rdf = reactor.resolve("ip.bittorrent.com") rdf.addCallback(connect) rdf.addErrback(lambda e: _got_result(None)) _host_ip_callbacks.append(df) return df
def begin_discovery(self): # bind to an available port, and join the multicast group df = get_deferred_host_ip() yield df hostip = df.getResult() for p in xrange(self.upnp_addr[1], self.upnp_addr[1] + 50): try: # Original RawServer cannot do this! s = self.rawserver.create_multicastsocket(p, hostip) self.transport = s self.rawserver.start_listening_multicast(s, self) df = s.listening_port.joinGroup(self.upnp_addr[0], socket.INADDR_ANY) yield df result = df.getResult() # blargh if twisted.copyright.version >= '2.4.0': success = None # ACKKKK..K. Prevents "Unhandled error in Deferred" if df._debugInfo is not None: df._debugInfo.failResult = None else: success = 1 if result is success: break elif isinstance(result, twisted.python.failure.Failure): # HACK. If the failure contains a 'No such device' error # then we abort the discovery because this error denotes # that the peer is not connected to the network. if hasattr( result.value, "__getitem__" ) and \ result.value[2] == 19: yield 0 # abort discovery. else: # I suppose keep trying on different ports, but why would # joinGroup fail? self.transport = None x = s.listening_port.stopListening() if isinstance(x, defer.Deferred): yield x x.getResult() except socket.error, e: # may look weird, but spin the event loop once on failure yield defer.succeed(True)
def begin_discovery(self): # bind to an available port, and join the multicast group df = get_deferred_host_ip() yield df hostip = df.getResult() for p in xrange(self.upnp_addr[1], self.upnp_addr[1]+50): try: # Original RawServer cannot do this! s = self.rawserver.create_multicastsocket(p, hostip) self.transport = s self.rawserver.start_listening_multicast(s, self) df = s.listening_port.joinGroup(self.upnp_addr[0], socket.INADDR_ANY) yield df result = df.getResult() # blargh if twisted.copyright.version >= '2.4.0': success = None # ACKKKK..K. Prevents "Unhandled error in Deferred" if df._debugInfo is not None: df._debugInfo.failResult = None else: success = 1 if result is success: break elif isinstance(result, twisted.python.failure.Failure): # HACK. If the failure contains a 'No such device' error # then we abort the discovery because this error denotes # that the peer is not connected to the network. if hasattr( result.value, "__getitem__" ) and \ result.value[2] == 19: yield 0 # abort discovery. else: # I suppose keep trying on different ports, but why would # joinGroup fail? self.transport = None x = s.listening_port.stopListening() if isinstance(x, defer.Deferred): yield x x.getResult() except socket.error, e: # may look weird, but spin the event loop once on failure yield defer.succeed(True)
def exit(self): self.killswitch.set() self.add_deferred(defer.succeed(True))