next-share: add peer logging support
authorP2P-Next <p2p-next@cs.pub.ro>
Mon, 20 Sep 2010 13:39:50 +0000 (16:39 +0300)
committerP2P-Next <p2p-next@cs.pub.ro>
Mon, 20 Sep 2010 13:39:50 +0000 (16:39 +0300)
Peer logging is printing peer specific information such as local
download speed, upload speed, etc. Peer logging is enabled through the
use of the -l (or --peer-logging) option to the cmdlinedl.py script.
Currently printing in state_callback function is quite spartan. Needs to
be taken care of.

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

index b6d4405..487b6b8 100644 (file)
@@ -23,6 +23,11 @@ from traceback import print_exc
 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"
@@ -41,6 +46,8 @@ def usage():
     print "\t\t\t\t(default is no upload limit)"
     print "\t--no-hash-check"
     print "\t-c\t\t\tdisable downloaded file hash checking"
+    print "\t--peer-logging"
+    print "\t-l\t\t\tenable peer logging (per-peer information)"
     print "\t--version"
     print "\t-v\t\t\tprint version and exit"
     print "\t--help"
@@ -63,13 +70,27 @@ def state_callback(ds):
             ds.get_current_speed(UPLOAD), \
             ds.get_current_speed(DOWNLOAD))
 
-    return (1.0, False)
+    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
+
+    global peer_logging
+    return (1.0, peer_logging)
 
 def main():
     try:
         # opts = a list of (option, value) pairs
         # args = the list of program arguments left after the option list was stripped
-        opts, args = getopt.getopt(sys.argv[1:], "hvcu:d:o:p:", ["help", "version", "no-hash-check", "upload-limit=", "download-limit=", "output-dir=", "port="])
+        opts, args = getopt.getopt(sys.argv[1:], "hvclu:d:o:p:", ["help", "version", "no-hash-check", "peer-logging", "upload-limit=", "download-limit=", "output-dir=", "port="])
     except getopt.GetoptError, err:
         print str(err)
         usage()
@@ -81,7 +102,7 @@ def main():
     output_dir = os.getcwd()
     port = random.randint(10000, 65535)
     enable_hash_check = True
-
+    global peer_logging
     for o, a in opts:
         if o in ("-h", "--help"):
             usage()
@@ -99,6 +120,8 @@ def main():
             sys.exit(0)
         elif o in ("-c", "--no-hash-check"):
             enable_hash_check = False
+        elif o in ("-l", "--peer-logging"):
+            peer_logging = True
         else:
             assert False, "unhandled option"
 
@@ -143,7 +166,7 @@ def main():
         raise ValueError("cmdlinedl does not support live torrents")
 
     d = s.start_download(tdef, dscfg)
-    d.set_state_callback(state_callback, getpeerlist=False)
+    d.set_state_callback(state_callback, getpeerlist=peer_logging)
 
     #
     # loop while waiting for CTRL-C (or any other signal/interrupt)