예제 #1
0
    def start_torrent(self, torrent):
        tdef = TorrentDef.load(torrent)

        if not os.access(self._directory, os.F_OK):
            os.makedirs(self._directory)

        dscfg = DownloadStartupConfig()
        dscfg.set_dest_dir(self._directory)
        dscfg.set_video_events([
            simpledefs.VODEVENT_START, simpledefs.VODEVENT_PAUSE,
            simpledefs.VODEVENT_RESUME
        ])
        dscfg.set_max_speed(simpledefs.DOWNLOAD, self._max_dl_rate)
        dscfg.set_max_speed(simpledefs.UPLOAD, self._max_ul_rate)
        dscfg.set_peer_type("S")
        #dscfg.set_video_event_callback(self.video_callback) # supporter should not play the files !

        d = self._session.start_download(tdef, dscfg)
        d.set_state_callback(self.state_callback)

        time.sleep(1)  # give the download some time to fully initialize
        d.sd.dow.choker.set_supporter_server(True)

        self._tracker_url = tdef.get_tracker()[:tdef.get_tracker().
                                               find("announce")]
        self._id = d.sd.peerid
        self._choke_objects.append(d.sd.dow.choker)
예제 #2
0
 def setUpDownloadConfig(self):
     dscfg = DownloadStartupConfig()
     print >>sys.stderr,"test: Downloading to",self.config_path
     dscfg.set_dest_dir(self.config_path)
     dscfg.set_breakup_seed_bitfield(False)
     
     return dscfg        
    def do_help(self, infohash, torrent_data, permid):

        basename = binascii.hexlify(
            infohash) + '.torrent'  # ignore .tribe stuff, not vital
        torrentfilename = os.path.join(self.helpdir, basename)

        tfile = open(torrentfilename, "wb")
        tfile.write(torrent_data)
        tfile.close()

        if DEBUG:
            print >> sys.stderr, "helpmsg: Got metadata required for helping", show_permid_short(
                permid)
            print >> sys.stderr, "helpmsg: torrent: ", torrentfilename

        tdef = TorrentDef.load(torrentfilename)
        if self.dlconfig is None:
            dscfg = DownloadStartupConfig()
        else:
            dscfg = DownloadStartupConfig(self.dlconfig)
        dscfg.set_coopdl_coordinator_permid(permid)
        dscfg.set_dest_dir(self.helpdir)

        # Start new download
        self.session.start_download(tdef, dscfg)
예제 #4
0
 def start_torrent(self, torrent):
     tdef = TorrentDef.load(torrent)
     
     if not os.access(self._directory, os.F_OK):
         os.makedirs(self._directory)
     
     dscfg = DownloadStartupConfig()
     dscfg.set_dest_dir(self._directory)
     dscfg.set_video_events([simpledefs.VODEVENT_START,
                             simpledefs.VODEVENT_PAUSE,
                             simpledefs.VODEVENT_RESUME])
     dscfg.set_max_speed(simpledefs.DOWNLOAD, self._max_dl_rate)
     dscfg.set_max_speed(simpledefs.UPLOAD, self._max_ul_rate)
     dscfg.set_peer_type("S")
     #dscfg.set_video_event_callback(self.video_callback) # supporter should not play the files !
     
     d = self._session.start_download(tdef, dscfg)
     d.set_state_callback(self.state_callback)
     
     time.sleep(1) # give the download some time to fully initialize
     d.sd.dow.choker.set_supporter_server(True)
     
     
     self._tracker_url = tdef.get_tracker()[:tdef.get_tracker().find("announce")]
     self._id = d.sd.peerid
     self._choke_objects.append(d.sd.dow.choker)
예제 #5
0
    def new_download(self, infohash, torrent_data, permid):
        """ Start a new download in order to get the pieces that will be requested by the coordinator.
        After the download is started, find the appropriate Helper object and call it's method.
        
        @param infohash: the infohash of the torrent for which help is requested
        @param torrent_data: the content of the .torrent file
        @param permid: the permid of the coordonator
        @param challenge: The challenge sent by the coordinator
        """        

        # Create the name for the .torrent file in the helper cache
        basename = binascii.hexlify(infohash)+'.torrent' # ignore .tribe stuff, not vital
        torrentfilename = os.path.join(self.helpdir,basename)

        # Write the .torrent information in the .torrent helper cache file 
        tfile = open(torrentfilename, "wb")
        tfile.write(torrent_data)
        tfile.close()

        if DEBUG:
            print >> sys.stderr, time.asctime(),'-', "helper: new_download: Got metadata required for helping",show_permid_short(permid)
            print >> sys.stderr, time.asctime(),'-', "helper: new_download: torrent: ",torrentfilename

        tdef = TorrentDef.load(torrentfilename)
        if self.dlconfig is None:
            dscfg = DownloadStartupConfig()
        else:
            dscfg = DownloadStartupConfig(self.dlconfig)
        dscfg.set_coopdl_coordinator_permid(permid)
        dscfg.set_dest_dir(self.helpdir)
        dscfg.set_proxy_mode(PROXY_MODE_OFF) # a helper does not use other helpers for downloading data

        # Start new download
        if DEBUG:
            print >> sys.stderr, time.asctime(),'-', "helper: new_download: Starting a new download"
        d=self.session.start_download(tdef,dscfg)
        d.set_state_callback(self.state_callback, getpeerlist=False)
        
        # Call the helper object got_ask_for_help method
        # If the object was created with start_helepr_download, an amount of time is required
        # before the download is fully operational, so the call to the the helper object got_ask_for_help method
        # is made using the network thread (the network thread executes tasks sequentially, so the start_download task should
        # be executed before the network_got_ask_for_help)
        network_got_ask_for_help_lambda = lambda:self.network_got_ask_for_help(permid, infohash)
        self.session.lm.rawserver.add_task(network_got_ask_for_help_lambda, 0)
예제 #6
0
    def setUpPostSession(self):
        """ override TestAsServer """
        TestAsServer.setUpPostSession(self)

        # Let Tribler start downloading an non-functioning torrent, so
        # we can talk to a normal download engine.
        
        self.torrentfn = os.path.join('extend_hs_dir','dummydata.merkle.torrent')
        tdef = TorrentDef.load(self.torrentfn)

        dscfg = DownloadStartupConfig()
        dscfg.set_dest_dir(self.config_path)
        
        self.session.start_download(tdef,dscfg)
        
        # This is the infohash of the torrent in test/extend_hs_dir
        self.infohash = '\xccg\x07\xe2\x9e!]\x16\xae{\xb8\x10?\xf9\xa5\xf9\x07\xfdBk'
        self.mylistenport = 4810
예제 #7
0
    def setUpPostSession(self):
        """ override TestAsServer """
        TestAsServer.setUpPostSession(self)

        # Let Tribler start downloading an non-functioning torrent, so
        # we can talk to a normal download engine.

        self.torrentfn = os.path.join('extend_hs_dir',
                                      'dummydata.merkle.torrent')
        tdef = TorrentDef.load(self.torrentfn)

        dscfg = DownloadStartupConfig()
        dscfg.set_dest_dir(self.config_path)
        dscfg.set_video_event_callback(self.vod_ready_callback)

        self.d = self.session.start_download(tdef, dscfg)

        # This is the infohash of the torrent in test/extend_hs_dir
        self.infohash = '\xccg\x07\xe2\x9e!]\x16\xae{\xb8\x10?\xf9\xa5\xf9\x07\xfdBk'
        self.mylistenport = 4810
예제 #8
0
    def create_torrent(self):

        [srchandle, self.sourcefn] = mkstemp()
        self.content = Rand.rand_bytes(self.contentlen)
        os.write(srchandle, self.content)
        os.close(srchandle)

        self.tdef = TorrentDef()
        self.tdef.add_content(self.sourcefn)
        self.tdef.set_piece_length(self.piecelen)
        self.tdef.set_tracker("http://127.0.0.1:12/announce")
        self.tdef.finalize()

        self.torrentfn = os.path.join(self.session.get_state_dir(), "gen.torrent")
        self.tdef.save(self.torrentfn)

        dscfg = DownloadStartupConfig()
        destdir = os.path.dirname(self.sourcefn)
        dscfg.set_dest_dir(destdir)
        dscfg.set_video_event_callback(self.sesscb_vod_event_callback)

        self.session.set_download_states_callback(self.states_callback)
        self.session.start_download(self.tdef, dscfg)
    def do_help(self, infohash, torrent_data, permid):

        basename = binascii.hexlify(infohash)+'.torrent' # ignore .tribe stuff, not vital
        torrentfilename = os.path.join(self.helpdir,basename)

        tfile = open(torrentfilename, "wb")
        tfile.write(torrent_data)
        tfile.close()

        if DEBUG:
            print >> sys.stderr,"helpmsg: Got metadata required for helping",show_permid_short(permid)
            print >> sys.stderr,"helpmsg: torrent: ",torrentfilename

        tdef = TorrentDef.load(torrentfilename)
        if self.dlconfig is None:
            dscfg = DownloadStartupConfig()
        else:
            dscfg = DownloadStartupConfig(self.dlconfig)
        dscfg.set_coopdl_coordinator_permid(permid)
        dscfg.set_dest_dir(self.helpdir)

        # Start new download
        self.session.start_download(tdef,dscfg)
예제 #10
0
# tdef.get_tracker() returns the announce-url; we must omit the "announce" part
    tracker_url = tdef.get_tracker()[:tdef.get_tracker().find("announce")]

    if tdef.get_bitrate() == None:
        print >>sys.stderr, "Provided torrent file has no bitrate information. Exiting."
        sys.exit(1)

    BITRATE = tdef.get_bitrate()
    print >>sys.stderr, "Calculated bitrate is %d" % BITRATE
    client_stats['video_duration'] = int(tdef.get_length() / tdef.get_bitrate())

    if not os.access(options.directory, os.F_OK):
            os.makedirs(options.directory)

    dscfg = DownloadStartupConfig()
    dscfg.set_dest_dir(options.directory)
    global my_dir
    my_dir = options.directory
    dscfg.set_video_events([simpledefs.VODEVENT_START,
                            simpledefs.VODEVENT_PAUSE,
                            simpledefs.VODEVENT_RESUME])
    dscfg.set_video_event_callback(vod_event_callback)
    dscfg.set_max_speed(simpledefs.DOWNLOAD, options.dlrate)
    dscfg.set_max_speed(simpledefs.UPLOAD, options.ulrate)

    if dscfg.get_mode() == simpledefs.DLMODE_VOD:
        print >>sys.stderr, 'Client runs in streaming mode'
    
    d = s.start_download(tdef, dscfg)
    
    d.set_state_callback(state_event_callback)
예제 #11
0
class TestMetadata(TestAsServer, MagnetHelpers):
    """
    Once we are downloading a torrent, our client should respond to
    the ut_metadata extention message.  This allows other clients to
    obtain the info part of the metadata from us.
    """
    def setUp(self):
        """ override TestAsServer """
        TestAsServer.setUp(self)
        print >>sys.stderr,time.asctime(),'-', "test: Giving MyLaunchMany time to startup"
        time.sleep(5)
        print >>sys.stderr,time.asctime(),'-', "test: MyLaunchMany should have started up"
    
        # the metadata that we want to transfer
        self.tdef = TorrentDef()
        self.tdef.add_content(os.path.join(os.getcwd(), "API", "file.wmv"))
        self.tdef.set_tracker(self.session.get_internal_tracker_url())
        # we use a small piece length to obtain multiple pieces
        self.tdef.set_piece_length(1) 
        self.tdef.finalize()
        # self.tdef.save(os.path.join(self.session.get_state_dir(), "gen.torrent"))
        
        MagnetHelpers.__init__(self, self.tdef)

    def setup_seeder(self):
        self.seeder_setup_complete = False
        self.seeder_teardown_complete = False
        self.seeder_teardown = False

        self.dscfg = DownloadStartupConfig()
        self.dscfg.set_dest_dir(os.getcwd())
        self.download = self.session.start_download(self.tdef, self.dscfg)
        self.download.set_state_callback(self.seeder_state_callback)

        counter = 0
        while not self.seeder_setup_complete:
            counter += 1
            time.sleep(1)
            assert counter < 30, "timeout"

        print >> sys.stderr, time.asctime(),'-', "test: setup_seeder() complete"

    def teardown_seeder(self):
        self.seeder_teardown_complete = False
        self.session.remove_download(self.download)

        counter = 0
        while not self.seeder_setup_complete:
            counter += 1
            time.sleep(1)
            assert counter < 30, "timeout"

        print >> sys.stderr, time.asctime(),'-', "test: teardown_seeder() complete"

    def seeder_state_callback(self,ds):
        assert not self.seeder_teardown_complete
        self.seeder_setup_complete = (ds.get_status() == DLSTATUS_DOWNLOADING)
        d = ds.get_download()
        print >> sys.stderr, time.asctime(),'-', "test: seeder:", `d.get_def().get_name()`, dlstatus_strings[ds.get_status()], ds.get_progress()
        if self.seeder_teardown:
            self.seeder_teardown_complete = True
        else:
            return (1.0, False)

    def test_all(self):
        self.setup_seeder()
        try:
            self.subtest_good_flood()
        finally:
            self.teardown_seeder()

        self.setup_seeder()
        try:
            self.subtest_good_request()
            self.subtest_bad_request()
        finally:
            self.teardown_seeder()

    def subtest_good_request(self):
        conn = BTConnection("localhost", self.hisport, user_infohash=self.tdef.get_infohash())
        conn.send(self.create_good_extend_handshake())
        conn.read_handshake_medium_rare()
        metadata_id = self.read_extend_handshake(conn)

        # request metadata block 0, 2, 3, and the last
        conn.send(self.create_good_extend_metadata_request(metadata_id, 0))
        conn.send(self.create_good_extend_metadata_request(metadata_id, 2))
        conn.send(self.create_good_extend_metadata_request(metadata_id, 3))
        conn.send(self.create_good_extend_metadata_request(metadata_id, len(self.metadata_list) - 1))

        self.read_extend_metadata_reply(conn, 0)
        self.read_extend_metadata_reply(conn, 2)
        self.read_extend_metadata_reply(conn, 3)
        self.read_extend_metadata_reply(conn, len(self.metadata_list) - 1)

    def subtest_good_flood(self):
        conn = BTConnection("localhost", self.hisport, user_infohash=self.tdef.get_infohash())
        conn.send(self.create_good_extend_handshake())
        conn.read_handshake_medium_rare()
        metadata_id = self.read_extend_handshake(conn)

        for counter in xrange(len(self.metadata_list) * 2):
            piece = counter % len(self.metadata_list)
            conn.send(self.create_good_extend_metadata_request(metadata_id, piece))

            if counter > len(self.metadata_list):
                self.read_extend_metadata_reject(conn, piece)
            else:
                self.read_extend_metadata_reply(conn, piece)

    def subtest_bad_request(self):
        self.bad_request_and_disconnect({"msg_type":0, "piece":len(self.metadata_list)})
        self.bad_request_and_disconnect({"msg_type":0, "piece":-1})
        self.bad_request_and_disconnect({"msg_type":0, "piece":"1"})
        self.bad_request_and_disconnect({"msg_type":0, "piece":[1,2]})
        self.bad_request_and_disconnect({"msg_type":0, "PIECE":1})
        
    def bad_request_and_disconnect(self, payload):
        conn = BTConnection("localhost", self.hisport, user_infohash=self.tdef.get_infohash())
        conn.send(self.create_good_extend_handshake())
        conn.read_handshake_medium_rare()
        metadata_id = self.read_extend_handshake(conn)

        conn.send(EXTEND + chr(metadata_id) + bencode(payload))
        self.read_extend_metadata_close(conn)
예제 #12
0
 def setUpDownloadConfig(self):
     dscfg = DownloadStartupConfig()
     dscfg.set_dest_dir(self.config_path)
     return dscfg        
예제 #13
0
    tracker_url = tdef.get_tracker()[:tdef.get_tracker().find("announce")]

    if tdef.get_bitrate() == None:
        print >> sys.stderr, "Provided torrent file has no bitrate information. Exiting."
        sys.exit(1)

    BITRATE = tdef.get_bitrate()
    print >> sys.stderr, "Calculated bitrate is %d" % BITRATE
    client_stats['video_duration'] = int(tdef.get_length() /
                                         tdef.get_bitrate())

    if not os.access(options.directory, os.F_OK):
        os.makedirs(options.directory)

    dscfg = DownloadStartupConfig()
    dscfg.set_dest_dir(options.directory)
    global my_dir
    my_dir = options.directory
    dscfg.set_video_events([
        simpledefs.VODEVENT_START, simpledefs.VODEVENT_PAUSE,
        simpledefs.VODEVENT_RESUME
    ])
    dscfg.set_video_event_callback(vod_event_callback)
    dscfg.set_max_speed(simpledefs.DOWNLOAD, options.dlrate)
    dscfg.set_max_speed(simpledefs.UPLOAD, options.ulrate)

    if dscfg.get_mode() == simpledefs.DLMODE_VOD:
        print >> sys.stderr, 'Client runs in streaming mode'

    d = s.start_download(tdef, dscfg)
예제 #14
0
 def setUpDownloadConfig(self):
     dscfg = DownloadStartupConfig()
     dscfg.set_dest_dir(self.config_path)
     return dscfg