def validChannelCastMsg(channelcast_data): """ Returns true if ChannelCastMsg is valid, format: {'signature':{'publisher_id':, 'publisher_name':, 'infohash':, 'torrenthash':, 'torrent_name':, 'timestamp':, 'signature':}} """ if not isinstance(channelcast_data,dict): return False for signature, ch in channelcast_data.items(): if not isinstance(ch,dict): if DEBUG: print >>sys.stderr,"rvalidChannelCastMsg: a: value not dict" return False if len(ch) !=6: if DEBUG: print >>sys.stderr,"rvalidChannelCastMsg: a: #keys!=6" return False if not ('publisher_id' in ch and 'publisher_name' in ch and 'infohash' in ch and 'torrenthash' in ch and 'torrentname' in ch and 'time_stamp' in ch): if DEBUG: print >>sys.stderr,"validChannelCastMsg: a: key missing, got",d.keys() return False if not (validPermid(ch['publisher_id']) and (isinstance(ch['publisher_name'],str) or isinstance(ch['publisher_name'], unicode)) and validInfohash(ch['infohash']) and validInfohash(ch['torrenthash']) and (isinstance(ch['torrentname'],str) or isinstance(ch['torrentname'],unicode)) and validTimestamp(ch['time_stamp'])): if DEBUG: print >>sys.stderr,"validChannelCastMsg: something not valid" return False # now, verify signature l = (ch['publisher_id'],ch['infohash'], ch['torrenthash'], ch['time_stamp']) if not verify_data(bencode(l),str2bin(ch['publisher_id']),str2bin(signature)): if DEBUG: print >>sys.stderr, "validChannelCastMsg: verification failed!" return False return True
def updateChannel(self, query_permid, query, hits): """ This function is called when there is a reply from remote peer regarding updating of a channel @param query_permid: the peer who returned the results @param query: the query string @param hits: details of all matching results related to the query """ records = [] for k, v in hits.items(): records.append( (v['publisher_id'], v['publisher_name'], v['infohash'], v['torrenthash'], v['torrentname'], v['time_stamp'], k)) for hit in records: if self.channelcastdb.existsTorrent(hit[2]): self.channelcastdb.addTorrent(hit) self.hits.append(hit) else: def usercallback(infohash, metadata, filename): print >> sys.stderr, "USERCALLBACK" self.channelcastdb.addTorrent(hit) self.hits.append(hit) self.rtorrent_handler.download_torrent(query_permid, str2bin(hit[2]), usercallback)
def validChannelCastMsg(channelcast_data): """ Returns true if ChannelCastMsg is valid, format: {'signature':{'publisher_id':, 'publisher_name':, 'infohash':, 'torrenthash':, 'torrent_name':, 'timestamp':, 'signature':}} """ if not isinstance(channelcast_data, dict): return False for signature, ch in channelcast_data.items(): if not isinstance(ch, dict): if DEBUG: print >> sys.stderr, "rvalidChannelCastMsg: a: value not dict" return False if len(ch) != 6: if DEBUG: print >> sys.stderr, "rvalidChannelCastMsg: a: #keys!=6" return False if not ('publisher_id' in ch and 'publisher_name' in ch and 'infohash' in ch and 'torrenthash' in ch and 'torrentname' in ch and 'time_stamp' in ch): if DEBUG: print >> sys.stderr, "validChannelCastMsg: a: key missing, got", d.keys( ) return False if not (validPermid(ch['publisher_id']) and (isinstance(ch['publisher_name'], str) or isinstance(ch['publisher_name'], unicode)) and validInfohash(ch['infohash']) and validInfohash(ch['torrenthash']) and (isinstance(ch['torrentname'], str) or isinstance(ch['torrentname'], unicode)) and validTimestamp(ch['time_stamp'])): if DEBUG: print >> sys.stderr, "validChannelCastMsg: something not valid" return False # now, verify signature l = (ch['publisher_id'], ch['infohash'], ch['torrenthash'], ch['time_stamp']) if not verify_data(bencode(l), str2bin(ch['publisher_id']), str2bin(signature)): if DEBUG: print >> sys.stderr, "validChannelCastMsg: verification failed!" return False return True
def test_selecteTorrentToCollect(self): db = PreferenceDBHandler.getInstance() tc = SimpleTorrentCollecting(None,None) truth = {3127:235, 994:20, 19:1, 5:0} for pid in truth: pl = db.getPrefList(str2bin(self.permid[pid])) assert len(pl) == truth[pid], [pid, len(pl)] # test random selection infohash = tc.selecteTorrentToCollect(pl, True) if pid == 994 or pid == 3127: assert len(infohash) == 20, infohash else: assert infohash is None, infohash #tc.updateAllCooccurrence() for pid in truth: pl = db.getPrefList(str2bin(self.permid[pid])) assert len(pl) == truth[pid], [pid, len(pl)] # test selecting most relevant torrent infohash = tc.selecteTorrentToCollect(pl, False) if pid == 994: tid = tc.torrent_db.getTorrentID(infohash) assert tid == 8979 permid = self.permid[pid] infohash = tc.updatePreferences(permid, pl) tid = tc.torrent_db.getTorrentID(infohash) assert tid == 8979 elif pid == 3127: tid = tc.torrent_db.getTorrentID(infohash) assert tid == 9170 permid = self.permid[pid] infohash = tc.updatePreferences(permid, pl) tid = tc.torrent_db.getTorrentID(infohash) assert tid == 9170 else: assert infohash is None, infohash
def updateChannel(self,query_permid, query, hits): """ This function is called when there is a reply from remote peer regarding updating of a channel @param query_permid: the peer who returned the results @param query: the query string @param hits: details of all matching results related to the query """ records = [] for k,v in hits.items(): records.append((v['publisher_id'],v['publisher_name'],v['infohash'],v['torrenthash'],v['torrentname'],v['time_stamp'],k)) for hit in records: if self.channelcastdb.existsTorrent(hit[2]): self.channelcastdb.addTorrent(hit) self.hits.append(hit) else: def usercallback(infohash,metadata,filename): print >> sys.stderr , "USERCALLBACK" self.channelcastdb.addTorrent(hit) self.hits.append(hit) self.rtorrent_handler.download_torrent(query_permid,str2bin(hit[2]),usercallback)
def _updateChannelcastDB(self, query_permid, query, hits, listOfAdditions): publisher_ids = Set() #08/04/10: Andrea: processing rich metadata part. self.richMetadataInterceptor.handleRMetadata(query_permid, hits, fromQuery = query is not None) tmp_hits = {} #"binary" key def usercallback(infohash,metadata,filename): if tmp_hits.has_key(infohash): hit = tmp_hits[infohash] if self.channelcastdb.addTorrent(hit): self.hits.append(hit) else: print >> sys.stderr, "channelcast: updatechannel: could not find infohash", bin2str(infohash) for hit in listOfAdditions: publisher_ids.add(hit[0]) infohash = str2bin(hit[2]) tmp_hits[infohash] = hit # effectively v['infohash'] == str2bin(hit[2]) if self.channelcastdb.existsTorrent(infohash): if self.channelcastdb.addTorrent(hit): self.hits.append(hit) else: self.rtorrent_handler.download_torrent(query_permid,infohash,usercallback) # Arno, 2010-02-24: Generate event for publisher_id in publisher_ids: try: self.notifier.notify(NTFY_CHANNELCAST, NTFY_UPDATE, publisher_id) except: print_exc()
if torrent["retried_times"] < self.retryThreshold: torrent["retried_times"] += 1 def tooMuchRetry(self, torrent): if torrent["retried_times"] > self.retryThreshold: return True return False if __name__ == "__main__": from BaseLib.Core.CacheDB.sqlitecachedb import init as init_db, str2bin configure_dir = sys.argv[1] config = {} config["state_dir"] = configure_dir config["install_dir"] = "." config["peer_icon_path"] = "." init_db(config) t = TorrentChecking() t.start() t.join() infohash_str = "TkFX5S4qd2DPW63La/VObgOH/Nc=" infohash = str2bin(infohash_str) del t t = TorrentChecking(infohash) t.start() t.join()
def test_get_dns_from_peerdb(self): permid_str_id_1 = "MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAAA6SYI4NHxwQ8P7P8QXgWAP+v8SaMVzF5+fSUHdAMrs6NvL5Epe1nCNSdlBHIjNjEiC5iiwSFZhRLsr" permid = str2bin(permid_str_id_1) self.loadData(2500) assert self.datahandler.get_dns_from_peerdb(permid) == ("68.108.115.221", 6881)
def test_get_dns_from_peerdb(self): permid_str_id_1 = 'MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAAA6SYI4NHxwQ8P7P8QXgWAP+v8SaMVzF5+fSUHdAMrs6NvL5Epe1nCNSdlBHIjNjEiC5iiwSFZhRLsr' permid = str2bin(permid_str_id_1) self.loadData(2500) assert self.datahandler.get_dns_from_peerdb(permid) == ('68.108.115.221', 6881)
torrent["ignored_times"] = torrent["retried_times"] elif torrent["status"] == "dead": # dead if torrent["retried_times"] < self.retryThreshold: torrent["retried_times"] += 1 def tooMuchRetry(self, torrent): if (torrent["retried_times"] > self.retryThreshold): return True return False if __name__ == '__main__': from BaseLib.Core.CacheDB.sqlitecachedb import init as init_db, str2bin configure_dir = sys.argv[1] config = {} config['state_dir'] = configure_dir config['install_dir'] = '.' config['peer_icon_path'] = '.' init_db(config) t = TorrentChecking() t.start() t.join() infohash_str = 'TkFX5S4qd2DPW63La/VObgOH/Nc=' infohash = str2bin(infohash_str) del t t = TorrentChecking(infohash) t.start() t.join()