def __init__(self):
        self.session = FakeSession()
        self.crawler = Crawler.get_instance(self.session)

        self.my_db = MyDBHandler.getInstance()
        self.peer_db = PeerDBHandler.getInstance()
        self.torrent_db = TorrentDBHandler.getInstance()
        self.torrent_db.register(Category.getInstance(), ".")
        self.mypref_db = MyPreferenceDBHandler.getInstance()
        self.pref_db = PreferenceDBHandler.getInstance()
        self.superpeer_db = SuperPeerDBHandler.getInstance()
        self.friend_db = FriendDBHandler.getInstance()
        self.bartercast_db = BarterCastDBHandler.getInstance()
        self.bartercast_db.registerSession(self.session)
        self.secure_overlay = FakeSecureOverlay()
        #        torrent_collecting_dir = os.path.abspath(config['torrent_collecting_dir'])
        self.listen_port = 1234

        self.channelcast_db = ChannelCastDBHandler.getInstance()
        self.channelcast_db.registerSession(self.session)

        self.votecast_db = VoteCastDBHandler.getInstance()
        self.votecast_db.registerSession(self.session)
        self.simi_db = SimilarityDBHandler.getInstance()
        self.pops_db = PopularityDBHandler.getInstance()
Esempio n. 2
0
    def __init__(self):
        if NatCheckMsgHandler.__single:
            raise RuntimeError, "NatCheckMsgHandler is singleton"
        NatCheckMsgHandler.__single = self
        self.crawler_reply_callbacks = []
        self._secure_overlay = SecureOverlay.getInstance()

        self.crawler = Crawler.get_instance()
        if self.crawler.am_crawler():
            self._file = open("natcheckcrawler.txt", "a")
            self._file.write("\n".join(
                ("# " + "*" * 80, strftime("%Y/%m/%d %H:%M:%S"),
                 "# Crawler started\n")))
            self._file.flush()
            self._file2 = open("nattraversalcrawler.txt", "a")
            self._file2.write("\n".join(
                ("# " + "*" * 80, strftime("%Y/%m/%d %H:%M:%S"),
                 "# Crawler started\n")))
            self._file2.flush()
            self.peerlist = []
            self.holePunchingIP = socket.gethostbyname(socket.gethostname())
            self.trav = {}

        else:
            self._file = None
Esempio n. 3
0
 def __init__(self):
     crawler = Crawler.get_instance()
     if crawler.am_crawler():
         self._file = open("puncturecrawler.txt", "a")
         self._file.write("# Crawler started at %.2f\n" % time.time())
         self._file.flush()
         self._repexlog = None
     else:
         self.reporter = get_reporter_instance()
Esempio n. 4
0
    def __init__(self):
        self._sqlite_cache_db = SQLiteCacheDB.getInstance()

        crawler = Crawler.get_instance()
        if crawler.am_crawler():
            self._file = open("databasecrawler.txt", "a")
            self._file.write("".join(("# ", "*" * 80, "\n# ", strftime("%Y/%m/%d %H:%M:%S"), " Crawler started\n")))
            self._file.flush()
        else:
            self._file = None
Esempio n. 5
0
 def __init__(self,session):
     crawler = Crawler.get_instance()
     if crawler.am_crawler():
         self._file = open("repexcrawler.txt", "a")
         self._file.write("".join(("# ", "*" * 78, "\n# ", strftime("%Y/%m/%d %H:%M:%S"), " Crawler started\n")))
         self._file.flush()
         self._repexlog = None
     else:
         self._file = None
         self._repexlog = RePEXLogDB.getInstance(session)
Esempio n. 6
0
 def __init__(self):
     crawler = Crawler.get_instance()
     if crawler.am_crawler():
         self._file = open("repexcrawler.txt", "a")
         self._file.write("".join(("# ", "*" * 78, "\n# ", strftime("%Y/%m/%d %H:%M:%S"), " Crawler started\n")))
         self._file.flush()
         self._repexlog = None
     else:
         self._file = None
         self._repexlog = RePEXLogDB.getInstance()
Esempio n. 7
0
    def __init__(self):
        self._sqlite_cache_db = SQLiteCacheDB.getInstance()

        crawler = Crawler.get_instance()
        if crawler.am_crawler():
            self._file = open("databasecrawler.txt", "a")
            self._file.write("".join(
                ("# ", "*" * 80, "\n# ", strftime("%Y/%m/%d %H:%M:%S"),
                 " Crawler started\n")))
            self._file.flush()
        else:
            self._file = None
    def __init__(self):
        if VideoPlaybackCrawler.__single is not None:
            raise RuntimeError, "VideoPlaybackCrawler is singleton"

        crawler = Crawler.get_instance()
        if crawler.am_crawler():
            self._file = open("videoplaybackcrawler.txt", "a")
            self._file.write("".join(("# ", "*" * 80, "\n# ", strftime("%Y/%m/%d %H:%M:%S"), " Crawler started\n")))
            self._file.flush()
            self._event_db = None

        else:
            self._file = None
            self._event_db = VideoPlaybackDBHandler.get_instance()
Esempio n. 9
0
    def __init__(self):
        if VideoPlaybackCrawler.__single is not None:
            raise RuntimeError, "VideoPlaybackCrawler is singleton"

        crawler = Crawler.get_instance()
        if crawler.am_crawler():
            self._file = open("videoplaybackcrawler.txt", "a")
            self._file.write("".join(
                ("# ", "*" * 80, "\n# ", strftime("%Y/%m/%d %H:%M:%S"),
                 " Crawler started\n")))
            self._file.flush()
            self._event_db = None

        else:
            self._file = None
            self._event_db = VideoPlaybackDBHandler.get_instance()
Esempio n. 10
0
    def __init__(self):
        if NatCheckMsgHandler.__single:
            raise RuntimeError, "NatCheckMsgHandler is singleton"
        NatCheckMsgHandler.__single = self
        self.crawler_reply_callbacks = []
        self._secure_overlay = SecureOverlay.getInstance()

        self.crawler = Crawler.get_instance()
        if self.crawler.am_crawler():
            self._file = open("natcheckcrawler.txt", "a")
            self._file.write("\n".join(("# " + "*" * 80, strftime("%Y/%m/%d %H:%M:%S"), "# Crawler started\n")))
            self._file.flush()
            self._file2 = open("nattraversalcrawler.txt", "a")
            self._file2.write("\n".join(("# " + "*" * 80, strftime("%Y/%m/%d %H:%M:%S"), "# Crawler started\n")))
            self._file2.flush()
            self.peerlist = []
            self.holePunchingIP = socket.gethostbyname(socket.gethostname())
            self.trav = {}

        else:
            self._file = None
    def __init__(self):
        self.session = FakeSession()
        self.crawler = Crawler.get_instance(self.session)

        self.my_db = MyDBHandler.getInstance()
        self.peer_db = PeerDBHandler.getInstance()
        self.torrent_db = TorrentDBHandler.getInstance()
        self.torrent_db.register(Category.getInstance(), '.')
        self.mypref_db = MyPreferenceDBHandler.getInstance()
        self.pref_db = PreferenceDBHandler.getInstance()
        self.superpeer_db = SuperPeerDBHandler.getInstance()
        self.friend_db = FriendDBHandler.getInstance()
        self.bartercast_db = BarterCastDBHandler.getInstance()
        self.bartercast_db.registerSession(self.session)
        self.secure_overlay = FakeSecureOverlay()
        #        torrent_collecting_dir = os.path.abspath(config['torrent_collecting_dir'])
        self.listen_port = 1234

        self.channelcast_db = ChannelCastDBHandler.getInstance()
        self.channelcast_db.registerSession(self.session)

        self.votecast_db = VoteCastDBHandler.getInstance()
        self.votecast_db.registerSession(self.session)
Esempio n. 12
0
    def register(self, overlay_bridge, session, launchmany, config,
                 requestPolicy):
        self.overlay_bridge = overlay_bridge
        self.launchmany = launchmany
        self.requestPolicy = requestPolicy
        self.text_mode = config.has_key('text_mode')

        # OverlayApps gets all messages, and demultiplexes
        overlay_bridge.register_recv_callback(self.handleMessage)
        overlay_bridge.register_conns_callback(self.handleConnection)

        # Create handler for metadata messages in two parts, as
        # download help needs to know the metadata_handler and we need
        # to know the download helper handler.
        # Part 1:
        self.metadata_handler = MetadataHandler.getInstance()

        if config['download_help']:
            # Create handler for messages to dlhelp coordinator
            self.coord_handler = CoordinatorMessageHandler(launchmany)
            self.register_msg_handler(HelpHelperMessages,
                                      self.coord_handler.handleMessage)

            # Create handler for messages to dlhelp helper
            self.help_handler = HelperMessageHandler()
            self.help_handler.register(session, self.metadata_handler,
                                       config['download_help_dir'],
                                       config.get('coopdlconfig', False))
            self.register_msg_handler(HelpCoordinatorMessages,
                                      self.help_handler.handleMessage)

        # Part 2:
        self.metadata_handler.register(overlay_bridge, self.help_handler,
                                       launchmany, config)
        self.register_msg_handler(MetadataMessages,
                                  self.metadata_handler.handleMessage)

        if not config['torrent_collecting']:
            self.torrent_collecting_solution = 0
        else:
            self.torrent_collecting_solution = config[
                'buddycast_collecting_solution']

        if config['buddycast']:
            # Create handler for Buddycast messages

            self.buddycast = BuddyCastFactory.getInstance(
                superpeer=config['superpeer'], log=config['overlay_log'])
            # Using buddycast to handle torrent collecting since they are dependent
            self.buddycast.register(overlay_bridge, launchmany,
                                    launchmany.rawserver_fatalerrorfunc,
                                    self.metadata_handler,
                                    self.torrent_collecting_solution,
                                    config['start_recommender'],
                                    config['buddycast_max_peers'])
            self.register_msg_handler(BuddyCastMessages,
                                      self.buddycast.handleMessage)
            self.register_connection_handler(self.buddycast.handleConnection)

        if config['dialback']:
            self.dialback_handler = DialbackMsgHandler.getInstance()
            # The Dialback mechanism needs the real rawserver, not the overlay_bridge
            self.dialback_handler.register(overlay_bridge, launchmany,
                                           launchmany.rawserver, config)
            self.register_msg_handler(
                [DIALBACK_REQUEST],
                self.dialback_handler.olthread_handleSecOverlayMessage)
            self.register_connection_handler(
                self.dialback_handler.olthread_handleSecOverlayConnection)
        else:
            self.register_msg_handler([DIALBACK_REQUEST],
                                      self.handleDisabledMessage)

        if config['socnet']:
            self.socnet_handler = SocialNetworkMsgHandler.getInstance()
            self.socnet_handler.register(overlay_bridge, launchmany, config)
            self.register_msg_handler(SocialNetworkMessages,
                                      self.socnet_handler.handleMessage)
            self.register_connection_handler(
                self.socnet_handler.handleConnection)

            self.friendship_handler = FriendshipMsgHandler.getInstance()
            self.friendship_handler.register(overlay_bridge,
                                             launchmany.session)
            self.register_msg_handler(FriendshipMessages,
                                      self.friendship_handler.handleMessage)
            self.register_connection_handler(
                self.friendship_handler.handleConnection)

        if config['rquery']:
            self.rquery_handler = RemoteQueryMsgHandler.getInstance()
            self.rquery_handler.register(overlay_bridge,
                                         launchmany,
                                         config,
                                         self.buddycast,
                                         log=config['overlay_log'])
            self.register_msg_handler(RemoteQueryMessages,
                                      self.rquery_handler.handleMessage)
            self.register_connection_handler(
                self.rquery_handler.handleConnection)

        if config['crawler']:
            crawler = Crawler.get_instance(session)
            self.register_msg_handler([CRAWLER_REQUEST],
                                      crawler.handle_request)

            database_crawler = DatabaseCrawler.get_instance()
            crawler.register_message_handler(
                CRAWLER_DATABASE_QUERY,
                database_crawler.handle_crawler_request,
                database_crawler.handle_crawler_reply)
            seeding_stats_crawler = SeedingStatsCrawler.get_instance()
            crawler.register_message_handler(
                CRAWLER_SEEDINGSTATS_QUERY,
                seeding_stats_crawler.handle_crawler_request,
                seeding_stats_crawler.handle_crawler_reply)
            friendship_crawler = FriendshipCrawler.get_instance(session)
            crawler.register_message_handler(
                CRAWLER_FRIENDSHIP_STATS,
                friendship_crawler.handle_crawler_request,
                friendship_crawler.handle_crawler_reply)
            natcheck_handler = NatCheckMsgHandler.getInstance()
            natcheck_handler.register(launchmany)
            crawler.register_message_handler(
                CRAWLER_NATCHECK, natcheck_handler.gotDoNatCheckMessage,
                natcheck_handler.gotNatCheckReplyMessage)
            crawler.register_message_handler(
                CRAWLER_NATTRAVERSAL, natcheck_handler.gotUdpConnectRequest,
                natcheck_handler.gotUdpConnectReply)
            videoplayback_crawler = VideoPlaybackCrawler.get_instance()
            crawler.register_message_handler(
                CRAWLER_VIDEOPLAYBACK_EVENT_QUERY,
                videoplayback_crawler.handle_event_crawler_request,
                videoplayback_crawler.handle_event_crawler_reply)
            repex_crawler = RepexCrawler.get_instance()
            crawler.register_message_handler(
                CRAWLER_REPEX_QUERY, repex_crawler.handle_crawler_request,
                repex_crawler.handle_crawler_reply)

            if crawler.am_crawler():

                # we will only accept CRAWLER_REPLY messages when we are actully a crawler
                self.register_msg_handler([CRAWLER_REPLY],
                                          crawler.handle_reply)
                self.register_connection_handler(crawler.handle_connection)

                if "database" in sys.argv:
                    # allows access to tribler database (boudewijn)
                    crawler.register_crawl_initiator(
                        database_crawler.query_initiator)

                if "videoplayback" in sys.argv:
                    # allows access to video-playback statistics (boudewijn)
                    crawler.register_crawl_initiator(
                        videoplayback_crawler.query_initiator)

                if "seedingstats" in sys.argv:
                    # allows access to seeding statistics (Boxun)
                    crawler.register_crawl_initiator(
                        seeding_stats_crawler.query_initiator,
                        frequency=60 * 30)

                if "friendship" in sys.argv:
                    # allows access to friendship statistics (Ali)
                    crawler.register_crawl_initiator(
                        friendship_crawler.query_initiator)

                if "natcheck" in sys.argv:
                    # allows access to nat-check statistics (Lucia)
                    crawler.register_crawl_initiator(
                        natcheck_handler.doNatCheck, 3600)

                if "repex" in sys.argv:
                    # allows access to RePEX log statistics (Raynor Vliegendhart)
                    crawler.register_crawl_initiator(
                        repex_crawler.query_initiator)

        else:
            self.register_msg_handler([CRAWLER_REQUEST, CRAWLER_REPLY],
                                      self.handleDisabledMessage)

        self.rtorrent_handler = RemoteTorrentHandler.getInstance()
        self.rtorrent_handler.register(overlay_bridge, self.metadata_handler,
                                       session)
        self.metadata_handler.register2(self.rtorrent_handler)

        # Add notifier as connection handler
        self.register_connection_handler(self.notifier_handles_connection)

        if config['buddycast']:
            # Arno: to prevent concurrency between mainthread and overlay
            # thread where BuddyCast schedules tasks
            self.buddycast.register2()
Esempio n. 13
0
    def register(self, overlay_bridge, session, launchmany, config, requestPolicy):
        self.overlay_bridge = overlay_bridge
        self.launchmany = launchmany
        self.requestPolicy = requestPolicy
        self.text_mode = config.has_key('text_mode')
        
        # OverlayApps gets all messages, and demultiplexes 
        overlay_bridge.register_recv_callback(self.handleMessage)
        overlay_bridge.register_conns_callback(self.handleConnection)

        # Arno, 2010-01-28: Start with crawler support, other mods depend on
        # that, e.g. BuddyCast
        i_am_crawler = False
        if config['crawler']:
            crawler = Crawler.get_instance(session)
            self.register_msg_handler([CRAWLER_REQUEST], crawler.handle_request)

            database_crawler = DatabaseCrawler.get_instance()
            crawler.register_message_handler(CRAWLER_DATABASE_QUERY, database_crawler.handle_crawler_request, database_crawler.handle_crawler_reply)
            seeding_stats_crawler = SeedingStatsCrawler.get_instance()
            crawler.register_message_handler(CRAWLER_SEEDINGSTATS_QUERY, seeding_stats_crawler.handle_crawler_request, seeding_stats_crawler.handle_crawler_reply)
            friendship_crawler = FriendshipCrawler.get_instance(session)
            crawler.register_message_handler(CRAWLER_FRIENDSHIP_STATS, friendship_crawler.handle_crawler_request, friendship_crawler.handle_crawler_reply)
            natcheck_handler = NatCheckMsgHandler.getInstance()
            natcheck_handler.register(launchmany)
            crawler.register_message_handler(CRAWLER_NATCHECK, natcheck_handler.gotDoNatCheckMessage, natcheck_handler.gotNatCheckReplyMessage)
            crawler.register_message_handler(CRAWLER_NATTRAVERSAL, natcheck_handler.gotUdpConnectRequest, natcheck_handler.gotUdpConnectReply)
            videoplayback_crawler = VideoPlaybackCrawler.get_instance()
            crawler.register_message_handler(CRAWLER_VIDEOPLAYBACK_EVENT_QUERY, videoplayback_crawler.handle_event_crawler_request, videoplayback_crawler.handle_event_crawler_reply)
            crawler.register_message_handler(CRAWLER_VIDEOPLAYBACK_INFO_QUERY, videoplayback_crawler.handle_info_crawler_request, videoplayback_crawler.handle_info_crawler_reply)
            repex_crawler = RepexCrawler.get_instance(session)
            crawler.register_message_handler(CRAWLER_REPEX_QUERY, repex_crawler.handle_crawler_request, repex_crawler.handle_crawler_reply)
            puncture_crawler = PunctureCrawler.get_instance()
            crawler.register_message_handler(CRAWLER_PUNCTURE_QUERY, puncture_crawler.handle_crawler_request, puncture_crawler.handle_crawler_reply)
            channel_crawler = ChannelCrawler.get_instance()
            crawler.register_message_handler(CRAWLER_CHANNEL_QUERY, channel_crawler.handle_crawler_request, channel_crawler.handle_crawler_reply)

            if crawler.am_crawler():
                i_am_crawler = True
                # we will only accept CRAWLER_REPLY messages when we are actully a crawler
                self.register_msg_handler([CRAWLER_REPLY], crawler.handle_reply)
                self.register_connection_handler(crawler.handle_connection)

                if "database" in sys.argv:
                    # allows access to tribler database (boudewijn)
                    crawler.register_crawl_initiator(database_crawler.query_initiator)

                if "videoplayback" in sys.argv:
                    # allows access to video-playback statistics (boudewijn)
                    crawler.register_crawl_initiator(videoplayback_crawler.query_initiator)

                if "seedingstats" in sys.argv:
                    # allows access to seeding statistics (Boxun)
                    crawler.register_crawl_initiator(seeding_stats_crawler.query_initiator, frequency=60*30)

                if "friendship" in sys.argv:
                    # allows access to friendship statistics (Ali)
                    crawler.register_crawl_initiator(friendship_crawler.query_initiator)

                if "natcheck" in sys.argv:
                    # allows access to nat-check statistics (Lucia)
                    crawler.register_crawl_initiator(natcheck_handler.doNatCheck, 3600)
                
                if "repex" in sys.argv:
                    # allows access to RePEX log statistics (Raynor Vliegendhart)
                    crawler.register_crawl_initiator(repex_crawler.query_initiator)

                if "puncture" in sys.argv:
                    # allows access to UDPPuncture log statistics (Gertjan)
                    crawler.register_crawl_initiator(puncture_crawler.query_initiator)
                
                if "channel" in sys.argv:
                    # allows access to tribler channels' database (nitin)
                    crawler.register_crawl_initiator(channel_crawler.query_initiator)
        else:
            self.register_msg_handler([CRAWLER_REQUEST, CRAWLER_REPLY], self.handleDisabledMessage)


        # Create handler for metadata messages in two parts, as 
        # download help needs to know the metadata_handler and we need
        # to know the download helper handler.
        # Part 1:
        self.metadata_handler = MetadataHandler.getInstance()

        if config['download_help']:
            # Create handler for messages to dlhelp coordinator
            self.coord_handler = CoordinatorMessageHandler(launchmany)
            self.register_msg_handler(HelpHelperMessages, self.coord_handler.handleMessage)

            # Create handler for messages to dlhelp helper
            self.help_handler = HelperMessageHandler()
            self.help_handler.register(session,self.metadata_handler,config['download_help_dir'],config.get('coopdlconfig', False))
            self.register_msg_handler(HelpCoordinatorMessages, self.help_handler.handleMessage)

        # Part 2:
        self.metadata_handler.register(overlay_bridge, self.help_handler, launchmany, config)
        self.register_msg_handler(MetadataMessages, self.metadata_handler.handleMessage)
        
        
        # 13-04-2010 Andrea: subtitles collecting
        if not config['subtitles_collecting'] : 
            self.subtitles_handler = None
        else:
            self.subtitles_handler = SubtitlesHandler.getInstance()
            self.subtitles_handler.register(self.overlay_bridge, self.launchmany.richmetadataDbHandler, self.launchmany.session)
            
            self.peersHaveManger = PeersHaveManager.getInstance()
            if not self.peersHaveManger.isRegistered():
                self.peersHaveManger.register(self.launchmany.richmetadataDbHandler, self.overlay_bridge)
            # I'm not sure if this is the best place to init this
            self.subtitle_support = SubtitlesSupport.getInstance()
                                                           
            keypair = self.launchmany.session.keypair
            permid = self.launchmany.session.get_permid()
            self.subtitle_support._register(self.launchmany.richmetadataDbHandler,
                                           self.subtitles_handler, 
                                           self.launchmany.channelcast_db, permid, 
                                           keypair, self.peersHaveManger,
                                           self.overlay_bridge)
            
            # cleanup the subtitles database at the first launch  
            self.subtitle_support.runDBConsinstencyRoutine()
            
        
        
        if not config['torrent_collecting']:
            self.torrent_collecting_solution = 0
        else:
            self.torrent_collecting_solution = config['buddycast_collecting_solution']
        
        if config['buddycast']:
            # Create handler for Buddycast messages
            
            self.buddycast = BuddyCastFactory.getInstance(superpeer=config['superpeer'], log=config['overlay_log'])
            # Using buddycast to handle torrent collecting since they are dependent
            self.buddycast.register(overlay_bridge, launchmany, 
                                    launchmany.rawserver_fatalerrorfunc,
                                    self.metadata_handler, 
                                    self.torrent_collecting_solution,
                                    config['start_recommender'],config['buddycast_max_peers'],i_am_crawler)
            
            self.register_msg_handler(BuddyCastMessages, self.buddycast.handleMessage)
            self.register_connection_handler(self.buddycast.handleConnection)

        if config['dialback']:
            self.dialback_handler = DialbackMsgHandler.getInstance()
            # The Dialback mechanism needs the real rawserver, not the overlay_bridge
            self.dialback_handler.register(overlay_bridge, launchmany, launchmany.rawserver, config)
            self.register_msg_handler([DIALBACK_REQUEST],
                                      self.dialback_handler.olthread_handleSecOverlayMessage)
            self.register_connection_handler(self.dialback_handler.olthread_handleSecOverlayConnection)
        else:
            self.register_msg_handler([DIALBACK_REQUEST], self.handleDisabledMessage)

        if config['socnet']:
            self.socnet_handler = SocialNetworkMsgHandler.getInstance()
            self.socnet_handler.register(overlay_bridge, launchmany, config)
            self.register_msg_handler(SocialNetworkMessages,self.socnet_handler.handleMessage)
            self.register_connection_handler(self.socnet_handler.handleConnection)

            self.friendship_handler = FriendshipMsgHandler.getInstance()
            self.friendship_handler.register(overlay_bridge, launchmany.session)
            self.register_msg_handler(FriendshipMessages,self.friendship_handler.handleMessage)
            self.register_connection_handler(self.friendship_handler.handleConnection)

        if config['rquery']:
            self.rquery_handler = RemoteQueryMsgHandler.getInstance()
            self.rquery_handler.register(overlay_bridge,launchmany,config,self.buddycast,log=config['overlay_log'])
            self.register_msg_handler(RemoteQueryMessages,self.rquery_handler.handleMessage)
            self.register_connection_handler(self.rquery_handler.handleConnection)
        
        if config['subtitles_collecting']:
            hndl = self.subtitles_handler.getMessageHandler()
            self.register_msg_handler(SubtitleMessages, hndl)
        
        self.rtorrent_handler = RemoteTorrentHandler.getInstance()
        self.rtorrent_handler.register(overlay_bridge,self.metadata_handler,session)
        self.metadata_handler.register2(self.rtorrent_handler)

        # Add notifier as connection handler
        self.register_connection_handler(self.notifier_handles_connection)
        
        if config['buddycast']:
            # Arno: to prevent concurrency between mainthread and overlay
            # thread where BuddyCast schedules tasks
            self.buddycast.register2()
    def register(self, overlay_bridge, session, launchmany, config, requestPolicy):
        self.overlay_bridge = overlay_bridge
        self.launchmany = launchmany
        self.requestPolicy = requestPolicy
        self.text_mode = config.has_key('text_mode')
        
        # OverlayApps gets all messages, and demultiplexes 
        overlay_bridge.register_recv_callback(self.handleMessage)
        overlay_bridge.register_conns_callback(self.handleConnection)

        # Create handler for metadata messages in two parts, as 
        # download help needs to know the metadata_handler and we need
        # to know the download helper handler.
        # Part 1:
        self.metadata_handler = MetadataHandler.getInstance()

        if config['download_help']:
            # Create handler for messages to dlhelp coordinator
            self.coord_handler = CoordinatorMessageHandler(launchmany)
            self.register_msg_handler(HelpHelperMessages, self.coord_handler.handleMessage)

            # Create handler for messages to dlhelp helper
            self.help_handler = HelperMessageHandler()
            self.help_handler.register(session,self.metadata_handler,config['download_help_dir'],config.get('coopdlconfig', False))
            self.register_msg_handler(HelpCoordinatorMessages, self.help_handler.handleMessage)

        # Part 2:
        self.metadata_handler.register(overlay_bridge, self.help_handler, launchmany, config)
        self.register_msg_handler(MetadataMessages, self.metadata_handler.handleMessage)
        
        if not config['torrent_collecting']:
            self.torrent_collecting_solution = 0
        else:
            self.torrent_collecting_solution = config['buddycast_collecting_solution']
        
        if config['buddycast']:
            # Create handler for Buddycast messages
            
            self.buddycast = BuddyCastFactory.getInstance(superpeer=config['superpeer'], log=config['overlay_log'])
            # Using buddycast to handle torrent collecting since they are dependent
            self.buddycast.register(overlay_bridge, launchmany, 
                                    launchmany.rawserver_fatalerrorfunc,
                                    self.metadata_handler, 
                                    self.torrent_collecting_solution,
                                    config['start_recommender'],config['buddycast_max_peers'])
            self.register_msg_handler(BuddyCastMessages, self.buddycast.handleMessage)
            self.register_connection_handler(self.buddycast.handleConnection)

        if config['dialback']:
            self.dialback_handler = DialbackMsgHandler.getInstance()
            # The Dialback mechanism needs the real rawserver, not the overlay_bridge
            self.dialback_handler.register(overlay_bridge, launchmany, launchmany.rawserver, config)
            self.register_msg_handler([DIALBACK_REQUEST],
                                      self.dialback_handler.olthread_handleSecOverlayMessage)
            self.register_connection_handler(self.dialback_handler.olthread_handleSecOverlayConnection)
        else:
            self.register_msg_handler([DIALBACK_REQUEST], self.handleDisabledMessage)

        if config['socnet']:
            self.socnet_handler = SocialNetworkMsgHandler.getInstance()
            self.socnet_handler.register(overlay_bridge, launchmany, config)
            self.register_msg_handler(SocialNetworkMessages,self.socnet_handler.handleMessage)
            self.register_connection_handler(self.socnet_handler.handleConnection)

            self.friendship_handler = FriendshipMsgHandler.getInstance()
            self.friendship_handler.register(overlay_bridge, launchmany.session)
            self.register_msg_handler(FriendshipMessages,self.friendship_handler.handleMessage)
            self.register_connection_handler(self.friendship_handler.handleConnection)

        if config['rquery']:
            self.rquery_handler = RemoteQueryMsgHandler.getInstance()
            self.rquery_handler.register(overlay_bridge,launchmany,config,self.buddycast,log=config['overlay_log'])
            self.register_msg_handler(RemoteQueryMessages,self.rquery_handler.handleMessage)
            self.register_connection_handler(self.rquery_handler.handleConnection)
            
        if config['crawler']:
            crawler = Crawler.get_instance(session)
            self.register_msg_handler([CRAWLER_REQUEST], crawler.handle_request)

            database_crawler = DatabaseCrawler.get_instance()
            crawler.register_message_handler(CRAWLER_DATABASE_QUERY, database_crawler.handle_crawler_request, database_crawler.handle_crawler_reply)
            seeding_stats_crawler = SeedingStatsCrawler.get_instance()
            crawler.register_message_handler(CRAWLER_SEEDINGSTATS_QUERY, seeding_stats_crawler.handle_crawler_request, seeding_stats_crawler.handle_crawler_reply)
            friendship_crawler = FriendshipCrawler.get_instance(session)
            crawler.register_message_handler(CRAWLER_FRIENDSHIP_STATS, friendship_crawler.handle_crawler_request, friendship_crawler.handle_crawler_reply)
            natcheck_handler = NatCheckMsgHandler.getInstance()
            natcheck_handler.register(launchmany)
            crawler.register_message_handler(CRAWLER_NATCHECK, natcheck_handler.gotDoNatCheckMessage, natcheck_handler.gotNatCheckReplyMessage)
            crawler.register_message_handler(CRAWLER_NATTRAVERSAL, natcheck_handler.gotUdpConnectRequest, natcheck_handler.gotUdpConnectReply)
            videoplayback_crawler = VideoPlaybackCrawler.get_instance()
            crawler.register_message_handler(CRAWLER_VIDEOPLAYBACK_EVENT_QUERY, videoplayback_crawler.handle_event_crawler_request, videoplayback_crawler.handle_event_crawler_reply)
            repex_crawler = RepexCrawler.get_instance()
            crawler.register_message_handler(CRAWLER_REPEX_QUERY, repex_crawler.handle_crawler_request, repex_crawler.handle_crawler_reply)


            if crawler.am_crawler():

                # we will only accept CRAWLER_REPLY messages when we are actully a crawler
                self.register_msg_handler([CRAWLER_REPLY], crawler.handle_reply)
                self.register_connection_handler(crawler.handle_connection)

                if "database" in sys.argv:
                    # allows access to tribler database (boudewijn)
                    crawler.register_crawl_initiator(database_crawler.query_initiator)

                if "videoplayback" in sys.argv:
                    # allows access to video-playback statistics (boudewijn)
                    crawler.register_crawl_initiator(videoplayback_crawler.query_initiator)

                if "seedingstats" in sys.argv:
                    # allows access to seeding statistics (Boxun)
                    crawler.register_crawl_initiator(seeding_stats_crawler.query_initiator, frequency=60*30)

                if "friendship" in sys.argv:
                    # allows access to friendship statistics (Ali)
                    crawler.register_crawl_initiator(friendship_crawler.query_initiator)

                if "natcheck" in sys.argv:
                    # allows access to nat-check statistics (Lucia)
                    crawler.register_crawl_initiator(natcheck_handler.doNatCheck, 3600)
                
                if "repex" in sys.argv:
                    # allows access to RePEX log statistics (Raynor Vliegendhart)
                    crawler.register_crawl_initiator(repex_crawler.query_initiator)

        else:
            self.register_msg_handler([CRAWLER_REQUEST, CRAWLER_REPLY], self.handleDisabledMessage)
        
        self.rtorrent_handler = RemoteTorrentHandler.getInstance()
        self.rtorrent_handler.register(overlay_bridge,self.metadata_handler,session)
        self.metadata_handler.register2(self.rtorrent_handler)

        # Add notifier as connection handler
        self.register_connection_handler(self.notifier_handles_connection)
        
        if config['buddycast']:
            # Arno: to prevent concurrency between mainthread and overlay
            # thread where BuddyCast schedules tasks
            self.buddycast.register2()
Esempio n. 15
0
    def __init__(self, rawserver, check_crawler, port = 0):
        # initialise connections now because it is used in shutdown which will
        # be called for Crawler instances as well
        self.connections = {}
        if check_crawler:
            from BaseLib.Core.Statistics.Crawler import Crawler
            crawler = Crawler.get_instance()
            if crawler.am_crawler():
                return

        # initialise connections now because it is used in shutdown which will
        # be called for Crawler instances as well
        self.connections = {}

        # 17/03/10 Boudewijn: obsolete code, see the same code a few
        # lines above that include the check_crawler if/else check
        # from BaseLib.Core.Statistics.Crawler import Crawler
        # crawler = Crawler.get_instance()
        # if crawler.am_crawler():
        #     return
        
        self.rawserver = rawserver
        self.socket = rawserver.create_udpsocket(port, "0.0.0.0")
        self.known_peers = {}
        self.nat_type = UDPHandler.NAT_UNKNOWN
        self.filter_type = UDPHandler.FILTER_UNKNOWN
        self.max_connections = 100
        self.connect_threshold = 75
        self.recv_unsolicited = 0
        self.recv_connect_total = 0
        self.recv_address = 0
        self.recv_different_address = 0
        self.sendqueue = deque([])
        self.last_connect = 0
        self.last_info_dump = time.time()
        self.natfw_version = 1
        self.keepalive_intvl = 100
        self.done = False
        self.reporter = None
        self.last_sends = {}

        rawserver.start_listening_udp(self.socket, self)

        # Contact NATSwarm tracker peer after 5 seconds
        if port == 9473:
            self.tracker = True

            # Tracker needs a known ID, so set it to all zero
            self.id = "\0\0\0\0"
            # Tracker should accept many more connections than other nodes
            self.max_connections = 1000
            rawserver.add_task(self.check_for_timeouts, 10)
        else:
            self.tracker = False

            # Create a 4 byte random ID
            self.id = (chr(random.getrandbits(8)) + chr(random.getrandbits(8)) +
                chr(random.getrandbits(8)) + chr(random.getrandbits(8)))
            if DEBUG:
                debug("My ID: %s" % self.id.encode('hex'))
            rawserver.add_task(self.bootstrap, 5)
            TimeoutFinder.TimeoutFinder(rawserver, False, self.timeout_report)
            TimeoutFinder.TimeoutFinder(rawserver, True, self.timeout_report)

            if not DEBUG:
                if check_crawler:
                    #~ from BaseLib.Core.Statistics.StatusReporter import get_reporter_instance
                    from BaseLib.Core.Statistics.PunctureCrawler import get_reporter_instance
                    self.reporter = get_reporter_instance()

        if self.reporter:
            my_wan_ip = guessip.get_my_wan_ip()
            if my_wan_ip == None and sys.platform == 'win32':
                try:
                    import os
                    for line in os.popen("netstat -nr").readlines():
                        words = line.split()
                        if words[0] == '0.0.0.0':
                            my_wan_ip = words[3]
                            break
                except:
                    pass
            if my_wan_ip == None:
                my_wan_ip = 'Unknown'
            self.reporter.add_event("UDPPuncture", "ID:%s;IP:%s" % (self.id.encode('hex'), my_wan_ip))