/*!
 Make a new buffer for a certain connection to server with specific rank
 \param [in] rank rank of connected server
 */
 void CContextClient::newBuffer(int rank)
 {
    if (!mapBufferSize_.count(rank))
    {
      error(0) << "WARNING: Unexpected request for buffer to communicate with server " << rank << std::endl;
      mapBufferSize_[rank] = CXios::minBufferSize;
      maxEventSizes[rank] = CXios::minBufferSize;
    }
    CClientBuffer* buffer = buffers[rank] = new CClientBuffer(interComm, rank, mapBufferSize_[rank], maxEventSizes[rank], maxBufferedEvents);
    // Notify the server
    CBufferOut* bufOut = buffer->getBuffer(sizeof(StdSize));
    bufOut->put(mapBufferSize_[rank]); // Stupid C++
    buffer->checkBuffer();
 }
/**
 * @brief
 *  Registers the buffer pBufObj to the client ClientID.
 *
 * @param byAction
 *  contains one of the values MSGBUF_ADD or MSGBUF_CLEAR.
 * @param ClientID
 *  ClientID, is the client ID.
 * @param [in,out]  pBufObj
 *  pBufObj, is pointer to CBaseCANBufFSE object.
 *
 * @return
 *  S_FALSE for failure.
 *  S_OK for success,
 *
 * @remarks  TODO: Don't forget to fill in the remarks!
 */
HRESULT CDIL_CAN_IXXAT_VCI::CAN_ManageMsgBuf(BYTE byAction, DWORD ClientID, CBaseCANBufFSE* pBufObj)
{
#ifdef _IXXAT_DEBUG
    LogMessage(TRUE, "------> CDIL_CAN_IXXAT_VCI::CAN_ManageMsgBuf  - Action: %u  ClientID: %u\n", byAction, ClientID);
#endif
    HRESULT hResult = S_FALSE;
    if (pBufObj)
    {
        CClientBuffer* pClientBuf = m_ClientList.GetClientByID(ClientID);
        if (pClientBuf)
        {
            if (byAction == MSGBUF_ADD)
            {
                /* Add msg buffer */
                hResult = pClientBuf->AddMsgBuf(pBufObj);
            }
            else if (byAction == MSGBUF_CLEAR)
            {
                /* clear msg buffer */
                if (pBufObj != nullptr)
                {
                    hResult = pClientBuf->RemoveMsgBuf(pBufObj);
                }
                else
                {
                    // remove all message buffers
                    hResult = pClientBuf->RemoveAllMsgBuf();
                }
            }
        }
        else
        {
            hResult = ERR_NO_CLIENT_EXIST;
        }
    }
    else
    {
        if (byAction == MSGBUF_CLEAR)
        {
            /* clear msg buffer */
            hResult = m_ClientList.RemoveAllMsgBufOnAllClients();
        }
    }
    return hResult;
}
void CCommanDIL_Flexray::vWriteIntoClientsBuffer(s_FLXMSG& sFlexRayData)
{
    //Write into the client's buffer and Increment message Count
    for (int iClientIndex=0; iClientIndex < m_ClientList.Size(); iClientIndex++)
    {
        CClientBuffer* pClient = m_ClientList.GetClientByIndex(iClientIndex);
        if (pClient)
        {
            for (int iBufIndex = 0; iBufIndex < pClient->NumOfSEBuffers(); iBufIndex++)
            {
                CBaseFLEXBufFSE* pFLEXBufSE = pClient->GetSEBufferByIndex(iBufIndex);
                if (pFLEXBufSE)
                {
                    pFLEXBufSE->WriteIntoBuffer(&sFlexRayData);
                }
            }
        }
    }
}
HRESULT CCommanDIL_Flexray::FLEXRAY_ManageMsgBuf(BYTE byAction, DWORD ClientID, CBaseFLEXBufFSE* pBufObj)
{
    HRESULT hResult = S_FALSE;
    if (pBufObj)
    {
        CClientBuffer* pClientBuf = m_ClientList.GetClientByID(ClientID);
        if (pClientBuf)
        {
            if (byAction == MSGBUF_ADD)
            {
                /* Add msg buffer */
                hResult = pClientBuf->AddMsgBuf(pBufObj);
            }
            else if (byAction == MSGBUF_CLEAR)
            {
                /* clear msg buffer */
                if (pBufObj != NULL)
                {
                    hResult = pClientBuf->RemoveMsgBuf(pBufObj);
                }
                else
                {
                    // remove all message buffers
                    hResult = pClientBuf->RemoveAllMsgBuf();
                }
            }
        }
        else
        {
            hResult = ERR_NO_CLIENT_EXIST;
        }
    }
    else
    {
        if (byAction == MSGBUF_CLEAR)
        {
            /* clear msg buffer */
            hResult = m_ClientList.RemoveAllMsgBufOnAllClients();
        }
    }
    return hResult;
}