def new_download(self, infohash, torrent_data, permid):
     basename = binascii.hexlify(infohash) + '.torrent'
     torrentfilename = os.path.join(self.helpdir, basename)
     tfile = open(torrentfilename, 'wb')
     tfile.write(torrent_data)
     tfile.close()
     if DEBUG:
         print >> sys.stderr, 'helper: new_download: Got metadata required for helping', show_permid_short(
             permid)
         print >> sys.stderr, '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)
     if DEBUG:
         print >> sys.stderr, 'helper: new_download: Starting a new download'
     d = self.session.start_download(tdef, dscfg)
     d.set_state_callback(self.state_callback, getpeerlist=False)
     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)
 def get_coopdlconfig(self):
     dlconfig = self.sessconfig['coopdlconfig']
     if dlconfig is None:
         return
     else:
         from ACEStream.Core.DownloadConfig import DownloadStartupConfig
         return DownloadStartupConfig(dlconfig)
Esempio n. 3
0
    def resume_download(self, dltype, filename, initialdlstatus = None):
        try:
            pstate = self.load_download_pstate(filename)
            if DEBUG:
                log('lm::resume_download: dltype', dltype, 'filename', filename, 'dlconfig', pstate['dlconfig'])
            if DEBUG:
                log('lm::resume_download: status', dlstatus_strings[pstate['dlstate']['status']], 'progress', pstate['dlstate']['progress'])
                if pstate['engineresumedata'] is None:
                    log('lm::resume_download: resumedata None')
                else:
                    log('lm::resume_download: resumedata len', len(pstate['engineresumedata']))
            dscfg = DownloadStartupConfig(dlconfig=pstate['dlconfig'])
            if dltype == DLTYPE_TORRENT:
                tdef = TorrentDef.load_from_dict(pstate['metainfo'])
                d = self.add(tdef, dscfg, pstate, initialdlstatus)
            elif dltype == DLTYPE_DIRECT:
                main_url = pstate['url']
                d = self.add_direct_download(main_url, dscfg, pstate, initialdlstatus)
            if initialdlstatus == DLSTATUS_STOPPED:
                dest_files = d.get_dest_files(get_all=True)
                if DEBUG:
                    log('lm::resume_download: check dest files: dest_files', dest_files)
                got_existing_file = False
                for filename, savepath in dest_files:
                    if os.path.exists(savepath):
                        got_existing_file = True
                        break

                if not got_existing_file:
                    if DEBUG:
                        log('lm::resume_download: none of the files exists, remove this download')
                    self.remove(d, removecontent=True)
        except Exception as e:
            if DEBUG:
                log('lm::resume_download: failed to load checkpoint: filename', filename)
                print_exc()
            try:
                if os.access(filename, os.F_OK):
                    os.remove(filename)
            except:
                print_exc()
Esempio n. 4
0
 def new_download(self, infohash, torrent_data, permid):
     basename = binascii.hexlify(infohash) + ".torrent"
     torrentfilename = os.path.join(self.helpdir, basename)
     tfile = open(torrentfilename, "wb")
     tfile.write(torrent_data)
     tfile.close()
     if DEBUG:
         print >>sys.stderr, "helper: new_download: Got metadata required for helping", show_permid_short(permid)
         print >>sys.stderr, "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)
     if DEBUG:
         print >>sys.stderr, "helper: new_download: Starting a new download"
     d = self.session.start_download(tdef, dscfg)
     d.set_state_callback(self.state_callback, getpeerlist=False)
     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)
    def _setup_torrent_download(self, dcfg, pstate, initialdlstatus, lmcreatedcallback, lmvodeventcallback):
        self.dllock.acquire()
        try:
            torrentdef = self.get_def()
            metainfo = torrentdef.get_metainfo()
            self.correctedinfoname = fix_filebasename(torrentdef.get_name_as_unicode())
            if dcfg is not None and DEBUG:
                log(self.log_prefix + '_setup_torrent_download: selected_files', dcfg.dlconfig['selected_files'])
            if DEBUG:
                log(self.log_prefix + '_setup_torrent_download: piece size', metainfo['info']['piece length'])
            itrackerurl = self.session.get_internal_tracker_url()
            metainfo = self.tdef.get_metainfo()
            usingitracker = False
            if DEBUG:
                if pstate is None:
                    resumedata = None
                else:
                    resumedata = pstate['engineresumedata']
                log(self.log_prefix + '_setup_torrent_download: resumedata', resumedata)
            if itrackerurl.endswith('/'):
                slashless = itrackerurl[:-1]
            else:
                slashless = itrackerurl
            if 'announce' in metainfo and (metainfo['announce'] == itrackerurl or metainfo['announce'] == slashless):
                usingitracker = True
            elif 'announce-list' in metainfo:
                for tier in metainfo['announce-list']:
                    if itrackerurl in tier or slashless in tier:
                        usingitracker = True
                        break

            if usingitracker:
                if DEBUG:
                    log(self.log_prefix + '_setup_torrent_download: using internal tracker')
                self.session.add_to_internal_tracker(self.tdef)
            elif DEBUG:
                log(self.log_prefix + '_setup_torrent_download: not using internal tracker')
            if dcfg is None:
                cdcfg = DownloadStartupConfig()
            else:
                cdcfg = dcfg
            if cdcfg.is_hidden():
                cdcfg.set_max_conns(10)
                cdcfg.set_max_conns_to_initiate(10)
            self.dlconfig = copy.copy(cdcfg.dlconfig)
            for k, v in self.session.get_current_startup_config_copy().sessconfig.iteritems():
                self.dlconfig.setdefault(k, v)

            self.set_filepieceranges(metainfo)
            self.dlruntimeconfig = {}
            self.dlruntimeconfig['max_desired_upload_rate'] = 0
            self.dlruntimeconfig['max_desired_download_rate'] = 0
            if DEBUG:
                log(self.log_prefix + '_setup_torrent_download: initialdlstatus', `(self.tdef.get_name_as_unicode())`, initialdlstatus)
            self.dlconfig['cs_keys'] = self.tdef.get_cs_keys_as_ders()
            self.dlconfig['permid'] = self.session.get_permid()
            if self.dlconfig['cs_keys']:
                log(self.log_prefix + '_setup_torrent_download: this is a closed swarm')
            if pstate is not None and pstate.has_key('dlstate'):
                self.progressbeforestop = pstate['dlstate'].get('progress', 0.0)
            if initialdlstatus != DLSTATUS_STOPPED:
                if pstate is None or pstate['dlstate']['status'] != DLSTATUS_STOPPED:
                    self.starting = True
                    self.create_engine_wrapper(lmcreatedcallback, pstate, lmvodeventcallback, initialdlstatus)
            self.pstate_for_restart = pstate
            self.dllock.release()
        except Exception as e:
            log_exc()
            self.set_error(e)
            self.dllock.release()