def handle_PIECE(self, peer_protocol, msg_payload): print 'received piece from peer %r' % (peer_protocol.transport.getPeer()) peer_connection = self.get_peer_connection(peer_protocol) index = get_4_bytes_in_decimal(msg_payload[0:4]) begin = get_4_bytes_in_decimal(msg_payload[4:8]) block = msg_payload[8:] if not self.is_piece_valid(block, index): # request piece again piece_length = self.get_piece_length(piece_index) self.request_piece(peer_connection, index, 0, piece_length) peer_connection.piece_requested = index return print 'received piece index=%d' % index # update which piece was received in master control bitfield self.set_piece_received(index) # reset requested piece peer_connection.piece_requested = None self.write_to_file(index, begin, block) # check if there are more pieces left over if self.has_retrieved_all_pieces(): print 'retrieved all pieces' peer_connection.finished() else: peer_connection = self.get_peer_connection(peer_protocol) self.request_next_piece(peer_connection)
def parse_message_helper(messages, buffer): # JEFF - harder to confuse with msg_len if in all caps / constant # Could use reduce here to create the aggregator and then call out to # a separate function. Fun! _msg_length_size = 4 # check for message length if len(buffer) < _msg_length_size: return messages, buffer msg_len = get_4_bytes_in_decimal(buffer[0:_msg_length_size]) # keep alive message if msg_len == 0: messages.append(Message(id=ID_KEEPALIVE)) buffer = buffer[4:] return parse_message_helper(messages, buffer) # check for incomplete message if len(buffer[1:]) < msg_len: return messages, buffer # consume complete message msg_body = buffer[_msg_length_size : _msg_length_size + msg_len] id, payload = get_message_id_and_payload(msg_body) message = Message(msg_len=msg_len, id=id, payload=payload) messages.append(message) # remove parsed message and continue with the rest of the buffer buffer = buffer[_msg_length_size + msg_len:] return parse_message_helper(messages, buffer)
def parse_message_helper(messages, buffer): _msg_length_size = 4 # check for message length if len(buffer) < _msg_length_size: return messages, buffer msg_len = get_4_bytes_in_decimal(buffer[0:_msg_length_size]) # keep alive message if msg_len == 0: messages.append(Message(id=ID_KEEPALIVE)) buffer = buffer[4:] return parse_message_helper(messages, buffer) # check for incomplete message if len(buffer[1:]) < msg_len: return messages, buffer # consume complete message msg_body = buffer[_msg_length_size : _msg_length_size + msg_len] id, payload = get_message_id_and_payload(msg_body) message = Message(msg_len=msg_len, id=id, payload=payload) messages.append(message) # remove parsed message and continue with the rest of the buffer buffer = buffer[_msg_length_size + msg_len:] return parse_message_helper(messages, buffer)
def handle_HAVE(self, peer_protocol, msg_payload): piece_index = get_4_bytes_in_decimal(msg_payload) peer_connection = self.get_peer_connection(peer_protocol) peer_connection.bitfield[piece_index] = True