def setUp(self): self.config_path = tempfile.mkdtemp() config = {} config['state_dir'] = self.config_path config['install_dir'] = os.path.join('..','..') config['torrent_collecting_dir'] = self.config_path config['peer_icon_path'] = os.path.join(self.config_path,'peer_icons') config['superpeer'] = False sqlitecachedb.init(config, self.rawserver_fatalerrorfunc) secover1 = SecureOverlay.getInstance() secover1.resetSingleton() secover2 = SecureOverlay.getInstance() secover2.resetSingleton() self.peer1 = Peer(self,1234,secover1) self.peer2 = Peer(self,5678,secover2) self.peer1.start() self.peer2.start() self.wanted = False self.wanted2 = False self.got = False self.got2 = False self.first = True print >>sys.stderr,"test: setUp: peer1 permid is",show_permid_short(self.peer1.my_permid) print >>sys.stderr,"test: setUp: peer2 permid is",show_permid_short(self.peer2.my_permid) sleep(2) # let server threads start
def tryHolePunching(self): if DEBUG: print >> sys.stderr, "NatCheckMsgHandler: first element in peerlist", self.peerlist[ len(self.peerlist) - 1] print >> sys.stderr, "NatCheckMsgHandler: second element in peerlist", self.peerlist[ len(self.peerlist) - 2] holePunchingPort = random.randrange(3200, 4200, 1) holePunchingAddr = (self.holePunchingIP, holePunchingPort) peer1 = self.peerlist[len(self.peerlist) - 1] peer2 = self.peerlist[len(self.peerlist) - 2] request_id = str( show_permid_short(peer1[0]) + show_permid_short(peer2[0]) + str(random.randrange(0, 1000, 1))) self.udpConnect(peer1[0], request_id, holePunchingAddr) self.udpConnect(peer2[0], request_id, holePunchingAddr) # Register peerinfo on file self._file2.write("; ".join( (strftime("%Y/%m/%d %H:%M:%S"), "REQUEST", request_id, show_permid(peer1[0]), str(peer1[1]), str(peer1[2]), str(self._secure_overlay.get_dns_from_peerdb(peer1[0])), show_permid(peer2[0]), str(peer2[1]), str(peer2[2]), str(self._secure_overlay.get_dns_from_peerdb(peer2[0])), "\n"))) self._file2.flush() self.trav[request_id] = (None, None) thread.start_new_thread(coordinateHolePunching, (peer1, peer2, holePunchingAddr))
def handle_connection(self, exc, permid, selversion, locally_initiated): """ Called when overlay received a connection. Note that this method is only registered with OverlayApps when running as a crawler (determined by our public key). """ if exc: # connection lost if DEBUG: print >>sys.stderr, "crawler: overlay connection lost", show_permid_short(permid), exc print >>sys.stderr, repr(permid) elif selversion >= OLPROTO_VER_SEVENTH: # verify that we do not already have deadlines for this permid already_known = False for tup in self._initiator_deadlines: if tup[4] == permid: already_known = True break if not already_known: if DEBUG: print >>sys.stderr, "crawler: new overlay connection", show_permid_short(permid) print >>sys.stderr, repr(permid) for initiator_callback, frequency, accept_frequency in self._crawl_initiators: self._initiator_deadlines.append([0, frequency, accept_frequency, initiator_callback, permid, selversion, 0]) self._initiator_deadlines.sort() # Start sending crawler requests self._check_deadlines(False) else: if DEBUG: print >>sys.stderr, "crawler: new overlay connection (can not use version %d)" % selversion, show_permid_short(permid) print >>sys.stderr, repr(permid)
def _handleGETSUBS(self, permid, message, selversion): if selversion < OLPROTO_VER_FOURTEENTH: if DEBUG: print >> sys.stderr, "The peer that sent the GET_SUBS request has an old" \ "protcol version: this is strange. Dropping the msg" return False decoded = self._decodeGETSUBSMessage(message) if decoded is None: if DEBUG: print >> sys.stderr, "Error decoding a GET_SUBS message from %s" %\ utilities.show_permid_short(permid) return False if DEBUG: channel_id, infohash, languages = decoded bitmask = self._languagesUtility.langCodesToMask(languages) print >> sys.stderr, "%s, %s, %s, %s, %d, %d" % ( "RG", show_permid_short(permid), show_permid_short(channel_id), bin2str(infohash), bitmask, len(message)) # no synch on _listenersList since both this method # and the registerListener method are called by # the OLThread for listener in self._listenersList: listener.receivedSubsRequest(permid, decoded, selversion) return True
def setUp(self): self.config_path = tempfile.mkdtemp() config = {} config['state_dir'] = self.config_path config['install_dir'] = os.path.join('..', '..') config['torrent_collecting_dir'] = self.config_path config['peer_icon_path'] = os.path.join(self.config_path, 'peer_icons') config['superpeer'] = False sqlitecachedb.init(config, self.rawserver_fatalerrorfunc) secover1 = SecureOverlay.getInstance() secover1.resetSingleton() secover2 = SecureOverlay.getInstance() secover2.resetSingleton() self.peer1 = Peer(self, 1234, secover1) self.peer2 = Peer(self, 5678, secover2) self.peer1.start() self.peer2.start() self.wanted = False self.wanted2 = False self.got = False self.got2 = False self.first = True print >> sys.stderr, "test: setUp: peer1 permid is", show_permid_short( self.peer1.my_permid) print >> sys.stderr, "test: setUp: peer2 permid is", show_permid_short( self.peer2.my_permid) sleep(2) # let server threads start
def tryHolePunching(self): if DEBUG: print >> sys.stderr, time.asctime(),'-', "NatCheckMsgHandler: first element in peerlist", self.peerlist[len(self.peerlist)-1] print >> sys.stderr, time.asctime(),'-', "NatCheckMsgHandler: second element in peerlist", self.peerlist[len(self.peerlist)-2] holePunchingPort = random.randrange(3200, 4200, 1) holePunchingAddr = (self.holePunchingIP, holePunchingPort) peer1 = self.peerlist[len(self.peerlist)-1] peer2 = self.peerlist[len(self.peerlist)-2] request_id = str(show_permid_short(peer1[0]) + show_permid_short(peer2[0]) + str(random.randrange(0, 1000, 1))) self.udpConnect(peer1[0], request_id, holePunchingAddr) self.udpConnect(peer2[0], request_id, holePunchingAddr) # Register peerinfo on file self._file2.write("; ".join((strftime("%Y/%m/%d %H:%M:%S"), "REQUEST", request_id, show_permid(peer1[0]), str(peer1[1]), str(peer1[2]), str(self._secure_overlay.get_dns_from_peerdb(peer1[0])), show_permid(peer2[0]), str(peer2[1]), str(peer2[2]), str(self._secure_overlay.get_dns_from_peerdb(peer2[0])), "\n"))) self._file2.flush() self.trav[request_id] = (None, None) thread.start_new_thread(coordinateHolePunching, (peer1, peer2, holePunchingAddr))
def _handleGETSUBS(self,permid, message, selversion): if selversion < OLPROTO_VER_FOURTEENTH: if DEBUG: print >> sys.stderr, "The peer that sent the GET_SUBS request has an old" \ "protcol version: this is strange. Dropping the msg" return False decoded = self._decodeGETSUBSMessage(message) if decoded is None: if DEBUG: print >> sys.stderr, "Error decoding a GET_SUBS message from %s" %\ utilities.show_permid_short(permid) return False if DEBUG: channel_id, infohash, languages = decoded bitmask = self._languagesUtility.langCodesToMask(languages) print >> sys.stderr, "%s, %s, %s, %s, %d, %d" % ("RG", show_permid_short(permid), show_permid_short(channel_id), bin2str(infohash), bitmask, len(message)) # no synch on _listenersList since both this method # and the registerListener method are called by # the OLThread for listener in self._listenersList: listener.receivedSubsRequest(permid, decoded, selversion) return True
def setUp(self): print >>sys.stderr,"test: TestOverlayThreadingBridge.setUp()" secover1 = SecureOverlay.getInstance() secover1.resetSingleton() secover2 = SecureOverlay.getInstance() secover2.resetSingleton() overbridge1 = OverlayThreadingBridge() overbridge1.register_bridge(secover1,None) overbridge2 = OverlayThreadingBridge() overbridge2.register_bridge(secover2,None) self.peer1 = Peer(self,1234,overbridge1) self.peer2 = Peer(self,5678,overbridge2) self.peer1.start() self.peer2.start() self.wanted = False self.wanted2 = False self.got = False self.got2 = False self.first = True print >>sys.stderr,"test: setUp: peer1 permid is",show_permid_short(self.peer1.my_permid) print >>sys.stderr,"test: setUp: peer2 permid is",show_permid_short(self.peer2.my_permid) time.sleep(2) # let server threads start
def setUp(self): print >> sys.stderr, "test: TestOverlayThreadingBridge.setUp()" secover1 = SecureOverlay.getInstance() secover1.resetSingleton() secover2 = SecureOverlay.getInstance() secover2.resetSingleton() overbridge1 = OverlayThreadingBridge() overbridge1.register_bridge(secover1, None) overbridge2 = OverlayThreadingBridge() overbridge2.register_bridge(secover2, None) self.peer1 = Peer(self, 1234, overbridge1) self.peer2 = Peer(self, 5678, overbridge2) self.peer1.start() self.peer2.start() self.wanted = False self.wanted2 = False self.got = False self.got2 = False self.first = True print >> sys.stderr, "test: setUp: peer1 permid is", show_permid_short( self.peer1.my_permid) print >> sys.stderr, "test: setUp: peer2 permid is", show_permid_short( self.peer2.my_permid) time.sleep(2) # let server threads start
def _handleSUBS(self, permid, message, selversion): if selversion < OLPROTO_VER_FOURTEENTH: if DEBUG: print >> sys.stderr, "The peer that sent the SUBS request has an old" \ "protcol version: this is strange. Dropping the msg" return False decoded = self._decodeSUBSMessage(message) if decoded is None: if DEBUG: print >> sys.stderr, "Error decoding a SUBS message from %s" %\ utilities.show_permid_short(permid) return False channel_id, infohash, bitmask,contents = decoded #if no subtitle was requested drop the whole message if DEBUG: print >> sys.stderr, "%s, %s, %s, %s, %d, %d" % ("RS", show_permid_short(permid), show_permid_short(channel_id), bin2str(infohash), bitmask, len(message)) requestedSubs = self._checkRequestedSubtitles(channel_id,infohash,bitmask) if requestedSubs == 0: if DEBUG: print >> sys.stderr, SUBS_LOG_PREFIX + "Received a SUBS message that was not"\ " requested. Dropping" return False requestedSubsCodes = self._languagesUtility.maskToLangCodes(requestedSubs) #drop from the contents subtitles that where not requested for lang in contents.keys(): if lang not in requestedSubsCodes: del contents[lang] #remove the received subtitles from the requested callbacks = \ self._removeFromRequestedSubtitles(channel_id, infohash, bitmask) #the receiver does not need the bitmask tuple = channel_id, infohash, contents # no synch on _listenersList since both this method # and the registerListener method are called by # the OLThread for listener in self._listenersList: listener.receivedSubsResponse(permid, tuple, callbacks, selversion) return True
def handleRMetadata(self, sender_permid, channelCastMessage, fromQuery=False): ''' Handles the reception of rich metadata. Called when an "erniched" channelCastMessage (v14) is received. @param sender_permid: the PermId of the peer who sent the message @param channelCastMessage: the received message @return: None ''' metadataDTOs = self._splitChannelcastAndRichMetadataContents( channelCastMessage) if DEBUG: print >> sys.stderr, "Handling rich metadata from %s..." % show_permid_short( sender_permid) for md_and_have in metadataDTOs: md = md_and_have[0] havemask = md_and_have[1] vote = self.votecastDB.getVote(bin2str(md.channel), bin2str(self.my_permid)) if DEBUG: id = "RQ" if fromQuery else "R" print >> sys.stderr, "%s, %s, %s, %s, %d" % \ (id, md.channel, md.infohash, \ show_permid_short(sender_permid), md.timestamp) #format "R|S (R: received - S: sent), channel, infohash, sender|destination,metadataCreationTimestamp" # 30-06-2010: "RQ" as received from query # check if the record belongs to a channel # who we have "reported spam" (negative vote) if vote == -1: # if so, ignore the incoming record continue isUpdate = self.rmdDb.insertMetadata(md) self.peerHaveManager.newHaveReceived(md.channel, md.infohash, sender_permid, havemask) if isUpdate is not None: #retrieve the metadataDTO from the database in the case it is an update md = self.rmdDb.getMetadata(md.channel, md.infohash) self._notifyRichMetadata(md, isUpdate) # if I am a subscriber send immediately a GET_SUBS to the # sender if vote == 2: if DEBUG: print >> sys.stderr, "Subscribed to channel %s, trying to retrieve" \ "all subtitle contents" % (show_permid_short(md.channel),) self._getAllSubtitles(md)
def _subs_send_callback(self, exc, permid): ''' Called by the OLThread when a SUBS message is succesfully sent ''' if exc is not None: print >> sys.stderr, "Warning: Sending of SUBS message to %s failed: %s" % \ (show_permid_short(permid), str(exc)) else: if DEBUG: print >> sys.stderr, "SUBS message succesfully sent to %s" % show_permid_short(permid)
def olthread_request_help_connect_callback(self,exc,dns,permid,selversion): if exc is None: if DEBUG: print >> sys.stderr,"dlhelp: Coordinator sending to",show_permid_short(permid) ## Create message according to protocol version dlhelp_request = self.infohash self.overlay_bridge.send(permid, DOWNLOAD_HELP + dlhelp_request,self.olthread_request_help_send_callback) else: if DEBUG: print >> sys.stderr,"dlhelp: DOWNLOAD_HELP: error connecting to",show_permid_short(permid),exc self.olthread_remove_unreachable_helper(permid)
def _get_subs_connect_callback(self, exception, dns, permid, selversion, channel_id, infohash, bitmask, msgSentCallback, usrCallback): """ Called by the Overlay Thread when a connection with permid is established. Performs the actual action of sending a GET_SUBS request to the peer identified by permid. It is called by the OLThread when a connection with that peer is established. """ if exception is not None: if DEBUG: print >> sys.stderr, SUBS_LOG_PREFIX + \ "GET_SUBS not sent. Unable to connect to " + \ utilities.show_permid_short(permid) else: if (selversion > 0 and selversion < OLPROTO_VER_FOURTEENTH): msg = "GET_SUBS not send, the other peers had an old protocol version: %d" %\ selversion if DEBUG: print >> sys.stderr, msg raise SubtitleMsgHandlerException(msg) if DEBUG: print >> sys.stderr, SUBS_LOG_PREFIX + "sending GET_SUBS to " + \ utilities.show_permid_short(permid) try: message = self._createGETSUBSMessage(channel_id, infohash, bitmask) if DEBUG: # Format: # SS|SG, destination, channel, infohash, bitmask, size print >> sys.stderr, "%s, %s, %s, %s, %d, %d" % ( "SG", show_permid_short(permid), show_permid_short(channel_id), bin2str(infohash), bitmask, len(message)) self._overlay_bridge.send(permid, message, lambda exc, permid: \ self._sent_callback(exc,permid, channel_id, infohash, bitmask, msgSentCallback, usrCallback)) except Exception, e: print_exc() msg = "GET_SUBS not sent: %s" % str(e) raise SubtitleMsgHandlerException(e)
def _get_subs_connect_callback(self, exception, dns, permid, selversion, channel_id, infohash, bitmask, msgSentCallback, usrCallback): """ Called by the Overlay Thread when a connection with permid is established. Performs the actual action of sending a GET_SUBS request to the peer identified by permid. It is called by the OLThread when a connection with that peer is established. """ if exception is not None: if DEBUG: print >> sys.stderr, SUBS_LOG_PREFIX + \ "GET_SUBS not sent. Unable to connect to " + \ utilities.show_permid_short(permid) else: if (selversion > 0 and selversion < OLPROTO_VER_FOURTEENTH): msg = "GET_SUBS not send, the other peers had an old protocol version: %d" %\ selversion if DEBUG: print >> sys.stderr, msg raise SubtitleMsgHandlerException(msg) if DEBUG: print >> sys.stderr, SUBS_LOG_PREFIX + "sending GET_SUBS to " + \ utilities.show_permid_short(permid) try : message = self._createGETSUBSMessage(channel_id, infohash, bitmask) if DEBUG: # Format: # SS|SG, destination, channel, infohash, bitmask, size print >> sys.stderr, "%s, %s, %s, %s, %d, %d" % ("SG",show_permid_short(permid), show_permid_short(channel_id), bin2str(infohash),bitmask,len(message)) self._overlay_bridge.send(permid, message, lambda exc, permid: \ self._sent_callback(exc,permid, channel_id, infohash, bitmask, msgSentCallback, usrCallback)) except Exception,e: print_exc() msg = "GET_SUBS not sent: %s" % str(e) raise SubtitleMsgHandlerException(e)
def _subs_send_callback(self, exc, permid): ''' Called by the OLThread when a SUBS message is succesfully sent ''' if exc is not None: print >> sys.stderr, "Warning: Sending of SUBS message to %s failed: %s" % \ (show_permid_short(permid), str(exc)) else: if DEBUG: print >> sys.stderr, "SUBS message succesfully sent to %s" % show_permid_short( permid)
def _handleSUBS(self, permid, message, selversion): if selversion < OLPROTO_VER_FOURTEENTH: if DEBUG: print >> sys.stderr, "The peer that sent the SUBS request has an old" \ "protcol version: this is strange. Dropping the msg" return False decoded = self._decodeSUBSMessage(message) if decoded is None: if DEBUG: print >> sys.stderr, "Error decoding a SUBS message from %s" %\ utilities.show_permid_short(permid) return False channel_id, infohash, bitmask, contents = decoded #if no subtitle was requested drop the whole message if DEBUG: print >> sys.stderr, "%s, %s, %s, %s, %d, %d" % ( "RS", show_permid_short(permid), show_permid_short(channel_id), bin2str(infohash), bitmask, len(message)) requestedSubs = self._checkRequestedSubtitles(channel_id, infohash, bitmask) if requestedSubs == 0: if DEBUG: print >> sys.stderr, SUBS_LOG_PREFIX + "Received a SUBS message that was not"\ " requested. Dropping" return False requestedSubsCodes = self._languagesUtility.maskToLangCodes( requestedSubs) #drop from the contents subtitles that where not requested for lang in contents.keys(): if lang not in requestedSubsCodes: del contents[lang] #remove the received subtitles from the requested callbacks = \ self._removeFromRequestedSubtitles(channel_id, infohash, bitmask) #the receiver does not need the bitmask tuple = channel_id, infohash, contents # no synch on _listenersList since both this method # and the registerListener method are called by # the OLThread for listener in self._listenersList: listener.receivedSubsResponse(permid, tuple, callbacks, selversion) return True
def get_metadata_connect_callback(self,exc,dns,permid,selversion,infohash): if exc is None: if DEBUG: print >> sys.stderr,"metadata: Sending GET_METADATA to",show_permid_short(permid) ## Create metadata_request according to protocol version try: metadata_request = bencode(infohash) self.overlay_bridge.send(permid, GET_METADATA + metadata_request,self.get_metadata_send_callback) self.requested_torrents.add(infohash) except: print_exc() elif DEBUG: print >> sys.stderr,"metadata: GET_METADATA: error connecting to",show_permid_short(permid)
def handleRMetadata(self, sender_permid, channelCastMessage, fromQuery = False): ''' Handles the reception of rich metadata. Called when an "erniched" channelCastMessage (v14) is received. @param sender_permid: the PermId of the peer who sent the message @param channelCastMessage: the received message @return: None ''' metadataDTOs = self._splitChannelcastAndRichMetadataContents(channelCastMessage) if DEBUG: print >> sys.stderr, "Handling rich metadata from %s..." % show_permid_short(sender_permid) for md_and_have in metadataDTOs: md = md_and_have[0] havemask = md_and_have[1] vote = self.votecastDB.getVote(bin2str(md.channel), bin2str(self.my_permid)) if DEBUG: id = "RQ" if fromQuery else "R" print >> sys.stderr, "%s, %s, %s, %s, %d" % \ (id, md.channel, md.infohash, \ show_permid_short(sender_permid), md.timestamp) #format "R|S (R: received - S: sent), channel, infohash, sender|destination,metadataCreationTimestamp" # 30-06-2010: "RQ" as received from query # check if the record belongs to a channel # who we have "reported spam" (negative vote) if vote == -1: # if so, ignore the incoming record continue isUpdate = self.rmdDb.insertMetadata(md) self.peerHaveManager.newHaveReceived(md.channel,md.infohash,sender_permid,havemask) if isUpdate is not None: #retrieve the metadataDTO from the database in the case it is an update md = self.rmdDb.getMetadata(md.channel,md.infohash) self._notifyRichMetadata(md, isUpdate) # if I am a subscriber send immediately a GET_SUBS to the # sender if vote == 2: if DEBUG: print >> sys.stderr, "Subscribed to channel %s, trying to retrieve" \ "all subtitle contents" % (show_permid_short(md.channel),) self._getAllSubtitles(md)
def _sent_callback(self,exc,permid,channel_id,infohash,bitmask, msgSentCallback, usrCallback): """ Called by the OverlayThread after a GET_SUBS request has been sent. """ if exc is not None: if DEBUG: print >> sys.stderr, SUBS_LOG_PREFIX + "Unable to send GET_SUBS to: " + \ utilities.show_permid_short(permid) + ": " + exc else: if DEBUG: print >> sys.stderr, SUBS_LOG_PREFIX + "GET_SUBS sent to %s" % \ (utilities.show_permid_short(permid)) self._addToRequestedSubtitles(channel_id, infohash, bitmask, usrCallback) if msgSentCallback is not None: msgSentCallback(exc,permid,channel_id,infohash,bitmask)
def setUpMyListenSocket(self): self.dest_keypair = EC.gen_params(EC.NID_sect233k1) self.dest_keypair.gen_key() self.destpermid = str(self.dest_keypair.pub().get_der()) self.destport = 4810 # Start our server side, to with Tribler will try to connect self.destss = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.destss.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.destss.bind(('', self.destport)) self.destss.listen(1) print >>sys.stderr,"test: my permid",show_permid_short(self.mypermid) print >>sys.stderr,"test: his permid",show_permid_short(self.hispermid) print >>sys.stderr,"test: dest permid",show_permid_short(self.destpermid)
def OnSelChanged(self, event): index = self.messageTree.GetPyData(event.GetItem()) if index == None: return self.currentMessage = self.messagedata[index] owner_id = self.currentMessage['owner_id'] if owner_id == 0: owner_permid = self.session.get_permid() else: owner_permid = self.peer_db.getPermid(self.currentMessage['owner_id']) author = show_permid_short(owner_permid) if owner_permid == self.session.get_permid(): name = self.session.sessconfig.get('nickname', '') if name: author = name else: peer = self.peer_db.getPeer(owner_permid) if peer and peer['name']: author = peer['name'] self.messageOwner.SetLabel("author: "+author) self.message.SetValue(self.currentMessage['content']) event.Skip() if self.newBtn.GetLabel() == 'Post': self.message.SetBackgroundColour(self.rightPanel.GetBackgroundColour()) self.message.SetEditable(False) self.newBtn.SetLabel('New') self.newBtn._bitmap = ChessImages.getNewBitmap() self.newBtn.Refresh()
def caller(permid): if DEBUG: print >> sys.stderr, "doe: error connecting to", show_permid_short( permid), "for stop relaying" self.overlay_bridge.connect( permid, self.olthread_stop_relaying_connect_callback)
def handleMessage(self,permid,selversion,message): """ Called by NetworkThread """ if DEBUG: print >>sys.stderr,time.asctime(),'-', "olbridge: handleMessage",show_permid_short(permid),selversion,getMessageName(message[0]),currentThread().getName() def olbridge_handle_msg_func(): # Called by OverlayThread if DEBUG: print >>sys.stderr,time.asctime(),'-', "olbridge: handle_msg_func",show_permid_short(permid),selversion,getMessageName(message[0]),currentThread().getName() try: if self.olappsmsghandler is None: ret = True else: ret = self.olappsmsghandler(permid,selversion,message) except: print_exc() ret = False if ret == False: self.close(permid) self.tqueue.add_task(olbridge_handle_msg_func,0) return True
def do_help(self, infohash, torrent_data, permid): basename = binascii.hexlify( infohash) + '.torrent' # ignore .tribe stuff, not vital torrentfilename = os.path.join(self.helpdir, basename) tfile = open(torrentfilename, "wb") tfile.write(torrent_data) tfile.close() if DEBUG: print >> sys.stderr, "helpmsg: Got metadata required for helping", show_permid_short( permid) print >> sys.stderr, "helpmsg: torrent: ", torrentfilename tdef = TorrentDef.load(torrentfilename) if self.dlconfig is None: dscfg = DownloadStartupConfig() else: dscfg = DownloadStartupConfig(self.dlconfig) dscfg.set_coopdl_coordinator_permid(permid) dscfg.set_dest_dir(self.helpdir) # Start new download self.session.start_download(tdef, dscfg)
def handleConnection(self,exc,permid,selversion,locally_initiated,hisdns): """ Called by NetworkThread """ # called by SecureOverlay.got_auth_connection() or cleanup_admin_and_callbacks() if DEBUG: print >>sys.stderr,"olbridge: handleConnection",exc,show_permid_short(permid),selversion,locally_initiated,hisdns,currentThread().getName() def olbridge_handle_conn_func(): # Called by OverlayThread if DEBUG: print >>sys.stderr,"olbridge: handle_conn_func",exc,show_permid_short(permid),selversion,locally_initiated,hisdns,currentThread().getName() try: if hisdns: self.secover.add_peer_to_db(permid,hisdns,selversion) if self.olappsconnhandler is not None: # self.olappsconnhandler = OverlayApps.handleConnection self.olappsconnhandler(exc,permid,selversion,locally_initiated) except: print_exc() if isinstance(exc,CloseException): self.secover.update_peer_status(permid,exc.was_auth_done()) self.tqueue.add_task(olbridge_handle_conn_func,0)
def load_data(self, permid, name=None): if DEBUG: print >> sys.stderr, "mugmgr: load_data permid", show_permid_short( permid), "name", ` name ` filename = self.find_filename(permid, name) if filename is None: if DEBUG: print >> sys.stderr, "mugmgr: load_data: filename is None" return [None, None] try: f = open(filename, "rb") data = f.read(-1) f.close() except: if DEBUG: print >> sys.stderr, "mugmgr: load_data: Error reading" return [None, None] if data == '' or len(data) > ICON_MAX_SIZE: if DEBUG: print >> sys.stderr, "mugmgr: load_data: data 0 or too big", len( data) return [None, None] else: return [NETW_MIME_TYPE, data]
def udpConnect(self, permid, request_id, holePunchingAddr): if DEBUG: print >> sys.stderr, "NatCheckMsgHandler: request UDP connection" mh_data = request_id + ":" + holePunchingAddr[0] + ":" + str( holePunchingAddr[1]) if DEBUG: print >> sys.stderr, "NatCheckMsgHandler: udpConnect message is", mh_data try: mh_msg = bencode(mh_data) except: print_exc() if DEBUG: print >> sys.stderr, "NatCheckMsgHandler: error mh_data:", mh_data return False # send the message self.crawler.send_request(permid, CRAWLER_NATTRAVERSAL, mh_msg, frequency=0, callback=self.udpConnectCallback) if DEBUG: print >> sys.stderr, "NatCheckMsgHandler: request for", show_permid_short( permid), "sent to crawler"
def handle_crawler_reply(self, permid, selversion, channel_id, channel_data, error, message, request_callback): """ Received a CRAWLER_REPEX_QUERY reply. @param permid The Crawler permid @param selversion The overlay protocol version @param channel_id Identifies a CRAWLER_REQUEST/CRAWLER_REPLY pair @param error The error value. 0 indicates success. @param message The message payload @param request_callback Call this function one or more times to send the requests: request_callback(message_id, payload) """ if error: if DEBUG: print >> sys.stderr, "repexcrawler: handle_crawler_reply", error, message self._file.write("; ".join((strftime("%Y/%m/%d %H:%M:%S"), " REPLY", show_permid(permid), str(error), message, "\n"))) self._file.flush() else: if DEBUG: print >> sys.stderr, "repexcrawler: handle_crawler_reply", show_permid_short(permid), len(message), "bytes" # The message is pickled, which we will just write to file. # To make later parsing easier, we base64 encode it self._file.write("; ".join((strftime("%Y/%m/%d %H:%M:%S"), " REPLY", show_permid(permid), str(error), base64.b64encode(message), "\n"))) self._file.flush()
def natCheckReplySendCallback(self, exc, permid): if DEBUG: print >> sys.stderr, "NATCHECK_REPLY was sent to", show_permid_short( permid), exc if exc is not None: return False return True
def _after_send_reply(exc, permid): if DEBUG: if exc: print >> sys.stderr, "crawler: could not send request", show_permid_short(permid), exc # call the optional callback supplied with send_request if callback: callback(exc, permid)
def handleMessage(self, permid, selversion, message): """ Handle the received message and call the appropriate function to solve it. As there are multiple doe instances, one for each download/upload, the right doe instance must be found prior to making a call to it's methods. @param permid: The permid of the peer who sent the message @param selversion: selected Overlay protocol version @param message: The message received """ message_type = message[0] if DEBUG: print >> sys.stderr, "doe message handler: received the message", getMessageName( message_type), "from", show_permid_short(permid) # Call the appropriate function if message_type == RELAY_ACCEPTED: return self.got_relay_accepted(permid, message, selversion) elif message_type == RELAY_DROPPED: return self.got_relay_dropped(permid, message, selversion) elif message_type == DROPPED_PIECE: return self.got_dropped_piece(permid, message, selversion) elif message_type == PROXY_HAVE: return self.got_proxy_have(permid, message, selversion) elif message_type == PROXY_UNHAVE: return self.got_proxy_unhave(permid, message, selversion) elif message_type == PIECE_DATA: return self.got_piece_data(permid, message, selversion)
def load_data(self,permid,name=None): if DEBUG: print >>sys.stderr,"mugmgr: load_data permid",show_permid_short(permid),"name",`name` filename = self.find_filename(permid,name) if filename is None: if DEBUG: print >>sys.stderr,"mugmgr: load_data: filename is None" return [None,None] try: f = open(filename,"rb") data = f.read(-1) f.close() except: if DEBUG: print >>sys.stderr,"mugmgr: load_data: Error reading" return [None,None] if data == '' or len(data) > ICON_MAX_SIZE: if DEBUG: print >>sys.stderr,"mugmgr: load_data: data 0 or too big",len(data) return [None,None] else: return [NETW_MIME_TYPE,data]
def handle_crawler_reply(self, permid, selversion, channel_id, channel_data, error, message, request_callback): """ Received a CRAWLER_USEREVENTLOG_QUERY reply. @param permid The Crawler permid @param selversion The overlay protocol version @param channel_id Identifies a CRAWLER_REQUEST/CRAWLER_REPLY pair @param error The error value. 0 indicates success. @param message The message payload @param request_callback Call this function one or more times to send the requests: request_callback(message_id, payload) """ if error: if DEBUG: print >> sys.stderr, "usereventlogcrawler: handle_crawler_reply", error, message msg = "; ".join(['REPLY', show_permid(permid), str(error), str(message)]) self.__log(msg) else: if DEBUG: print >> sys.stderr, "usereventlogcrawler: handle_crawler_reply", show_permid_short(permid), len(message), "bytes" try: loaded_message = decode(message) except: print_exc() loaded_message = cPickle.loads(message) msg = "; ".join(['REPLY', show_permid(permid), str(error), str(loaded_message)]) self.__log(msg)
def singtest_good_resp_he_already_at_mutual(self): """ Test good FRIENDSHIP REQ message: We set the friendDB as if Tribler already sent an invite, we then send a REQ, which should give an automatic reply. """ peerdb = self.session.open_dbhandler(NTFY_PEERS) peer = {} peer['permid'] = self.mypermid peer['ip'] = '127.0.0.2' peer['port'] = 5000 peer['last_seen'] = 0 peerdb.addPeer(peer['permid'], peer, update_dns=True, commit=True) frienddb = self.session.open_dbhandler(NTFY_FRIENDS) frienddb.setFriendState(self.mypermid, FS_MUTUAL) print >> sys.stderr, "test: already invited, setting", show_permid_short( self.mypermid) self.usercallbackexpected = False self.subtest_good_friendship_req(RESP, mresp=1, expectreply=False) frienddb = self.session.open_dbhandler(NTFY_FRIENDS) fs = frienddb.getFriendState(self.mypermid) self.assert_(fs == FS_MUTUAL)
def OnNew(self, event): if self.newBtn.GetLabel() == 'New': self.messageOwner.SetLabel('') self.message.SetBackgroundColour(wx.WHITE) self.message.SetEditable(True) self.message.SetValue('') self.newBtn.SetLabel('Post') self.newBtn._bitmap = ChessImages.getSendBitmap() self.newBtn.Refresh() self.messageTree.SelectItem(self.root) else: content = self.message.GetValue() if len(content) > 500: dialog = wx.MessageDialog(None, 'There are only 500 characters allowed in a single message.', 'Message too long', wx.OK | wx.ICON_EXCLAMATION) dialog.ShowModal() return self.currentMessage = {} self.currentMessage['game_id'] = self.currentGame['game_id'] self.currentMessage['game_owner_id'] = self.currentGame['owner_id'] self.currentMessage['content'] = content self.gamecast.executeDiscuss(self.currentMessage) name = self.session.sessconfig.get('nickname', '') author = name if name else show_permid_short(self.session.get_permid()) self.messageOwner.SetLabel("author: "+author) self.message.SetBackgroundColour(self.rightPanel.GetBackgroundColour()) self.message.SetEditable(False) self.newBtn.SetLabel('New') self.newBtn._bitmap = ChessImages.getNewBitmap() self.newBtn.Refresh()
def handleMessage(self,permid,selversion,message): t = message[0] if t == GET_METADATA: # the other peer requests a torrent if DEBUG: print >> sys.stderr,"metadata: Got GET_METADATA",len(message),show_permid_short(permid) return self.send_metadata(permid, message, selversion) elif t == METADATA: # the other peer sends me a torrent if DEBUG: print >> sys.stderr,"metadata: Got METADATA",len(message),show_permid_short(permid),selversion, currentThread().getName() return self.got_metadata(permid, message, selversion) else: if DEBUG: print >> sys.stderr,"metadata: UNKNOWN OVERLAY MESSAGE", ord(t) return False
def handleMessage(self,permid,selversion,message): """ Called by NetworkThread """ if DEBUG: print >>sys.stderr,"olbridge: handleMessage",show_permid_short(permid),selversion,getMessageName(message[0]),currentThread().getName() def olbridge_handle_msg_func(): # Called by OverlayThread if DEBUG: print >>sys.stderr,"olbridge: handle_msg_func",show_permid_short(permid),selversion,getMessageName(message[0]),currentThread().getName() try: if self.olappsmsghandler is None: ret = True else: ret = self.olappsmsghandler(permid,selversion,message) except: print_exc() ret = False if ret == False: self.close(permid) self.tqueue.add_task(olbridge_handle_msg_func,0) return True
def handleConnection(self, exc, permid, selversion, locally_initiated, hisdns): """ Called by NetworkThread """ # called by SecureOverlay.got_auth_connection() or cleanup_admin_and_callbacks() if DEBUG: print >> sys.stderr, "olbridge: handleConnection", exc, show_permid_short( permid), selversion, locally_initiated, hisdns, currentThread( ).getName() def olbridge_handle_conn_func(): # Called by OverlayThread if DEBUG: print >> sys.stderr, "olbridge: handle_conn_func", exc, show_permid_short( permid ), selversion, locally_initiated, hisdns, currentThread( ).getName() try: if hisdns: self.secover.add_peer_to_db(permid, hisdns, selversion) if self.olappsconnhandler is not None: # self.olappsconnhandler = OverlayApps.handleConnection self.olappsconnhandler(exc, permid, selversion, locally_initiated) except: print_exc() if isinstance(exc, CloseException): self.secover.update_peer_status(permid, exc.was_auth_done()) self.tqueue.add_task(olbridge_handle_conn_func, 0)
def handle_crawler_request(self, permid, selversion, channel_id, message, reply_callback): """ Received a CRAWLER_UDPUNCTURE_QUERY request. @param permid The Crawler permid @param selversion The overlay protocol version @param channel_id Identifies a CRAWLER_REQUEST/CRAWLER_REPLY pair @param message The message payload @param reply_callback Call this function once to send the reply: reply_callback(payload [, error=123]) """ if DEBUG: print >> sys.stderr, "puncturecrawler: handle_crawler_request", show_permid_short( permid), message SimpleFileReporter.lock.acquire() try: if not self.reporter.file: try: self.reporter.file = open(self.reporter.path, 'a+b') except Exception, e: reply_callback(str(e), error=1) return file = self.reporter.file try: file.seek(0) result = ("%.2f CRAWL\n" % time.time()) + file.read() result = zlib.compress(result) reply_callback(result) file.truncate(0) except Exception, e: reply_callback(str(e), error=1)
def get_metadata_send_callback(self,exc,permid): if exc is not None: if DEBUG: print >> sys.stderr,"metadata: GET_METADATA: error sending to",show_permid_short(permid),exc pass else: pass
def handle_crawler_reply(self, permid, selversion, channel_id, channel_data, error, message, request_callback): """ Received a CRAWLER_DATABASE_QUERY reply. @param permid The Crawler permid @param selversion The overlay protocol version @param channel_id Identifies a CRAWLER_REQUEST/CRAWLER_REPLY pair @param error The error value. 0 indicates success. @param message The message payload @param request_callback Call this function one or more times to send the requests: request_callback(message_id, payload) """ if error: if DEBUG: print >> sys.stderr, "databasecrawler: handle_crawler_reply", error, message self._file.write("; ".join( (strftime("%Y/%m/%d %H:%M:%S"), " REPLY", show_permid(permid), str(error), message, "\n"))) self._file.flush() else: if DEBUG: print >> sys.stderr, "databasecrawler: handle_crawler_reply", show_permid_short( permid), len(message), "bytes" # 24/06/11 boudewijn: we are storing the received message in HEX format. unfortunately # this will make it unreadable in the text file, however, it will protect against pickle # security issues while still being compatible with both the secure (encode) and the # unsecure (pickle) crawlers on the client side. when parsing the logs care needs to be # taken when parsing the pickled data! self._file.write("; ".join( (strftime("%Y/%m/%d %H:%M:%S"), " REPLY", show_permid(permid), str(error), message.encode("HEX"), "\n"))) # self._file.write("; ".join((strftime("%Y/%m/%d %H:%M:%S"), " REPLY", show_permid(permid), str(error), str(cPickle.loads(message)), "\n"))) self._file.flush()
def got_proxy_unhave(self, permid, selversion, aggregated_string): """ Take the list of pieces the proxy sent and combine it with the numhaves in the piece picker @param permid: The permid of the node sending the message @param selversion: selected Overlay protocol version @param aggregated_string: a bitstring of available pieces built by the proxy based on UNHAVE messages it received """ if DEBUG: print >> sys.stderr, "doe: received a PROXY_UNHAVE message from", show_permid_short( permid) # TODO: make this test using a different approach # if len(aggregated_string) != self.num_pieces: # print >> sys.stderr, "doe: got_proxy_have: invalid payload in received PROXY_HAVE message. self.num_pieces=", self.num_pieces, "len(aggregated_string)=", len(aggregated_string) # Search for the SingleDownload object that has the connection with this peer if DEBUG: debug_found_connection = False for single_dl in self.proxydownloader.downloads: if permid == single_dl.proxy_permid: # If the connection is found, add the piece_list information to the d.have information single_dl.proxy_have = Bitfield( length=self.btdownloader.numpieces, bitstring=aggregated_string) if DEBUG: debug_found_connection = True break if DEBUG: if debug_found_connection: print >> sys.stderr, "doe: got_proxy_unhave: found a data connection for the received PROXY_UNHAVE" else: print >> sys.stderr, "doe: got_proxy_unhave: no data connection for the received PROXY_UNHAVE has been found"
def got_proxy_unhave(self, permid, selversion, aggregated_string): """ Take the list of pieces the proxy sent and combine it with the numhaves in the piece picker @param permid: The permid of the node sending the message @param selversion: selected Overlay protocol version @param aggregated_string: a bitstring of available pieces built by the proxy based on UNHAVE messages it received """ if DEBUG: print >> sys.stderr, "doe: received a PROXY_UNHAVE message from", show_permid_short(permid) # TODO: make this test using a different approach # if len(aggregated_string) != self.num_pieces: # print >> sys.stderr, "doe: got_proxy_have: invalid payload in received PROXY_HAVE message. self.num_pieces=", self.num_pieces, "len(aggregated_string)=", len(aggregated_string) # Search for the SingleDownload object that has the connection with this peer if DEBUG: debug_found_connection = False for single_dl in self.proxydownloader.downloads: if permid == single_dl.proxy_permid: # If the connection is found, add the piece_list information to the d.have information single_dl.proxy_have = Bitfield(length=self.btdownloader.numpieces, bitstring=aggregated_string) if DEBUG: debug_found_connection = True break if DEBUG: if debug_found_connection: print >> sys.stderr, "doe: got_proxy_unhave: found a data connection for the received PROXY_UNHAVE" else: print >> sys.stderr, "doe: got_proxy_unhave: no data connection for the received PROXY_UNHAVE has been found"
def olthread_stop_help_connect_callback(self,exc,dns,permid,selversion): if exc is None: ## Create message according to protocol version stop_request = self.infohash self.overlay_bridge.send(permid,STOP_DOWNLOAD_HELP + stop_request,self.olthread_stop_help_send_callback) elif DEBUG: print >> sys.stderr,"dlhelp: STOP_DOWNLOAD_HELP: error connecting to",show_permid_short(permid),exc
def handle_crawler_reply(self, permid, selversion, channel_id, channel_data, error, message, request_callback): """ Received a CRAWLER_REPEX_QUERY reply. @param permid The Crawler permid @param selversion The overlay protocol version @param channel_id Identifies a CRAWLER_REQUEST/CRAWLER_REPLY pair @param error The error value. 0 indicates success. @param message The message payload @param request_callback Call this function one or more times to send the requests: request_callback(message_id, payload) """ if error: if DEBUG: print >> sys.stderr, "repexcrawler: handle_crawler_reply", error, message self._file.write("; ".join( (strftime("%Y/%m/%d %H:%M:%S"), " REPLY", show_permid(permid), str(error), message, "\n"))) self._file.flush() else: if DEBUG: print >> sys.stderr, "repexcrawler: handle_crawler_reply", show_permid_short( permid), len(message), "bytes" # The message is pickled, which we will just write to file. # To make later parsing easier, we base64 encode it self._file.write("; ".join( (strftime("%Y/%m/%d %H:%M:%S"), " REPLY", show_permid(permid), str(error), base64.b64encode(message), "\n"))) self._file.flush()
def got_conn_outgoing_conns_callback(self,exc,permid,selversion,locally_initiated): print >> sys.stderr,"test: got_conn_outgoing_conns_callback",exc,show_permid_short(permid) self.assert_(exc is None) self.assert_(permid == self.peer2.my_permid) self.assert_(selversion == OLPROTO_VER_CURRENT) self.assert_(locally_initiated) self.got = True
def OnTimer(self, event = None): if DEBUG: print >> sys.stderr,"dlhelperframe: ON TIMER" peer_db = self.utility.session.open_dbhandler(NTFY_PEERS) self.lock.acquire() realhelpers = peer_db.getPeers(self.download_state.get_coopdl_helpers(), ['permid', 'name', 'ip']) self.lock.release() shownhelpers = self.rightListCtl.getFriends() removehelpers = [] removeitems = [] for i, shown in enumerate(shownhelpers): found = False for real in realhelpers: if real['permid'] == shown['permid']: found = True break if not found: if DEBUG: print >>sys.stderr,"dlhelperframe: Helper",shown['name'],show_permid_short(shown['permid']),"could not be reached apparently, removing" removehelpers.append(shown) removeitems.append(i) self.rightListCtl.removeFriends(removeitems) self.leftListCtl.addFriends(removehelpers)
def udpConnectReplySendCallback(self, exc, permid): if DEBUG: print >>sys.stderr, "NATTRAVERSAL_REPLY was sent to", show_permid_short(permid), exc if exc is not None: return False return True
def query_initiator(self, permid, selversion, request_callback): """ Established a new connection. Send a CRAWLER_DATABASE_QUERY request. @param permid The Tribler peer permid @param selversion The oberlay protocol version @param request_callback Call this function one or more times to send the requests: request_callback(message_id, payload) """ if DEBUG: print >>sys.stderr, "databasecrawler: query_initiator", show_permid_short(permid) sql = [] if selversion >= OLPROTO_VER_SEVENTH: sql.extend(("SELECT 'peer_count', count(*) FROM Peer", "SELECT 'torrent_count', count(*) FROM Torrent")) if selversion >= OLPROTO_VER_ELEVENTH: sql.extend(("SELECT 'my_subscriptions', count(*) FROM VoteCast where voter_id='" + show_permid(permid) + "' and vote=2", "SELECT 'my_negative_votes', count(*) FROM VoteCast where voter_id='" + show_permid(permid) + "' and vote=-1", "SELECT 'my_channel_files', count(*) FROM ChannelCast where publisher_id='" + show_permid(permid) + "'", "SELECT 'all_subscriptions', count(*) FROM VoteCast where vote=2", "SELECT 'all_negative_votes', count(*) FROM VoteCast where vote=-1")) # if OLPROTO_VER_EIGHTH <= selversion <= 11: # sql.extend(("SELECT 'moderations_count', count(*) FROM ModerationCast")) # if selversion >= OLPROTO_VER_EIGHTH: # sql.extend(("SELECT 'positive_votes_count', count(*) FROM Moderators where status=1", # "SELECT 'negative_votes_count', count(*) FROM Moderators where status=-1")) request_callback(CRAWLER_DATABASE_QUERY, ";".join(sql), callback=self._after_request_callback)
def handle_crawler_reply(self, permid, selversion, channel_id, channel_data, error, message, request_callback): """ Received a CRAWLER_DATABASE_QUERY reply. @param permid The Crawler permid @param selversion The overlay protocol version @param channel_id Identifies a CRAWLER_REQUEST/CRAWLER_REPLY pair @param error The error value. 0 indicates success. @param message The message payload @param request_callback Call this function one or more times to send the requests: request_callback(message_id, payload) """ if error: if DEBUG: print >> sys.stderr, "databasecrawler: handle_crawler_reply", error, message self._file.write("; ".join((strftime("%Y/%m/%d %H:%M:%S"), " REPLY", show_permid(permid), str(error), message, "\n"))) self._file.flush() else: if DEBUG: print >> sys.stderr, "databasecrawler: handle_crawler_reply", show_permid_short(permid), len(message), "bytes" # 24/06/11 boudewijn: we are storing the received message in HEX format. unfortunately # this will make it unreadable in the text file, however, it will protect against pickle # security issues while still being compatible with both the secure (encode) and the # unsecure (pickle) crawlers on the client side. when parsing the logs care needs to be # taken when parsing the pickled data! self._file.write("; ".join((strftime("%Y/%m/%d %H:%M:%S"), " REPLY", show_permid(permid), str(error), message.encode("HEX"), "\n"))) # self._file.write("; ".join((strftime("%Y/%m/%d %H:%M:%S"), " REPLY", show_permid(permid), str(error), str(cPickle.loads(message)), "\n"))) self._file.flush()
def udpConnectReplySendCallback(self, exc, permid): if DEBUG: print >> sys.stderr, "NATTRAVERSAL_REPLY was sent to", show_permid_short( permid), exc if exc is not None: return False return True
def get_metadata_send_callback(self, exc, permid): if exc is not None: if DEBUG: print >> sys.stderr, "metadata: GET_METADATA: error sending to", show_permid_short( permid), exc pass else: pass
def olbridge_connect_dns_callback(cexc,cdns,cpermid,cselver): # Called by network thread if DEBUG: print >>sys.stderr,"olbridge: connect_dns_callback",cexc,cdns,show_permid_short(cpermid),cselver olbridge_connect_dns_callback_lambda = lambda:callback(cexc,cdns,cpermid,cselver) self.add_task(olbridge_connect_dns_callback_lambda,0)
def _sent_callback(self, exc, permid, channel_id, infohash, bitmask, msgSentCallback, usrCallback): """ Called by the OverlayThread after a GET_SUBS request has been sent. """ if exc is not None: if DEBUG: print >> sys.stderr, SUBS_LOG_PREFIX + "Unable to send GET_SUBS to: " + \ utilities.show_permid_short(permid) + ": " + exc else: if DEBUG: print >> sys.stderr, SUBS_LOG_PREFIX + "GET_SUBS sent to %s" % \ (utilities.show_permid_short(permid)) self._addToRequestedSubtitles(channel_id, infohash, bitmask, usrCallback) if msgSentCallback is not None: msgSentCallback(exc, permid, channel_id, infohash, bitmask)
def receivedSubsRequest(self, permid, request, selversion): """ Reads a received GET_SUBS message and possibly sends a response. @param permid: the permid of the sender of the GET_SUBS message @param request: a tuple made of channel_id, infohash, language code @param selversion: the protocol version of the requesting peer @return: False if the message had something wrong. (a return value of False makes the caller close the connection). Otherwise True """ assert self.registered, SUBS_LOG_PREFIX + "Handler not yet registered" channel_id, infohash, languages = request #happily unpacking #diction {lang : Subtitle} allSubtitles = self.subtitlesDb.getAllSubtitles(channel_id, infohash) contentsList = {} #{langCode : path} #for each requested language check if the corresponding subtitle #is available for lang in sorted(languages): if lang in allSubtitles.keys(): if allSubtitles[lang].subtitleExists(): content = self._readSubContent(allSubtitles[lang].path) if content is not None: contentsList[lang] = content else: if DEBUG: print >> sys.stderr, SUBS_LOG_PREFIX + "File not available for channel %s, infohash %s, lang %s" % \ (show_permid_short(channel_id), bin2str(infohash),lang) self.subtitlesDb.updateSubtitlePath(channel_id,infohash,lang,None) else: if DEBUG: print >> sys.stderr, SUBS_LOG_PREFIX + "Subtitle not available for channel %s, infohash %s, lang %s" % \ (show_permid_short(channel_id), bin2str(infohash),lang) if len(contentsList) == 0: #pathlist is empty if DEBUG: print >> sys.stderr, SUBS_LOG_PREFIX + "None of the requested subtitles were available. No answer will be sent to %s" % \ show_permid_short(permid) return True return self._subsMsgHndlr.sendSubtitleResponse(permid, (channel_id,infohash,contentsList), selversion)
def _after_send_reply(exc, permid): if DEBUG: if exc: print >> sys.stderr, "crawler: could not send request", show_permid_short( permid), exc # call the optional callback supplied with send_request if callback: callback(exc, permid)
def _after_request_callback(self, exc, permid): """ Called by the Crawler with the result of the request_callback call in the query_initiator method. """ if not exc: if DEBUG: print >>sys.stderr, "channelcrawler: request send to", show_permid_short(permid) self._file.write("; ".join((strftime("%Y/%m/%d %H:%M:%S"), "REQUEST", show_permid(permid), "\n"))) self._file.flush()
def get_metadata_connect_callback(self, exc, dns, permid, selversion, infohash): if exc is None: if DEBUG: print >> sys.stderr, "metadata: Sending GET_METADATA to", show_permid_short( permid) ## Create metadata_request according to protocol version try: metadata_request = bencode(infohash) self.overlay_bridge.send(permid, GET_METADATA + metadata_request, self.get_metadata_send_callback) self.requested_torrents.add(infohash) except: print_exc() elif DEBUG: print >> sys.stderr, "metadata: GET_METADATA: error connecting to", show_permid_short( permid)