int rmb_pub_get_target_dcn()

in eventmesh-sdks/eventmesh-sdk-c/src/rmb_pub.c [1342:1477]


int rmb_pub_get_target_dcn (StRmbPub * pStPub, StRmbMsg * pStMsg)
{
  //search cache
  //int iFLagForReqGsl = 1;
  StServiceStatus tmpStatus;
  tmpStatus.ulInvalidTime = 0;
  StServiceStatus *pTmpService = &tmpStatus;

  pTmpService->cFlagForOrgId = pStMsg->iFLagForOrgId;
  if (pStMsg->iFLagForOrgId == RMB_COMMIT_BY_OWN)
  {
    strncpy (pTmpService->strTargetOrgId, pStMsg->strTargetOrgId,
             sizeof (pTmpService->strTargetOrgId) - 1);
  }
  else
  {
    strncpy (pTmpService->strTargetOrgId, pRmbStConfig->strOrgId,
             sizeof (pTmpService->strTargetOrgId) - 1);
  }
  strncpy (pTmpService->strServiceId, pStMsg->strServiceId,
           sizeof (pTmpService->strServiceId) - 1);
  strncpy (pTmpService->strScenarioId, pStMsg->strScenarioId,
           sizeof (pTmpService->strScenarioId) - 1);

  StServiceStatus *tmpService =
    bsearch (pTmpService, pRmbStConfig->serviceStatusList,
             pRmbStConfig->iCacheServiceNums, sizeof (StServiceStatus),
             cmpServiceStatusStr);
  if (tmpService != NULL)
  {
    //if (tmpService->ulGetTimes + pRmbStConfig->iCacheTimeoutTime * 1000 >= pRmbStConfig->ulNowTtime)
    if (tmpService->ulInvalidTime >= pRmbStConfig->ulNowTtime)
    {
      //if (tmpService->cResult == 1)
//                      if (tmpService->cResult == 0x11 || tmpService->cResult == 0x21 || tmpService->cResult == 0x31 ||
//                                      tmpService->cResult == 0x41 || tmpService->cResult == 0x51 || tmpService->cResult == 0x61)
      if ((tmpService->cResult & 0x0F) == 0x01)
      {
        LOGRMB (RMB_LOG_ERROR, "Cache:[%s-%s-%s-%d] service=NULL",
                pTmpService->strServiceId,
                pTmpService->strScenarioId,
                pTmpService->strTargetOrgId,
                (int) pTmpService->cFlagForOrgId);
        rmb_errno = RMB_ERROR_GSL_SERVICE_ID_NULL;
        return 1;
      }
//                      else if (tmpService->cResult == 2)
//                      {
//                              LOGRMB(RMB_LOG_ERROR, "Cache:[%s-%s-%s-%d] service error",
//                                      pTmpService->strServiceId,
//                                      pTmpService->strScenarioId,
//                                      pTmpService->strTargetOrgId,
//                                      (int)pTmpService->cFlagForOrgId
//                                      );
//                              rmb_errno = RMB_ERROR_GSL_SERVICE_ID_ERROR;
//                              return 2;
//                      }
      else if (tmpService->cResult == 0)
      {
        if (tmpService->cRouteFlag == 0 || tmpService->cRouteFlag == 1)
        {
          return 0;
        }
        else
        {
          //LOGRMB(RMB_LOG_DEBUG, "nowTime=%lu,Cache:[%s]", pRmbStConfig->ulNowTtime, rmb_printf_service_status(pStPub, tmpService));
          if (strcmp (pStMsg->strTargetDcn, tmpService->strTargetDcn) != 0)
          {
            LOGRMB (RMB_LOG_INFO, "GSL DCN=%s is diffrent with input DCN=%s",
                    tmpService->strTargetDcn, pStMsg->strTargetDcn);
            strncpy (pStMsg->strTargetDcn, tmpService->strTargetDcn,
                     sizeof (pStMsg->strTargetDcn) - 1);
            //LOGRMB(RMB_LOG_INFO, "GSL DCN=%s is diffrent with input DCN=%s", tmpService->strTargetDcn, pStMsg->strTargetDcn);
          }
          return 0;
        }
      }
    }
    else
    {
      LOGRMB (RMB_LOG_INFO, "cache time out!nowTime=%lu, Cache:[%s] ",
              pRmbStConfig->ulNowTtime, rmb_printf_service_status (pStPub,
                                                                   tmpService));
    }
  }
  pthread_mutex_lock (&pStPub->pubMutex);
  int iRet =
    rmb_pub_send_gsl_and_insert_cache (pStPub, pStMsg, pTmpService,
                                       tmpService);
  pthread_mutex_unlock (&pStPub->pubMutex);
  if (iRet != 0)
  {
//              if (iRet == 3 && tmpService != NULL)
    if (iRet == 4 && tmpService != NULL && tmpService->cResult == 0)
    {
      //copy dcn to msg
      if (tmpService->cRouteFlag == 2)
      {
        if (strcmp (pStMsg->strTargetDcn, tmpService->strTargetDcn) != 0)
        {
          LOGRMB (RMB_LOG_INFO, "GSL DCN=%s is diffrent with input DCN=%s",
                  tmpService->strTargetDcn, pStMsg->strTargetDcn);
          strncpy (pStMsg->strTargetDcn, tmpService->strTargetDcn,
                   sizeof (pStMsg->strTargetDcn) - 1);
          //LOGRMB(RMB_LOG_INFO, "GSL DCN=%s is diffrent with input DCN=%s", tmpService->strTargetDcn, pStMsg->strTargetDcn);
        }
      }
      LOGRMB (RMB_LOG_ERROR, "ReqGsl error,but use cache! gsl=[%s],cache=%s",
              rmb_printf_service_status (pStPub, pTmpService),
              rmb_printf_service_status (pStPub, tmpService));
      return 0;
    }
    LOGRMB (RMB_LOG_ERROR, "ReqGsl gsl=[%s]",
            rmb_printf_service_status (pStPub, pTmpService));
    return iRet;
  }
  else
  {
    //copy dcn to msg
    //
    if (pTmpService->cResult == 0 && pTmpService->cRouteFlag == 2)
    {
      if (strcmp (pStMsg->strTargetDcn, pTmpService->strTargetDcn) != 0)
      {
        LOGRMB (RMB_LOG_INFO, "GSL DCN=%s is diffrent with input DCN=%s",
                pTmpService->strTargetDcn, pStMsg->strTargetDcn);
        strncpy (pStMsg->strTargetDcn, pTmpService->strTargetDcn,
                 sizeof (pStMsg->strTargetDcn) - 1);
        //LOGRMB(RMB_LOG_INFO, "GSL DCN=%s is diffrent with input DCN=%s", pTmpService->strTargetDcn, pStMsg->strTargetDcn);
      }
    }
    LOGRMB (RMB_LOG_INFO, "ReqGsl succ! gsl=[%s]",
            rmb_printf_service_status (pStPub, pTmpService));
  }
  return 0;
}