void clMsgQueueCkptDataUnmarshal(ClMsgQueueCkptDataT *qCkptData, const ClCachedCkptDataT *inData)
{
    ClUint8T *copyData;
    ClUint32T network_byte_order;

    saNameCopy(&qCkptData->qName, &inData->sectionName);
    qCkptData->qAddress = inData->sectionAddress.iocPhyAddress;

    copyData = inData->data;

    memcpy(&network_byte_order,copyData, sizeof(ClUint32T));
    qCkptData->qServerAddress.nodeAddress = (ClUint32T) ntohl((ClUint32T)network_byte_order);
    copyData = copyData + sizeof(ClUint32T);

    memcpy(&network_byte_order,copyData, sizeof(ClUint32T));
    qCkptData->qServerAddress.portId = (ClUint32T) ntohl((ClUint32T)network_byte_order);
    copyData = copyData + sizeof(ClUint32T);

    memcpy(&network_byte_order,copyData, sizeof(ClUint32T));
    qCkptData->state = (ClMsgQueueStateFlagT) (ClUint32T) ntohl((ClUint32T)network_byte_order);
    copyData = copyData + sizeof(ClUint32T);

    memcpy(&network_byte_order,copyData, sizeof(ClUint32T));
    qCkptData->creationFlags = (ClUint32T) ntohl((ClUint32T)network_byte_order);
    copyData = copyData + sizeof(ClUint32T);
}
ClRcT clMsgQueueCkptDataMarshal(ClMsgQueueCkptDataT *qCkptData, ClCachedCkptDataT *outData)
{
    ClRcT rc = CL_OK;
    ClUint8T *copyData;
    ClUint32T network_byte_order;

    saNameCopy(&outData->sectionName, &qCkptData->qName);
    outData->sectionAddress.iocPhyAddress = qCkptData->qAddress;
    outData->dataSize = CL_MSG_QUEUE_DATA_SIZE;

    outData->data = (ClUint8T *) clHeapAllocate(outData->dataSize);
    copyData = outData->data;

    network_byte_order = (ClUint32T) htonl((ClUint32T)qCkptData->qServerAddress.nodeAddress);
    memcpy(copyData, &network_byte_order, sizeof(ClUint32T));
    copyData = copyData + sizeof(ClUint32T);

    network_byte_order = (ClUint32T) htonl((ClUint32T)qCkptData->qServerAddress.portId);
    memcpy(copyData, &network_byte_order, sizeof(ClUint32T));
    copyData = copyData + sizeof(ClUint32T);

    network_byte_order = (ClUint32T) htonl((ClUint32T)qCkptData->state);
    memcpy(copyData, &network_byte_order, sizeof(ClUint32T));
    copyData = copyData + sizeof(ClUint32T);

    network_byte_order = (ClUint32T) htonl((ClUint32T)qCkptData->creationFlags);
    memcpy(copyData, &network_byte_order, sizeof(ClUint32T));
    copyData = copyData + sizeof(ClUint32T);

    return rc;
}
void clMsgQueueFree(ClMsgQueueInfoT *pQInfo)
{
    ClRcT rc = CL_OK;
    SaNameT qName = {strlen("--Unknow--"), "--Unknow--"};
    ClUint32T i;

    CL_OSAL_MUTEX_LOCK(&pQInfo->qLock);

    (void)clMsgUnblockThreadsOfQueue(pQInfo);

    if(pQInfo->unlinkFlag == CL_FALSE && pQInfo->pQueueEntry != NULL)
    {
        saNameCopy(&qName, &pQInfo->pQueueEntry->qName);
    }
    
    if(pQInfo->timerHandle != 0)
    {
        rc = clTimerDelete(&pQInfo->timerHandle);
        if(rc != CL_OK)
            clLogError("QUE", "FREE", "Failed to delete [%.*s]'s timer handle. error code [0x%x].", 
                       qName.length, qName.value, rc);
    }
    
    clMsgQueueEmpty(pQInfo);

    for(i = 0; i < CL_MSG_QUEUE_PRIORITIES ; i++)
    {
        rc = clCntDelete(pQInfo->pPriorityContainer[i]);
        if(rc != CL_OK)
            clLogError("QUE", "FREE", "Failed to delete the [%.*s]'s [%d] priority container. error code [0x%x].", 
                       qName.length, qName.value, i, rc);
        else
            pQInfo->pPriorityContainer[i] = 0;
    }

    rc = clOsalCondDelete(pQInfo->qCondVar);
    if(rc != CL_OK)
        clLogError("QUE",  "FREE", "Failed to delete the [%.*s]'s condtional variable. error code [0x%x].",
                   qName.length, qName.value, rc);

    pQInfo->qCondVar = 0;

    CL_OSAL_MUTEX_UNLOCK(&pQInfo->qLock);

    rc = clOsalMutexDestroy(&pQInfo->qLock);
    if(rc != CL_OK)
        clLogError("QUE",  "FREE", "Failed to delete the [%.*s]'s lock. error code [0x%x].",
                   qName.length, qName.value, rc);

    clLogDebug("QUE", "FREE", "Freed the queue [%.*s].", qName.length, qName.value);

    return;
}
static ClBoolT
clLogMasterStreamIsValid(SaNameT  *pNodeName)
{
    ClCpmSlotInfoT  slotInfo  = {0};

    saNameCopy(&slotInfo.nodeName, pNodeName);
    if( CL_OK == (clCpmSlotGet(CL_CPM_NODENAME, &slotInfo)) )
    {
        return CL_TRUE;
    }
    return CL_FALSE;
}
ClRcT clMsgQGroupCkptDataUnmarshal(ClMsgQGroupCkptDataT *qCkptData, const ClCachedCkptDataT *inData)
{
    ClRcT rc = CL_OK;
    ClUint8T *copyData;
    ClUint32T network_byte_order;
    ClUint16T len16 = 0;
    ClUint32T i;

    saNameCopy(&qCkptData->qGroupName, &inData->sectionName);
    qCkptData->qGroupAddress = inData->sectionAddress.iocPhyAddress;

    copyData = inData->data;

    memcpy(&network_byte_order,copyData, sizeof(ClUint32T));
    qCkptData->policy = (SaMsgQueueGroupPolicyT) (ClUint32T) ntohl((ClUint32T)network_byte_order);
    copyData = copyData + sizeof(ClUint32T);

    memcpy(&network_byte_order,copyData, sizeof(ClUint32T));
    qCkptData->numberOfQueues = (ClUint32T) ntohl((ClUint32T)network_byte_order);
    copyData = copyData + sizeof(ClUint32T);

    if (qCkptData->numberOfQueues > 0)
    {
        qCkptData->pQueueList = (SaNameT *)clHeapCalloc(qCkptData->numberOfQueues, sizeof(SaNameT));
        if (qCkptData->pQueueList == NULL)
        {
            rc = CL_ERR_NO_MEMORY;
            clLogError("MSG", "UMS", "Failed to allocate memory for %zd bytes.", qCkptData->numberOfQueues * sizeof(SaNameT));
            return rc;
        }
    }

    for (i = 0; i < qCkptData->numberOfQueues; i++)
    {
        SaNameT *queueName = (SaNameT *)(qCkptData->pQueueList + i);
        memcpy(&len16, copyData, sizeof(len16));
        queueName->length = (ClUint16T)ntohs(len16);
        copyData = copyData + sizeof(len16);
        memcpy(queueName->value, copyData, queueName->length);
        copyData = copyData + queueName->length;
    }
    return rc;
}
ClRcT clMsgQGroupCkptDataMarshal(ClMsgQGroupCkptDataT *qCkptData, ClCachedCkptDataT *outData)
{
    ClRcT rc = CL_OK;
    ClUint8T *copyData;
    ClUint32T network_byte_order;
    ClUint16T len16 = 0;
    ClUint32T i, dataSize;

    saNameCopy(&outData->sectionName, &qCkptData->qGroupName);
    outData->sectionAddress.iocPhyAddress = qCkptData->qGroupAddress;
    dataSize = 2 * sizeof(ClUint32T);
    for (i = 0; i < qCkptData->numberOfQueues; i++)
    {
        SaNameT *queueName = (SaNameT *)(qCkptData->pQueueList + i);
        dataSize += sizeof(ClUint16T);
        dataSize += queueName->length;
    }
    outData->dataSize = dataSize;

    outData->data = (ClUint8T *) clHeapAllocate(outData->dataSize);
    copyData = outData->data;

    network_byte_order = (ClUint32T) htonl((ClUint32T)qCkptData->policy);
    memcpy(copyData, &network_byte_order, sizeof(ClUint32T));
    copyData = copyData + sizeof(ClUint32T);

    network_byte_order = (ClUint32T) htonl((ClUint32T)qCkptData->numberOfQueues);
    memcpy(copyData, &network_byte_order, sizeof(ClUint32T));
    copyData = copyData + sizeof(ClUint32T);

    for (i = 0; i < qCkptData->numberOfQueues; i++)
    {
        SaNameT *queueName = (SaNameT *)(qCkptData->pQueueList + i);
        len16 = (ClUint16T)htons((ClUint16T)queueName->length);
        memcpy(copyData, &len16, sizeof(len16));
        copyData = copyData + sizeof(len16);
        memcpy(copyData, queueName->value, queueName->length);
        copyData = copyData + queueName->length;
    }

    return rc;
}
void clMsgQGroupCkptHeaderUnmarshal(ClMsgQGroupCkptDataT *qCkptData, const ClCachedCkptDataT *inData)
{
    ClUint8T *copyData;
    ClUint32T network_byte_order;

    saNameCopy(&qCkptData->qGroupName, &inData->sectionName);
    qCkptData->qGroupAddress = inData->sectionAddress.iocPhyAddress;

    copyData = inData->data;

    memcpy(&network_byte_order,copyData, sizeof(ClUint32T));
    qCkptData->policy = (SaMsgQueueGroupPolicyT) (ClUint32T) ntohl((ClUint32T)network_byte_order);
    copyData = copyData + sizeof(ClUint32T);

    memcpy(&network_byte_order,copyData, sizeof(ClUint32T));
    qCkptData->numberOfQueues = (ClUint32T) ntohl((ClUint32T)network_byte_order);
    copyData = copyData + sizeof(ClUint32T);

    /* Get header only */
    qCkptData->pQueueList = NULL;

    return;
}
ClRcT _clCkpMastertReplicaAddressUpdate(ClHandleT         mastHdl,
                                        ClIocNodeAddressT actAddr)
{
    ClRcT                   rc               = CL_OK;
    ClCntNodeHandleT        nodeHdl          = 0;
    ClCntDataHandleT        dataHdl          = 0;
    CkptMasterDBEntryT      *pMasterDBEntry  = NULL;
    ClCkptClientUpdInfoT    eventInfo        = {0};
    ClEventIdT              eventId          = 0;

    clLogDebug(CL_CKPT_AREA_MAS_DEP, CL_CKPT_CTX_PEER_DOWN, 
               "Changing the active address of ckpt handle [%#llX]",
               mastHdl);
    /*
     * Retrieve the information associated with the master hdl.
     */
    if( CL_OK != (rc = clHandleCheckout(gCkptSvr->masterInfo.masterDBHdl,
                                        mastHdl, (void **) &pMasterDBEntry)))
    {
        clLogError(CL_CKPT_AREA_MAS_DEP, CL_CKPT_CTX_PEER_DOWN, 
                   "Master db entry doesn't have this handle [%#llX]", 
                   mastHdl);
        return rc;
    }  

    /*
     * Delete the entry of the node that went down from the checkpoint's
     * replica list.
     */
    if (pMasterDBEntry->replicaList)
        clCntAllNodesForKeyDelete(pMasterDBEntry->replicaList, (ClPtrT)(ClWordT)actAddr);
    else
    {
        clLogWarning(CL_CKPT_AREA_MAS_DEP, CL_CKPT_CTX_PEER_DOWN,"Replicalist for %s is empty",pMasterDBEntry->name.value);
    }


    /*
     * Store the node's address as prev active address.
     */
    pMasterDBEntry->prevActiveRepAddr = actAddr; 

    /*
     * Select the new active address. In case of COLLOCATED checkpoint,
     * the new active address to UNINIT value.
     */
    if (CL_CKPT_IS_COLLOCATED(pMasterDBEntry->attrib.creationFlags)) 
    {
        if(pMasterDBEntry->activeRepAddr == actAddr)
        {
            pMasterDBEntry->activeRepAddr = CL_CKPT_UNINIT_ADDR;
        }
        else 
        {
            if(pMasterDBEntry->activeRepAddr != CL_CKPT_UNINIT_ADDR)
            {
                /*
                 * If we have traces of the master handle in our peer list 
                 * missed by the active replica set, then remove it here.
                 */
                clLogNotice(CL_CKPT_AREA_MAS_DEP, CL_CKPT_CTX_PEER_DOWN, 
                            "Active replica is [%d]."
                            "Removing master ckpt handle [%#llX] from last active [%d]",
                            pMasterDBEntry->activeRepAddr, mastHdl, actAddr);
                _ckptPeerListMasterHdlAdd(mastHdl, actAddr, CL_CKPT_UNINIT_ADDR);
            }
            goto exitOnError;
        }
    }
    else 
    {
        rc = clCntFirstNodeGet(pMasterDBEntry->replicaList, &nodeHdl);
        if(CL_ERR_INVALID_HANDLE == CL_GET_ERROR_CODE(rc) || 
           nodeHdl == 0)
        {
            rc = CL_OK;
            pMasterDBEntry->activeRepAddr = CL_CKPT_UNINIT_ADDR;
        }
        CKPT_ERR_CHECK(CL_CKPT_SVR,CL_LOG_SEV_ERROR,
                       ("clCkptActiveReplicaAddrGet failed rc[0x %x]\n",rc),
                       rc);
        if( nodeHdl != 0 ) 
        {
            rc = clCntNodeUserDataGet(pMasterDBEntry->replicaList, nodeHdl, &dataHdl);
            CKPT_ERR_CHECK(CL_CKPT_SVR,CL_LOG_SEV_ERROR,
                           ("clCkptActiveReplicaAddrGet failed rc[0x %x]\n",rc),
                           rc);
            pMasterDBEntry->activeRepAddr = (ClIocNodeAddressT)(ClWordT)dataHdl;
        }
    }

    /*
     * Inform the client about the change in active replica.
     */
    if((gCkptSvr->masterInfo.masterAddr == gCkptSvr->localAddr) ||
       (clCpmIsSCCapable() && 
        (pMasterDBEntry->prevActiveRepAddr == 
         gCkptSvr->masterInfo.masterAddr)))
    {
        eventInfo.eventType   = htonl(CL_CKPT_ACTIVE_REP_CHG_EVENT);
        eventInfo.actAddr     = htonl(pMasterDBEntry->activeRepAddr);
        saNameCopy(&eventInfo.name, &pMasterDBEntry->name);
        eventInfo.name.length = htons(pMasterDBEntry->name.length);

        clLogNotice(CL_CKPT_AREA_MAS_DEP, CL_CKPT_CTX_PEER_DOWN, 
                    "Changing the address from [%d] to [%d] for checkpoint [%.*s]",
                    actAddr, pMasterDBEntry->activeRepAddr, pMasterDBEntry->name.length, pMasterDBEntry->name.value);
        rc = clEventPublish(gCkptSvr->clntUpdEvtHdl, 
                            (const void*)&eventInfo,
                            sizeof(ClCkptClientUpdInfoT), &eventId);
    }

    /*
     * Delete the masterHdl from old address's peerlist and add to 
     * new address's peerlist.
     */
    if(!CL_CKPT_IS_COLLOCATED(pMasterDBEntry->attrib.creationFlags))
    {
        _ckptPeerListMasterHdlAdd(mastHdl, actAddr,
                                  pMasterDBEntry->activeRepAddr);
    }   
     
    exitOnError:
    {
        /* 
         * Checkin the updated stuff.
         */
        clHandleCheckin(gCkptSvr->masterInfo.masterDBHdl, mastHdl);
        return rc;
    }
}