def join_community(): """ find the community/channel id, then join """ try: self.community = dispersy.get_community(dispersy_cid, True) self.register_task( str(self.source) + "_get_id", reactor.callLater(1, get_channel_id)) except CommunityNotFoundException: allchannelcommunity = None for community in dispersy.get_communities(): if isinstance(community, AllChannelCommunity): allchannelcommunity = community break if allchannelcommunity: self.community = ChannelCommunity.init_community( dispersy, dispersy.get_member(mid=dispersy_cid), allchannelcommunity._my_member, self.session) self._logger.info("Joined channel community %s", dispersy_cid.encode("HEX")) self.register_task( str(self.source) + "_get_id", reactor.callLater(1, get_channel_id)) else: self._logger.error("Could not find AllChannelCommunity")
def start(self): super(ChannelSource, self).start() # Join the community if needed dispersy = self.session.get_dispersy_instance() try: self.community = dispersy.get_community(unhexlify(self.source), True) except CommunityNotFoundException: allchannelcommunity = None for community in dispersy.get_communities(): if isinstance(community, AllChannelCommunity): allchannelcommunity = community if allchannelcommunity: self.community = ChannelCommunity.init_community(dispersy, dispersy.get_member(mid=unhexlify(self.source)), allchannelcommunity.my_member, self.session) self._logger.info('Joined channel community %s', self.source) else: self._logger.error('Could not find AllChannelCommunity') return # Add torrents from database channel_id = self.community.get_channel_id() torrents = self.channelcast_db.getTorrentsFromChannelId(channel_id, True, ['infohash', 'ChannelTorrents.name']) for infohash_bin, name in torrents: self.torrent_insert_callback(self.source, hexlify(infohash_bin), name) self.session.add_observer(self.on_torrent_discovered, NTFY_TORRENT, [NTFY_DISCOVERED])
def test_chn_exist_lookup(self): """ testing existing channel as a source. It also tests how boosting manager cope with unknown channel with retrying the lookup """ self.session.get_dispersy = lambda: True self.session.lm.dispersy = Dispersy(ManualEnpoint(0), self.getStateDir()) dispersy_cid_hex = "abcd" * 9 + "0012" dispersy_cid = binascii.unhexlify(dispersy_cid_hex) # create channel and insert torrent self.create_fake_allchannel_community() self.create_torrents_in_channel(dispersy_cid_hex) # channel is exist community = ChannelCommunity.init_community( self.session.lm.dispersy, self.session.lm.dispersy.get_member(mid=dispersy_cid), self.session.lm.dispersy._communities['allchannel']._my_member, self.session) # make the id unknown so boosting manager can test repeating search id_tmp = community._channel_id community._channel_id = 0 def _set_id_channel(channel_id): """ set channel id manually (emulate finding) """ community._channel_id = channel_id reactor.callLater(5, _set_id_channel, id_tmp) self.boosting_manager.add_source(dispersy_cid) chn_obj = self.boosting_manager.get_source_object(dispersy_cid) chn_obj._load_torrent = self._load def clean_community(_): """ cleanly exit the community we are in """ if chn_obj.community: chn_obj.community.cancel_all_pending_tasks() chn_obj.kill_tasks() d = self.check_source(dispersy_cid) d.addCallback(clean_community) return d
def _get_channel_community(self, cid): assert isinstance(cid, str) assert len(cid) == 20 try: return self._dispersy.get_community(cid, True) except CommunityNotFoundException: if self.auto_join_channel: self._logger.info("join channel community %s", cid.encode("HEX")) return ChannelCommunity.init_community(self._dispersy, self._dispersy.get_member(mid=cid), self._my_member, tribler_session=self.tribler_session) else: self._logger.info("join preview community %s", cid.encode("HEX")) return PreviewChannelCommunity.init_community(self._dispersy, self._dispersy.get_member(mid=cid), self._my_member, tribler_session=self.tribler_session)
def test_existing_channel_lookup(self): # Find AllChannel for community in self.session.lm.dispersy.get_communities(): if isinstance(community, AllChannelCommunity): allchannelcommunity = community # Load the channel community = ChannelCommunity.init_community(self.session.lm.dispersy, self.session.lm.dispersy.get_member(mid=unhexlify(self.cid)), allchannelcommunity.my_member, self.session) # Check if we find the channel source = ChannelSource(self.session, self.cid, lambda: None) source.start() self.assertEqual(source.community, community, 'ChannelSource failed to find existing ChannelCommunity') source.stop()
def test_existing_channel_lookup(self): # Find AllChannel for community in self.session.lm.dispersy.get_communities(): if isinstance(community, AllChannelCommunity): allchannelcommunity = community # Load the channel community = ChannelCommunity.init_community( self.session.lm.dispersy, self.session.lm.dispersy.get_member(mid=unhexlify(self.cid)), allchannelcommunity.my_member, self.session) # Check if we find the channel source = ChannelSource(self.session, self.cid, lambda: None) source.start() self.assertEqual( source.community, community, 'ChannelSource failed to find existing ChannelCommunity') source.stop()
def _get_channel_community(self, cid): assert isinstance(cid, str) assert len(cid) == 20 try: return self._dispersy.get_community(cid, True) except CommunityNotFoundException: if self.auto_join_channel: self._logger.info("join channel community %s", cid.encode("HEX")) return ChannelCommunity.init_community( self._dispersy, self._dispersy.get_member(mid=cid), self._my_member, tribler_session=self.tribler_session) else: self._logger.info("join preview community %s", cid.encode("HEX")) return PreviewChannelCommunity.init_community( self._dispersy, self._dispersy.get_member(mid=cid), self._my_member, tribler_session=self.tribler_session)