Exemplo n.º 1
0
def message_received(self, length, msg):
    try:
        message = None

        if length == 0:
            message = protocol.parse_keep_alive(msg)
            self._messages.append(message)
            return
        
        id = msg[0]
        
        if not self.ready() and id != protocol.BITFIELD:
            self._haves = bitfield.Bitfield(self._pieces)
        elif self.ready() and id == protocol.BITFIELD:
            raise ValueError(\
                "Already received BITFIELD from peer %s" % self)

        if id == protocol.CHOKE:
            message = protocol.parse_choke(msg)
            self._choked = True
        elif id == protocol.UNCHOKE:
            message = protocol.parse_unchoke(msg)
            self._choked = False
        elif id == protocol.INTERESTED:
            message = protocol.parse_interested(msg)
            self._interested = True
        elif id == protocol.NOT_INTERESTED:
            message = protocol.parse_not_interested(msg)
            self._interested = False
        elif id == protocol.HAVE:
            message = protocol.parse_have(msg)
            self._haves.set(message.piece)
        elif id == protocol.BITFIELD:
            message = protocol.parse_bitfield(msg, self._pieces)
            self._haves = message.bitfield
        elif id == protocol.REQUEST:
            message = protocol.parse_request(msg)
            with self._requests:
                self._requests.append(message)
        elif id == protocol.PIECE:
            message = protocol.parse_piece(msg)
        elif id == protocol.CANCEL:
            message = protocol.parse_cancel(msg)
        elif id == protocol.PORT:
            # Not supported. Ignore.
            pass
        else:
            raise ValueError(\
                "Message with unknown id %d received" % id)

        self._messages.append(message)
    except(ValueError, TypeError, IndexError, struct.error) as err:
        pass
Exemplo n.º 2
0
    def message_received(self, length, msg):
        try:
            if length == 0:
                protocol.parse_keep_alive(msg)
                return

            id = msg[0]

            if not self.ready() and id != protocol.BITFIELD:
                self._haves = bitfield.Bitfield(self._pieces)
            elif self.ready() and id == protocol.BITFIELD:
                raise ValueError(\
                    "Already received BITFIELD from peer %s" % self)

            if id == protocol.CHOKE:
                # If we are downloading, stop - DONE
                protocol.parse_choke(msg)
                self._choked = True
                del self._outstanding_requests[:]
            elif id == protocol.UNCHOKE:
                # If we are interested start downloading - DONE
                protocol.parse_unchoke(msg)
                self._choked = False
                self._request_piece()
            elif id == protocol.INTERESTED:
                protocol.parse_interested(msg)
                self._interested = True
                self._manager.interested_received(self)
            elif id == protocol.NOT_INTERESTED:
                protocol.parse_not_interested(msg)
                self._interested = False
            elif id == protocol.HAVE:
                # Check with coordinator if we are interested - DONE
                have = protocol.parse_have(msg)
                self._haves.set(have.piece)
                self.interested(self._manager.have_received(have.piece))
            elif id == protocol.BITFIELD:
                # Only receive bitfield once after handshake - DONE
                # Check with coordinator if we are interested - DONE
                field = protocol.parse_bitfield(msg, self._pieces)
                self._haves = field.bitfield
                self.interested(\
                    self._manager.bitfield_received(field.bitfield))
            elif id == protocol.REQUEST:
                req = protocol.parse_request(msg)
                if not self._choking and self._interested:
                    piece = self._manager.request_received(req.index)
                    if piece:
                        data = piece.block(req.offset, req.length)
                        self.piece(req.index, req.offset, data)
                # If we are choking this peer ignore - DONE
                # Else get piece from storage and send via out - DONE
            elif id == protocol.PIECE:
                piece = protocol.parse_piece(msg)
                #self.task.piece_received(piece, self._request)
                self._piece(piece)
                self._request_piece()
                # Tell coordinator we received a piece - DONE
            elif id == protocol.CANCEL:
                cancel = protocol.parse_cancel(msg)
                self._out.cancel_piece(cancel.index, \
                                           cancel.offset, cancel.length)
            elif id == protocol.PORT:
                # Not supported. Ignore.
                pass
            else:
                raise ValueError(\
                    "Message with unknown id %d received" % id)
        except (ValueError, TypeError, IndexError, struct.error) as err:
            # Invalid message recevied from peer. Close connection?
            _logger.info(\
                "Invalid message recevied from peer %s: %s" % (self, err))
            #raise
            self._request_piece()
Exemplo n.º 3
0
    def message_received(self, length, msg):
        try:
            if length == 0:
                protocol.parse_keep_alive(msg)
                return

            id = msg[0]

            if not self.ready() and id != protocol.BITFIELD:
                self._haves = bitfield.Bitfield(self._pieces)
            elif self.ready() and id == protocol.BITFIELD:
                raise ValueError(\
                    "Already received BITFIELD from peer %s" % self)

            if id == protocol.CHOKE:
                # If we are downloading, stop - DONE
                protocol.parse_choke(msg)
                self._choked = True
                del self._outstanding_requests[:]
            elif id == protocol.UNCHOKE:
                # If we are interested start downloading - DONE
                protocol.parse_unchoke(msg)
                self._choked = False
                self._request_piece()
            elif id == protocol.INTERESTED:
                protocol.parse_interested(msg)
                self._interested = True
                self._manager.interested_received(self)
            elif id == protocol.NOT_INTERESTED:
                protocol.parse_not_interested(msg)
                self._interested = False
            elif id == protocol.HAVE:
                # Check with coordinator if we are interested - DONE
                have = protocol.parse_have(msg)
                self._haves.set(have.piece)
                self.interested(self._manager.have_received(have.piece))
            elif id == protocol.BITFIELD:
                # Only receive bitfield once after handshake - DONE
                # Check with coordinator if we are interested - DONE
                field = protocol.parse_bitfield(msg, self._pieces)
                self._haves = field.bitfield
                self.interested(\
                    self._manager.bitfield_received(field.bitfield))
            elif id == protocol.REQUEST:
                req = protocol.parse_request(msg)
                if not self._choking and self._interested:
                    piece = self._manager.request_received(req.index)
                    if piece:
                        data = piece.block(req.offset, req.length)
                        self.piece(req.index, req.offset, data)
                # If we are choking this peer ignore - DONE
                # Else get piece from storage and send via out - DONE
            elif id == protocol.PIECE:
                piece = protocol.parse_piece(msg)
                #self.task.piece_received(piece, self._request)
                self._piece(piece)
                self._request_piece()
                # Tell coordinator we received a piece - DONE
            elif id == protocol.CANCEL:
                cancel = protocol.parse_cancel(msg)
                self._out.cancel_piece(cancel.index, \
                                           cancel.offset, cancel.length)
            elif id == protocol.PORT:
                # Not supported. Ignore.
                pass
            else:
                raise ValueError(\
                    "Message with unknown id %d received" % id)
        except(ValueError, TypeError, IndexError, struct.error) as err:
            # Invalid message recevied from peer. Close connection?
            _logger.info(\
                "Invalid message recevied from peer %s: %s" % (self, err))
            #raise
            self._request_piece()