Exemplo n.º 1
0
def _get_all_Msgs(sock: socket.socket):
    """ Get all the prepared messages from socket """

    all_msgs = []
    blocking_state = sock.getblocking()
    sock.setblocking(False)

    while True:
        try:
            msg_type, fingerprint, body = _receive_and_decode_Msg(sock)
            all_msgs.append({
                'msg_type': msg_type,
                'fingerprint': fingerprint,
                'body': body
            })
        except BlockingIOError:
            sock.setblocking(blocking_state)
            return all_msgs
Exemplo n.º 2
0
def _receive_and_decode_Msg(sock: socket.socket):
    """ Receive bytes and parse to a message(message type, fingerprint(SHA256 of the task), body) """
    def _get_body_content_one():
        body_bytes = int.from_bytes(_recv_exact_n_bytes(sock, 8), 'big')
        return bytes.decode(_recv_exact_n_bytes(sock, body_bytes), 'utf-8')

    msg_type = _MsgType(int.from_bytes(_recv_exact_n_bytes(sock, 1), 'big'))
    blocking_state = sock.getblocking()
    sock.setblocking(True)

    fingerprint = None

    if msg_type == _MsgType.HeartBeat or msg_type == _MsgType.HeartBeat_Res:
        body = {'slave_id': _get_body_content_one()}
    else:
        fingerprint = hex(int.from_bytes(sock.recv(32), 'big'))[2:]
        body = dict()

        if msg_type == _MsgType.AllTask:
            body['mapFunc'] = _get_body_content_one()
            body['reduceFunc'] = _get_body_content_one()
            body['file'] = json.loads(_get_body_content_one())

        elif msg_type == _MsgType.MapTask:
            body['mapFunc'] = _get_body_content_one()
            body['file'] = json.loads(_get_body_content_one())

        elif msg_type == _MsgType.ReduceTask:
            body['reduceFunc'] = _get_body_content_one()
            body['file'] = json.loads(_get_body_content_one())

        elif msg_type == _MsgType.MapTask_Res or msg_type == _MsgType.ReduceTask_Res or msg_type == _MsgType.AllTask_Res:
            body['result'] = json.loads(_get_body_content_one())

    sock.setblocking(blocking_state)

    return msg_type, fingerprint, body