123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032 |
- #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>
- #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));
- } else {
- PostEventFIFO(new FSMEvent(USER_EVT_JMP_DISABLE));
- }
- }
- ErrorCodeEnum UploadFSM::OnInit()
- {
- ErrorCodeEnum Error;
- Error = LoadServerConfig();
- if (Error != Error_Succeed)
- {
- LOG_TRACE("load server config failed!");
- 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
- PostEventLIFO(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::LoadServerConfig()
- {
- 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(m_uploading_file->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", m_uploading_file->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;
- }
|