def handle_bitfield(self, bitfield): logging.debug('handle_bitfield - %s - %s' % (self.ip, bitfield.bitfield)) self.bit_field = bitfield.bitfield if self.is_choking() and not self.state['am_interested']: interested = message.Interested().to_bytes() self.send_to_peer(interested) self.state['am_interested'] = True
def handle_have(self, have): logging.debug('handle_have - ip: %s - piece: %s' % (self.ip, have.piece_index)) self.bit_field[have.piece_index] = True if self.is_choking() and not self.state['am_interested']: interested = message.Interested().to_bytes() self.send_to_peer(interested) self.state['am_interested'] = True
def parseNonHandshakeMessage(data,sock): bytestring = data if (bytestring[0:4] == '\x00\x00\x00\x00'): # Its a Keep Alive message # message_obj = message.KeepAlive(response=bytestring) else: message_obj = { 0: lambda: message.Choke(response=bytestring), 1: lambda: message.Unchoke(response=bytestring), 2: lambda: message.Interested(response=bytestring), 3: lambda: message.Interested(response=bytestring), 4: lambda: message.Have(response=bytestring), 5: lambda: message.Bitfield(response=bytestring), 6: lambda: message.Request(response=bytestring), 7: lambda: message.Piece(response=bytestring), 8: lambda: message.Cancel(response=bytestring), 9: lambda: message.Port(response=bytestring), }[ struct.unpack('!b',data[4])[0] ]() # The 5th byte in 'data' is the message type/id process_message(message_obj,sock)
def handle_bitfield(self, bitfield): """ :type bitfield: message.BitField """ logging.debug('handle_bitfield - %s' % self.ip) self.bit_field = bitfield.bitfield if self.is_choking(): interested = message.Interested().to_bytes() self.send_to_peer(interested) self.state['am_interested'] = True
def handle_have(self, have): """ :type have: message.Have """ logging.debug('handle_have - %s' % self.ip) self.bit_field[have.piece_index] = True if self.is_choking(): interested = message.Interested().to_bytes() self.send_to_peer(interested) self.state['am_interested'] = True
def parseHandshake(sock,data,torrentObj): # If the Info Hash matches the torrent's info hash, add the peer to the successful handshake set #print "in parseHandshake" handshake_response_data = message.Handshake(data) handshake_info_hash = handshake_response_data.info_hash #print "peer's infohash" + repr(handshake_info_hash) #print "our infohash" + repr(torrentObj.info_hash) if handshake_info_hash == torrentObj.info_hash: handshaken.append(sock) print "the infohash has matched" # Check if Should we be interested or not k=message.Interested() print "interested message send by us " +repr(k) #print "lalal" #y=raw_input("there?") sock.socket.send(str(k)) sock.bufferRead += data[68:] else : print "not matched" return
def handle_have(self, have): self.bit_field[have.piece_index] = True if self.is_choking() and not self.state['am_interested']: interested = message.Interested().to_bytes() self.send_to_peer(interested) self.state['am_interested'] = True