next-share: complete support for peer logging
authorP2P-Next <p2p-next@cs.pub.ro>
Mon, 20 Sep 2010 17:43:21 +0000 (20:43 +0300)
committerP2P-Next <p2p-next@cs.pub.ro>
Mon, 20 Sep 2010 17:43:21 +0000 (20:43 +0300)
Update cmdlinedl.py to print out periodic information (upload speed,
download speed) about connected peers.

instrumentation/next-share/BaseLib/Tools/cmdlinedl.py

index 487b6b8..5dff96b 100644 (file)
@@ -5,6 +5,8 @@
 #       * fix help message for output dir option
 #            (--output -> --output-dir)
 #       * add support for upload/download speed limitation
+#       * add support for disabling hash checking (useful for fast seeders)
+#       * add support for peer logging: print information about connected peers
 #
 # Razvan Deaconescu, 2008:
 #       * corrected problem when running in background
@@ -24,10 +26,6 @@ from BaseLib.Core.API import *
 from BaseLib.Core.BitTornado.__init__ import version, report_email
 
 
-# use -l/--peer-logging option to enable peer logging
-peer_logging = False
-
-
 # Print usage message
 def usage():
     print "Usage: python cmdlinedl.py [options] torrentfile_or_url"
@@ -59,31 +57,47 @@ def usage():
 def print_version():
     print version, "<" + report_email + ">"
 
+# use -l/--peer-logging option to enable peer logging
+peer_logging = False
+
+# number of state_callback calls to print peer list information
+PEERLIST_OUTPUT_INTERVAL = 5
+
+# number of callback counts
+callback_count = 0
+
 # Print torrent statistics
 def state_callback(ds):
     d = ds.get_download()
-    print >>sys.stderr, '%s %s %5.2f%% %s up %8.2fKB/s down %8.2fKB/s' % \
-            (d.get_def().get_name(), \
+    print >>sys.stderr, 'peers: %s status: %s progres: %5.2f%% up: %8.2fKB/s down: %8.2fKB/s error: %s' % \
+            (ds.get_num_peers(), \
             dlstatus_strings[ds.get_status()], \
             ds.get_progress() * 100, \
-            ds.get_error(), \
             ds.get_current_speed(UPLOAD), \
-            ds.get_current_speed(DOWNLOAD))
-
-    peers = ds.get_num_peers()
-    (seeds, leechers) = ds.get_num_seeds_peers()
-    if seeds is None:
-        print >>sys.stderr, '%d peers' % ds.get_num_peers()
-    else:
-        print >>sys.stderr, '%d peers (%d seeds, %d leechers)' % (peers, seeds, leechers)
-
-    peerlist = ds.get_peerlist()
-    if peerlist is None:
-        pass
-    else:
-        print "list: ", peerlist
+            ds.get_current_speed(DOWNLOAD), \
+            ds.get_error())
+
+    # no peer listing if peer logging is disabled
+    if not peer_logging:
+        return (1.0, peer_logging)
+
+    # peer listing each PEERLIST_OUTPUT_INTERVAL state_callback calls
+    global callback_count
+    callback_count = callback_count + 1
+
+    if callback_count == PEERLIST_OUTPUT_INTERVAL:
+        callback_count = 0
+        peerlist = ds.get_peerlist()
+        print >>sys.stderr, '--Peers: (%s) ' % (time.strftime("%b %d %H:%M:%S", time.localtime())),
+        if peerlist is None or not peerlist:
+            pass
+        else:
+            for item in peerlist:
+                print >>sys.stderr, \
+                        '[ ip: %s, dl: %.2fKB/s, ul: %.2fKB/s ]' % \
+                        (item['ip'], item['downrate']/1000, item['uprate']/1000),
+        print >>sys.stderr
 
-    global peer_logging
     return (1.0, peer_logging)
 
 def main():