ClRcT _cpmClusterConfigList(ClInt32T argc, ClCharT **retStr)
{
    ClCpmLT *cpmL = NULL;
    ClRcT rc = CL_OK;
    ClCntNodeHandleT hNode = 0;
    ClUint32T cpmLCount = 0;
    ClCharT tempStr[256];
    ClCharT *tmpStr = NULL;
    ClBufferHandleT message;

    rc = clBufferCreate(&message);
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to create message \n"), rc);

    if (argc != ONE_ARGUMENT)
    {
        sprintf(tempStr, "Usage: clusterList");
        rc = clBufferNBytesWrite(message, (ClUint8T *) tempStr,
                                        strlen(tempStr));
        CL_CPM_CHECK(CL_DEBUG_ERROR, ("Unable to write message %x\n", rc), rc);
        goto done;
    }

    /*
     * Print the local stuff first 
     */
    sprintf(tempStr, "%s\n", "nodeName | status | iocAddress | iocPort ");
    rc = clBufferNBytesWrite(message, (ClUint8T *) tempStr,
                                    strlen(tempStr));
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to write message \n"), rc);

    sprintf(tempStr, "%s\n",
            "-----------------------------------------------------------------------");
    rc = clBufferNBytesWrite(message, (ClUint8T *) tempStr,
                                    strlen(tempStr));
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to write message \n"), rc);

    /*
     * Now even the CPM/G information is stored in the list.
     * So no need of printing the above information.
     */
#if 0
    if (gpClCpm->pCpmLocalInfo->status == CL_CPM_EO_DEAD)
        sprintf(tempStr, "%10s | DEAD | %8d |   0x%x\n",
                gpClCpm->pCpmLocalInfo->nodeName,
                gpClCpm->pCpmLocalInfo->cpmAddress.nodeAddress,
                gpClCpm->pCpmLocalInfo->cpmAddress.portId);
    else
        sprintf(tempStr, "%10s | ALIVE | %8d |   0x%x\n",
                gpClCpm->pCpmLocalInfo->nodeName,
                gpClCpm->pCpmLocalInfo->cpmAddress.nodeAddress,
                gpClCpm->pCpmLocalInfo->cpmAddress.portId);
    rc = clBufferNBytesWrite(message, (ClUint8T *) tempStr,
                                    strlen(tempStr));
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to write message \n"), rc);
#endif
    /*_cpmListAllSU();*/
    /*
     * Get all the CPMs one by one and delete the stuff.
     */
    rc = clCntFirstNodeGet(gpClCpm->cpmTable, &hNode);
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("Unable to get first cpmTable Node %x\n", rc),
                 rc);
    rc = clCntNodeUserDataGet(gpClCpm->cpmTable, hNode,
                              (ClCntDataHandleT *) &cpmL);
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("Unable to get container Node data %x\n", rc),
                 rc);
    cpmLCount = gpClCpm->noOfCpm;

    while (cpmLCount)
    {
        if (cpmL->pCpmLocalInfo != NULL)
        {
            if (cpmL->pCpmLocalInfo->status == CL_CPM_EO_DEAD)
            {
                sprintf(tempStr, "%10s | DEAD  | %8d |   0x%x\n",
                        cpmL->nodeName,
                        cpmL->pCpmLocalInfo->cpmAddress.nodeAddress,
                        cpmL->pCpmLocalInfo->cpmAddress.portId);
            }
            else
            {
                sprintf(tempStr, "%10s | ALIVE | %8d |   0x%x\n",
                        cpmL->nodeName,
                        cpmL->pCpmLocalInfo->cpmAddress.nodeAddress,
                        cpmL->pCpmLocalInfo->cpmAddress.portId);
            }
            rc = clBufferNBytesWrite(message, (ClUint8T *) tempStr,
                                            strlen(tempStr));
            CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to write message \n"), rc);
        }
        else
        {
            sprintf(tempStr, "%10s \n", cpmL->nodeName);
            rc = clBufferNBytesWrite(message, (ClUint8T *) tempStr,
                                            strlen(tempStr));
            CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to write message \n"), rc);
        }
#if 0
        rc = clCntFirstNodeGet(cpmL->suTable, &hSU);
        CL_CPM_CHECK(CL_DEBUG_ERROR,
                     ("Unable to get first su Node in cpmL %x\n", rc), rc);
        if (cpmL->pCpmLocalInfo != NULL)
            clOsalPrintf("%10s | %8d | %8d | 0x%8x| 0x%8x \n",
                         cpmL->pCpmLocalInfo->nodeName,
                         cpmL->pCpmLocalInfo->status,
                         cpmL->pCpmLocalInfo->nodeId,
                         cpmL->pCpmLocalInfo->cpmAddress.nodeAddress,
                         cpmL->pCpmLocalInfo->cpmAddress.portId);
        else
            clOsalPrintf("%10s \n", cpmL->nodeName);

        suCount = cpmL->noOfsu;
        while (suCount)
        {
            rc = clCntNodeUserDataGet(cpmL->suTable, hSU,
                                      (ClCntDataHandleT *) &su);
            CL_CPM_CHECK(CL_DEBUG_ERROR,
                         ("Unable to get first container su Node data %x\n",
                          rc), rc);

            clOsalPrintf("\t %10s |%15s |%11s |%16s \n", su->suName,
                         _cpmPresenceStateNameGet(su->suPresenceState),
                         _cpmOperStateNameGet(su->suOperState),
                         _cpmReadinessStateNameGet(su->suReadinessState));
            tempCompRef = su->suCompList;
            while (tempCompRef != NULL)
            {
                clOsalPrintf
                    ("-----------------------------------------------------------------------\n");
                clOsalPrintf("\t\t%10s %14d |%15s |%11s |%16s \n",
                             tempCompRef->ref->compConfig->compName,
                             tempCompRef->ref->compRestartCount,
                             _cpmPresenceStateNameGet(tempCompRef->ref->
                                                      compPresenceState),
                             _cpmOperStateNameGet(tempCompRef->ref->
                                                  compOperState),
                             _cpmReadinessStateNameGet(tempCompRef->ref->
                                                       compReadinessState));
                tempCompRef = tempCompRef->pNext;
            }
            suCount--;
            if (suCount)
            {
                rc = clCntNextNodeGet(cpmL->suTable, hSU, &hSU);
                CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to Get Node  Data \n"),
                             rc);
            }
        }
#endif
        sprintf(tempStr, "%s",
                "-----------------------------------------------------------------------\n");
        rc = clBufferNBytesWrite(message, (ClUint8T *) tempStr,
                                        strlen(tempStr));
        CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to write message \n"), rc);
        cpmLCount--;
        if (cpmLCount)
        {
            rc = clCntNextNodeGet(gpClCpm->cpmTable, hNode, &hNode);
            CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to Get Node  Data \n"),
                         rc);
            rc = clCntNodeUserDataGet(gpClCpm->cpmTable, hNode,
                                      (ClCntDataHandleT *) &cpmL);
            CL_CPM_CHECK(CL_DEBUG_ERROR,
                         ("Unable to get container Node data %d\n", rc), rc);
        }
    }

    /*
     * Bug 4986 :
     * Moved the code to NULL terminate the string
     * below the done: label, so that the usage string
     * written to the buffer is also NULL terminated.
     */
  done:
    /*
     * NULL terminate the string 
     */
    sprintf(tempStr, "%s", "\0");
    rc = clBufferNBytesWrite(message, (ClUint8T *) tempStr, 1);
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to write message \n"), rc);

    rc = clBufferFlatten(message, (ClUint8T **) &tmpStr);
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to flatten message \n"), rc);

    *retStr = tmpStr;

    clBufferDelete(&message);
    return rc;

  failure:
    clBufferDelete(&message);
    return rc;
}
ClRcT corAmfObjectCreate(ClUint32T entityId, ClAmsEntityT *entity, ClCorMOIdT *pMoId)
{
    ClRcT rc = CL_OK;
    ClBufferHandleT msg = 0;
    ClUint32T moIdBufferLen = 0;
    ClUint8T *moIdBuffer = NULL;
    ClCorMOIdT tempMoId;
    ClCorMOServiceIdT svcId = CL_COR_SVC_ID_PROVISIONING_MANAGEMENT;
    ClCorAttributeValueListT attrValueList = {0};

    if(!gClAmfMibLoaded) 
        svcId = CL_COR_SVC_ID_AMF_MANAGEMENT;

    /*
     *Faster than a clone
     */
    memcpy(&tempMoId, pMoId, sizeof(tempMoId));

    rc = clCorObjectCreate(&sessionId, &tempMoId, NULL);
    if(rc != CL_OK)
    {
        clLogError("COR", "AMF", "COR MO object create for entity [%s] returned [%#x]",
                   entity->name.value, rc);
        goto out;
    }

    rc = clCorMoIdServiceSet(&tempMoId, svcId);
    CL_ASSERT(rc == CL_OK);

    rc = corAmfEntityAttributeListGet(&entityId, entity, NULL, &attrValueList);
    if(rc != CL_OK) goto out;
    
    rc = clCorObjectCreateAndSet(&sessionId, &tempMoId, &attrValueList, NULL);
    if(rc != CL_OK)
    {
        clLogError("COR", "AMF", "COR MSO object create for entity [%s] returned [%#x]",
                   entity->name.value, rc);
        goto out;
    }

    rc = clBufferCreate(&msg);
    CL_ASSERT(rc == CL_OK);

    rc = VDECL_VER(clXdrMarshallClCorMOIdT, 4, 0, 0)((ClUint8T*)&tempMoId, msg, 0);
    CL_ASSERT(rc == CL_OK);

    rc = clBufferLengthGet(msg, &moIdBufferLen);
    CL_ASSERT(rc == CL_OK);

    rc = clBufferFlatten(msg, &moIdBuffer);
    CL_ASSERT(rc == CL_OK);

    /*
     * Now store this moid buffer into the entity user area for the entity key.
     */
    rc = clAmsMgmtEntityUserDataSetKey(mgmtHandle, entity, &entity->name, 
                                       (ClCharT*)moIdBuffer, moIdBufferLen);
    if(rc != CL_OK)
    {
        clLogError("COR", "AMF", "Entity user data set for [%s] returned [%#x]",
                   entity->name.value, rc);
        goto out;
    }

    out:
    if(attrValueList.pAttributeValue) clHeapFree(attrValueList.pAttributeValue);
    if(moIdBuffer) clHeapFree(moIdBuffer);
    if(msg) clBufferDelete(&msg);
    return rc;
}
ClRcT _clCpmComponentListAll(ClInt32T argc, ClCharT **retStr)
{
    ClCntNodeHandleT hNode = 0;
    ClCpmComponentT *comp = NULL;
    ClUint32T rc = CL_OK, count;
    ClCpmEOListNodeT *eoList = NULL;
    ClCharT state[10] = "\0";
    ClCharT status[10] = "\0";
    ClCharT tempStr[256];
    ClCharT *tmpStr = NULL;
    ClBufferHandleT message;
    ClCharT cpmCompName[CL_MAX_NAME_LENGTH] = {0};
    
    rc = clBufferCreate(&message);
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("Unable to create message %x\n", rc), rc);

    if (argc != ONE_ARGUMENT)
    {
        rc = clBufferNBytesWrite(message, (ClUint8T *) STR_AND_SIZE("Usage: compList"));
        CL_CPM_CHECK(CL_DEBUG_ERROR, ("Unable to write message %x\n", rc), rc);
        rc = CL_CPM_RC(CL_ERR_INVALID_PARAMETER);
        goto done;
    }

    count = gpClCpm->noOfComponent;
    snprintf(cpmCompName,
             CL_MAX_NAME_LENGTH-1,
             "%s_%s",
             CL_CPM_COMPONENT_NAME,
             gpClCpm->pCpmLocalInfo->nodeName);

    rc = clCntFirstNodeGet(gpClCpm->compTable, &hNode);
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to Get First component \n"), rc);

    rc = clBufferNBytesWrite(message, (ClUint8T *) STR_AND_SIZE("################### List Of Components ########################\n"));
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to write message \n"), rc);

    rc = clCntNodeUserDataGet(gpClCpm->compTable, hNode,(ClCntDataHandleT *) &comp);
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to Get Node  Data \n"), rc);

    rc = clBufferNBytesWrite(message, STR_AND_SIZE("        CompName              | compId  |  eoPort  |   PID   | RestartCount  | PresenceState\n"));
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to write message \n"), rc);

    rc = clBufferNBytesWrite(message, STR_AND_SIZE("\t\t     ID |     Port |     Name  |    Health |Recv Threads \n"));   
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to write message \n"), rc);

    rc = clBufferNBytesWrite(message, STR_AND_SIZE("========================================================================================\n"));    
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to write message \n"), rc);

    while (count)
    {
        
        if (strcmp(comp->compConfig->compName, cpmCompName)!=0)  /* Skip if it is the CPM component */
        {
            
            eoList = comp->eoHandle;

            int len = sprintf(tempStr, "%30s| 0x%x | 0x%x |%8d |%14d |%15s\n",
                              comp->compConfig->compName, comp->compId,
                              comp->eoPort, comp->processId, comp->compRestartCount,
                              _cpmPresenceStateNameGet(comp->compPresenceState));
            
            if (comp->compConfig->compProperty != CL_AMS_COMP_PROPERTY_SA_AWARE)
            {

                if(!eoList || !eoList->eoptr)
                {
                    len += snprintf(tempStr + len, sizeof(tempStr) - len, 
                                    "\t\t   0x%x  |  0x%x   |%10s |%10s |%04d \n",
                                    0, 0, "-", "-", 0);
                }
            }

            rc = clBufferNBytesWrite(message, (ClUint8T *) tempStr,len);
            CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to write message \n"), rc);

            while (eoList != NULL && eoList->eoptr != NULL)
            {
                compMgrStateStatusGet(eoList->status, eoList->eoptr->state, status, sizeof(status),
                                      state, sizeof(state));
                int noOfThreads = (eoList->eoptr->appType == CL_EO_USE_THREAD_FOR_RECV) ? eoList->eoptr->noOfThreads + 1: eoList->eoptr->noOfThreads;

                int len = sprintf(tempStr, "\t\t   0x%llx  |  0x%x   |%10s |%10s |%04d \n",
                                  eoList->eoptr->eoID, eoList->eoptr->eoPort,
                                  eoList->eoptr->name, status,
                                  noOfThreads);
                rc = clBufferNBytesWrite(message, (ClUint8T *) tempStr, len);
                CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to write message \n"), rc);
                eoList = eoList->pNext;
            }
        
            rc = clBufferNBytesWrite(message, STR_AND_SIZE("-----------------------------------------------------------------------------------------\n"));
            CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to write message \n"), rc);
        
        }
        
        count--;
        if (count)
        {
            rc = clCntNextNodeGet(gpClCpm->compTable, hNode, &hNode);
            CL_CPM_CHECK(CL_DEBUG_ERROR, ("Unable to Get Node  Data %x\n", rc), rc);

            rc = clCntNodeUserDataGet(gpClCpm->compTable, hNode, (ClCntDataHandleT *) &comp);
            CL_CPM_CHECK(CL_DEBUG_ERROR, ("Unable to Get Node user Data %x\n", rc), rc);
        }

    }

    /*
     * Bug 4986 :
     * Moved the code to NULL terminate the string
     * below the done: label, so that the usage string
     * written to the buffer is also NULL terminated.
     */
done:
    /*
     * NULL terminate the string 
     */
    rc = clBufferNBytesWrite(message, (ClUint8T *) "\0", 1);
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to write message \n"), rc);

    rc = clBufferFlatten(message, (ClUint8T **) &tmpStr);
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("Unable to flatten the message \n"), rc);

    *retStr = tmpStr;
    
    clBufferDelete(&message);
    return rc;

failure:
    clBufferDelete(&message);
    return rc;
}
ClRcT clCpmExecutionObjectListShow(ClInt32T argc,
                                   ClIocNodeAddressT compAddr,
                                   ClUint32T flag,
                                   ClEoIdT eoId,
                                   ClCharT **retStr)
{
    /*
     * ClCpmEOListNodeT* ptr = gpClCpm->eoList;
     */
    ClCpmEOListNodeT *ptr = NULL;
    ClCharT name[32] = "\0";
    ClCharT state[10] = "\0";
    ClCharT status[10] = "\0";
    ClUint32T compCount = 0;
    ClCntNodeHandleT hNode = 0;
    ClCpmComponentT *comp = NULL;
    ClRcT rc = CL_OK;
    ClCharT tempStr[256];
    ClCharT *tmpStr = NULL;
    ClBufferHandleT message = 0;

    rc = clBufferCreate(&message);
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("Unable to create message %x\n", rc), rc);

    if (argc != ONE_ARGUMENT)
    {
        sprintf(tempStr, "Usage: EOShow");
        rc = clBufferNBytesWrite(message, (ClUint8T *) tempStr,
                                        strlen(tempStr));
        CL_CPM_CHECK(CL_DEBUG_ERROR, ("Unable to write message %x\n", rc), rc);
        goto done;
    }


    sprintf(tempStr,
            "\n   ID  |   Port   |   Name    |   Health   |   EO State   | Recv Threads ");
    rc = clBufferNBytesWrite(message, (ClUint8T *) tempStr,
                                    strlen(tempStr));
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("Unable to write message %x\n", rc), rc);

    sprintf(tempStr,
            "\n ===================================================================== ");
    rc = clBufferNBytesWrite(message, (ClUint8T *) tempStr,
                                    strlen(tempStr));
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("Unable to write message %x\n", rc), rc);

    /*
     * take the semaphore 
     */
    if ((rc = clOsalMutexLock(gpClCpm->eoListMutex)) != CL_OK)
        CL_DEBUG_PRINT(CL_DEBUG_ERROR,
                       ("Could not get Lock successfully------\n"));

    rc = clCntFirstNodeGet(gpClCpm->compTable, &hNode);
    CL_CPM_LOCK_CHECK(CL_DEBUG_ERROR, ("Unable to Get First component \n"), rc);

    rc = clCntNodeUserDataGet(gpClCpm->compTable, hNode,
                              (ClCntDataHandleT *) &comp);
    CL_CPM_LOCK_CHECK(CL_DEBUG_ERROR, ("Unable to Get Node  Data \n"), rc);

    compCount = gpClCpm->noOfComponent;
    while (compCount != 0)
    {
        ptr = comp->eoHandle;

        if (flag == 0)
        {
            while (ptr != NULL && ptr->eoptr != NULL)
            {
                strcpy(name, ptr->eoptr->name);
                /*
                 * Obtain the state and status in string format 
                 */
                compMgrStateStatusGet(ptr->status, ptr->eoptr->state, status, sizeof(status),
                                      state, sizeof(state));
                if (ptr->eoptr->appType == CL_EO_USE_THREAD_FOR_RECV)
                    sprintf(tempStr, "\n 0x%llx| 0x%x | %10s | %10s | %10s | %04d ",
                            ptr->eoptr->eoID, ptr->eoptr->eoPort, name, status,
                            state, (ptr->eoptr->noOfThreads + 1));
                else
                    sprintf(tempStr, "\n 0x%llx| 0x%x | %10s | %10s | %10s | %04d ",
                            ptr->eoptr->eoID, ptr->eoptr->eoPort, name, status,
                            state, ptr->eoptr->noOfThreads);
                rc = clBufferNBytesWrite(message, (ClUint8T *) tempStr,
                                                strlen(tempStr));
                CL_CPM_LOCK_CHECK(CL_DEBUG_ERROR,
                                  ("\n Unable to write message \n"), rc);
                ptr = ptr->pNext;
            }
        }
        else
        {
            while (ptr != NULL && ptr->eoptr != NULL)
            {
                if (ptr->eoptr->eoID == eoId)
                {
                    /*
                     * obtain the state and status in string format 
                     */
                    compMgrStateStatusGet(ptr->status, ptr->eoptr->state,
                                          status, sizeof(status), state, sizeof(state));
                    strcpy(name, ptr->eoptr->name);

                    if (ptr->eoptr->appType == CL_EO_USE_THREAD_FOR_RECV)
                        sprintf(tempStr, "\n 0x%llx| 0x%x | %10s | %10s | %10s | %04d | ",
                                ptr->eoptr->eoID, ptr->eoptr->eoPort, name,
                                status, state, ptr->eoptr->noOfThreads + 1);
                    else
                        sprintf(tempStr, "\n 0x%llx| 0x%x | %10s | %10s | %10s | %04d | ",
                                ptr->eoptr->eoID, ptr->eoptr->eoPort, name,
                                status, state, ptr->eoptr->noOfThreads);
                    rc = clBufferNBytesWrite(message,
                                                    (ClUint8T *) tempStr,
                                                    strlen(tempStr));
                    CL_CPM_LOCK_CHECK(CL_DEBUG_ERROR,
                                      ("\n Unable to write message \n"), rc);
                    break;
                }
                ptr = ptr->pNext;
            }
#if 0
            if (ptr == NULL)
                CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("EOID not found\n"));
#endif
        }
        compCount--;
        if (compCount)
        {
            rc = clCntNextNodeGet(gpClCpm->compTable, hNode, &hNode);
            CL_CPM_LOCK_CHECK(CL_DEBUG_ERROR,
                              ("\n Unable to Get Node  Data \n"), rc);
            rc = clCntNodeUserDataGet(gpClCpm->compTable, hNode,
                                      (ClCntDataHandleT *) &comp);
            CL_CPM_LOCK_CHECK(CL_DEBUG_ERROR,
                              ("\n Unable to Get Node  Data \n"), rc);
        }
    }
    /*
     * Release the semaphore 
     */
    rc = clOsalMutexUnlock(gpClCpm->eoListMutex);
    CL_CPM_CHECK(CL_DEBUG_ERROR,
                 ("COMP_MGR: Could not UnLock successfully------\n"), rc);

    /*
     * Bug 4986 :
     * Moved the code to NULL terminate the string
     * below the done: label, so that the usage string
     * written to the buffer is also NULL terminated.
     */
  done:
    /*
     * NULL terminate the string 
     */
    sprintf(tempStr, "%s", "\0");
    rc = clBufferNBytesWrite(message, (ClUint8T *) tempStr, 1);
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("\n Unable to write message \n"), rc);

    /*
     * Construct the return buffer 
     */
    rc = clBufferFlatten(message, (ClUint8T **) &tmpStr);
    CL_CPM_CHECK(CL_DEBUG_ERROR, ("Unable to flatten the message \n"), rc);

    *retStr = tmpStr;
    
    clBufferDelete(&message);

    return (CL_OK);

  withlock:
    /*
     * Release the semaphore 
     */
    rc = clOsalMutexUnlock(gpClCpm->eoListMutex);
    CL_CPM_CHECK(CL_DEBUG_ERROR,
                 ("COMP_MGR: Could not UnLock successfully------\n"), rc);
  failure:
    clBufferDelete(&message);
    return rc;
}
static ClRcT clLogMasterEoEntryCheckpoint(ClLogMasterEoDataT *pMasterEoEntry)
{
    ClRcT            rc          = CL_OK;
    ClBufferHandleT  hEoEntryBuf = CL_HANDLE_INVALID_VALUE;
    ClUint8T         *pBuffer    = NULL;
    ClUint32T        bufferLen   = 0;
    ClVersionT       version     = { CL_RELEASE_VERSION, CL_MAJOR_VERSION, CL_MINOR_VERSION };
    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clBufferCreate(&hEoEntryBuf);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferCreate(): rc[0x %x]", rc));
        return rc;
    }

    rc = clXdrMarshallClVersionT(&version, hEoEntryBuf, 0);

    if( CL_OK != rc)
    {
        CL_LOG_DEBUG_ERROR(("clXdrMarshallClVersionT(): rc[%#x]", rc));
        CL_LOG_CLEANUP(clBufferDelete(&hEoEntryBuf), CL_OK);
        return rc;
    }

    rc = clXdrMarshallClUint16T(&(pMasterEoEntry->nextStreamId),
                                hEoEntryBuf, 0);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clXdrMarshallClUint16T(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clBufferDelete(&hEoEntryBuf), CL_OK);
        return rc;
    }

    rc = clBufferLengthGet(hEoEntryBuf, &bufferLen);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferLengthGet: rc[0x %x]", rc));
        CL_LOG_CLEANUP(clBufferDelete(&hEoEntryBuf), CL_OK);
        return rc;
    }

    rc = clBufferFlatten(hEoEntryBuf, &pBuffer);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferFlatten(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clBufferDelete(&hEoEntryBuf), CL_OK);
        return rc;
    }

    rc = clCkptSectionOverwrite(pMasterEoEntry->hCkpt, &gLogMasterDefaultSectionId, pBuffer, bufferLen);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clCkptSectionOverwrite(): rc[0x %x]", rc));
    }

    clHeapFree(pBuffer);
    CL_LOG_CLEANUP(clBufferDelete(&hEoEntryBuf), CL_OK);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
int clGmsSendMsg(ClGmsViewMemberT       *memberNodeInfo,
                 ClGmsGroupIdT           groupId, 
                 ClGmsMessageTypeT       msgType,
                 ClGmsMemberEjectReasonT ejectReason,
                 ClUint32T               dataSize,
                 ClPtrT                  dataPtr)
{
    mar_req_header_t        header = {0};
    struct VDECL(req_exec_gms_nodejoin) req_exec_gms_nodejoin = {{0}};
    struct iovec    req_exec_gms_iovec = {0};
    int             result = -1;
    ClRcT           rc = CL_OK;
    ClUint32T       clusterVersion = 0;
    ClBufferHandleT bufferHandle = 0;
    ClUint8T        *message = NULL;
    ClUint32T       length = 0;
    ClPtrT          temp = NULL;

    rc = clNodeCacheMinVersionGet(NULL, &clusterVersion);
    if(clusterVersion >= CL_VERSION_CODE(5, 0, 0) 
       && 
       clAspNativeLeaderElection())
    {
        clLog(DBG, OPN, AIS, 
              "Skipped sending msgtype [%d] since node cache is used to form the cluster view",
              msgType);
        return 0;
    }

    if (rc != CL_OK)
    {
        clLog(ERROR,OPN,AIS,
                "Error while getting version from the version cache. rc 0x%x",rc);

        curVer.releaseCode = CL_RELEASE_VERSION;
        curVer.majorVersion = CL_MAJOR_VERSION;
        curVer.minorVersion = CL_MINOR_VERSION;
    } 
    else 
    {
        curVer.releaseCode = CL_VERSION_RELEASE(clusterVersion);
        curVer.majorVersion = CL_VERSION_MAJOR(clusterVersion);
        curVer.minorVersion = CL_VERSION_MINOR(clusterVersion);
    }

    /* Get the version and send it */
    req_exec_gms_nodejoin.version.releaseCode = curVer.releaseCode;
    req_exec_gms_nodejoin.version.majorVersion = curVer.majorVersion;
    req_exec_gms_nodejoin.version.minorVersion = curVer.minorVersion;

    /* For now we send message without caring about version. Later on
     * we need to change it accordingly */
    
    switch(msgType)
    {
        case CL_GMS_CLUSTER_JOIN_MSG:
        case CL_GMS_CLUSTER_LEAVE_MSG:
        case CL_GMS_CLUSTER_EJECT_MSG:
            clLog(DBG,OPN,AIS,
                    "Sending cluster %s multicast message",
                    msgType == CL_GMS_CLUSTER_JOIN_MSG ? "join":
                    msgType == CL_GMS_CLUSTER_LEAVE_MSG ? "leave" : "eject");
            req_exec_gms_nodejoin.ejectReason = ejectReason;
            memcpy (&req_exec_gms_nodejoin.specificMessage.gmsClusterNode, &memberNodeInfo->clusterMember,
                    sizeof (ClGmsClusterMemberT));
            req_exec_gms_nodejoin.contextHandle = memberNodeInfo->contextHandle;
            break;
        case CL_GMS_GROUP_CREATE_MSG:
        case CL_GMS_GROUP_DESTROY_MSG:
        case CL_GMS_GROUP_JOIN_MSG:
        case CL_GMS_GROUP_LEAVE_MSG:
            clLog(DBG,OPN,AIS,
                    "Sending group %s multicast message",
                    msgType == CL_GMS_GROUP_CREATE_MSG ? "create" : 
                    msgType == CL_GMS_GROUP_DESTROY_MSG ? "destroy" :
                    msgType == CL_GMS_GROUP_JOIN_MSG ? "join" : "leave");
            memcpy (&req_exec_gms_nodejoin.specificMessage.groupMessage.gmsGroupNode, 
                    &memberNodeInfo->groupMember,
                    sizeof (ClGmsGroupMemberT));
            memcpy (&req_exec_gms_nodejoin.specificMessage.groupMessage.groupData, 
                    &memberNodeInfo->groupData,
                    sizeof(ClGmsGroupInfoT));
            req_exec_gms_nodejoin.contextHandle = memberNodeInfo->contextHandle;
            break;
        case CL_GMS_COMP_DEATH:
            clLog(DBG,OPN,AIS,
                    "Sending comp death multicast message");
            memcpy (&req_exec_gms_nodejoin.specificMessage.groupMessage.gmsGroupNode, 
                    &memberNodeInfo->groupMember,
                    sizeof (ClGmsGroupMemberT));
            req_exec_gms_nodejoin.contextHandle = memberNodeInfo->contextHandle;
            break;
        case CL_GMS_LEADER_ELECT_MSG:
            clLog(DBG,OPN,AIS,
                    "Sending leader elect multicast message");
            memcpy (&req_exec_gms_nodejoin.specificMessage.gmsClusterNode, &memberNodeInfo->clusterMember,
                    sizeof (ClGmsClusterMemberT));
            req_exec_gms_nodejoin.contextHandle = memberNodeInfo->contextHandle;
            break;
        case CL_GMS_SYNC_MESSAGE:
            clLog(DBG,OPN,AIS,
                    "Sending gms synch multicast message");
            req_exec_gms_nodejoin.dataPtr = dataPtr;
            break;
        case CL_GMS_GROUP_MCAST_MSG:
            memcpy (&req_exec_gms_nodejoin.specificMessage.mcastMessage.groupInfo.gmsGroupNode, 
                    &memberNodeInfo->groupMember,
                    sizeof (ClGmsGroupMemberT));
            memcpy (&req_exec_gms_nodejoin.specificMessage.mcastMessage.groupInfo.groupData,
                    &memberNodeInfo->groupData,
                    sizeof(ClGmsGroupInfoT));
            req_exec_gms_nodejoin.contextHandle = memberNodeInfo->contextHandle;
            req_exec_gms_nodejoin.specificMessage.mcastMessage.userDataSize = dataSize;
            req_exec_gms_nodejoin.dataPtr = dataPtr;
            break;
        default:
            clLog(DBG,OPN,AIS,
                    "Requested wrong message to be multicasted. Message type %d",
                    msgType);
            return CL_GMS_RC(CL_ERR_INVALID_PARAMETER);
    }

    req_exec_gms_nodejoin.gmsMessageType = msgType;
    req_exec_gms_nodejoin.gmsGroupId = groupId;

    /* Create a buffer handle and marshall the eliments */
    rc = clBufferCreate(&bufferHandle);
    if (rc != CL_OK)
    {
        clLogError(OPN,AIS,
                "Failed to create buffer while sending message on totem. rc 0x%x",rc);
        return rc;
    }

    rc = marshallReqExecGmsNodeJoin(&req_exec_gms_nodejoin,bufferHandle);
    if (rc != CL_OK)
    {
        clLogError(OPN,AIS,
                "Failed to marshall the data while sending message on totem. rc 0x%x",rc);
        goto buffer_delete_return;
    }

    rc = clBufferLengthGet(bufferHandle, &length);
    if (rc != CL_OK)
    {
        clLogError(OPN,AIS,
                "Failed to get buffer length. rc 0x%x",rc);
        goto buffer_delete_return;
    }

    rc = clBufferFlatten(bufferHandle, &message);
    if (rc != CL_OK)
    {
        clLogError(OPN,AIS,
                "clBufferFlatten failed with rc 0x%x",rc);
        goto buffer_delete_return;
    }

    header.id = SERVICE_ID_MAKE (GMS_SERVICE, MESSAGE_REQ_EXEC_GMS_NODEJOIN);
    header.size = length + sizeof(mar_req_header_t);

    /* We need to prepend the total message length in the beginning of the
     * message so that we can find the length while unmarshalling */
    temp = clHeapAllocate(header.size);
    if (temp == NULL)
    {
        clLogError(OPN,AIS, 
                "Failed to allocate memory while sending the message");
        goto buffer_delete_return;
    }

    memcpy(temp,&header, sizeof(mar_req_header_t));
    memcpy(temp+sizeof(mar_req_header_t), message, length);

    req_exec_gms_iovec.iov_base = temp;
    req_exec_gms_iovec.iov_len = length + sizeof(mar_req_header_t);

    result = totempg_groups_mcast_joined (openais_group_handle, &req_exec_gms_iovec, 1, TOTEMPG_AGREED);

    clLog(DBG,OPN,AIS,
            "Done with sending multicast message of type %d",msgType);

buffer_delete_return:
    if (message != NULL)
        clHeapFree(message);

    if (temp != NULL)
        clHeapFree(temp);

    clBufferDelete(&bufferHandle);
    return result;
}
static int gms_nodejoin_send (void)
{
    /* For now this function sends only latest version. It needs to be 
     * modified in future when version changes */
    /* Send the join message with given version */
    mar_req_header_t                    header = {0};
	struct VDECL(req_exec_gms_nodejoin) req_exec_gms_nodejoin;
	struct iovec                 req_exec_gms_iovec;
    ClGmsClusterMemberT          thisGmsClusterNode;
	int                          result;
    ClRcT                        rc = CL_OK;
    ClUint32T                    clusterVersion;
    ClBufferHandleT bufferHandle = 0;
    ClUint8T        *message = NULL;
    ClPtrT          temp = NULL;
    ClUint32T       length = 0;

    rc = clNodeCacheMinVersionGet(NULL, &clusterVersion);
    if (rc != CL_OK)
    {
        clLog(ERROR,OPN,AIS,
                "Error while getting version from the version cache. rc 0x%x",rc);

        curVer.releaseCode = CL_RELEASE_VERSION;
        curVer.majorVersion = CL_MAJOR_VERSION;
        curVer.minorVersion = CL_MINOR_VERSION;
    } else {
        curVer.releaseCode = CL_VERSION_RELEASE(clusterVersion);
        curVer.majorVersion = CL_VERSION_MAJOR(clusterVersion);
        curVer.minorVersion = CL_VERSION_MINOR(clusterVersion);
    }

    clLog(DBG,OPN,AIS,
            "This node is sending join message for version %d, %d, %d",
            curVer.releaseCode, curVer.majorVersion, curVer.minorVersion);
    /* Get the version and send it */
    req_exec_gms_nodejoin.version.releaseCode = curVer.releaseCode;
    req_exec_gms_nodejoin.version.majorVersion = curVer.majorVersion;
    req_exec_gms_nodejoin.version.minorVersion = curVer.minorVersion;

    _clGmsGetThisNodeInfo(&thisGmsClusterNode);

    // node join is send for default cluster group - 0
    req_exec_gms_nodejoin.gmsGroupId     = 0;
    memcpy (&req_exec_gms_nodejoin.specificMessage.gmsClusterNode, &thisGmsClusterNode, sizeof (ClGmsClusterMemberT));
    req_exec_gms_nodejoin.gmsMessageType = CL_GMS_CLUSTER_JOIN_MSG; 

    /* Create a buffer handle and marshall the elements */
    rc = clBufferCreate(&bufferHandle);
    if (rc != CL_OK)
    {
        clLogError(OPN,AIS,
                "Failed to create buffer while sending message on totem. rc 0x%x",rc);
        return rc;
    }

    rc = marshallReqExecGmsNodeJoin(&req_exec_gms_nodejoin,bufferHandle);
    if (rc != CL_OK)
    {
        clLogError(OPN,AIS,
                "Failed to marshall the data while sending message on totem. rc 0x%x",rc);
        goto buffer_delete_return;
    }

    rc = clBufferLengthGet(bufferHandle, &length);
    if (rc != CL_OK)
    {
        clLogError(OPN,AIS,
                "Failed to get buffer length. rc 0x%x",rc);
        goto buffer_delete_return;
    }

    rc = clBufferFlatten(bufferHandle, &message);
    if (rc != CL_OK)
    {
        clLogError(OPN,AIS,
                "clBufferFlatten failed with rc 0x%x",rc);
        goto buffer_delete_return;
    }

    /* We need to prepend the total message length in the beginning of the
     * message so that we can find the length while unmarshalling */
    temp = clHeapAllocate(length + sizeof(mar_req_header_t));
    if (temp == NULL)
    {
        clLogError(OPN,AIS, 
                "Failed to allocate memory while sending the message");
        goto buffer_delete_return;
    }

	header.id = SERVICE_ID_MAKE (GMS_SERVICE, MESSAGE_REQ_EXEC_GMS_NODEJOIN);
    header.size = length + sizeof(mar_req_header_t);

    memcpy(temp,&header,sizeof(mar_req_header_t));
    memcpy(temp+sizeof(mar_req_header_t), message, length);

    req_exec_gms_iovec.iov_base = temp;
    req_exec_gms_iovec.iov_len = length + sizeof(mar_req_header_t);

    clLog(DBG,OPN,AIS,
            "Sending node join from this node in sync_process");
    result = totempg_groups_mcast_joined (openais_group_handle, &req_exec_gms_iovec, 1, TOTEMPG_AGREED);

buffer_delete_return:
    if (message != NULL)
        clHeapFree(message);
    if (temp != NULL)
        clHeapFree(temp);
    clBufferDelete(&bufferHandle);
    return result;
}