'Processes an incoming packet as a receiver.
:param packet: The incoming packet dictionary.'

def _handle_packet_as_receiver(self, session, packet):

    if (packet['opcode'] == OPCODE_OACK):
        if (session.last_received_packet is None):
            if (session.block_size != packet['options']['blksize']):
                msg = ('%s    OACK    blksize    mismatch:    %s    !=    %s    (expected)' % (session, session.block_size, packet['options']['blksize']))
                self._logger.error(msg)
                self._handle_error(session, 0, error_msg=msg)
                return
            if (session.timeout != packet['options']['timeout']):
                msg = ('%s    OACK    timeout    mismatch:    %s    !=    %s    (expected)' % (session, session.timeout, packet['options']['timeout']))
                self._logger.error(msg)
                self._handle_error(session, 0, error_msg=msg)
                return
            session.file_size = packet['options']['tsize']
            session.checksum = packet['options']['checksum']
            if (session.request == OPCODE_RRQ):
                self._send_ack_packet(session, session.block_number)
                session.block_number += 1
                session.file_data = ''
        else:
            self._logger.error(u'%s    Got    OPCODE    %s    which    is    not    expected', session, packet['opcode'])
            self._handle_error(session, 4)
        return
    if (packet['opcode'] != OPCODE_DATA):
        self._logger.error(u'%s    Got    OPCODE    %s    while    expecting    %s', session, packet['opcode'], OPCODE_DATA)
        self._handle_error(session, 4)
        return
    self._logger.debug(u'%s    Got    data,    #block    =    %s    size    =    %s', session, packet['block_number'], len(packet['data']))
    if (packet['block_number'] < session.block_number):
        self._logger.warn(u'%s    ignore    old    block    number    DATA    %s    <    %s', session, packet['block_number'], session.block_number)
        return
    if (packet['block_number'] != session.block_number):
        msg = ('%s    Got    ACK    with    block#    %s    while    expecting    %s' % (session, packet['block_number'], session.block_number))
        self._logger.error(msg)
        self._handle_error(session, 0, error_msg=msg)
        return
    session.file_data += packet['data']
    self._send_ack_packet(session, session.block_number)
    session.block_number += 1
    if (len(packet['data']) < session.block_size):
        self._logger.info(u'%s    transfer    finished.    checking    data    integrity...', session)
        if (session.file_size != len(session.file_data)):
            self._logger.error(u"%s    file    size    %s    doesn't    match    expectation    %s", session, len(session.file_data), session.file_size)
            session.is_failed = True
            return
        data_checksum = b64encode(sha1(session.file_data).digest())
        if (session.checksum != data_checksum):
            self._logger.error(u"%s    file    checksum    %s    doesn't    match    expectation    %s", session, data_checksum, session.checksum)
            session.is_failed = True
            return
        session.is_done = True
