📄 ctempfilemanager.cpp
字号:
}/***********************************************************************Function Name: TempFileManager*Sescription: 管理临时文件:循环所有存在map中的临时文件,删除已经到达* 指定时间的临时文件*Input: NONE*Output: [bool] true--成功; false--失败*Remark: 如果按时间保存文件开关关闭,那么,就不需要该函数来管理临时文件**********************************************************************/bool CTempFileManager::TempFileManager(){ bool bRet = true; string strName = ""; short nBs = 0; char cTemp[LEN_OF_CDR_FILE_NAME]; char cGgsnAddr[LEN_OF_ADDRESS+1]; char cFileName[LEN_OF_CDR_FILE_NAME]; map<string, MAPBSOBJECT>::iterator mapFileIte; map<short,CFNPManager*>::iterator mapBsAndFileInfo; map<string, short>::iterator mapBsIte; if (m_bIsSerialNumToZero) { #if IS_Thread_DEBUG { printf("\n*****pthread_mutex_lock(&m_ResetSerialMutex) TempFileManager Start *****\n"); pthread_mutex_lock( &m_ResetSerialMutex ); printf("\n*****pthread_mutex_lock(&m_ResetSerialMutex) TempFileManager End *****\n"); } #else { pthread_mutex_lock( &m_ResetSerialMutex ); } #endif }#if IS_Thread_DEBUG{ printf("\n*****pthread_mutex_lock(&m_ManagerMutex) TempFileManager Start *****\n"); pthread_mutex_lock( &m_ManagerMutex ); printf("\n*****pthread_mutex_lock(&m_ManagerMutex) TempFileManager End *****\n");}#else{ pthread_mutex_lock( &m_ManagerMutex );}#endif //如果按统一和网元策略存储,则需要加锁 if (m_nSaveUpFileType != EACHCDR) { #if IS_Thread_DEBUG { printf("\n*****pthread_mutex_lock(&m_UnionMutex) TempFileManager Start *****\n"); //给临界变量加锁 pthread_mutex_lock( &m_UnionMutex ); printf("\n*****pthread_mutex_lock(&m_UnionMutex) TempFileManager End *****\n"); } #else { //给临界变量加锁 pthread_mutex_lock( &m_UnionMutex ); } #endif } map<string, string> mapTemp = m_mapTempFileAndMacro; map<string, string>::iterator mapManagerIte = mapTemp.begin(); for(; mapManagerIte != mapTemp.end(); mapManagerIte++) { memset(cGgsnAddr, 0, sizeof(cGgsnAddr)); memset(cTemp, 0, sizeof(cTemp)); memset(cFileName, 0, sizeof(cFileName)); mapBsIte = m_mapTempFileAndBs.find(mapManagerIte->first); if (mapBsIte != m_mapTempFileAndBs.end()) { nBs = mapBsIte->second; } memcpy(cFileName, mapManagerIte->first.c_str(), mapManagerIte->first.size() + 1); strName = mapManagerIte->second; //得到三种存储方式对应的重新命名后的名字 getCdrSFNameFromMacro(strName, cTemp); //根据得到的宏串在map中得到该临时文件的信息 mapFileIte = m_mapCFNPManager.find(strName); if (mapFileIte != m_mapCFNPManager.end()) { //获得bs对应的文件的创建信息 mapBsAndFileInfo = mapFileIte->second.find(nBs); if (mapBsAndFileInfo != mapFileIte->second.end()) { if(m_nSaveUpFileType == GGSSCDR) { //得到GGSN和SGSN地址 getAddrForGgsnAndSgsn(strName, nBs, cFileName, cGgsnAddr); if (strlen(cGgsnAddr) > 0) { map<short, MAPMACROADDRINFO>::iterator mapBsGgsnAndSgsnIte; mapBsGgsnAndSgsnIte = m_mapGgsnAndSgsnFile.find(nBs); if (mapBsGgsnAndSgsnIte != m_mapGgsnAndSgsnFile.end()) { map<string, MAPADDRANDFILEINFO>::iterator mapGgsnAndSgsnFileIte; mapGgsnAndSgsnFileIte = mapBsGgsnAndSgsnIte->second.find(strName); if (mapGgsnAndSgsnFileIte != mapBsGgsnAndSgsnIte->second.end()) { map<string, sFILEINFO>::iterator mapFileAddrIte; mapFileAddrIte = mapGgsnAndSgsnFileIte->second.find(cGgsnAddr); if (mapFileAddrIte != mapGgsnAndSgsnFileIte->second.end()) { // 根据时间, 保存文件 if (isTimeOfTempFileReached(mapFileAddrIte->second.nTimeOfCdrFiltered)) { if (!saveFile(strName, nBs, cFileName, cTemp, mapBsAndFileInfo->second, cGgsnAddr)) { bRet = false; printf("save file for time is failed.\n"); break; } //删除该GGSN或SGSN地址对应的文件信息 mapGgsnAndSgsnFileIte->second.erase(mapFileAddrIte); if (mapGgsnAndSgsnFileIte->second.size() < 1) { mapBsGgsnAndSgsnIte->second.erase(mapGgsnAndSgsnFileIte); if (mapBsGgsnAndSgsnIte->second.size() < 1) { m_mapGgsnAndSgsnFile.erase(mapBsGgsnAndSgsnIte); } } } } } } } else { if (isTimeOfTempFileReached(mapBsAndFileInfo->second->m_nTimeOfCdrFiltered)) { if (!saveFile(strName, nBs, cFileName, cTemp, mapBsAndFileInfo->second, cGgsnAddr)) { bRet = false; printf("save file for time is failed.\n"); break; } } } } else { if (isTimeOfTempFileReached(mapBsAndFileInfo->second->m_nTimeOfCdrFiltered)) { if (!saveFile(strName, nBs, cFileName, cTemp, mapBsAndFileInfo->second, cGgsnAddr)) { bRet = false; printf("save file for time is failed.\n"); break; } } } } } } //如果按统一和网元策略存储,则需要释放锁 if (m_nSaveUpFileType != EACHCDR) { #if IS_Thread_DEBUG { printf("\n*****pthread_mutex_unlock(&m_UnionMutex) TempFileManager Start *****\n"); //释放锁 pthread_mutex_unlock( &m_UnionMutex ); printf("\n*****pthread_mutex_unlock(&m_UnionMutex) TempFileManager End *****\n"); } #else { //释放锁 pthread_mutex_unlock( &m_UnionMutex ); } #endif } #if IS_Thread_DEBUG { printf("\n*****pthread_mutex_unlock(&m_ManagerMutex) TempFileManager Start *****\n"); //释放锁 pthread_mutex_unlock( &m_ManagerMutex ); printf("\n*****pthread_mutex_unlock(&m_ManagerMutex) TempFileManager End *****\n"); //sleep(1); } #else { //释放锁 pthread_mutex_unlock( &m_ManagerMutex ); } #endif if (m_bIsSerialNumToZero) { #if IS_Thread_DEBUG { printf("\n*****pthread_mutex_unlock(&m_ResetSerialMutex) TempFileManager Start *****\n"); pthread_mutex_unlock( &m_ResetSerialMutex ); printf("\n*****pthread_mutex_unlock(&m_ResetSerialMutex) TempFileManager End *****\n"); } #else { pthread_mutex_unlock( &m_ResetSerialMutex ); } #endif } return bRet;}/***********************************************************************Function Name: CreateTempFile*Sescription: 判断临时文件是否存在,如果不存在则生成临时文件*Input: [const string&] 5种存储方式对应的宏* [const short] 对应的三种bs之一*Output: [bool] true--成功; false--失败*Remark: **********************************************************************/bool CTempFileManager::CreateTempFile(const string& strCdr, const short nBs){ if (m_bIsSerialNumToZero) { #if IS_Thread_DEBUG { printf("\n*****pthread_mutex_lock(&m_ResetSerialMutex) CreateTempFile Start *****\n"); pthread_mutex_lock( &m_ResetSerialMutex ); printf("\n*****pthread_mutex_lock(&m_ResetSerialMutex) CreateTempFile End *****\n"); } #else { pthread_mutex_lock( &m_ResetSerialMutex ); } #endif } //如果时间戳打开,则需要给临时文件管理和5种cdr之间加锁 if (m_bIsTimeInterval) { #if IS_Thread_DEBUG { printf("\n*****pthread_mutex_lock(&m_ManagerMutex) %s CreateTempFile Start *****\n", strTemp.c_str()); pthread_mutex_lock( &m_ManagerMutex ); printf("\n*****pthread_mutex_lock(&m_ManagerMutex) %s CreateTempFile End *****\n", strTemp.c_str()); } #else { pthread_mutex_lock( &m_ManagerMutex ); } #endif } //如果按统一和网元策略存储,则需要加锁 if (m_nSaveUpFileType != EACHCDR) { #if IS_Thread_DEBUG { printf("\n*****pthread_mutex_lock(&m_UnionMutex) %s CreateTempFile Start *****\n", strTemp.c_str()); //给临界变量加锁 pthread_mutex_lock( &m_UnionMutex ); printf("\n*****pthread_mutex_lock(&m_UnionMutex) %s CreateTempFile End *****\n", strTemp.c_str()); } #else { //给临界变量加锁 pthread_mutex_lock( &m_UnionMutex ); } #endif } #if IS_DEBUG printf("\n------create file start %d !------\n\n", nBs); #endif if (!createFile(strCdr, nBs)) { //如果按统一和网元策略存储,则需要释放锁 if (m_nSaveUpFileType != EACHCDR) { //释放锁 pthread_mutex_unlock( &m_UnionMutex ); } if (m_bIsTimeInterval) { //释放锁 pthread_mutex_unlock( &m_ManagerMutex ); } if (m_bIsSerialNumToZero) { pthread_mutex_unlock( &m_ResetSerialMutex ); } //sleep(1); return false; } #if IS_DEBUG printf("\n------create file end!------\n\n"); #endif return true;}/***********************************************************************Function Name: OperateTempFile*Sescription: 操作临时文件:当满足三种条件时保存临时文件,否则将CDR写入* 临时文件*Input: [const CDRINFO&] CDR的信息*Output: [bool] true--成功; false--失败*Remark: **********************************************************************/bool CTempFileManager::OperateTempFile(const CDRINFO& sCdrInfo){ bool bRet = true; if (sCdrInfo.bHaveData) { #if IS_DB_DEBUG { if (sCdrInfo.strCdr.compare(GCDR) == 0) { cout << "*******OperateTempFile******" << endl; cout << "SeqId: " << static_cast<S_CGCDR*>(sCdrInfo.pCDR)->SeqId << endl; cout << "*******OperateTempFile******" << endl; } } #endif //得到三种存储方式对应的宏 string strTemp = getCdrStringFromMacro(sCdrInfo.strCdr); //根据传入的strCdr在map中查找 map<string, MAPBSOBJECT>::iterator mapIte; map<short,CFNPManager*>::iterator mapBsAndFileInfo; mapIte = m_mapCFNPManager.find(strTemp); if (mapIte == m_mapCFNPManager.end()) { bRet = false; } else { mapBsAndFileInfo = mapIte->second.find(sCdrInfo.nBs); if (mapBsAndFileInfo == mapIte->second.end()) { bRet = false; } } if (!bRet) { //如果按统一和网元策略存储,则需要释放锁 if (m_nSaveUpFileType != EACHCDR) { //释放锁 pthread_mutex_unlock( &m_UnionMutex ); } if (m_bIsTimeInterval) { //释放锁 pthread_mutex_unlock( &m_ManagerMutex ); } if (m_bIsSerialNumToZero) { pthread_mutex_unlock( &m_ResetSerialMutex ); } //sleep(1); printf("Error in OperateTempFile function!\n"); return bRet; } if (m_nSaveUpFileType == GGSSCDR) { if (!ggsnAndSgsnFileTempOpr(sCdrInfo, mapBsAndFileInfo->second)) { bRet = false; } } else { #if IS_DEBUG printf("\n------operate file start!------\n\n"); #endif //得到三种存储方式对应的重新命名后的名字 char cTemp[LEN_OF_CDR_FILE_NAME]; memset(cTemp, 0, sizeof(cTemp)); getCdrSFNameFromMacro(strTemp, cTemp); //得到临时文件名字 char CdrNameTempBack[LEN_OF_CDR_FILE_NAME]; memset(CdrNameTempBack, 0, sizeof(CdrNameTempBack)); getCdrTempFileName(strTemp, sCdrInfo.nBs, CdrNameTempBack); //如果没有保存临时文件则将cdr写入临时文件中 int nReturn = saveTempFile(strTemp, sCdrInfo.nBs, sCdrInfo.nCdrLength, CdrNameTempBack, cTemp, mapBsAndFileInfo->second); if (nReturn != -1) { if (nReturn == 1) { //创建一个临时文件,将该笔数据写入文件 createFile(sCdrInfo.strCdr, sCdrInfo.nBs); } writeTempFile(sCdrInfo.strCdr, sCdrInfo.nCdrLength, CdrNameTempBack, sCdrInfo.membuf, sCdrInfo.pCDR); // 将需要存入文件的话单大小递增 mapBsAndFileInfo->second->m_nSizeOfCdrSavedForTransforFile += sCdrInfo.nCdrLength; // 将需要存入文件的话单数量递增 mapBsAndFileInfo->second->m_nNumOfCdrSavedForTransforFile++; } else { bRet = false; printf("save file is failed.\n"); } #if IS_DEBUG printf("\n------operate file end!------\n\n"); #endif } } //如果按统一和网元策略存储,则需要释放锁 if (m_nSaveUpFileType != EACHCDR) { #if IS_Thread_DEBUG { printf("\n*****pthread_mutex_unlock(&m_UnionMutex) %s CreateTempFile Start *****\n", sCdrInfo.strCdr.c_str()); //释放锁 pthread_mutex_unlock( &m_UnionMutex ); printf("\n*****pthread_mutex_unlock(&m_UnionMutex) %s CreateTempFile End *****\n", sCdrInfo.strCdr.c_str()); } #else { //释放锁 pthread_mutex_unlock( &m_UnionMutex ); } #endif } if (m_bIsTimeInterval) { #if IS_Thread_DEBUG { printf("\n*****pthread_mutex_unlock(&m_ManagerMutex) %s CreateTempFile Start *****\n", sCdrInfo.strCdr.c_str()); //释放锁 pthread_mutex_unlock( &m_ManagerMutex ); printf("\n*****pthread_mutex_unlock(&m_ManagerMutex) %s CreateTempFile End *****\n", sCdrInfo.strCdr.c_str()); } #else { //释放锁 pthread_mutex_unlock( &m_ManagerMutex ); } #endif } if (m_bIsSerialNumToZero) { #if IS_Thread_DEBUG {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -