打印

代码重构是敏捷开发的必要条件。

[复制链接]
841|1
手机看帖
扫描二维码
随时随地手机跟帖
沙发
keer_zu|  楼主 | 2015-11-23 13:07 | 只看该作者
比如这样一个函数,有重构的必要吗?
如何重构?



void DsManage::OnDocSvrRegister(RegisterInfo &regInfo, TsSession &session)
{
    string json;
    TsMessageCodec tmpTsMessageCodec;
    RegisterResponse* pResponse = new RegisterResponse;
    u32 dwTsIndex=0;
    u32 dwTsFreeIndex = 0;
    bool bFind = FALSE;
    bool bResult = TRUE;
    TsType tsType = regInfo.GetTsType();
    LoggerWrapper dsLog= LoggerWrapper::GetInstance();
    dsLog.Log(TRUE,LOGGER_DEBUG,"session:%d,GetTsId:%s,GetTsIp:%s,GetTstype:%d , [%s][%s][%d]\n",&session,regInfo.GetTsId().c_str(),regInfo.GetTsIp().c_str(),regInfo.GetTsType(),__FILE__,__PRETTY_FUNCTION__,__LINE__);   

//    printf("GetTsId:%s,GetTsIp:%s,GetTstype:%d\n",regInfo.GetTsId().c_str(),regInfo.GetTsIp().c_str(),regInfo.GetTsType());
    //添加注册的转换服务器进入转换服务器队列
    for(dwTsIndex=1;dwTsIndex<MAX_DOC_TRANS_SERVER_NUM;dwTsIndex++)
    {
        if(TS_UNREGISTERED == g_cDsManage.DocTs[dwTsIndex].GetState())
        {
            if(FALSE == bFind)
            {
                bFind = TRUE;
                dwTsFreeIndex = dwTsIndex;
            }
            
        }
        else if((TS_FREE == g_cDsManage.DocTs[dwTsIndex].GetState()) ||(TS_RUNNING== g_cDsManage.DocTs[dwTsIndex].GetState()))
        {
            if((g_cDsManage.DocTs[dwTsIndex].GetIpAddr() == regInfo.GetTsIp()) && (g_cDsManage.DocTs[dwTsIndex].GetTsId() == regInfo.GetTsId()))
            {
                bResult = FALSE;
                break;
            }
        }
    }

    if(bFind && bResult)
    {
        DocTs[dwTsFreeIndex].SetIpAddr(regInfo.GetTsIp());
        DocTs[dwTsFreeIndex].SetTsId( regInfo.GetTsId());
        DocTs[dwTsFreeIndex].SetTsType( regInfo.GetTsType());
        DocTs[dwTsFreeIndex].SetTsSession( &session);
        DocTs[dwTsFreeIndex].SetState(TS_FREE);
    }
    //发送响应消息给转换服务器
    if(bFind && bResult)
    {
        pResponse->SetResult(TRUE);
        pResponse->SetErrorCode(RET_OK);
        pResponse->SetErrorDetail("");
    }
    else if(FALSE == bResult)
    {
        pResponse->SetResult(FALSE);
        pResponse->SetErrorCode(RET_E_REPEATED_TS_ID);
        pResponse->SetErrorDetail("Same ts id   can not be registered repeated!");
    }
    else if(FALSE == bFind)
    {
        pResponse->SetResult(FALSE);
        pResponse->SetErrorCode(RET_E_TS_USED_UP);
        pResponse->SetErrorDetail("No enough ts resource can be registered!");
    }

    session.SendMessage(*pResponse);
    if((FALSE == bResult) || (FALSE == bFind))
    {
        session.Close();
        return;
    }

    //如果注册成功检查任务等待队列是否有转换任务在等待,没有则直接返回
    //如果有,则将该任务分配给该服务器,并发送转换命令给转换服务器,同时将该任务添加到运行队列

    if (( !m_TransDocWaitQueue.empty() ) && (TRUE == bResult)&& (TRUE == bFind))
    {
        AppTaskMessage*  pTaskMessage = m_TransDocWaitQueue.top( );
//                printf("4444  target:%s,prefix:%s,type:%d\n",pTaskMessage->GetTargetFullPathFileName().c_str(),pTaskMessage->GetFilePrefix().c_str(),pTaskMessage->GetMessagePara().unTransDoc.enumTansType);        
//            printf("m_TransDocWaitQueue.size:%d\n",m_TransDocWaitQueue.size());  
        m_TransDocWaitQueue.pop( );      
        TaskTimeInfo tTaskTimeInfo ;
        if(FALSE == DsGetTimeInfo(&tTaskTimeInfo))
        {
            return;
        }
        pTaskMessage->SetTaskTimeInfo(tTaskTimeInfo);

        m_TransDocRunQueue.push_back(pTaskMessage);

        DocTs[dwTsFreeIndex].SetMessageInTs(pTaskMessage);
        DocTs[dwTsFreeIndex].SetState(TS_RUNNING);
        pTaskMessage->SetTsNum(dwTsFreeIndex);
        
        TransitionRequest* pTransitionRequest = new TransitionRequest;
        pTransitionRequest->SetMessageId(pTaskMessage->GetMessageId());
        pTransitionRequest->SetSrcFile(pTaskMessage->GetFullPathFileName());
        pTransitionRequest->SetTargetFolder(pTaskMessage->GetTargetFullPathFileName());
        pTransitionRequest->SetPrefix(pTaskMessage->GetFilePrefix());
        pTransitionRequest->SetTransType(pTaskMessage->GetMessagePara().unTransDoc.enumTansType);

        DocTs[dwTsFreeIndex].GetTsSession()->SendMessage(*pTransitionRequest);
//                printf("55555  target:%s,prefix:%s,type:%d\n",pTaskMessage->GetTargetFullPathFileName().c_str(),pTaskMessage->GetFilePrefix().c_str(),pTaskMessage->GetMessagePara().unTransDoc.enumTansType);        
//               printf("m_TransDocWaitQueue.size:%d\n",m_TransDocWaitQueue.size());         
        if(NULL !=  pTransitionRequest)
        {
            delete pTransitionRequest;
            pTransitionRequest = NULL;
        }
//                printf("666  target:%s,prefix:%s,type:%d\n",pTaskMessage->GetTargetFullPathFileName().c_str(),pTaskMessage->GetFilePrefix().c_str(),pTaskMessage->GetMessagePara().unTransDoc.enumTansType);        
//               printf("m_TransDocWaitQueue.size:%d\n",m_TransDocWaitQueue.size());         

    }

}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1349

主题

12425

帖子

53

粉丝