def remove_torrent(self, hash): torrent = Torrent.instantiate(hash) if hash in self.torrents: torrent = self.torrents[hash] del self.torrents[hash] torrent.remove( ) #causes connections to close and other bookkeeping
def got_handshake(self, data): if options.verbose > 2: logging.info('got handshake %s' % [data]) self.handshake = parse_handshake(data) if options.verbose > 1: logging.info('parsed handshake %s' % [self.handshake]) if self.handshake: self.peerid = self.handshake['peerid'] self.peer = Peer.instantiate({'peerid':self.peerid}) self.infohash = self.handshake['infohash'] if not self.torrent: # check if this torrent is started, and in the current client's list of torrents! #self.torrent = Torrent.instantiate( binascii.hexlify(self.handshake['infohash']) ) self.torrent = Torrent.instantiate( self.handshake['infohash'] ) self.torrent.connections.append(self) logging.info('connection has torrent %s with hash %s%s' % (self.torrent, [self.torrent.hash], ' (with metadata)' if self.torrent.meta else '')) if not self._sent_handshake: self.send_handshake() if not self._sent_extension_handshake: self.send_extension_handshake() if self.torrent and self.torrent.meta: self.send_bitmask() self.get_more_messages() else: self.get_more_messages() else: logging.info('invalid/unrecognized handshake') self.stream.close()
def add_torrent(self, hash): torrent = Torrent.instantiate(hash) if hash not in self.torrents: self.torrents[hash] = torrent # notify any sessions #self.notify_sessions(added={'btapp/torrent':torrent}) #Session.notify(client=self, added=torrent) return True
def added_torrent_url(self, response): if response.code == 200: meta = bencode.bdecode(response.body) infohash = hashlib.sha1(bencode.bencode(meta['info'])).digest() torrent = Torrent.instantiate(infohash) if not torrent.meta: # update? torrent.update_meta(meta) torrent.save_metadata() self.add_torrent(torrent.hash)
def initiate_connected(cls, conn, infohash): logging.info('connected to %s' % str(conn.address)) conn._connecting = False conn.infohash = infohash conn.torrent = Torrent.instantiate( infohash ) conn.torrent.connections.append(conn) conn.send_handshake(infohash=infohash) conn.send_extension_handshake() if conn.torrent.meta: conn.send_bitmask() conn.when_connected()
def got_handshake(self, data): logging.info('got handshake %s' % [data]) self.handshake = parse_handshake(data) if self.handshake: self.torrent = Torrent.instantiate( binascii.hexlify(self.handshake['infohash']) ) logging.info('connection has torrent %s with hash %s%s' % (self.torrent, self.torrent.hash, ' (with metadata)' if self.torrent.meta else '')) if not self._sent_handshake: self.send_handshake() if self.torrent and self.torrent.meta: self.send_bitmask() self.get_more_messages() else: logging.info('invalid/unrecognized handshake') self.stream.close()
def __init__(self, data=None): self.torrents = {} self.sessions = {} self.torrent_althashes = {} if data: self.id = data['id'] if 'torrents' in data: self.torrents = dict((t, Torrent.instantiate(t)) for t in data['torrents'] if len(t) == 20) for hash, t in self.torrents.iteritems(): t.get_bitmask( force_create=False ) # force a load of metadata from .torrent files #t.load_attributes() # done in the init else: self.id = str(uuid4()).split('-')[0]