示例#1
0
class SupporterServer(object):
    def __init__(self, options):
        self._directory = options.directory
        self._port = options.port
        self._torrent = options.torrent
        self._max_dl_rate = options.dlrate
        self._max_ul_rate = options.ulrate
        self._min_peer = options.min_peer
        self._max_peer = options.max_peer
        self._choke_objects = []  # list of all chokers...

    def init_session(self):
        scfg = SessionStartupConfig()
        scfg.set_state_dir(tempfile.mkdtemp())
        scfg.set_listen_port(self._port)
        scfg.set_overlay(False)
        scfg.set_megacache(False)
        scfg.set_upnp_mode(simpledefs.UPNPMODE_DISABLED)
        scfg.set_dialback(False)
        scfg.set_social_networking(False)
        scfg.set_buddycast(False)
        scfg.set_crawler(False)
        scfg.set_internal_tracker(False)

        self._session = Session(scfg)

    def start_torrents(self):
        torrents = []
        if os.path.isdir(self._torrent):
            # walk the dir and start all torrents in there
            torrents = files_list(
                self._torrent,
                [constants.TORRENT_DOWNLOAD_EXT, constants.TORRENT_VOD_EXT])
        else:
            torrents.append(self._torrent)

        for torrent in torrents:
            self.start_torrent(torrent)

    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)

    def init_communicator(self):
        self._communicator = HTTPCommunicator({
            'url': self._tracker_url,
            'id': self._id,
            'port': self._port,
            'min_peer': self._min_peer,
            'max_peer': self._max_peer
        })
        self._communicator.send_registration()

    def run_forever(self):
        self.init_session()
        self.start_torrents()
        self.init_communicator()

        xmlrpc_server_handler = SupporterXMLRPCServer(self)
        xmlrpc_server = SimpleXMLRPCServer.SimpleXMLRPCServer(
            ("0.0.0.0", self._port + 1))
        xmlrpc_server.register_instance(xmlrpc_server_handler)
        xmlrpc_server.serve_forever()

    def state_callback(self, ds):
        return (0, False)

    def video_callback(self, d, event, params):
        pass

    def push_supportee_list_to_choker(self, supportee_list):
        for choker in self._choke_objects:
            choker.receive_supportee_list(supportee_list)
示例#2
0
        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)

    time.sleep(1)

    communicator = TrackerCommunicator(tracker_url, options.port, d.sd.peerid)
    #communicator.send_registration()

    try:
        while True:
            time.sleep(5)
    except KeyboardInterrupt:
        exit_handler()
    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)

    time.sleep(1)

    communicator = TrackerCommunicator(tracker_url, options.port, d.sd.peerid)
    #communicator.send_registration()

    try:
        while True:
            time.sleep(5)
    except KeyboardInterrupt:
        exit_handler()
class SupporterServer(object):
    def __init__(self, options):
        self._directory = options.directory
        self._port = options.port
        self._torrent = options.torrent
        self._max_dl_rate = options.dlrate
        self._max_ul_rate = options.ulrate
        self._min_peer = options.min_peer
        self._max_peer = options.max_peer
        self._choke_objects = [] # list of all chokers...
        
    def init_session(self):
        scfg = SessionStartupConfig()
        scfg.set_state_dir(tempfile.mkdtemp())
        scfg.set_listen_port(self._port)
        scfg.set_overlay(False)
        scfg.set_megacache(False)
        scfg.set_upnp_mode(simpledefs.UPNPMODE_DISABLED)
        scfg.set_dialback(False)
        scfg.set_social_networking(False)
        scfg.set_buddycast(False)
        scfg.set_crawler(False)
        scfg.set_internal_tracker(False)
        
        self._session = Session(scfg)
        
    def start_torrents(self):
        torrents = []
        if os.path.isdir(self._torrent):
            # walk the dir and start all torrents in there
            torrents = files_list(self._torrent, [constants.TORRENT_DOWNLOAD_EXT, constants.TORRENT_VOD_EXT])
        else:
            torrents.append(self._torrent)
            
        for torrent in torrents:
            self.start_torrent(torrent)
        
    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)
        
    def init_communicator(self):
        self._communicator = HTTPCommunicator({'url' : self._tracker_url,
                                               'id' : self._id,
                                               'port' : self._port,
                                               'min_peer' : self._min_peer,
                                               'max_peer' : self._max_peer})
        self._communicator.send_registration()
        
    def run_forever(self):
        self.init_session()
        self.start_torrents()
        self.init_communicator()
        
        xmlrpc_server_handler = SupporterXMLRPCServer(self)
        xmlrpc_server = SimpleXMLRPCServer.SimpleXMLRPCServer(("0.0.0.0", self._port+1))
        xmlrpc_server.register_instance(xmlrpc_server_handler)
        xmlrpc_server.serve_forever()
        
    def state_callback(self, ds):
        return (0, False)
    
    def video_callback(self, d, event, params):
        pass
    
    def push_supportee_list_to_choker(self, supportee_list):
        for choker in self._choke_objects:
            choker.receive_supportee_list(supportee_list)