Ejemplo n.º 1
0
/****************************************************************
 * NAME: receiverFunc 
 *
 * DESCRIPTION: This is the function that takes care of servicing
 *              the first among the three threads spawned i.e.
 *              the receiver threads which does the following:
 *              i) If I am a LEADER approve join requests from 
 *                 member hosts
 *              ii) Receive heartbeats
 *              
 * PARAMETERS: NONE 
 *
 * RETURN:
 * (int) ZERO if success
 *       ERROR otherwise
 * 
 ****************************************************************/
int receiverFunc()
{

    funcEntry(logF, ipAddress, "receiverFunc");

    int rc = SUCCESS,                    // Return code
        numOfBytesRec,                   // Number of bytes received
        i_rc,                            // Temp RC
        op_code;                         // Operation code

    char recMsg[LONG_BUF_SZ],            // Received message
         tokenRecMsg[LONG_BUF_SZ],       // Received message without join op code 
         buffer[SMALL_BUF_SZ];           // Temp buffer

    struct sockaddr_in memberAddress;    // Address of host 
 
    struct hb_entry * recMsgStruct;      // Heart beat table that holds received message

    recMsgStruct = (struct hb_entry *) malloc(4*sizeof(struct hb_entry));

    /*
     * 1) Receive UDP packet
     * 2) Check operation code
     * 3) If JOIN message: 
     *    i) Extract message
     *    ii) Update heartbeat table
     * 4) Else
     *    i) Extract message 
     *    ii) Update heartbeat table
     */

    for(;;)
    {
        /////////
        // Step 1
        /////////
        memset(recMsg, '\0', LONG_BUF_SZ);
        
        // Debug
        printToLog(logF, "recMsg before recvUDP", recMsg);

        numOfBytesRec = recvUDP(recMsg, LONG_BUF_SZ, memberAddress);
        // Check if 0 bytes is received 
        if ( SUCCESS == numOfBytesRec )
        {
             sprintf(logMsg, "Number of bytes received is ZERO = %d", numOfBytesRec);
             printf("\n%s\n", logMsg);
             printToLog(logF, ipAddress, logMsg);
             continue;
        }

        // Debug
        printToLog(logF, "recMsg after recvUDP", recMsg);

        /////////
        // Step 2
        /////////
        i_rc = checkOperationCode(recMsg, &op_code, tokenRecMsg);
        if ( i_rc != SUCCESS ) 
        {
            printToLog(logF, ipAddress, "Unable to retrieve opcode");
            continue;
        }
        /////////
        // Step 3
        /////////
        if ( JOIN_OP_CODE == op_code )
        {
            sprintf(logMsg, "JOIN msg from %s", inet_ntop(AF_INET, &memberAddress.sin_addr, buffer, sizeof(buffer)));
            printToLog(logF, ipAddress, logMsg);
            ///////////
            // Step 3i
            ///////////
            
            // Debug. uncomment if req
            printToLog(logF, ipAddress, "\nBefore clear_temp_entry_table\n"); 

            clear_temp_entry_table(recMsgStruct);

            printToLog(logF, ipAddress, "\nAfter c_t_e_t\n");

            printToLog(logF, ipAddress, "\nbefore extract_msg\n");

            sprintf(logMsg, "Token received message before e_m: %s", tokenRecMsg);
            printToLog(logF, ipAddress, logMsg);

            recMsgStruct = extract_message(tokenRecMsg);

            printToLog(logF, ipAddress, "\nafter e_m\n");

            //printToLog(logF, ipAddress, "\nToken Received Message: %s", tokenRecMsg);

            sprintf(logMsg, "Token received message: %s", tokenRecMsg);
            printToLog(logF, ipAddress, logMsg);

            if ( NULL == recMsgStruct )
            {
                printToLog(logF, ipAddress, "Unable to extract message");
                continue;
            }
            ////////////
            // Step 3ii
            ////////////
            i_rc = update_table(recMsgStruct);
            if ( i_rc != SUCCESS )
            {
                 printToLog(logF, ipAddress, "Unable to update heart beat table");
                 continue;
            }

        } // End of if ( JOIN_OP_CODE == op_code )
        /////////
        // Step 4
        /////////
        else
        {
            //////////
            // Step 4i
            //////////
            clear_temp_entry_table(recMsgStruct);
            recMsgStruct = extract_message(recMsg);
            if ( NULL == recMsgStruct )
            {
                printToLog(logF, ipAddress, "Unable to extract message");
                continue;
            }
            ///////////
            // Step 4ii
            ///////////
            i_rc = update_table(recMsgStruct);
            if ( i_rc != SUCCESS )
            {
                 printToLog(logF, ipAddress, "Unable to update heart beat table");
                 continue;
            }
        } // End of else
    } // End of for(;;)

  rtn:
    funcExit(logF, ipAddress, "receiverFunc", rc);
    return rc;

} // End of receiverFunc()
Ejemplo n.º 2
0
 void BroadcastSock::pumpIn()
 {
     recvUDP();
 }