/**
 * Initializes this as a dynamic client
 */
bool DynamicClientInitializer::initializeClient()
{
    if ( !basicInit() )
    {
        LOG_ERR ( "Basic initialization failed" );
        return false;
    }

    LOG ( "Basic initialization done" );

    // Second: Creating an UDP socket, to send the NMS_INIT broadcast
    if ( !setupUdpSocket() )
    {
        return false;
    }

    // Send the NMS_INIT message, and wait for an acceptable response, or the occurrence of a timeout

    LOG ( "Sending UDP" );
    if ( !sendUdpMessage() )
    {
        if ( getLastErrorCode() != ErrorCodes::TIMEOUT )
        {
            LOG ( "Cannot send UDP message" );
            setLastErrorCode ( 888 );
        }
        else if ( getLastErrorCode() == ErrorCodes::TIMEOUT )
        {
            LOG ( "Timeout ..." );
        }
        return false;
    }

    if ( daemonFirstResponse == COMMAND_NOINIT ) // NOINIT ... unknown reasons
    {
        LOG ( "Not a valid response from daemon" );
        return false;
    }

    if ( !analyzeResponse ( daemonFirstResponse.c_str() ) )
    {
        LOG ( "Could not analyze the daemons response" );
        return false;
    }

    return true;
}
Esempio n. 2
0
int CUdpClient::sendMessage(const std::string &msg, std::string &answ)
{
    if (sendUdpMessage(m_sock, m_serverAddr, msg))
        return -1;

    // read
    CUdpMsgStore store;
    struct pollfd polling[1];

    polling[0].revents = 0;
    polling[0].events = POLLIN;
    polling[0].fd = m_sock;

    for (; poll(polling, 1, CUDPCLIENT_TIMEOUT) > 0; )
    {


        char buff[MTU_SIZE];
        // без проверки отправителя
        int buffLen = recv(m_sock, buff, MTU_SIZE, 0);
        if (buffLen < 0) return -1;

        if (buffLen > 0)
        {
            if (store.addPacket(buff, buffLen)) return -1;
        }
        else // buffLen == 0
        {
            int answLen = store.getMessage(answ);
            if (answLen > 0) answ.resize(answLen);

            return answLen;
        }
    }

    return -1; //timeout
}