class AbstractTestChannelCommunity(AbstractTestCommunity): # We have to initialize Dispersy and the tunnel community on the reactor thread @inlineCallbacks def setUp(self): yield super(AbstractTestChannelCommunity, self).setUp() self.channel_community = ChannelCommunity(self.dispersy, self.master_member, self.member) @inlineCallbacks def tearDown(self): # Don't unload_community() as it never got registered in dispersy on the first place. self.channel_community.cancel_all_pending_tasks() self.channel_community = None yield super(AbstractTestChannelCommunity, self).tearDown()
def selectTorrentsToCollect(self, infohashes): to_collect = ChannelCommunity.selectTorrentsToCollect(self, infohashes) #Reducing the number of samples to collect for unsubscribed channels if len(to_collect) > 2: return sample(to_collect, 2) return to_collect
def execute_scenario_cmds(self, commands): torrents = [] for command in commands: cur_command = command.split() if cur_command[0] == 'create': log(self._logfile, "creating-community") self.my_channel = ChannelCommunity.create_community(self.my_member, integrate_with_tribler = False) log(self._logfile, "creating-channel-message") self.my_channel.create_channel(u'', u'') elif cur_command[0] == 'publish': if self.my_channel: infohash = str(self.torrentindex) infohash += ''.join(choice(letters) for _ in xrange(20-len(infohash))) name = u''.join(choice(letters) for _ in xrange(100)) files = [] for _ in range(10): files.append((u''.join(choice(letters) for _ in xrange(30)), 123455)) trackers = [] for _ in range(10): trackers.append(''.join(choice(letters) for _ in xrange(30))) files = tuple(files) trackers = tuple(trackers) self.torrentindex += 1 torrents.append((infohash, int(time()), name, files, trackers)) elif cur_command[0] == 'post': if self.joined_community: text = ''.join(choice(letters) for i in xrange(160)) self.joined_community._disp_create_comment(text, int(time()), None, None, None, None) elif cur_command[0] == 'join': self.want_to_join = True if self.want_to_join: from Tribler.dispersy.dispersy import Dispersy dispersy = Dispersy.get_instance() log(self._logfile, "trying-to-join-community") for community in dispersy.get_communities(): if isinstance(community, PreviewChannelCommunity) and community._channel_id: self._community.disp_create_votecast(community.cid, 2, int(time())) log(self._logfile, "joining-community") self.joined_community = community self.want_to_join = False break if len(torrents) > 0: log(self._logfile, "creating-torrents") self.my_channel._disp_create_torrents(torrents)
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 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 create(self): msg("creating-community") from Tribler.community.channel.community import ChannelCommunity self.my_channel = ChannelCommunity.create_community(self._dispersy, self._my_member, integrate_with_tribler=False) msg("creating-channel-message") self.my_channel._disp_create_channel(u'', u'')
class AbstractTestChannelCommunity(AbstractTestCommunity): # We have to initialize Dispersy and the tunnel community on the reactor thread @blocking_call_on_reactor_thread @inlineCallbacks def setUp(self, annotate=True): yield super(AbstractTestChannelCommunity, self).setUp(annotate=annotate) self.channel_community = ChannelCommunity(self.dispersy, self.master_member, self.member) @blocking_call_on_reactor_thread @inlineCallbacks def tearDown(self, annotate=True): # Don't unload_community() as it never got registered in dispersy on the first place. self.channel_community.cancel_all_pending_tasks() self.channel_community = None yield super(AbstractTestChannelCommunity, self).tearDown(annotate=annotate)
def create(self): msg("creating-community") from Tribler.community.channel.community import ChannelCommunity self.my_channel = ChannelCommunity.create_community( self._dispersy, self._my_member, integrate_with_tribler=False) msg("creating-channel-message") self.my_channel._disp_create_channel(u'', u'')
def _create_channel(): channel_mode = self._channel_mode_map[mode] community = ChannelCommunity.create_community( self.dispersy, self.session.dispersy_member, tribler_session=self.session) community.set_channel_mode(channel_mode) community.create_channel(name, description)
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 KeyError: if self.auto_join_channel: logger.debug("join channel community %s", cid.encode("HEX")) return ChannelCommunity.join_community(self._dispersy, self._dispersy.get_temporary_member_from_id(cid), self._my_member, self.integrate_with_tribler) else: logger.debug("join preview community %s", cid.encode("HEX")) return PreviewChannelCommunity.join_community(self._dispersy, self._dispersy.get_temporary_member_from_id(cid), self._my_member, self.integrate_with_tribler)
def _get_channel_community(self, cid): assert isinstance(cid, str) assert len(cid) == 20 try: return self._dispersy.get_community(cid, True) except KeyError: if self.auto_join_channel: if __debug__: dprint("join channel community ", cid.encode("HEX")) return ChannelCommunity.join_community(DummyMember(cid), self._my_member, self.integrate_with_tribler) else: if __debug__: dprint("join preview community ", cid.encode("HEX")) return PreviewChannelCommunity.join_community(DummyMember(cid), self._my_member, self.integrate_with_tribler)
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 create_channel(self, name, description, mode, rss_url=None): """ Creates a new Channel. :param name: Name of the Channel. :param description: Description of the Channel. :param mode: Mode of the Channel ('open', 'semi-open', or 'closed'). :param rss_url: RSS URL for the Channel. :return: Channel ID :raises DuplicateChannelNameError if name already exists """ assert isinstance( name, basestring), u"name is not a basestring: %s" % type(name) assert isinstance( description, basestring ), u"description is not a basestring: %s" % type(description) assert mode in self._channel_mode_map, u"invalid mode: %s" % mode assert isinstance( rss_url, basestring ) or rss_url is None, u"rss_url is not a basestring or None: %s" % type( rss_url) # if two channels have the same name, this will not work for channel_object in self._channel_list: if name == channel_object.name: raise DuplicateChannelNameError( u"Channel name already exists: %s" % name) channel_mode = self._channel_mode_map[mode] community = ChannelCommunity.create_community( self.dispersy, self.session.dispersy_member, tribler_session=self.session) channel_obj = ChannelObject(self.session, community) channel_obj.initialize() community.set_channel_mode(channel_mode) community.create_channel(name, description) # create channel object self._channel_list.append(channel_obj) if rss_url is not None: channel_obj.create_rss_feed(rss_url) self._logger.debug(u"creating channel '%s', %s", channel_obj.name, hexlify(community.cid)) return channel_obj.channel_id
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 create_channel(self, name, description, mode, rss_url=None): """ Creates a new Channel. :param name: Name of the Channel. :param description: Description of the Channel. :param mode: Mode of the Channel ('open', 'semi-open', or 'closed'). :param rss_url: RSS URL for the Channel. :return: Channel ID :raises DuplicateChannelNameError if name already exists """ assert isinstance(name, basestring), u"name is not a basestring: %s" % type(name) assert isinstance(description, basestring), u"description is not a basestring: %s" % type(description) assert mode in self._channel_mode_map, u"invalid mode: %s" % mode assert isinstance(rss_url, basestring) or rss_url is None, u"rss_url is not a basestring or None: %s" % type(rss_url) # if two channels have the same name, this will not work for channel_object in self._channel_list: if name == channel_object.name: raise DuplicateChannelNameError(u"Channel name already exists: %s" % name) channel_mode = self._channel_mode_map[mode] community = ChannelCommunity.create_community(self.dispersy, self.session.dispersy_member, tribler_session=self.session) channel_obj = ChannelObject(self.session, community) channel_obj.initialize() community.set_channel_mode(channel_mode) community.create_channel(name, description) # create channel object self._channel_list.append(channel_obj) if rss_url is not None: channel_obj.create_rss_feed(rss_url) self._logger.debug(u"creating channel '%s', %s", channel_obj.name, hexlify(community.cid)) return channel_obj.channel_id
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 _create_channel(): channel_mode = self._channel_mode_map[mode] community = ChannelCommunity.create_community(self.dispersy, self.session.dispersy_member, tribler_session=self.session) community.set_channel_mode(channel_mode) community.create_channel(name, description)
def setUp(self): yield super(AbstractTestChannelCommunity, self).setUp() self.channel_community = ChannelCommunity(self.dispersy, self.master_member, self.member)
def setUp(self, annotate=True): yield super(AbstractTestChannelCommunity, self).setUp(annotate=annotate) self.channel_community = ChannelCommunity(self.dispersy, self.master_member, self.member)