1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039 |
- #include "stdafx.h"
- #include "UploadFSM.h"
- #include <thread>
- #include "upload.h"
- #include "libtoolkit/path.h"
- #ifdef RVC_OS_WIN
- #else
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <time.h>
- #include <stdio.h>
- #include <cstdio>
- #include <iostream>
- #include <errno.h>
- #endif
- UploadFSM::UploadFSM() : m_pConnection(NULL)
- {
- }
- UploadFSM::~UploadFSM()
- {
- #ifndef _WIN32
- if (m_uploading_handle) {
- fclose(m_uploading_handle);
- m_uploading_handle = NULL;
- }
- #endif //NOT _WIN32
- }
- 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));
- } 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<CSimpleStringA>();
- m_uploadDateList = new list<CSimpleStringA>();
- {
- CSmartPointer<IEntityFunction> spFunction = m_pEntity->GetFunction();;
- CSmartPointer<IConfigInfo> 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<CSimpleStringA> dList = dateList.Split(',');
- for(int i=0; i<dList.GetCount(); i++){
- //当读入的日期大于10天,则不加载后续的日期,防止后续日期字符串太长
- if(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<IConfigInfo> 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
- PostEventFIFO(new FSMEvent(USER_EVT_JMP_ENABLE));
- } 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<IEntityFunction> spFunction = m_pEntity->GetFunction();
- CSmartPointer<IConfigInfo> 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<CSimpleStringA>::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<IEntityFunction> spFunction = m_pEntity->GetFunction();;
- CSmartPointer<IConfigInfo> spConfig;
- ErrorCodeEnum Error = spFunction->OpenConfig(Config_Run, spConfig);
- if (Error == Error_Succeed) {
- list<CSimpleStringA>::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<CSimpleStringA> &strList )
- {
- ErrorCodeEnum Error = Error_Succeed;
- EnterCriticalSection(&m_cs);
- //CSmartPointer<IEntityFunction> spFunction = m_pEntity->GetFunction();;
- //CSmartPointer<IConfigInfo> 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<CSimpleStringA>::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<CSimpleStringA>::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<CSimpleStringA>::iterator iter;
- CSimpleStringA strDate="";
- for(iter=m_uploadDateList->begin();iter!=m_uploadDateList->end();++iter){
- if(*iter==uploadDate){
- ret=true;
- break;
- }
- }
- return ret;
- }
|