Commits

Matt Joiner committed 8c89c27

Move pretty_bitfield to util.

  • Participants
  • Parent commits 8c4db7d

Comments (0)

Files changed (5)

 
 import gthread
 import logging
-gthread.logger.setLevel(logging.INFO)
+gthread.logger.setLevel(logging.DEBUG)
+gthread.logger.addHandler(logging.FileHandler('gthread.log'))
 
 def main():
     import util
 import gthread
 
 from bencoding import buncode, bencode
+from util import pretty_bitfield
 
 
 class ConnectionClosed(Exception): pass
                     bitfield.add(index)
         self.got_bitfield(bitfield)
 
-    @staticmethod
-    def pretty_bitfield(bitfield):
-        def ranges(bitfield=bitfield):
-            bitfield = iter(sorted(bitfield))
-            start = end = next(bitfield)
-            for index in bitfield:
-                if index == end + 1:
-                    end = index
-                else:
-                    yield start, end
-                    start = end = index
-            yield start, end
-        return '[{}]'.format(
-            ', '.join(
-                str(s) if s == e else '{s}..{e}'.format(**vars())
-                for s, e in ranges()))
-
     def got_bitfield(self, bitfield):
-        self.logger.debug('%s: Got bitfield: %s', self, self.pretty_bitfield(bitfield))
+        self.logger.debug('%s: Got bitfield: %s', self, pretty_bitfield(bitfield))
         self.peer_bitfield = bitfield
 
     def recv_request(self, length):
         assert self.our_extensions[5] & 0x10, self.our_extensions
         assert 'm' in msg, msg
         data = bytes([type]) + bencode(msg)
-        self.logger.debug('%s: Sending extended message: type=%d, data=%r', self, type, data)
+        import pprint
+        self.logger.debug(
+            'Sending extended message (type=%s):\n%s',
+            type,
+            pprint.pformat(msg))
         self.send_message(self.EXTENDED, data)
 
 from data import SingleFileTorrentData, MultipleFileTorrentData
 from metainfo import Metainfo
 from trackers import AnnounceError, Tracker
-from util import multiset, extract_packed_peer_addrs
+from util import multiset, extract_packed_peer_addrs, pretty_bitfield
 
 logger = logging.getLogger('torrent')
 
             if not self.have_piece(index):
                 self.wanted_blocks[index] = set(self.piece_blocks(index))
 
+        self.logger.info('Missing pieces: %s', pretty_bitfield(self.wanted_blocks.keys()))
+
         self.ip_address = 0
         self.downloaded = 0
         self.uploaded = 0
         try:
             self.socket.connect(self.addr)
         except socket.error as exc:
-            raise AnnounceError('Error connecting', exc)
+            raise AnnounceError('Error connecting: %s' % exc)
         self.transaction_id = random.getrandbits(32)
         self.logger.debug('%s: Generated transaction ID: %08x', self, self.transaction_id)
         request = struct.pack('>QII',
             try:
                 data = self.socket.recv(4096)
             except (socket.timeout, socket.error) as exc:
-                raise AnnounceError from exc
+                raise AnnounceError(str(exc)) from exc
             response_action, transaction_id = struct.unpack_from('>II', data)
             if transaction_id != self.transaction_id:
                 self.logger.warning("%s: Transaction ID doesn't match: %08x", self, transaction_id)
         packed_ip, port = struct.unpack_from(data, offset)
         offset += struct.size
         yield socket.inet_ntoa(packed_ip), port
+
+def pretty_bitfield(bitfield):
+    def ranges(bitfield=bitfield):
+        bitfield = iter(sorted(bitfield))
+        start = end = next(bitfield)
+        for index in bitfield:
+            if index == end + 1:
+                end = index
+            else:
+                yield start, end
+                start = end = index
+        yield start, end
+    return '[{}]'.format(
+        ', '.join(
+            str(s) if s == e else '{s}..{e}'.format(**vars())
+            for s, e in ranges()))