#include "stdafx.h" #include "UploadFSM.h" #include #include "upload.h" #include "libtoolkit/path.h" #ifdef RVC_OS_WIN #else #include #include #include #include #include #include #include #endif UploadFSM::UploadFSM() : m_pConnection(NULL) { } UploadFSM::~UploadFSM() { } void UploadFSM::OnStateTrans( int iSrcState, int iDstState ) { Dbg("trans from %s to %s", GetStateName(iSrcState), GetStateName(iDstState)); } void UploadFSM::OnSysVarEvent( const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName ) { Dbg("rx sysvar %s from %s, %s to %s", pszKey, pszEntityName, pszOldValue, pszValue); if (pszValue[0] == 'O') { //PostEventFIFO(new FSMEvent(USER_EVT_JMP_ENABLE)); LogWarn(Severity_High, Error_NotSupport, 0, "一轮测试期间不做任何上传操作!"); PostEventFIFO(new FSMEvent(USER_EVT_JMP_DISABLE)); } else { PostEventFIFO(new FSMEvent(USER_EVT_JMP_DISABLE)); } } ErrorCodeEnum UploadFSM::OnInit() { ErrorCodeEnum Error; Error = LoadServerConfigFromCenterSetting(); if (Error != Error_Succeed) { LogWarn(Severity_High, Error, 0, "从集中配置读取配置信息失败"); return Error; } INIT_LIST_HEAD(&m_updir_list); m_check_dir = new list(); m_uploadDateList = new list(); { CSmartPointer spFunction = m_pEntity->GetFunction();; CSmartPointer spConfig; Error = spFunction->OpenConfig(Config_Run, spConfig); if (Error == Error_Succeed) { CSimpleStringA dateList =""; Error = spConfig->ReadConfigValue("uploadDate", "dateList", dateList); bool bError= false; if(Error == Error_Succeed){ CAutoArray dList = dateList.Split(','); for(int i=0; i=10){ break; } CSimpleStringA strDate = dList[i]; //判断日期长度,只读入正确的日期参数,防止读入文件是错误的参数 if(strDate.GetLength()==10){ m_uploadDateList->push_back(strDate); Dbg("upload.ini uploadDate/dateList is: %s ",strDate.GetData()); }else{ Dbg("upload.ini uploadDate/dateList param is error : %s ",strDate.GetData()); bError=true; } } //重置错误的参数文件 if(bError){ if(SaveUploadDate()==Error_Succeed){ Dbg("upload.ini param reset is Success!"); }else{ Dbg("upload.ini param reset is fail!"); } } }else{ Dbg("read upload.ini param uploadDate/dateList fail!"); return Error; } }else{ Dbg("open runcfg upload.ini fail, please check out upload.ini!"); return Error; } } CSimpleStringA checkStr=""; { CSmartPointer spConfig; m_pEntity->GetFunction()->OpenConfig(Config_Software, spConfig); Error = (ErrorCodeEnum)upload_create(&m_updir_list, m_pEntity->GetFunction().GetRawPointer(), spConfig.GetRawPointer(),checkStr); if (Error != Error_Succeed) { LOG_TRACE("load main config failed!"); return Error; }else{ Dbg("checkdirStr is %s",checkStr.GetData()); char *p, *c; char checkType_str[1024]; strcpy(checkType_str, (LPCSTR)checkStr); p = strtok_s(checkType_str, ", ", &c); while (p) { CSimpleStringA str = p; m_check_dir->push_back(str); Dbg("checkdir is %s",str.GetData()); p = strtok_s(NULL, ", ", &c); } } } //初始化临界区 InitializeCriticalSection(&m_cs); Error = m_pEntity->GetFunction()->RegistSysVarEvent("CallState", this); m_dBeginTime=0; m_dEndTime=0; m_iSpeed=0; //Error = Error_Succeed; if (Error == Error_Succeed) { AddStateHooker(this); } //...... return Error; } ErrorCodeEnum UploadFSM::OnExit() { //释放临界区 DeleteCriticalSection(&m_cs); m_uploadDateList->clear(); m_pEntity->GetFunction()->UnregistSysVarEvent("CallState"); return Error_Succeed; } void UploadFSM::s0_on_entry() { CSimpleStringA strValue; ErrorCodeEnum Error = m_pEntity->GetFunction()->GetSysVar("CallState", strValue); if (Error == Error_Succeed) { if (strValue[0] == 'O') { // only upload when CallState in offline state //PostEventLIFO(new FSMEvent(USER_EVT_JMP_ENABLE)); PostEventLIFO(new FSMEvent(USER_EVT_JMP_DISABLE)); LogWarn(Severity_High, Error_NotSupport, 0, "一轮测试期间不做任何上传操作!"); } else { PostEventLIFO(new FSMEvent(USER_EVT_JMP_DISABLE)); } } } void UploadFSM::s0_on_exit() { } unsigned int UploadFSM::s0_on_event( FSMEvent* event ) { return 0; } void UploadFSM::s1_on_entry() { } void UploadFSM::s1_on_exit() { } unsigned int UploadFSM::s1_on_event(FSMEvent* event) { return 0; } void UploadFSM::s2_on_entry() { if (find_first_upload_file() == NULL) { upload_fresh(&m_updir_list); } if (find_first_upload_file() == NULL) { ScheduleTimer(1, 30000); } else { //把新增加的上传日期加入集合里面 insertUploadDate(); PostEventLIFO(new FSMEvent(USER_EVT_JMP_CONNECT)); } } void UploadFSM::s2_on_exit() { CancelTimer(1); } unsigned int UploadFSM::s2_on_event(FSMEvent* event) { if (event->iEvt == EVT_TIMER) { if (find_first_upload_file() == NULL) { upload_fresh(&m_updir_list); } if (find_first_upload_file() == NULL) { ScheduleTimer(1, 10000); } else { //把新增加的上传日期加入集合里面 insertUploadDate(); PostEventLIFO(new FSMEvent(USER_EVT_JMP_CONNECT)); } } return 0; } void UploadFSM::s3_on_entry() { if (!m_pConnection) { m_pConnection = new UploadConnection(m_pEntity, this); if(m_pConnection->ConnectFromCentralSetting() && m_pConnection->IsConnectionOK()){ //创建成功 }else{ m_pConnection->Close(); m_pConnection->DecRefCount(); m_pConnection = NULL; } } if (!m_pConnection) { ScheduleTimer(2, 10000); // try 30 seconds later } else { PostEventLIFO(new FSMEvent(USER_EVT_JMP_UPLOAD)); } } void UploadFSM::s3_on_exit() { CancelTimer(2); } unsigned int UploadFSM::s3_on_event(FSMEvent* event) { if (event->iEvt == EVT_TIMER) { m_pConnection = new UploadConnection(m_pEntity, this); if(m_pConnection->ConnectFromCentralSetting()&&m_pConnection->IsConnectionOK()){ //创建成功 }else{ m_pConnection->Close(); m_pConnection->DecRefCount(); m_pConnection = NULL; } if (!m_pConnection) { ScheduleTimer(2, 30000); // try 30 seconds later } else { PostEventLIFO(new FSMEvent(USER_EVT_JMP_UPLOAD)); } } return 0; } void UploadFSM::s4_on_entry() { m_uploading_file = find_first_upload_file();//获取要处理的文件 #ifdef RVC_OS_WIN m_uploading_handle = INVALID_HANDLE_VALUE;//文件句柄 #else m_uploading_handle = NULL; #endif // RVC_OS_WIN //开始统计时间 m_dBeginTime=GetTickCount();//开始时间 //Dbg("m_dBeginTime:%d",m_dBeginTime); m_pConnection->SendUpReq(m_uploading_file);//发送上传请求 ScheduleTimer(4,30000); } void UploadFSM::s4_on_exit() { m_dBeginTime = 0; m_dEndTime = 0; m_iSpeed = 0; #ifdef RVC_OS_WIN if (m_uploading_handle != INVALID_HANDLE_VALUE) { CloseHandle(m_uploading_handle); m_uploading_handle = INVALID_HANDLE_VALUE; } #else if (m_uploading_handle != NULL) { fclose(m_uploading_handle); m_uploading_handle = NULL; } #endif // RVC_OS_WIN m_uploading_file = NULL; CancelTimer(4); } unsigned int UploadFSM::s4_on_event(FSMEvent* event) { CancelTimer(4); if (event->iEvt == USER_EVT_UPLOAD_ANS) { // ..... UploadAnsEvent *ans = (UploadAnsEvent *)event; if (ans->m_reply.ResultCode == UploadCtlCode::Begin || ans->m_reply.ResultCode == UploadCtlCode::Continua) { m_uploading_block_id = ans->m_reply.BeginBlock; #ifdef RVC_OS_WIN m_uploading_handle = CreateFileA(m_uploading_file->path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (m_uploading_handle == INVALID_HANDLE_VALUE) { // open file handle failed, we ignore this file //list_del(&m_uploading_file->entry);//从链表删除文件 //upload_file_destroy(m_uploading_file);//销毁文件 DWORD err = GetLastError(); Dbg("USER_EVT_UPLOAD_ANS CreateFileA is error file name [%s] , GetLastError = %d", m_uploading_file->name, err); updir_del_file(m_uploading_file);//把文件从文件夹列表删除 m_pConnection->Close(); m_pConnection->DecRefCount(); m_pConnection = NULL; return 1; // goto s5 } else { memcpy(m_upload_id, ans->m_reply.UploadID, sizeof(m_upload_id)); //当发送不成功,则把文件从列表删除,进行下一个文件传送 if (!m_pConnection->SendBlockReq(m_uploading_file, m_uploading_handle, m_upload_id, m_uploading_block_id)) { updir_del_file(m_uploading_file);//把文件从文件夹列表删除 m_pConnection->Close(); m_pConnection->DecRefCount(); m_pConnection = NULL; return 1; // goto s5 } } #else m_uploading_handle = fopen(m_uploading_file->path, "rb"); if (m_uploading_handle == NULL) { Dbg("USER_EVT_UPLOAD_ANS fopen is error file name [%s] ", m_uploading_file->name); updir_del_file(m_uploading_file);//把文件从文件夹列表删除 m_pConnection->Close(); m_pConnection->DecRefCount(); m_pConnection = NULL; return 1; // goto s5 } else { memcpy(m_upload_id, ans->m_reply.UploadID, sizeof(m_upload_id)); //当发送不成功,则把文件从列表删除,进行下一个文件传送 if (!m_pConnection->SendBlockReq(m_uploading_file, m_uploading_handle, m_upload_id, m_uploading_block_id)) { updir_del_file(m_uploading_file);//把文件从文件夹列表删除 m_pConnection->Close(); m_pConnection->DecRefCount(); m_pConnection = NULL; return 1; // goto s5 } } #endif // RVC_OS_WIN } else if (ans->m_reply.ResultCode == UploadCtlCode::Timeout) { //文件是否过期,不用再上传,现有配置是日志文件31天过期,受分行服务配置文件控制 post_process();//后续本地处理此文件 m_uploading_file = find_first_upload_file();//获取下一个文件 if (m_uploading_file) { m_pConnection->SendUpReq(m_uploading_file);//发送上传文件指令 } else { m_pConnection->Close(); m_pConnection->DecRefCount(); m_pConnection = NULL; return 1; // goto s5 } } else {//(会有ErrorType错误)不做任务任何处理,仅仅把要处理的文件从列表移除 //list_del(&m_uploading_file->entry); //upload_file_destroy(m_uploading_file); updir_del_file(m_uploading_file);//把文件从文件夹列表删除 m_pConnection->Close(); m_pConnection->DecRefCount(); m_pConnection = NULL; return 1; // goto s5 } } else if (event->iEvt == USER_EVT_BLOCK_ANS) { BlockAnsEvent *ans = (BlockAnsEvent*)event; if (ans->m_reply.ResultCode == UploadCtlCode::Continua) { m_dEndTime = GetTickCount(); if(m_dEndTime-m_dBeginTime!=0){ m_iSpeed = 1000*32/(m_dEndTime-m_dBeginTime); }else{ } m_dBeginTime = GetTickCount(); m_uploading_block_id++; //当发送不成功,则把文件从列表删除,进行下一个文件传送 if(!m_pConnection->SendBlockReq(m_uploading_file, m_uploading_handle, m_upload_id, m_uploading_block_id)){ updir_del_file(m_uploading_file);//把文件从文件夹列表删除 m_pConnection->Close(); m_pConnection->DecRefCount(); m_pConnection = NULL; return 1; // goto s5 } } else if (ans->m_reply.ResultCode == UploadCtlCode::ErrorBlock) { //不做任务任何处理,仅仅把要处理的文件从列表移除 Dbg("rx error block reply, resend!"); //m_uploading_block_id = ans->m_reply. //m_pConnection->SendBlockReq(m_uploading_file, m_uploading_handle, m_upload_id, m_uploading_block_id); //list_del(&m_uploading_file->entry); //upload_file_destroy(m_uploading_file); updir_del_file(m_uploading_file);//把文件从文件夹列表删除 m_pConnection->Close(); m_pConnection->DecRefCount(); m_pConnection = NULL; return 1; // goto s5 } else if (ans->m_reply.ResultCode == UploadCtlCode::Begin) { //重新传文件 m_uploading_block_id = 0; // upload again if(!m_pConnection->SendBlockReq(m_uploading_file, m_uploading_handle, m_upload_id, m_uploading_block_id)){ updir_del_file(m_uploading_file);//把文件从文件夹列表删除 m_pConnection->Close(); m_pConnection->DecRefCount(); m_pConnection = NULL; return 1; // goto s5 } } else if (ans->m_reply.ResultCode == UploadCtlCode::ErrorType) { // (不存在)server do not support this//不做任务任何处理,仅仅把要处理的文件从列表移除 Dbg("server do not support this type!"); //list_del(&m_uploading_file->entry); updir_del_file(m_uploading_file);//把文件从文件夹列表删除 m_pConnection->Close(); m_pConnection->DecRefCount(); m_pConnection = NULL; //upload_file_destroy(m_uploading_file); //m_uploading_file = NULL; //CloseHandle(m_uploading_handle); //m_uploading_handle = INVALID_HANDLE_VALUE; return 1; } else if (ans->m_reply.ResultCode == UploadCtlCode::ErrorUploadId) { //(概率很小)不做任务任何处理,仅仅把要处理的文件从列表移除 Dbg("server do not support this type!"); //list_del(&m_uploading_file->entry); updir_del_file(m_uploading_file);//把文件从文件夹列表删除 m_pConnection->Close(); m_pConnection->DecRefCount(); m_pConnection = NULL; //upload_file_destroy(m_uploading_file); //m_uploading_file = NULL; //CloseHandle(m_uploading_handle); //m_uploading_handle = INVALID_HANDLE_VALUE; return 1; } else if (ans->m_reply.ResultCode == UploadCtlCode::Finish) { //后续本地处理此文件 m_dEndTime = GetTickCount(); if(m_dEndTime-m_dBeginTime!=0){ m_iSpeed = 1000*32/(m_dEndTime-m_dBeginTime); }else{ } m_dBeginTime = GetTickCount(); post_process(); m_uploading_file = find_first_upload_file(); if (m_uploading_file) { m_pConnection->SendUpReq(m_uploading_file); } else { m_pConnection->Close(); m_pConnection->DecRefCount(); m_pConnection = NULL; return 1; // goto s5 } } else { //不做任务任何处理,仅仅把要处理的文件从列表移除 Dbg("server do not support this file! ResultCode:%d", ans->m_reply.ResultCode); //list_del(&m_uploading_file->entry); updir_del_file(m_uploading_file);//把文件从文件夹列表删除 m_pConnection->Close(); m_pConnection->DecRefCount(); m_pConnection = NULL; //upload_file_destroy(m_uploading_file); //m_uploading_file = NULL; //CloseHandle(m_uploading_handle); //m_uploading_handle = INVALID_HANDLE_VALUE; return 1; // goto s5 } } else if (event->iEvt == USER_EVT_DISCONNECT) { // server disconnected m_pConnection->Close(); m_pConnection->DecRefCount(); m_pConnection = NULL; return 1; // goto s6 } else if (event->iEvt == USER_EVT_JMP_DISABLE) { // aborted any way m_pConnection->Close(); m_pConnection->DecRefCount(); m_pConnection = NULL; } else if (event->iEvt == EVT_TIMER) { Dbg("SendUpReq timeout!!!"); } return 0; } void UploadFSM::s5_on_entry() { ScheduleTimer(5, 500); // sleep for a short to collect OnClose and OnDisconnect event } void UploadFSM::s5_on_exit() { CancelTimer(5); } unsigned int UploadFSM::s5_on_event(FSMEvent* event) { if (event->iEvt == USER_EVT_DISCONNECT) { Dbg("rx disconnect evt at s5"); // goto s2 } else if (event->iEvt == EVT_TIMER) { // goto s2 } return 0; } void UploadFSM::s6_on_entry() { ScheduleTimer(6, 10000); } void UploadFSM::s6_on_exit() { CancelTimer(6); } unsigned int UploadFSM::s6_on_event(FSMEvent* event) { if (event->iEvt == EVT_TIMER) { // goto s2 Dbg("disconnect sleeping 10s"); } return 0; } static ErrorCodeEnum ParseIPAddress( const char *str, CSimpleStringA &ip, int &port ) { ErrorCodeEnum Error = Error_Unexpect; if (str) { char tmp1[32] = {}; char tmp2[16] = {}; sscanf(str, "%s %s", tmp1, tmp2); if (tmp1[0] != 0 && tmp2[0] != 0) { ip = tmp1; port = atoi(tmp2); Error = Error_Succeed; } else return Error; } return Error; } ErrorCodeEnum UploadFSM::LoadServerConfigFromCenterSetting() { CSmartPointer spFunction = m_pEntity->GetFunction(); CSmartPointer spConfig; ErrorCodeEnum Error = spFunction->OpenConfig(Config_CenterSetting, spConfig); if (Error == Error_Succeed) { CSimpleStringA str; Error = spConfig->ReadConfigValue("Upload", "Server", str); if (Error == Error_Succeed) { Error = ParseIPAddress(str, m_server1, m_server1_port); } if (Error == Error_Succeed) { ErrorCodeEnum ErrorCode; ErrorCode = spConfig->ReadConfigValue("Upload", "Server_Backup", str); if (ErrorCode == Error_Succeed) { ErrorCode = ParseIPAddress(str, m_server2, m_server2_port); } if (ErrorCode!= Error_Succeed) { m_server2 = NULL; m_server2_port = 0; } } } return Error; } file_t * UploadFSM::find_first_upload_file() { upload_dir_t *pos; list_for_each_entry(pos, &m_updir_list, upload_dir_t, entry) { if (!list_empty(&pos->candidate_list)) { return list_first_entry(&pos->candidate_list, file_t, entry); } } return NULL; } void UploadFSM::post_process() { int flags = m_uploading_file->owner->flags; if (flags & UPLOAD_FLAG_AUTODELETE) { Dbg("delete file %s", m_uploading_file->name); #ifdef RVC_OS_WIN if (m_uploading_handle != INVALID_HANDLE_VALUE) { CloseHandle(m_uploading_handle); m_uploading_handle = INVALID_HANDLE_VALUE; } DWORD dwAttr = GetFileAttributesA(m_uploading_file->path); dwAttr &= ~FILE_ATTRIBUTE_READONLY;//去除只读属性 SetFileAttributesA(m_uploading_file->path, dwAttr); if (DeleteFileA(m_uploading_file->path)) { Dbg("delete file is ok"); } else { DWORD err = GetLastError(); Dbg("delete file is error , GetLastError = %d", err); } #else if (m_uploading_handle != NULL) { fclose(m_uploading_handle); m_uploading_handle = NULL; } if (changeFileAtt(m_uploading_file->path)==0) { if (remove(m_uploading_file->path) == 0) { Dbg("delete file is ok"); } else { Dbg("delete file is error,errno=%d , file=%s", errno, m_uploading_file->path); } } else { Dbg("delete file is error,changefileAtt fail ,errno=%d , file=%s ", errno,m_uploading_file->path); } #endif // RVC_OS_WIN } else if (flags & UPLOAD_FLAG_MOVEPATH) { char to_path[MAX_PATH]; strcpy(to_path, m_uploading_file->owner->movepath); strcat(to_path, SPLIT_SLASH_STR); strcat(to_path, m_uploading_file->name); Dbg("move file %s to %s", m_uploading_file->path, to_path); #ifdef RVC_OS_WIN if (m_uploading_handle != INVALID_HANDLE_VALUE) { CloseHandle(m_uploading_handle); m_uploading_handle = INVALID_HANDLE_VALUE; } DWORD dwAttr = GetFileAttributesA(m_uploading_file->path); dwAttr &= ~FILE_ATTRIBUTE_READONLY;//一定要设置只读属性 SetFileAttributesA(m_uploading_file->path, dwAttr); { int i; int tries = 10; for (i = 0; i < tries; ++i) { if (MoveFileExA(m_uploading_file->path, to_path, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) { Dbg("move ok! tries:%d", i); break; } else { Dbg("move %s -> %s failed! GetLastError:%d", m_uploading_file->path, to_path, GetLastError()); Sleep(1000); } } } #else if (m_uploading_handle != NULL) { fclose(m_uploading_handle); m_uploading_handle = NULL; } if (changeFileAtt(m_uploading_file->path) == 0) { int i; int tries = 10; for (i = 0; i < tries; ++i) { if (rename(m_uploading_file->path, to_path) == 0) { Dbg("move ok! tries:%d", i); break; } else { Dbg("move %s -> %s failed! errno=%d", m_uploading_file->path, to_path,errno); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } } } else { Dbg("move %s -> %s failed! , changefileAtt fail,errno=%d", m_uploading_file->path, to_path,errno); } #endif // RVC_OS_WIN } if (m_uploading_file) { //list_del(&m_uploading_file->entry); //upload_file_destroy(m_uploading_file); updir_del_file(m_uploading_file);//把文件从文件夹列表删除 m_uploading_file = NULL; } #ifdef RVC_OS_WIN if (m_uploading_handle != INVALID_HANDLE_VALUE) { CloseHandle(m_uploading_handle); m_uploading_handle = INVALID_HANDLE_VALUE; } #else if (m_uploading_handle != NULL) { fclose(m_uploading_handle); m_uploading_handle = NULL; } #endif // RVC_OS_WIN } int UploadFSM::getCheckDirFile(int silentTime) { //把要检查的目录在要上传的目录里面查询,并搜索文件 list::iterator iter; int sum =0; int count=0; for(iter=m_check_dir->begin();iter!=m_check_dir->end();++iter){ CSimpleStringA checkstr= *iter; upload_dir_t *pos; list_for_each_entry(pos, &m_updir_list, upload_dir_t, entry) { if(strcmp(pos->name,checkstr.GetData())==0){ int fileLen=0;//文件长度,长度是k count = check_dir_fresh(pos->path,pos->child_count_limitation,silentTime,fileLen); Dbg("交易文件夹:%s 还有未上传的文件个数 %d,文件总大小 %d k",pos->name,count,fileLen); sum +=count; }else{ continue;; } } } return sum; } void UploadFSM::getUploadProgress(UploadProgress &progress) { int fileSumlen=0; if(m_iState==s0){ progress.uploadState=0; progress.elapseTime="--:--:--"; progress.uploadNumber=getUploadFileNum(fileSumlen); }else if(m_iState==s1){ progress.uploadState=1; progress.elapseTime="--:--:--"; progress.uploadNumber=getUploadFileNum(fileSumlen); }else if(m_iState==s2){ progress.uploadState=2; progress.elapseTime="--:--:--"; progress.uploadNumber=getUploadFileNum(fileSumlen); }else if(m_iState==s3){ progress.uploadState=3; progress.elapseTime="--:--:--"; progress.uploadNumber=getUploadFileNum(fileSumlen); }else if(m_iState==s4){ progress.uploadState=4; progress.uploadNumber=getUploadFileNum(fileSumlen); //Dbg("总文件长度:%d",fileSumlen); if(m_iSpeed!=0){ int loop = fileSumlen/m_iSpeed ; //Dbg("loop:%d",loop); CSimpleStringA hour=CSimpleStringA::Format("%d",loop/3600); //Dbg("hour:%s",hour); loop=loop%3600; CSimpleStringA ss = CSimpleStringA::Format("%02d",loop/60); //Dbg("ss:%s",ss); loop = loop%60; CSimpleStringA mm = CSimpleStringA::Format("%02d",loop); //Dbg("mm:%s",mm); CSimpleStringA strloop = hour+":"+ss+":"+mm; progress.elapseTime=strloop;//动态获取估计时间 Dbg("总预估时间:%s",strloop.GetData()); }else{ progress.elapseTime="--:--:--"; } }else if(m_iState==s5){ progress.uploadState=5; progress.elapseTime="--:--:--"; progress.uploadNumber=getUploadFileNum(fileSumlen); }else if(m_iState==s6){ progress.uploadState=6; progress.elapseTime="--:--:--"; progress.uploadNumber=getUploadFileNum(fileSumlen); }else{ progress.uploadState=-1; progress.elapseTime="--:--:--"; progress.uploadNumber=getUploadFileNum(fileSumlen); } } int UploadFSM::getUploadFileNum(int &fileSumlen) { int sum=0; upload_dir_t *pos; int count=0; list_for_each_entry(pos, &m_updir_list, upload_dir_t, entry) { int fileLen=0;//文件长度,长度是k count = check_dir_fresh(pos->path,pos->child_count_limitation,pos->silent_time,fileLen); if(count!=0){ Dbg("文件夹:%s 未上传的文件个数 %d,文件大小 %d k",pos->name,count,fileLen); } sum += count; fileSumlen += fileLen; } Dbg("上传进度查询:未上传的文件总个数 %d,文件总大小 %d k",sum,fileSumlen); return sum; } bool UploadFSM::clearUploadDate() { bool ret = false; EnterCriticalSection(&m_cs); m_uploadDateList->clear(); ErrorCodeEnum Error = SaveUploadDate(); if(Error==Error_Succeed){ ret = true; }else{ ret = false; } LeaveCriticalSection(&m_cs); return ret; } ErrorCodeEnum UploadFSM::SaveUploadDate() { CSmartPointer spFunction = m_pEntity->GetFunction();; CSmartPointer spConfig; ErrorCodeEnum Error = spFunction->OpenConfig(Config_Run, spConfig); if (Error == Error_Succeed) { list::iterator iter; CSimpleStringA strDate=""; for(iter=m_uploadDateList->begin();iter!=m_uploadDateList->end();++iter){ CSimpleStringA date = (CSimpleStringA)*iter; strDate.Append(date); strDate.Append(","); } CSimpleStringA writeStr =""; if(strDate.GetLength()>0){ writeStr = strDate.SubString(0,strDate.GetLength()-1); Dbg("prepare write uploadDate/dateList : %s",writeStr.GetData()); Error = spConfig->WriteConfigValue("uploadDate", "dateList", writeStr.GetData()); }else{ Dbg("prepare write uploadDate/dateList : "); Error = spConfig->WriteConfigValue("uploadDate", "dateList", writeStr.GetData()); } if(Error==Error_Succeed){ Dbg("write uploadDate/dateList success: %s",writeStr.GetData()); }else{ Dbg("write uploadDate/dateList fail: %s error:%d",writeStr.GetData(),Error); } }else{ Dbg("SaveUploadDate is fail , open runcfg/upload.ini fail!"); } return Error; } ErrorCodeEnum UploadFSM::getUploadDate( CAutoArray &strList ) { ErrorCodeEnum Error = Error_Succeed; EnterCriticalSection(&m_cs); //CSmartPointer spFunction = m_pEntity->GetFunction();; //CSmartPointer spConfig; //Error = spFunction->OpenConfig(Config_Run, spConfig); //if (Error == Error_Succeed) { // CSimpleStringA dateList =""; // Error = spConfig->ReadConfigValue("uploadDate", "dateList", dateList); // if(Error == Error_Succeed){ // strList = dateList.Split(','); // }else{ // Dbg("getUploadDate read upload.ini param uploadDate/dateList fail!"); // } //}else{ // Dbg("getUploadDate open runcfg upload.ini fail, please check out upload.ini!"); //} //因运行时文件可能会有错误的日期,故从内存中读取 strList.Init(m_uploadDateList->size()); list::iterator iter; CSimpleStringA strDate=""; int i=0; for(iter=m_uploadDateList->begin();iter!=m_uploadDateList->end();++iter){ CSimpleStringA strDate = *iter; strList[i]=strDate; Dbg("getUploadDate strDate[%d]:%s",i,strDate.GetData()); i++; } LeaveCriticalSection(&m_cs); return Error; } ErrorCodeEnum UploadFSM::insertUploadDate() { ErrorCodeEnum Error = Error_Succeed; EnterCriticalSection(&m_cs); list::iterator iter; for(iter=m_check_dir->begin();iter!=m_check_dir->end();++iter){ CSimpleStringA checkstr= *iter; upload_dir_t *pos; list_for_each_entry(pos, &m_updir_list, upload_dir_t, entry) { if(strcmp(pos->name,checkstr.GetData())==0){ if (!list_empty(&pos->candidate_list)) { file_t *fpos; list_for_each_entry(fpos,&pos->candidate_list,file_t,entry){ #ifdef RVC_OS_WIN WIN32_FILE_ATTRIBUTE_DATA attr; FILETIME fttmp; SYSTEMTIME filest; if (GetFileAttributesExA(fpos->path, GetFileExInfoStandard, &attr)) { FileTimeToLocalFileTime(&attr.ftLastWriteTime, &fttmp); FileTimeToSystemTime(&fttmp, &filest); CSimpleStringA strDate = CSimpleStringA::Format("%4.4d-%2.2d-%2.2d", filest.wYear, filest.wMonth, filest.wDay); if (!uploadDate_exist(strDate)) { Dbg("insertUploadDate %s:%s", fpos->path, strDate.GetData()); m_uploadDateList->push_back(strDate); if (SaveUploadDate() == Error_Succeed) { Dbg("insertUploadDate is success! %s:%s", fpos->path, strDate.GetData()); } else { Dbg("insertUploadDate is fail%s:%s", fpos->path, strDate.GetData()); //因文件写入失败,删除当前的日期,后续还有文件上传会继续添加进来 m_uploadDateList->pop_back(); } } } #else struct stat attr_of_src; if (lstat(fpos->path, &attr_of_src) == 0) { struct tm flocalTime = { 0 }; localtime_r(&attr_of_src.st_mtime,&flocalTime); CSimpleStringA strDate = CSimpleStringA::Format("%4.4d-%2.2d-%2.2d", (1900 + flocalTime.tm_year), (1 + flocalTime.tm_mon), flocalTime.tm_mday); if (!uploadDate_exist(strDate)) { Dbg("insertUploadDate %s:%s", fpos->path, strDate.GetData()); m_uploadDateList->push_back(strDate); if (SaveUploadDate() == Error_Succeed) { Dbg("insertUploadDate is success! %s:%s", fpos->path, strDate.GetData()); } else { Dbg("insertUploadDate is fail%s:%s", fpos->path, strDate.GetData()); //因文件写入失败,删除当前的日期,后续还有文件上传会继续添加进来 m_uploadDateList->pop_back(); } } } else { Dbg("get file attr fail,file=%s", fpos->path); } #endif // RVC_OS_WIN } } } } } LeaveCriticalSection(&m_cs); return Error; } bool UploadFSM::uploadDate_exist( CSimpleStringA uploadDate ) { bool ret = false; list::iterator iter; CSimpleStringA strDate=""; for(iter=m_uploadDateList->begin();iter!=m_uploadDateList->end();++iter){ if(*iter==uploadDate){ ret=true; break; } } return ret; }