Exemplo n.º 1
0
    def getPeerList(self, url, data):
        """TODO: This is in serious need of refactoring...
        """
        if self.status == 'stopped':
            return
        
        try:
            page = yield getPage(url + '?' + data)

        except Exception as error:
            log.err('Failed to connect to tracker: {0}'.format(url))

            yield sleep(self.interval)
            self.getPeerList(url, data)

        else:
            try:
                res = bdecode(page)
            except BTError:
                log.err("Received an invalid peer list from the tracker: " +\
                    "{0}".format(url))
            else:
                if len(res) == 1:
                    log.msg('Tracker: {0}'.format(res)) # TODO: What is this?
                    return

                peers = res['peers']
                peers_list = []
                try: # Try parsing in binary format first
                    while peers:
                        addr = socket.inet_ntoa(peers[:4])
                        port = struct.unpack('!H', peers[4:6])[0]
                        peers_list.append((addr, port))
                        peers = peers[6:]
                except: # Now try parsing in dictionary format
                    try:
                        for p in peers:
                            peers_list.append((p["ip"], p["port"]))
                    except:
                        log.err("Received an invalid peer list from the " +\
                            "tracker: {0}".format(url))

                log.msg('Received {0} peers from tracker: {1}'.format(
                    len(peers_list), url))
                self.btm.add_peers(peers_list)
                interval = res.get('interval', self.interval)
                yield sleep(interval)
                self.getPeerList(url, data)
Exemplo n.º 2
0
    def deamon_read(self):
        while self.status == 'started':
            size = len(self.buffer)
            if size > self.buffer_max_size :
                remove_count = size - self.buffer_max_size
                remove_count += self.buffer_max_size / 5
                for idx in self.buffer_record[:remove_count] :
                    del self.buffer[idx]
                del self.buffer_record[:remove_count]

            yield sleep(10)
Exemplo n.º 3
0
    def __sendTaskRequest(self, new_task, timeout=None):
        if not new_task:
            return

        if timeout is None:
            timeout = len(new_task) * 60

        for task in new_task :
            i, (beg, size) = task
            self.protocol.send_request(i, beg, size)
            self.piece_doing.append(task)

        yield sleep(timeout)
        self.__checkTimeout(new_task)
Exemplo n.º 4
0
    def connectRetry(self, connector):
        addr = self.getPeerAddr(connector)

        if addr in self.peers_retry:
            retry = self.peers_retry[addr]
        else:
            retry = 0

        if retry > 50:
            self.peers_failed.add(addr)
            self.peers_connecting.remove(addr)
            del self.peers_retry[addr]
        else:
            yield sleep(5)
            connector.connect()
            retry += 1
            self.peers_retry[addr] = retry
Exemplo n.º 5
0
    def start(self):
        self.status = 'started'

        info_hash = self.btm.metainfo.info_hash
        peer_id = self.btm.my_peer_id
        port = self.btm.app.btServer.listen_port
        request = {
            'info_hash' : info_hash,
            'peer_id' : peer_id,
            'port' : port,
            'compact' : 1,
            #'key' : 'abcd', # This is optional anyways
            'uploaded' : 0,
            'downloaded' : 0,
            'left' : 100,
            'event' : 'started'
            }
        request_encode = urlencode(request)

        for url in self.btm.metainfo.announce_list :
            self.getPeerList(url, request_encode)
            yield sleep(1)
Exemplo n.º 6
0
 def conncectPeers(self, peers):
     for addr, port in peers:
         reactor.connectTCP(addr, port, self)
         yield sleep(0)
Exemplo n.º 7
0
 def deamon_write(self):
     while self.status == 'started':
         self.__thread_write()
         yield sleep(10)
Exemplo n.º 8
0
 def send_keep_alive(self):
     yield sleep(60.0)
     while self.connected:
         self.send_data('')
         yield sleep(60.0)