def resolve(addr): """resolve host if enabled in config""" if config.RESOLVE_HOSTS: try: return socket.gethostbyaddr(addr)[0] except socket.herror, e: logger.debug(e) return addr def discover(async=False): """Discover other hosts in the network""" for item in config.STATICHOSTS: url = "http://%s/" % item yield (item, url) s = network.broadcast_dgram_socket(config.CLIENT_PORT) hello = config.NETWORK_NAME s.sendto(hello, (config.BROADCAST_IP, config.PORT)) for data in recv(s, config.DISCOVER_TIMEOUT, async): if data: msg, (addr, port) = data if config.STATICHOSTS and\ "%s:%i" % (addr, port) in config.STATICHOSTS: continue if msg.startswith(hello): name = msg[len(hello) + 1:] url = "http://%s:%i/" % (resolve(addr), port) yield (name, url) else: yield None else:
def __init__(self, fi): threading.Thread.__init__(self) self.setDaemon(True) self.fileindex = fi self.socket = network.broadcast_dgram_socket(config.PORT)
def resolve(addr): """resolve host if enabled in config""" if config.RESOLVE_HOSTS: try: return socket.gethostbyaddr(addr)[0] except socket.herror, e: logger.debug(e) return addr def discover(async=False): """Discover other hosts in the network""" for item in config.STATICHOSTS: url = "http://%s/" % item yield (item, url) s = network.broadcast_dgram_socket(config.CLIENT_PORT) hello = config.NETWORK_NAME s.sendto(hello, (config.BROADCAST_IP, config.PORT)) for data in recv(s, config.DISCOVER_TIMEOUT, async): if data: msg, (addr, port) = data if config.STATICHOSTS and\ "%s:%i" % (addr, port) in config.STATICHOSTS: continue if msg.startswith(hello): name = msg[len(hello)+1:] url = "http://%s:%i/" % (resolve(addr), port) yield (name, url) else: yield None else: