void clMsgCommIdlFinalize(void)
{
    ClRcT rc;

    rc = clIdlHandleFinalize(gIdlUcastHandle);
    if(rc != CL_OK)
        clLogError("MSG", "IDL", "Failed to do the finalize the IDL handle. error code [0x%x].", rc);

    rc = clIdlHandleFinalize(gIdlBcastHandle);
    if(rc != CL_OK)
        clLogError("MSG", "IDL", "Failed to do the finalize the IDL handle. error code [0x%x].", rc);

}
ClRcT clMsgQueueAllocateThroughIdl(
        ClIocPhysicalAddressT destNode,
        ClNameT *pQName,
        SaMsgQueueOpenFlagsT openFlags,
        SaMsgQueueCreationAttributesT *pCreationAttrs,
        SaMsgQueueHandleT *pQHandle)
{
    ClRcT rc;
    ClIdlHandleObjT idlObj = {0};
    ClIdlHandleT idlHandle = 0;
    
    memcpy(&idlObj, &gIdlUcastObj, sizeof(idlObj));
    idlObj.address.address.iocAddress.iocPhyAddress = destNode;

    rc = clIdlHandleInitialize(&idlObj, &idlHandle);
    if(rc != CL_OK)
    {
        clLogError("IDL", "ALOC", "Failed to initialize the IDL handle. error code [0x%x].", rc);
        goto error_out;
    }

    clLogTrace("IDL", "ALOC", "Allocate-request for queue [%.*s] on node [%d].", pQName->length, pQName->value, destNode.nodeAddress);

    rc = VDECL_VER(clMsgQueueAllocateClientSync, 4, 0, 0)(idlHandle, pQName, openFlags, pCreationAttrs, pQHandle);
    if(rc != CL_OK)
    {
        clLogError("IDL", "ALOC", "Queue [%.*s] allocation failed on node [%d]. error code [0x%x].", pQName->length, pQName->value, destNode.nodeAddress, rc);
    }
    clIdlHandleFinalize(idlHandle);

error_out:
    return rc;
}
ClRcT clMsgCallClientsTrackCallback(ClIocPhysicalAddressT compAddr,
        ClNameT *pGroupName,
        SaMsgHandleT appHandle,
        SaMsgQueueGroupNotificationBufferT *pData)
{
    ClRcT rc;
    ClIdlHandleObjT idlObj = {0};
    ClIdlHandleT idlHandle = 0;
    
    memcpy(&idlObj, &gIdlUcastObj, sizeof(idlObj));
    idlObj.address.address.iocAddress.iocPhyAddress = compAddr;

    rc = clIdlHandleInitialize(&idlObj, &idlHandle);
    if(rc != CL_OK)
    {
        clLogError("IDL", "TRCb", "Failed to initialize the IDL handle. error code [0x%x].", rc);
        goto error_out;
    }

    clLogTrace("IDL", "TRCb", "Calling track callback for client [0x%x:0x%x].", compAddr.nodeAddress, compAddr.portId);

    rc = VDECL_VER(clMsgClientsTrackCallbackClientAsync, 4, 0, 0)(idlHandle, appHandle, pGroupName, pData, NULL, NULL);
    if(rc != CL_OK)
    {
        clLogError("IDL", "TRCb", "Failed to make an Async RMD to client. error code [0x%x].", rc);
    }
    clIdlHandleFinalize(idlHandle);

error_out:
    return rc;
}
ClRcT clMsgMessageGet_Idl(
        ClIocPhysicalAddressT destNode,
        ClNameT *pQName,
        SaTimeT timeout)
{
    ClRcT rc;
    ClIdlHandleObjT idlObj = {0};
    ClIdlHandleT idlHandle = 0;
    
    memcpy(&idlObj, &gIdlUcastObj, sizeof(idlObj));
    idlObj.address.address.iocAddress.iocPhyAddress = destNode;

    rc = clIdlHandleInitialize(&idlObj, &idlHandle);
    if(rc != CL_OK)
    {
        clLogError("IDL", "GET", "Failed to initialize the IDL handle. error code [0x%x].", rc);
        goto error_out;
    }

    clLogTrace("IDL", "GET", "Get message request for queue [%.*s] on node [%d].", pQName->length, pQName->value, destNode.nodeAddress);

    rc = VDECL_VER(clMsgMessageGetClientSync, 4, 0, 0)(idlHandle, pQName, timeout);
    if(rc != CL_OK)
    {
        clLogError("IDL", "GET", "Queue [%.*s] message get failed on node [%d]. error code [0x%x].", pQName->length, pQName->value, destNode.nodeAddress, rc);
    }
    clIdlHandleFinalize(idlHandle);

error_out:
    return rc;
}
ClRcT
clLogClntIdlHandleFinalize(ClIdlHandleT  *phClntIdl)
{
    ClRcT rc = CL_OK;

    CL_LOG_DEBUG_TRACE(("Enter"));

    CL_LOG_PARAM_CHK((NULL == phClntIdl), CL_LOG_RC(CL_ERR_NULL_POINTER));

    rc = clIdlHandleFinalize(*phClntIdl);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clIdlHandleInitialize(): rc[0x %x]", rc));
        return rc;
    }

    CL_LOG_DEBUG_TRACE(("Enter"));
    return rc;
}
ClRcT clMsgCommIdlInitialize(void)
{
    ClRcT rc;
    ClRcT retCode;

    gIdlUcastObj.objId = clObjId_ClIdlHandleObjT; 
    gIdlUcastObj.address.addressType = CL_IDL_ADDRESSTYPE_IOC;
    gIdlUcastObj.address.address.iocAddress.iocPhyAddress.nodeAddress = clIocLocalAddressGet();
    gIdlUcastObj.address.address.iocAddress.iocPhyAddress.portId = CL_IOC_MSG_PORT;
    gIdlUcastObj.flags = 0; 
    gIdlUcastObj.options.timeout = CL_RMD_TIMEOUT_FOREVER;
    gIdlUcastObj.options.retries = 3; 
    gIdlUcastObj.options.priority = CL_IOC_DEFAULT_PRIORITY;

    rc = clIdlHandleInitialize(&gIdlUcastObj, &gIdlUcastHandle);
    if(rc != CL_OK)
    {    
        clLogError("MSG", "IDL", "Failed to initialize the the IDL. error code [0x%x].", rc); 
        goto error_out;
    }    

    memcpy(&gIdlBcastObj, &gIdlUcastObj, sizeof(gIdlBcastObj));
    gIdlBcastObj.address.address.iocAddress.iocPhyAddress.nodeAddress = CL_IOC_BROADCAST_ADDRESS;
    gIdlBcastObj.address.address.iocAddress.iocPhyAddress.portId = CL_IOC_MSG_PORT;
    gIdlBcastObj.options.priority = CL_IOC_ORDERED_PRIORITY;
    rc = clIdlHandleInitialize(&gIdlBcastObj, &gIdlBcastHandle);
    if(rc != CL_OK)
    {    
        clLogError("MSG", "IDL", "Failed to initialize the the IDL. error code [0x%x].", rc); 
        goto error_out_1;
    } 

    goto done_out;

error_out_1:
    retCode = clIdlHandleFinalize(gIdlUcastHandle);
    if(retCode != CL_OK)
        clLogError("MSG", "IDL", "Failed to do the finalize the IDL handle. error code [0x%x].", retCode);
error_out:
done_out:
    return rc;
}
ClRcT
clLogSvrStreamCurrentFilterGet(ClCntNodeHandleT  hStreamOwnerNode,
                               SaNameT           *pStreamName,
                               SaNameT           *pStreamNodeName,
                               ClPtrT            pStreamHeader)
{
    ClRcT              rc              = CL_OK;
    ClIdlHandleT       hStreamOwnerIdl = CL_HANDLE_INVALID_VALUE;
    ClLogStreamScopeT  streamScope     = CL_LOG_STREAM_GLOBAL;

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clLogStreamScopeGet(pStreamNodeName, &streamScope);
    if( CL_OK != rc )
    {
        return rc;
    }
    rc = clLogSvrIdlHandleInitialize(streamScope, &hStreamOwnerIdl);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogSvrIdlHandleInitialize(): rc[0x %x]\n", rc));
        return rc;
    }

#if 0
    FIXME - streamOwnerShould expose the function
    rc = clLogStreamOwnerFilterGetClientAsync(hStreamOwnerIdl, pStreamName,
            streamScope, pStreamNodeName,
            &filter, clLogSvrSOFGResponse,
            (void *) hStreamOwnerNode);
#endif
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogStreamOwnerFilterGetClientAsync(): rc[0x %x]\n",
                            rc));
    }
    CL_LOG_CLEANUP(clIdlHandleFinalize(hStreamOwnerIdl), CL_OK);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
ClRcT clMsgQueueUnlinkToServer(ClNameT *pQName)
{
    ClRcT rc;
    /* Look up msg queue in the cached checkpoint */
    ClMsgQueueCkptDataT queueData;
    if(clMsgQCkptExists((ClNameT *)pQName, &queueData) == CL_FALSE)
    {
        rc = CL_MSG_RC(CL_ERR_DOESNT_EXIST);
        clLogError("MSG", "UNL", "Failed to get the message queue information.");
        goto error_out;
    }

    /* Get Ioc address of the given MSG queue */
    ClIdlHandleObjT idlObj = {0};
    ClIdlHandleT idlHandle = 0;

    memcpy(&idlObj, &gIdlUcastObj, sizeof(idlObj));
    if ((queueData.creationFlags == SA_MSG_QUEUE_PERSISTENT) &&
            (queueData.qServerAddress.nodeAddress != 0))
    {
        idlObj.address.address.iocAddress.iocPhyAddress = queueData.qServerAddress;
    }

    rc = clIdlHandleInitialize(&idlObj, &idlHandle);
    if(rc != CL_OK)
    {
        clLogError("MSG", "UNL", "Failed to initialize the IDL handle. error code [0x%x].", rc);
        goto error_out;
    }

    rc = VDECL_VER(clMsgQueueUnlinkClientSync, 4, 0, 0)(idlHandle, (ClNameT *)pQName);
    if(rc != CL_OK)
    {
        clLogError("MSG", "UNL", "Failed to close the message queue at message server. error code [0x%x].", rc);
    }

    clIdlHandleFinalize(idlHandle);

error_out:
    return rc;
}
ClRcT
clLogSvrShutdown(void)
{
	CL_LOG_DEBUG_TRACE(("Enter"));	

    CL_LOG_CLEANUP(clLogDebugDeregister(), CL_OK);
    CL_LOG_CLEANUP(clLogEvtFinalize(CL_TRUE), CL_OK);
    CL_LOG_CLEANUP(clLogSvrEoDataFinalize(), CL_OK);
    CL_LOG_CLEANUP(clLogFileOwnerShutdown(), CL_OK);
    CL_LOG_CLEANUP(clLogStreamOwnerGlobalShutdown(), CL_OK);
    CL_LOG_CLEANUP(clLogStreamOwnerLocalShutdown(), CL_OK);
    CL_LOG_CLEANUP(clLogMasterShutdown(), CL_OK);
    CL_LOG_CLEANUP(clLogSvrCommonDataFinalize(), CL_OK);
    CL_LOG_CLEANUP(clLogFileOwnerEoDataFree(), CL_OK);
    CL_LOG_CLEANUP(clLogStreamOwnerEoDataFree(), CL_OK);
    CL_LOG_CLEANUP(clLogSvrEoDataFree(), CL_OK);
    CL_LOG_CLEANUP(clIdlHandleFinalize(shLogDummyIdl), CL_OK);

	CL_LOG_DEBUG_TRACE(("Exit"));	
    return CL_OK;
}
ClRcT clLogSvrInitialize(ClUint32T argc,ClCharT   *argv[])
{
    SaAmfCallbacksT     callbacks = {0};
    SaVersionT          version;
    
    ClRcT            rc            = CL_OK;
    ClBoolT          *pCookie      = NULL;
	
    clLogCompName =(ClCharT*) "LOG"; /* Override generated eo name with a short name for our server */
    gClLogServer = CL_FALSE;  /* Mark me as the log server */

    clLogInfo(CL_LOG_AREA_UNSPECIFIED, CL_LOG_CONTEXT_UNSPECIFIED, "Log Server initialization started...");

    clAppConfigure(&clEoConfig,clEoBasicLibs,clEoClientLibs);

    version.releaseCode  = 'B';
    version.majorVersion = 0x01;
    version.minorVersion = 0x01;

    callbacks.saAmfHealthcheckCallback          = NULL; /* rarely necessary because SAFplus monitors the process */
    callbacks.saAmfComponentTerminateCallback   = clLogSvrTerminate;
    callbacks.saAmfCSISetCallback               = NULL;
    callbacks.saAmfCSIRemoveCallback            = NULL;
    callbacks.saAmfProtectionGroupTrackCallback = NULL;        
    callbacks.saAmfProxiedComponentInstantiateCallback    = NULL;
    callbacks.saAmfProxiedComponentCleanupCallback    = NULL;

    rc = saAmfInitialize(&amfHandle, &callbacks, &version);
    if( SA_AIS_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("saAmfInitialize(): rc[0x %x]", rc));
        return rc;
    }
          

    rc = saAmfComponentNameGet(amfHandle, &logServerName);
    if( SA_AIS_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("saAmfComponentNameGet(): rc[0x %x]", rc));
        saAmfFinalize(amfHandle);
        CL_LOG_CLEANUP(clLogSvrEoDataFinalize(), CL_OK);
        CL_LOG_CLEANUP(clLogSvrEoDataFree(), CL_OK);
        clHeapFree(pCookie);
        CL_LOG_CLEANUP(clLogStreamOwnerLocalShutdown(), CL_OK);
        CL_LOG_CLEANUP(clLogStreamOwnerEoDataFree(), CL_OK);
        CL_LOG_CLEANUP(clLogSvrCommonDataFinalize(), CL_OK);
        CL_LOG_CLEANUP(clIdlHandleFinalize(shLogDummyIdl), CL_OK);
        return rc;
    }
    rc = saAmfComponentRegister(amfHandle, &logServerName, NULL);
    if( SA_AIS_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("saAmfComponentRegister(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clLogSvrEoDataFinalize(), CL_OK);
        CL_LOG_CLEANUP(clLogSvrEoDataFree(), CL_OK);
        clHeapFree(pCookie);
        CL_LOG_CLEANUP(clLogStreamOwnerLocalShutdown(), CL_OK);
        CL_LOG_CLEANUP(clLogStreamOwnerEoDataFree(), CL_OK);
        CL_LOG_CLEANUP(clLogSvrCommonDataFinalize(), CL_OK);
        CL_LOG_CLEANUP(clIdlHandleFinalize(shLogDummyIdl), CL_OK);
        saAmfFinalize(amfHandle);
        return rc;
    }   

    clLogNotice(CL_LOG_AREA_UNSPECIFIED, CL_LOG_CONTEXT_UNSPECIFIED, "Log Server partially up");

    CL_LOG_DEBUG_TRACE(("Exit"));
    return CL_OK;					    
}
/*
 * Called with the log server flusher stream lock. held.
 */
static ClRcT
clLogFlusherRecordsMcast(ClLogSvrStreamDataT  *pStreamData,
                         ClUint32T            nRecords)
{
    ClRcT      rc       = CL_OK;
    ClLogStreamHeaderT  *pHeader  = pStreamData->pStreamHeader;
    ClUint8T            *pRecords = pStreamData->pStreamRecords;
    ClUint32T           startIdx  = 0;
    ClUint32T           buffLen   = 0;
    ClHandleT           hFlusher  = CL_HANDLE_INVALID_VALUE;     
    ClTimerHandleT      hTimer    = CL_HANDLE_INVALID_VALUE;  
    ClIdlHandleT        hIdlHdl   = CL_HANDLE_INVALID_VALUE;
    ClIocNodeAddressT   localAddr = 0;
    ClUint8T            *pBuffer  = NULL;
    ClUint32T           size      = 0;
    ClUint32T           firstBatch = 0;
    ClUint32T           secondPatch = 0;
    ClBoolT             doMulticast = 
        ( (0 < (pStreamData->ackersCount + pStreamData->nonAckersCount)) &&
          (pHeader->streamMcastAddr.iocMulticastAddress != 0) )? CL_TRUE: CL_FALSE;

    clLogDebug("SVR", "FLU", "Enter: nRecords: %u seqNum: %u fileOwnerAddr:[%d]", nRecords,
            pStreamData->seqNum, pStreamData->fileOwnerAddr);

    if( (pStreamData->ackersCount + pStreamData->nonAckersCount) == 1 && 
        (pStreamData->fileOwnerAddr == clIocLocalAddressGet()) )
    {
        doMulticast = CL_FALSE;
    }
    /* 
     * If some fileowner has registered for this stream, if then multicast,
     * otherwise just send it to local guy 
     */
    if( CL_TRUE == doMulticast )
    {
        rc = clLogIdlHandleInitialize(pHeader->streamMcastAddr, &hIdlHdl);
        if( CL_OK != rc )
        {
            return rc;
        }
        if( pStreamData->ackersCount > 0 )
        {
            rc = clLogFlusherCookieHandleCreate(nRecords, &hTimer, &hFlusher);
            if( CL_OK != rc )
            {
                CL_LOG_CLEANUP(clIdlHandleFinalize(hIdlHdl), CL_OK);
                return rc;
            }
            CL_LOG_DEBUG_TRACE(("hFlusher: %#llX", hFlusher));
        }
    }

    localAddr = clIocLocalAddressGet();
    startIdx = pHeader->startAck % pHeader->maxRecordCount;
    if(nRecords > pHeader->maxRecordCount)
        nRecords = pHeader->maxRecordCount;

    buffLen = nRecords * pHeader->recordSize;
    /*
     * Drop the server stream mutex unlock now and grab it again after file owner writes
     * and before exiting out of the function
     */
    clLogDebug(CL_LOG_AREA_SVR, "FLU", "startIdx: %u maxRec: %u nRecords: %u startIdx: %d recordIdx: %d", startIdx,
                    pHeader->maxRecordCount, nRecords, pHeader->startAck, pHeader->recordIdx);
    if( (startIdx + nRecords) <= pHeader->maxRecordCount )
    {
        pBuffer = pRecords + (startIdx * pHeader->recordSize);
        if( pStreamData->fileOwnerAddr == localAddr ) 
        {
            /* File onwer function which flushes the data into file */
            rc = clLogFileOwnerEntryFindNPersist(&pStreamData->fileName, 
                    &pStreamData->fileLocation, nRecords, pBuffer);
            if( CL_OK != rc )
            {
                return rc;
            }
        }
        if( doMulticast == CL_TRUE )
        {
            rc = VDECL_VER(clLogClntFileHdlrDataReceiveClientAsync, 4, 0, 0)(hIdlHdl, 
                    pHeader->streamMcastAddr.iocMulticastAddress, 
                    pStreamData->seqNum, localAddr, 
                    hFlusher, nRecords, buffLen, pBuffer,
                    NULL, 0);
            CL_LOG_CLEANUP(clIdlHandleFinalize(hIdlHdl), CL_OK);
        }
        CL_LOG_DEBUG_VERBOSE(("Copied from: %p to %u", pRecords + startIdx,
                    nRecords * pHeader->recordSize));
    }
    else
    {
        CL_LOG_DEBUG_TRACE(("startIdx: %u maxRec: %u nRecords: %u", startIdx,
                    pHeader->maxRecordCount, nRecords));
        firstBatch = pHeader->maxRecordCount - startIdx;
        pBuffer = pRecords + (startIdx * pHeader->recordSize);
        secondPatch = nRecords - firstBatch;
        if( pStreamData->fileOwnerAddr == localAddr )
        {
            /*
             *  Make two calls to fileowner function, so that no need to
             *  allocate memory, it just direct copy from shared memory 
             */
            rc = clLogFileOwnerEntryFindNPersist(&pStreamData->fileName, 
                    &pStreamData->fileLocation, firstBatch, pBuffer);
            if( CL_OK != rc )
            {
                return rc;
            }
            rc = clLogFileOwnerEntryFindNPersist(&pStreamData->fileName, 
                    &pStreamData->fileLocation, secondPatch, pRecords);
            if( CL_OK != rc )
            {
                return rc;
            }
        }
        if( doMulticast == CL_TRUE )
        {
            pBuffer = clHeapCalloc(buffLen, sizeof(ClUint8T));
            if( NULL == pBuffer )
            {
                clLogError("LOG", "FLS", "clHeapCalloc() : rc[0x %x]", rc);
                CL_LOG_CLEANUP(clLogFlusherCookieHandleDestroy(hFlusher, CL_FALSE), CL_OK);
                CL_LOG_CLEANUP(clIdlHandleFinalize(hIdlHdl), CL_OK);
                return CL_LOG_RC(CL_ERR_NO_MEMORY);
            }
            size = firstBatch * pHeader->recordSize;
            memcpy(pBuffer, pRecords + (startIdx * pHeader->recordSize), size);
            memcpy(pBuffer + size, pRecords, buffLen - size); 
            rc = VDECL_VER(clLogClntFileHdlrDataReceiveClientAsync, 4, 0, 0)(hIdlHdl, 
                    pHeader->streamMcastAddr.iocMulticastAddress, 
                    pStreamData->seqNum, localAddr, 
                    hFlusher, nRecords, buffLen, pBuffer,
                    NULL, 0);
            clHeapFree(pBuffer);
            CL_LOG_CLEANUP(clIdlHandleFinalize(hIdlHdl), CL_OK);
        }
    }
    if( (doMulticast == CL_TRUE) && (CL_OK != rc) )
    {
        clLogError("LOG", "FLS", "VDECL_VER(clLogClntFileHdlrDataReceiveClientAsync, 4, 0, 0)(): "
                "rc[0x %x]", rc);
        CL_LOG_CLEANUP(clLogFlusherCookieHandleDestroy(hFlusher, CL_FALSE), CL_OK);
        return rc;
    }    

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
static void logRecordsFlush(ClLogFlushRecordT *pFlushRecord)
{
    ClRcT rc = CL_OK;
    ClIdlHandleT idlHdl = CL_HANDLE_INVALID_VALUE;
    ClIocNodeAddressT localAddr = clIocLocalAddressGet();
    ClUint32T seqNum = 0;
    ClUint32T i = 0;

    seqNum = pFlushRecord->seqNum;
    if(pFlushRecord->multicast)
    {
        rc = clLogIdlHandleInitialize(pFlushRecord->mcastAddr, &idlHdl);
        if( CL_OK != rc )
        {
            goto out_free;
        }
    }

    for(i = 0; i < pFlushRecord->numBufs; ++i)
    {
        ClUint32T numRecords = pFlushRecord->pBuffs[i].numRecords;
        ClUint8T* pRecord = pFlushRecord->pBuffs[i].pRecord;
        ClUint32T buffLen = pFlushRecord->recordSize * numRecords;
        ClTimerHandleT hTimer = CL_HANDLE_INVALID_VALUE;
        ClHandleT hFlusher = CL_HANDLE_INVALID_VALUE;
        if(pFlushRecord->multicast > 0)
        {
            if(pFlushRecord->ackersCount > 0)
            {
                rc = clLogFlusherCookieHandleCreate(numRecords, &hTimer, &hFlusher);
                if(rc != CL_OK)
                    goto out_free;
            }
            rc = VDECL_VER(clLogClntFileHdlrDataReceiveClientAsync, 4, 0, 0)(idlHdl,
                                                                             pFlushRecord->mcastAddr.iocMulticastAddress,
                                                                             seqNum,
                                                                             localAddr,
                                                                             hFlusher, numRecords,
                                                                             buffLen, pRecord,
                                                                             NULL, 0);
            if(rc != CL_OK)
            {
                if(hFlusher)
                {
                    CL_LOG_CLEANUP(clLogFlusherCookieHandleDestroy(hFlusher, CL_FALSE), CL_OK);
                }
            }
        }
        else
        {
            if(pFlushRecord->fileOwnerAddr == localAddr)
            {
                rc = clLogFileOwnerEntryFindNPersist(&pFlushRecord->fileName,
                                                     &pFlushRecord->fileLocation,
                                                     numRecords, pRecord);
                if(rc != CL_OK)
                {
                    /* 
                     * do nothing
                     */
                }
            }
        }
        seqNum += numRecords;
        clHeapFree(pRecord);
    }

    out_free:
    {
        ClUint32T j;
        for(j = i; j < pFlushRecord->numBufs; ++j)
        {
            if(pFlushRecord->pBuffs[j].pRecord)
                clHeapFree(pFlushRecord->pBuffs[j].pRecord);
        }
    }
    if(idlHdl)
    {
        CL_LOG_CLEANUP(clIdlHandleFinalize(idlHdl), CL_OK);
    }
    if(pFlushRecord->fileLocation.pValue)
    {
        clHeapFree(pFlushRecord->fileLocation.pValue);
        pFlushRecord->fileLocation.pValue = NULL;
        pFlushRecord->fileLocation.length = 0;
    }
    if(pFlushRecord->fileName.pValue)
    {
        clHeapFree(pFlushRecord->fileName.pValue);
        pFlushRecord->fileName.pValue = NULL;
        pFlushRecord->fileName.length = 0;
    }
    if(pFlushRecord->pBuffs)
    {
        clHeapFree(pFlushRecord->pBuffs);
        pFlushRecord->pBuffs = NULL;
    }
}
ClRcT
clLogFileRecordsGet(ClLogFileHandleT  hFileHdlr,
                    ClTimeT           *pStartTime,
                    ClTimeT           *pEndTime, 
                    ClUint32T         *pNumRecords,
                    ClPtrT            *pLogRecords)
{
    ClRcT              rc        = CL_OK;
    ClIocAddressT      address   = {{0}};
    ClIdlHandleT       hLogIdl   = CL_HANDLE_INVALID_VALUE;
    ClLogClntFileKeyT  *pFileKey = NULL;
    ClBoolT            isDelete  = CL_FALSE;
    ClUint32T          version   = 0;
    ClUint64T          startRec  = 0;
    ClUint32T          buffLen   = 0;

    CL_LOG_DEBUG_TRACE(("Enter"));

    CL_LOG_PARAM_CHK((NULL == pNumRecords), CL_LOG_RC(CL_ERR_NULL_POINTER));
    CL_LOG_PARAM_CHK((NULL == pLogRecords), CL_LOG_RC(CL_ERR_NULL_POINTER));

    rc = clLogFileHdlrHandleInfoGet(hFileHdlr, &isDelete, &version, 
                                    &startRec, &pFileKey);
    if( CL_OK != rc )
    {
        return rc;
    }

    rc = clLogFileHdlrAddressGet(&pFileKey->fileLocation, &address);
    if( CL_OK != rc )
    {
        return rc;
    }

    rc = clLogClntIdlHandleInitialize(address, &hLogIdl);
    if( CL_OK != rc )
    {
        return rc;
    }

    CL_LOG_DEBUG_ERROR(("fileName: %s fileLoc: %s", pFileKey->fileName.pValue, 
                                                    pFileKey->fileLocation.pValue));

    CL_LOG_DEBUG_ERROR(("Version: %d isDelete: %d startRec: %llu", version, 
                                                   isDelete, startRec));
    
    rc = VDECL_VER(clLogFileHdlrFileRecordsGetClientSync, 4, 0, 0)(hLogIdl, &pFileKey->fileName,
                                     &pFileKey->fileLocation, isDelete, 
                                     &startRec, &version, pNumRecords, &buffLen, 
                                     (ClUint8T **) pLogRecords); 
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogSvrFileHdlrFileMetaDataGet(): rc[0x %x]",
                            rc));
        CL_LOG_CLEANUP(clIdlHandleFinalize(hLogIdl), CL_OK);
        return rc;
    }
    CL_LOG_DEBUG_VERBOSE(("NumRecords: %d", *pNumRecords));
    CL_LOG_CLEANUP(clIdlHandleFinalize(hLogIdl), CL_OK);
    if( (CL_FALSE == isDelete))
    {
       rc = clLogClntFileHdlrHandleUpdate(hFileHdlr, version, startRec); 
       if( CL_OK != rc )
       {
           return rc;
       }
       updateVersion = CL_TRUE;
    }

    rc = clLogClntFileHdlrTimeStampGet(*pNumRecords, buffLen, *pLogRecords, 
                                       pStartTime, pEndTime);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogClntFileHdlrTimeStampGet(): rc[0x %x]",
                    rc));
    }

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
ClRcT
clLogFileMetaDataGet(ClLogFileHandleT        hFileHdlr,
                     ClLogStreamAttributesT  *pStreamAttr,
                     ClUint32T               *pNumStreams,
                     ClLogStreamMapT         **ppLogStreams)
{
    ClRcT                rc            = CL_OK;
    ClIocAddressT        address       = {{0}}; 
    ClIdlHandleT         hLogIdl       = CL_HANDLE_INVALID_VALUE;
    ClLogClntFileKeyT    *pFileKey     = NULL;
    ClLogStreamAttrIDLT  streamAttrIdl = {{0}};

    ClBoolT              isDelete      = CL_FALSE;
    ClUint32T            version       = 0;
    ClUint64T            startRec      = 0;

    CL_LOG_DEBUG_TRACE(("Enter"));

    CL_LOG_PARAM_CHK((NULL == pStreamAttr), CL_LOG_RC(CL_ERR_NULL_POINTER));
    CL_LOG_PARAM_CHK((NULL == pNumStreams), CL_LOG_RC(CL_ERR_NULL_POINTER));
    CL_LOG_PARAM_CHK((NULL == ppLogStreams), CL_LOG_RC(CL_ERR_NULL_POINTER));

    rc = clLogFileHdlrHandleInfoGet(hFileHdlr, &isDelete, &version, 
                                    &startRec, &pFileKey);
    if( CL_OK != rc )
    {
        return rc;
    }

    rc = clLogFileHdlrAddressGet(&pFileKey->fileLocation, &address);
    if( CL_OK != rc )
    {
        return rc;
    }
    CL_LOG_DEBUG_ERROR(("FileOwner's Address: %d",
                address.iocPhyAddress.nodeAddress));

    rc = clLogClntIdlHandleInitialize(address, &hLogIdl);
    if( CL_OK != rc )
    {
        return rc;
    }

    CL_LOG_DEBUG_ERROR(("fileName: %s fileLoc: %s", pFileKey->fileName.pValue, 
                                                    pFileKey->fileLocation.pValue));
    
    rc = VDECL_VER(clLogFileHdlrFileMetaDataGetClientSync, 4, 0, 0)(hLogIdl, &pFileKey->fileName,
                                      &pFileKey->fileLocation,
                                      &version, &streamAttrIdl, 
                                      pNumStreams, ppLogStreams); 
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogSvrFileHdlrFileMetaDataGet(): rc[0x %x]",
                            rc));
        CL_LOG_CLEANUP(clIdlHandleFinalize(hLogIdl), CL_OK);
        return rc;
    }
    CL_LOG_DEBUG_ERROR(("NumStreams: %d", *pNumStreams));

    CL_LOG_CLEANUP(clIdlHandleFinalize(hLogIdl), CL_OK);
    {
        rc = clLogClntFileHdlrHandleUpdate(hFileHdlr, version, 0);
        if( CL_OK != rc )
        {
            clHeapFree(streamAttrIdl.fileLocation.pValue);
            clHeapFree(streamAttrIdl.fileName.pValue);
            clHeapFree(ppLogStreams);
            return rc;
        }
        updateVersion = CL_TRUE;
    }

    rc = clLogFileHdlrStreamAttributesCopy(&streamAttrIdl, pStreamAttr);
    if( CL_OK != rc )
    {
        clHeapFree(streamAttrIdl.fileLocation.pValue);
        clHeapFree(streamAttrIdl.fileName.pValue);
        clHeapFree(ppLogStreams);
        return rc;
    }

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
ClRcT
clLogStreamListGet(ClLogHandleT      hLog,
                   ClUint32T         *pNumStreams,
                   ClLogStreamInfoT  **ppLogStreams)
{
    ClRcT          rc       = CL_OK;
    ClIocAddressT  address  = {{0}};
    ClIdlHandleT   hLogIdl  = CL_HANDLE_INVALID_VALUE;
    ClUint8T       *pBuffer = NULL;
    ClUint32T      buffLen  = 0;

    CL_LOG_DEBUG_TRACE(("Enter"));

    CL_LOG_PARAM_CHK((NULL == pNumStreams), CL_LOG_RC(CL_ERR_NULL_POINTER));
    CL_LOG_PARAM_CHK((NULL == ppLogStreams), CL_LOG_RC(CL_ERR_NULL_POINTER));

    rc = clLogHandleCheck(hLog, CL_LOG_INIT_HANDLE);
    if( CL_OK != rc )
    {
        return rc;
    }

    rc = clLogMasterAddressGet(&address);
    if( CL_OK != rc )
    {
        return rc;
    }

    rc = clLogClntIdlHandleInitialize(address, &hLogIdl);
    if( CL_OK != rc )
    {
        return rc;
    }
    
    rc = VDECL_VER(clLogMasterStreamListGetClientSync, 4, 0, 0)(hLogIdl, pNumStreams, &buffLen, &pBuffer); 
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("VDECL_VER(clLogMasterStreamListGet, 4, 0, 0)(): rc[0x %x]",
                            rc));
        CL_LOG_CLEANUP(clIdlHandleFinalize(hLogIdl), CL_OK);
        return rc;
    }
    CL_LOG_DEBUG_VERBOSE(("NumRecords: %d", *pNumStreams));
    CL_LOG_CLEANUP(clIdlHandleFinalize(hLogIdl), CL_OK);

    if(!*pNumStreams)
    {
        if(pBuffer)
            clHeapFree(pBuffer);
        if(ppLogStreams)
            *ppLogStreams = NULL;
        return CL_OK;
    }

    rc = clLogClntStreamListUnpack(*pNumStreams, buffLen, pBuffer,
                                    ppLogStreams);
    if( CL_OK != rc )
    {
        clHeapFree(pBuffer);
        return rc;
    }
    clHeapFree(pBuffer);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
ClRcT 
clLogSvrInitialize(ClUint32T argc,ClCharT   *argv[])
{
    SaAmfCallbacksT     callbacks = {0};
    SaVersionT          version;
    
    ClRcT            rc            = CL_OK;
    ClLogSvrEoDataT  *pSvrEoEntry  = NULL;
    ClBoolT          *pCookie      = NULL;
    ClIocAddressT    invalidAddr   = {{0}};
	
    clLogCompName =(ClCharT*) "LOG"; /* Override generated eo name with a short name for our server */
    gClLogServer = CL_FALSE;  /* Mark me as the log server */

    clLogInfo(CL_LOG_AREA_UNSPECIFIED, CL_LOG_CONTEXT_UNSPECIFIED, "Log Server initialization started...");

    clAppConfigure(&clEoConfig,clEoBasicLibs,clEoClientLibs);

    version.releaseCode  = 'B';
    version.majorVersion = 0x01;
    version.minorVersion = 0x01;

    callbacks.saAmfHealthcheckCallback          = NULL; /* rarely necessary because SAFplus monitors the process */
    callbacks.saAmfComponentTerminateCallback   = clLogSvrTerminate;
    callbacks.saAmfCSISetCallback               = NULL;
    callbacks.saAmfCSIRemoveCallback            = NULL;
    callbacks.saAmfProtectionGroupTrackCallback = NULL;        
    callbacks.saAmfProxiedComponentInstantiateCallback    = NULL;
    callbacks.saAmfProxiedComponentCleanupCallback    = NULL;

    rc = saAmfInitialize(&amfHandle, &callbacks, &version);
    if( SA_AIS_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("saAmfInitialize(): rc[0x %x]", rc));
        return rc;
    }
          

#if defined(CL_DEBUG) && defined(CL_DEBUG_START)
    clLogDebugLevelSet();
#endif
    CL_LOG_DEBUG_TRACE(("Enter"));	

    clLogSvrMutexModeSet();
    
    /* 
     * Here dummy initialization of Idl handle to avoid mutiple database 
     * Initialization & finalization databases. Keeping this handle alive 
     * will avoid this. coz Idl library will delete the handle database if the
     * handle count becomes zero. Mutiple times we are initializing & deleting
     * the handles in our log service usage.
    */
    rc = clLogIdlHandleInitialize(invalidAddr, &shLogDummyIdl);
    if( CL_OK != rc )
    {
        
        return rc;
    }
    rc = clLogSvrCommonDataInit();
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogSvrCommonDataInit(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clIdlHandleFinalize(shLogDummyIdl), CL_OK);
        return rc;
    }
    rc = clLogStreamOwnerLocalBootup();
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogStreamOwnerLocalInit(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clLogSvrCommonDataFinalize(), CL_OK);
        CL_LOG_CLEANUP(clIdlHandleFinalize(shLogDummyIdl), CL_OK);
        return rc;
    }    
    pCookie = (ClBoolT*) clHeapCalloc(1, sizeof(ClBoolT));
    if( NULL == pCookie )
    {
        CL_LOG_DEBUG_ERROR(("clHeapCalloc()"));
        CL_LOG_CLEANUP(clLogStreamOwnerLocalShutdown(), CL_OK);
        CL_LOG_CLEANUP(clLogStreamOwnerEoDataFree(), CL_OK);
        CL_LOG_CLEANUP(clLogSvrCommonDataFinalize(), CL_OK);
        CL_LOG_CLEANUP(clIdlHandleFinalize(shLogDummyIdl), CL_OK);
        return CL_LOG_RC(CL_ERR_NO_MEMORY);
    }
    *pCookie = CL_FALSE;
    rc = clLogSvrBootup(pCookie);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogSvrDataInit(): rc[0x %x]", rc));
        clHeapFree(pCookie);
        CL_LOG_CLEANUP(clLogStreamOwnerLocalShutdown(), CL_OK);
        CL_LOG_CLEANUP(clLogSvrCommonDataFinalize(), CL_OK);
        CL_LOG_CLEANUP(clIdlHandleFinalize(shLogDummyIdl), CL_OK);
        return rc;
    }    
    clLogInfo(CL_LOG_AREA_UNSPECIFIED, CL_LOG_CONTEXT_UNSPECIFIED, 
              "Log server boot type is [%s]", 
              (*pCookie == 1)? "Restart": "Normal");

    rc = clLogSvrEoEntryGet(&pSvrEoEntry, NULL);
    if( CL_OK != rc )
    {
        CL_LOG_CLEANUP(clLogSvrEoDataFinalize(), CL_OK);
        CL_LOG_CLEANUP(clLogSvrEoDataFree(), CL_OK);
        clHeapFree(pCookie);
        CL_LOG_CLEANUP(clLogStreamOwnerLocalShutdown(), CL_OK);
        CL_LOG_CLEANUP(clLogStreamOwnerEoDataFree(), CL_OK);
        CL_LOG_CLEANUP(clLogSvrCommonDataFinalize(), CL_OK);
        CL_LOG_CLEANUP(clIdlHandleFinalize(shLogDummyIdl), CL_OK);
        return rc;
    }
    pSvrEoEntry->hCpm = amfHandle;
    rc = saAmfComponentNameGet(amfHandle, &logServerName);
    if( SA_AIS_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("saAmfComponentNameGet(): rc[0x %x]", rc));
        saAmfFinalize(amfHandle);
        CL_LOG_CLEANUP(clLogSvrEoDataFinalize(), CL_OK);
        CL_LOG_CLEANUP(clLogSvrEoDataFree(), CL_OK);
        clHeapFree(pCookie);
        CL_LOG_CLEANUP(clLogStreamOwnerLocalShutdown(), CL_OK);
        CL_LOG_CLEANUP(clLogStreamOwnerEoDataFree(), CL_OK);
        CL_LOG_CLEANUP(clLogSvrCommonDataFinalize(), CL_OK);
        CL_LOG_CLEANUP(clIdlHandleFinalize(shLogDummyIdl), CL_OK);
        return rc;
    }
    rc = saAmfComponentRegister(amfHandle, &logServerName, NULL);
    if( SA_AIS_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("saAmfComponentRegister(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clLogSvrEoDataFinalize(), CL_OK);
        CL_LOG_CLEANUP(clLogSvrEoDataFree(), CL_OK);
        clHeapFree(pCookie);
        CL_LOG_CLEANUP(clLogStreamOwnerLocalShutdown(), CL_OK);
        CL_LOG_CLEANUP(clLogStreamOwnerEoDataFree(), CL_OK);
        CL_LOG_CLEANUP(clLogSvrCommonDataFinalize(), CL_OK);
        CL_LOG_CLEANUP(clIdlHandleFinalize(shLogDummyIdl), CL_OK);
        saAmfFinalize(amfHandle);
        return rc;
    }   
    pSvrEoEntry->hTimer = CL_HANDLE_INVALID_VALUE;
    if( CL_FALSE == *pCookie )
    {
        rc = clLogSvrTimerDeleteNStart(pSvrEoEntry, pCookie);
    }
    else
    {
        rc = clLogTimerCallback((void *) pCookie);
    }
    if( CL_OK != rc )
    {
        saAmfComponentUnregister(amfHandle, &logServerName, NULL);
        CL_LOG_CLEANUP(clLogSvrEoDataFinalize(), CL_OK);
        CL_LOG_CLEANUP(clLogSvrEoDataFree(), CL_OK);
        clHeapFree(pCookie);
        CL_LOG_CLEANUP(clLogStreamOwnerLocalShutdown(), CL_OK);
        CL_LOG_CLEANUP(clLogStreamOwnerEoDataFree(), CL_OK);
        CL_LOG_CLEANUP(clLogSvrCommonDataFinalize(), CL_OK);
        CL_LOG_CLEANUP(clIdlHandleFinalize(shLogDummyIdl), CL_OK);
        saAmfFinalize(amfHandle);
        return rc;
    }
    rc = clLogDebugRegister();
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogDebugRegister(): rc[0x %x]", rc));
    }
    clLogNotice(CL_LOG_AREA_UNSPECIFIED, CL_LOG_CONTEXT_UNSPECIFIED, 
                "Log Server partially up");

    CL_LOG_DEBUG_TRACE(("Exit"));
    return CL_OK;					    
}
ClRcT clMsgSendMessage_idl(ClMsgMessageSendTypeT sendType,
        ClIocPhysicalAddressT compAddr,
        ClNameT *pName,
        ClMsgMessageIovecT *pMessage,
        SaTimeT sendTime,
        ClHandleT senderHandle,
        SaTimeT timeout,
        ClBoolT isSync,
        SaMsgAckFlagsT ackFlag,
        MsgCltSrvClMsgMessageReceivedAsyncCallbackT fpAsyncCallback,
        void *cookie)
 
{
    ClRcT rc = CL_OK;
    ClIdlHandleT idlHandle = 0;
    ClIdlHandleObjT idlObj = {0};

    if(compAddr.nodeAddress == CL_IOC_BROADCAST_ADDRESS)
    {
        rc = VDECL_VER(clMsgMessageReceivedClientAsync, 4, 0, 0)(gIdlBcastHandle, sendType, pName, pMessage, sendTime, senderHandle, timeout, NULL, NULL);
        if(rc != CL_OK)
            clLogError("IDL", "BCAST", "Failed to broadcast a message. error code [0x%x].", rc);
        return rc;
    }

    memcpy(&idlObj, &gIdlUcastObj, sizeof(idlObj));
    idlObj.address.address.iocAddress.iocPhyAddress = compAddr;

    if (pMessage->priority == SA_MSG_MESSAGE_HIGHEST_PRIORITY)
    {
        idlObj.options.priority = CL_IOC_HIGH_PRIORITY;
    }

    rc = clIdlHandleInitialize(&idlObj, &idlHandle);
    if(rc != CL_OK)
    {
        clLogError("IDL", "SND", "Failed to initialize the IDL handle. error code [0x%x].", rc);
        goto error_out;
    }

    clLogTrace("IDL", "SND", "Sending a message to component [0x%x,0x%x].", compAddr.nodeAddress, compAddr.portId);

    if (isSync == CL_TRUE)
    {
        rc = VDECL_VER(clMsgMessageReceivedClientSync, 4, 0, 0)(idlHandle, sendType, pName, pMessage, sendTime, senderHandle, timeout);
        if(rc != CL_OK)
            clLogError("IDL", "SND", "Failed to send a message to component [0x%x,0x%x]. error code [0x%x].", compAddr.nodeAddress, compAddr.portId, rc);
    }
    else
    {
        if(ackFlag == SA_MSG_MESSAGE_DELIVERED_ACK)
        {
            rc = VDECL_VER(clMsgMessageReceivedClientAsync, 4, 0, 0)(idlHandle, sendType, pName, pMessage, sendTime, senderHandle, timeout, fpAsyncCallback, cookie);
            if(rc != CL_OK)
                clLogError("IDL", "SND", "Failed to send a message to component [0x%x,0x%x]. error code [0x%x].", compAddr.nodeAddress, compAddr.portId, rc);
        }
        else
        {
            rc = VDECL_VER(clMsgMessageReceivedClientAsync, 4, 0, 0)(idlHandle, sendType, pName, pMessage, sendTime, senderHandle, timeout, NULL, NULL);
            if(rc != CL_OK)
                clLogError("IDL", "SND", "Failed to send a message to component [0x%x,0x%x]. error code [0x%x].", compAddr.nodeAddress, compAddr.portId, rc);
        }
    }

    clIdlHandleFinalize(idlHandle);

error_out:
    return rc;
}