11import os
22import sys
3+ import signal
34import time
45
56import click
1213
1314
1415BWSCAN_VERSION = '0.0.1'
16+ BASEURL = 'https://siv.sunet.se/bwauth/'
1517
1618
1719class ScanInstance (object ):
@@ -21,6 +23,7 @@ class ScanInstance(object):
2123 def __init__ (self , data_dir ):
2224 self .data_dir = data_dir
2325 self .measurement_dir = os .path .join (data_dir , 'measurements' )
26+ self .tor_dir = os .path .join (data_dir , 'tor_data' )
2427
2528 def __repr__ (self ):
2629 return '<BWScan %r>' % self .data_dir
@@ -58,13 +61,18 @@ def cli(ctx, data_dir, loglevel, logfile, launch_tor, circuit_build_timeout):
5861 os .makedirs (ctx .obj .measurement_dir )
5962
6063 # Create a connection to a Tor instance
61- ctx .obj .tor_state = connect_to_tor (launch_tor , circuit_build_timeout )
64+ ctx .obj .tor_state = connect_to_tor (launch_tor , circuit_build_timeout ,
65+ ctx .obj .tor_dir )
6266
6367 # Set up the logger to only output log lines of level `loglevel` and above.
6468 setup_logging (log_level = loglevel , log_name = logfile )
6569
6670
6771@cli .command (short_help = "Measure the Tor relays." )
72+ # FIXME: when having a configuration file the default will be given by it.
73+ @click .option ('--baseurl' ,
74+ help = 'URL that provides the files to perform the measurements with' ,
75+ default = BASEURL )
6876@click .option ('--partitions' , '-p' , default = 1 ,
6977 help = 'Divide the set of relays into subsets. 1 by default.' )
7078@click .option ('--current-partition' , '-c' , default = 1 ,
@@ -75,7 +83,7 @@ def cli(ctx, data_dir, loglevel, logfile, launch_tor, circuit_build_timeout):
7583 help = 'Limit the number of simultaneous bandwidth measurements '
7684 '(default: %d).' % 10 )
7785@pass_scan
78- def scan (scan , partitions , current_partition , timeout , request_limit ):
86+ def scan (scan , baseurl , partitions , current_partition , timeout , request_limit ):
7987 """
8088 Start a scan through each Tor relay to measure it's bandwidth.
8189 """
@@ -88,18 +96,31 @@ def scan(scan, partitions, current_partition, timeout, request_limit):
8896 os .makedirs (scan_data_dir )
8997
9098 def rename_finished_scan (deferred ):
99+ log .debug ('renaming finished scan with deferred' )
91100 click .echo (deferred )
92101 os .rename (scan_data_dir , os .path .join (scan .measurement_dir , scan_time ))
93102
94- scan .tor_state .addCallback (BwScan , reactor , scan_data_dir ,
103+ log .debug ('adding callback to tor_state' )
104+ scan .tor_state .addCallback (BwScan , reactor , scan_data_dir , baseurl ,
95105 request_timeout = timeout ,
96106 request_limit = request_limit ,
97107 partitions = partitions ,
98108 this_partition = current_partition )
109+ # NOTE: in measurements?
110+ log .debug ('adding callback to tor_state' )
99111 scan .tor_state .addCallback (lambda scanner : scanner .run_scan ())
112+ # NOTE: why lambda here?
113+ log .debug ('adding callback to tor_state' )
100114 scan .tor_state .addCallback (lambda _ : reactor .stop ())
115+ log .debug ('adding callback to tor_state' )
101116 scan .tor_state .addCallback (rename_finished_scan )
117+ def signal_handler (signal , frame ):
118+ print "signal caught, stopping probe"
119+ reactor .stop ()
120+ signal .signal (signal .SIGINT , signal_handler )
121+ signal .signal (signal .SIGTERM , signal_handler )
102122
123+ log .debug ('reactor run' )
103124 reactor .run ()
104125
105126
0 commit comments