Exemplo n.º 1
0
def AcceptTestClients(svr):

    print('INFO:', 'Accepting clients\' connection...')

    conn, addr = svr.accept()

    if conn == None:
        raise RuntimeError('Failed to accept client\'s connection.')

    totalClient = st.SocketRecv_uint64(conn)
    clientI = st.SocketRecv_uint64(conn)
    if totalClient <= 0:
        raise RuntimeError('totalClient', totalClient, 'out of range.')
    if clientI >= totalClient:
        raise RuntimeError('ClientI', clientI, 'out of range.')

    connArr = [None for i in range(0, totalClient)]
    connArr[clientI] = conn

    print('INFO:', 'Test client', clientI, 'is connected from', addr)

    for i in range(0, totalClient - 1):
        conn, addr = svr.accept()

        if conn == None:
            raise RuntimeError('Failed to accept client\'s connection!')

        totalClient = st.SocketRecv_uint64(conn)
        clientI = st.SocketRecv_uint64(conn)

        if totalClient != len(connArr):
            raise RuntimeError('Total number of clients said by client',
                               clientI, 'doesn\'t match', len(connArr),
                               'said by first client.')
        if clientI >= totalClient:
            raise RuntimeError('ClientI', clientI, 'out of range.')
        if connArr[clientI] != None:
            raise RuntimeError('Client', clientI, 'is already connected.')

        connArr[clientI] = conn

        print('INFO:', 'Test client', clientI, 'is connected from', addr)

    return connArr
Exemplo n.º 2
0
def RecvNumOfNode(conn, maxNumOfNode):

    numOfNode = st.SocketRecv_uint64(conn)

    if not (1 <= numOfNode and numOfNode <= maxNumOfNode):
        st.SocketSendPack(conn, 'Err')
        raise RuntimeError('The total number of node given (i.e. ' +
                           numOfNode + ') is out of range!')
    else:
        st.SocketSendPack(conn, 'OK')

    return numOfNode