Esempio n. 1
0
class MyCommunity(Community):
    master_peer = Peer(ECCrypto().generate_key(u"medium"))

    def __init__(self, *args, **kwargs):
        super(MyCommunity, self).__init__(*args, **kwargs)
        self.request_cache = RequestCache()

    def unload(self):
        self.request_cache.shutdown()
        super(MyCommunity, self).unload()

    def finish_ping(self, cache, include=True):
        global RESULTS
        print(cache.hostname, cache.address, time.time() - cache.starttime)
        if include:
            if (cache.hostname, cache.address) in RESULTS:
                RESULTS[(cache.hostname,
                         cache.address)].append(time.time() - cache.starttime)
            else:
                RESULTS[(cache.hostname,
                         cache.address)] = [time.time() - cache.starttime]
        elif (cache.hostname, cache.address) not in RESULTS:
            RESULTS[(cache.hostname, cache.address)] = []

        self.next_ping()

    def next_ping(self):
        global CHECK_QUEUE
        if CHECK_QUEUE:
            hostname, address = CHECK_QUEUE.pop()
            packet = self.create_introduction_request(address)
            self.request_cache.add(
                PingCache(self, hostname, address, time.time()))
            self.endpoint.send(address, packet)
        else:
            reactor.callFromThread(reactor.stop)

    def introduction_response_callback(self, peer, dist, payload):
        if self.request_cache.has(u"introping", payload.identifier):
            cache = self.request_cache.pop(u"introping", payload.identifier)
            self.finish_ping(cache)

    def started(self):
        global CHECK_QUEUE

        dnsmap = {}
        for (address, port) in _DNS_ADDRESSES:
            try:
                ip = gethostbyname(address)
                dnsmap[(ip, port)] = address
            except:
                pass

        UNKNOWN_NAME = '*'

        for (ip, port) in _DEFAULT_ADDRESSES:
            hostname = dnsmap.get((ip, port), None)
            if not hostname:
                hostname = UNKNOWN_NAME
                UNKNOWN_NAME = UNKNOWN_NAME + '*'
            CHECK_QUEUE.append((hostname, (ip, port)))

        CHECK_QUEUE = CHECK_QUEUE * CONST_REQUESTS

        self.next_ping()
Esempio n. 2
0
class MyCommunity(Community):
    community_id = os.urandom(20)

    def __init__(self, *args, **kwargs):
        super(MyCommunity, self).__init__(*args, **kwargs)
        self.request_cache = RequestCache()

    def unload(self):
        self.request_cache.shutdown()
        super(MyCommunity, self).unload()

    def finish_ping(self, cache, include=True):
        global RESULTS
        print(cache.hostname, cache.address, time.time() - cache.starttime)  # noqa: T001
        if include:
            if (cache.hostname, cache.address) in RESULTS:
                RESULTS[(cache.hostname, cache.address)].append(time.time() - cache.starttime)
            else:
                RESULTS[(cache.hostname, cache.address)] = [time.time() - cache.starttime]
        elif (cache.hostname, cache.address) not in RESULTS:
            RESULTS[(cache.hostname, cache.address)] = []

        self.next_ping()

    def next_ping(self):
        global CHECK_QUEUE
        if CHECK_QUEUE:
            hostname, address = CHECK_QUEUE.pop()
            packet = self.create_introduction_request(UDPv4Address(*address))
            self.request_cache.add(PingCache(self, hostname, address, time.time()))
            self.endpoint.send(address, packet)
        else:
            get_event_loop().stop()

    def introduction_response_callback(self, peer, dist, payload):
        if self.request_cache.has(u"introping", payload.identifier):
            cache = self.request_cache.pop(u"introping", payload.identifier)
            self.finish_ping(cache)

    def started(self):
        global CHECK_QUEUE

        dnsmap = {}
        for (address, port) in DISPERSY_BOOTSTRAPPER['init']['dns_addresses']:
            try:
                ip = gethostbyname(address)
                dnsmap[(ip, port)] = address
            except OSError:
                pass

        unknown_name = '*'

        for (ip, port) in DISPERSY_BOOTSTRAPPER['init']['ip_addresses']:
            hostname = dnsmap.get((ip, port), None)
            if not hostname:
                hostname = unknown_name
                unknown_name = unknown_name + '*'
            CHECK_QUEUE.append((hostname, (ip, port)))

        CHECK_QUEUE = CHECK_QUEUE * CONST_REQUESTS

        self.next_ping()