1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878 |
- #include "stdafx.h"
- #include "UploadFSM.h"
- #include <thread>
- #include "upload.h"
- #include "libtoolkit/path.h"
- #include <regex>
- #include <fileutil.h>
- #include <array.h>
- #ifdef RVC_OS_WIN
- #include <cstdlib>
- #include <ctime>
- #include "..\EventCode.h"
- #include "..\..\other\libfilecryption\filecryption.h"
- #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>
- #include "EventCode.h"
- #endif
- #define UPLOAD_CONTENT_EACH_LEN 2*1024*1024
- UploadFSM::UploadFSM() : m_pConnection(NULL),m_currUploadFile(NULL)
- {
- m_uploadList = new list<UploadFileInfo*>();
- m_uploadPlanList = new list<UploadPlan *>();
- m_eachUploadLen=UPLOAD_CONTENT_EACH_LEN;
- m_uploadMaxNum = 1000;
- m_isOffLine=true;//初始离线状态为true
- }
- UploadFSM::~UploadFSM()
- {
- #ifndef _WIN32
- if (m_uploading_handle) {
- fclose(m_uploading_handle);
- m_uploading_handle = NULL;
- }
- #endif //NOT _WIN32
- }
- namespace Task
- {
- struct SendFileTask : public ITaskSp
- {
- UploadFSM* m_fsm;
- explicit SendFileTask(UploadFSM* e) : m_fsm(e) {}
- void Process() {
- //判断是分行url请求还是总行url请求
- //判断是临时文件还是永久文件
- //循环上传
- //登记上传流水或者失败流水
- //根据最终上传结果返回成功还是失败
- CSystemStaticInfo info;
- auto rc = m_fsm->GetEntityBase()->GetFunction()->GetSystemStaticInfo(info);
- if (rc != Error_Succeed)
- {
- Dbg("get CSystemStaticInfo fail,file = %s",m_fsm->m_currUploadFile->filePath.c_str());
- m_fsm->PostEventFIFO(new FSMEvent(USER_EVT_UPLOAD_FAIL));//失败发送
- return;
- }
- bool isLanTerminal=true;//行内行外标志
- //if(info.strSite.Compare("CMB.LIB",true)==0||info.strSite.Compare("CMB.SSB",true)==0){
- // isLanTerminal=true;
- //}else{
- // isLanTerminal=false;
- //}
- if(strcmp(m_fsm->m_currUploadFile->upload_address_env.c_str(),"BIZ")==0){
- isLanTerminal=true;
- }else{
- isLanTerminal=false;
- }
- IHttpFunc* client;
- client = create_http(m_fsm->HttpsLogCallBack);
- bool hasSend=false;
- if(m_fsm->m_currUploadFile->plan_type=="T"){
- //查询失败统一需要上传
- if(queryTempFile(client,info)){
- hasSend=true;
- }
- }
- //是否需要上传文件
- bool sendOk = false;
- if(!hasSend){
- string errmsg ="";
- if(isLanTerminal){
- Dbg("上传文件到分行:%s",m_fsm->m_currUploadFile->filePath.c_str());
- }else{
- Dbg("上传文件到总行:%s",m_fsm->m_currUploadFile->filePath.c_str());
- }
- string MD5str = ""; //返回的MD5值
- string branchFilePath = ""; //返回的分行保存路径值
- //修改为重试上传机制,防止上传当中网络抖动,避免等待下次上传
- bool isUploadSucc=false;
- for (int i =0 ; i<2;i++ )
- {
- errmsg="";//每次上传前清空错误信息,只记录最后一次的
- if(uploadFile(client,info,MD5str,branchFilePath,errmsg)){
- isUploadSucc = true;
- break;
- }
- Sleep(3000);//等待3秒下次传送
- }
- if(isUploadSucc){
- if(updateState(client,info,isLanTerminal,MD5str,branchFilePath,errmsg)){
- if(operateFile(errmsg)){
- sendOk = true;//成功
- }else{
- m_fsm->m_uploadReport.operateFileFail++;//添加操作失败计数
- Dbg("operateFile fail,file = %s",m_fsm->m_currUploadFile->filePath.c_str());
- addUploadFail(client,info,isLanTerminal,"2",errmsg);//操作失败
- }
- }else{
- m_fsm->m_uploadReport.updateState++;//添加登记失败计数
- Dbg("updateState fail,file = %s",m_fsm->m_currUploadFile->filePath.c_str());
- addUploadFail(client,info,isLanTerminal,"1",errmsg);//登记失败
- }
- }else{
- m_fsm->m_uploadReport.uploadFail++;//添加上传失败计数
- Dbg("uploadFile fail,file = %s",m_fsm->m_currUploadFile->filePath.c_str());
- addUploadFail(client,info,isLanTerminal,"1",errmsg);//上传失败
- }
- }else{
- sendOk=true;
- }
- client->Destory();
- if(sendOk){
- m_fsm->PostEventFIFO(new FSMEvent(USER_EVT_UPLOAD_SUCC));//成功发送
- }else{
- m_fsm->PostEventFIFO(new FSMEvent(USER_EVT_UPLOAD_FAIL));//失败发送
- }
- }
- //上传文件:断点续传,多次调用
- bool uploadFile(IHttpFunc* client,CSystemStaticInfo info,string &MD5str,string &branchFilePath, string &errmsg){
- string reqUrl;
- unsigned seed;
- seed = time(0);
- srand(seed);
- if(m_fsm->m_branchIpList.size()>0){
- int branchSeq = (rand() % m_fsm->m_branchIpList.size()) + 1;
- map<int,string>::iterator it = m_fsm->m_branchIpList.find(branchSeq);
- if(it != m_fsm->m_branchIpList.end())
- {
- reqUrl = it->second;
- }else{
- Dbg("uploadFile is fail ,branchIp[%d] is null",branchSeq);
- errmsg = CSimpleStringA::Format("uploadFile is fail ,branchIp[%d] is null",branchSeq).GetData();
- return false;
- }
- }else{
- Dbg("uploadFile is fail ,branchIp is null");
- errmsg = "uploadFile is fail ,branchIp is null";
- return false;
- }
- //解密文件
- unsigned char* pdecdata = NULL;//记得释放内存
- size_t udecheadlen = 0;
- int decOffSet = 0;//文件明文头和密文头的偏移量:可能是负数、零、正数
- char* pHeadJson = NULL;//记得释放内存
- size_t uheadJsonLen = 0;
- #ifdef RVC_OS_WIN
- filecryption_callback_t cb = {0};
- cb.dbg = &Dbg;
- int decRet = rvc_file_decrypt(&pdecdata,&udecheadlen,&decOffSet,&pHeadJson,&uheadJsonLen,m_fsm->m_currUploadFile->filePath.c_str(),&cb);
- #else
- int decRet = 0;
- #endif
- if(decRet!=0){
- //解密报错
- Dbg("uploadFile is fail, decode file fail=%d",decRet);
- errmsg=CSimpleStringA::Format("uploadFile is fail, decode file fail=%d",decRet).GetData();
- #ifdef RVC_OS_WIN
- rvc_free_data((void**)&pdecdata);
- rvc_free_data((void**)&pHeadJson);
- #endif
- m_fsm->m_uploadReport.decodeFail++;//添加解密失败计数
- return false;
- }
- //计算出解密后总长度:
- m_fsm->m_currUploadFile->decFileLen = m_fsm->m_currUploadFile->fileLen-decOffSet;//重新赋值
- Dbg("decode file len=%d, decOffSet=%d, decHeadLen=%d, uheadJsonLen=%d",m_fsm->m_currUploadFile->decFileLen,decOffSet,(int)udecheadlen,(int)uheadJsonLen);
-
- //解密自定义头赋值
- if(pHeadJson!=NULL){
- m_fsm->m_currUploadFile->decHeadJson = pHeadJson;
- }else{
- m_fsm->m_currUploadFile->decHeadJson = "";
- }
- //Dbg("decode file decHeadJson = %s",m_fsm->m_currUploadFile->decHeadJson.c_str());
- //获取服务器文件长度
- unsigned char* contentFirst = new unsigned char[1];
- memset(contentFirst,0,1);
- UploadDataRet respFirst ={0};
- bool is_finish =false; //是否续传
- if(!uploadFileImpl(client,info,reqUrl,contentFirst,0,"",respFirst)){
- delete[] contentFirst;
- errmsg=CSimpleStringA::Format("url=%s, uploadFileImpl first is fail",reqUrl.c_str()).GetData();
- #ifdef RVC_OS_WIN
- rvc_free_data((void**)&pdecdata);
- rvc_free_data((void**)&pHeadJson);
- #endif
- return false;//返回失败
- }else{
- delete[] contentFirst;
- }
- long uploadedLen = respFirst.uploaded_length;//服务器返回已上传的长度
- //Dbg("服务器返回临时长度 uploadedLen = %d",uploadedLen);
- if(uploadedLen>m_fsm->m_currUploadFile->decFileLen){
- //服务器返回长度大于终端文件长度
- Dbg("uploadFile is fail, server filelen [%d] is over terminal decfilelen [%d]",uploadedLen,m_fsm->m_currUploadFile->decFileLen);
- errmsg=CSimpleStringA::Format("uploadFile is fail, server filelen [%d] is over terminal decfilelen [%d]",uploadedLen,m_fsm->m_currUploadFile->decFileLen).GetData();
- #ifdef RVC_OS_WIN
- rvc_free_data((void**)&pdecdata);
- rvc_free_data((void**)&pHeadJson);
- #endif
- return false;
- }
- //密文解密
- if(uploadedLen==0){
- //文件头传输
- if(udecheadlen>0){
- //密文数据,传送首块,传解密字节块
- UploadDataRet firstResp ={0};
- if(!uploadFileImpl(client,info,reqUrl,pdecdata,(long)udecheadlen,"",firstResp)){
- #ifdef RVC_OS_WIN
- rvc_free_data((void**)&pdecdata);
- rvc_free_data((void**)&pHeadJson);
- #endif
- errmsg="uploadFileImpl first data upload is fail";
- Dbg("uploadFile fail url=%s",reqUrl.c_str());
- return false;//返回错误
- }
- }
- uploadedLen += (long)udecheadlen+decOffSet;//改变文件游标偏移量
- }else{
- //数据传输
- uploadedLen += decOffSet; //改变文件游标偏移量
- }
- #ifdef RVC_OS_WIN
- rvc_free_data((void**)&pdecdata);//释放解密内存
- rvc_free_data((void**)&pHeadJson);
- #endif
- //Dbg("decode file decHeadJson = %s",strHeadJson.c_str());
- if(uploadedLen>m_fsm->m_currUploadFile->fileLen){
- //续传起始位置大于终端文件长度
- Dbg("uploadFile is fail, begin read filelen [%d] is over terminal filelen [%d]",uploadedLen,m_fsm->m_currUploadFile->fileLen);
- errmsg=CSimpleStringA::Format("uploadFile is fail, begin read filelen [%d] is over terminal filelen [%d]",uploadedLen,m_fsm->m_currUploadFile->fileLen).GetData();
- return false;
- }
- if(uploadedLen<0){
- //服务器报错的头文件内容加上偏移量后,极端情况可能会出现负数,设置为零,最后服务端做长度比较不相同,会清空临时文件重传
- Dbg("uploadFile decode uploadedLen beginPos < 0");
- uploadedLen =0;
- }
- Dbg("文件开始传送位置 uploadedLen = %d",uploadedLen);
- //循环传送数据块
- FILE* pFile =fopen(m_fsm->m_currUploadFile->filePath.c_str(),"rb");
- if(pFile==NULL){
- //读取文件失败;
- Dbg("uploadFile is fail, file open fail");
- errmsg="uploadFile is fail, file open fail";
- return false;
- }
- bool uploadSucc= false;
- while(true){
- //续传
- if((m_fsm->m_currUploadFile->fileLen-uploadedLen)>m_fsm->m_eachUploadLen){
- int uploadLen = m_fsm->m_eachUploadLen;//传送数据长度
- unsigned char* content = new unsigned char[uploadLen];
- memset(content,0,uploadLen);
- //从文件拷贝字节
- //Dbg("拷贝字节开始,文件[%s],起始位:%d,拷贝字节数:%d",m_fsm->m_currUploadFile->filePath.c_str(),uploadedLen,uploadLen);
- if(m_fsm->getFileContent(pFile,content,uploadedLen,uploadLen)){
- UploadDataRet resp ={0};
- if(uploadFileImpl(client,info,reqUrl,content,uploadLen,"",resp)){
- delete[] content;
- uploadedLen +=uploadLen;//改变已上传长度
- }else{
- delete[] content;
- errmsg=CSimpleStringA::Format("url=%s, uploadFileImpl continue is fail",reqUrl.c_str()).GetData();
- break;//上传失败
- }
- }else{
- delete[] content;
- errmsg="uploadFile is fail , getFileContent fail";
- break;//获取文件内存失败
- }
- }else{
- //获取最后一次传送时文件长度
- CSimpleStringA filepath = m_fsm->m_currUploadFile->filePath.c_str();
- long fileLastLen = 0;
- if(!m_fsm->GetFileLength(filepath.GetData(),fileLastLen)){
- errmsg="uploadFile is fail , end get file len fail";
- break;//计算sm3出错
- }
-
- string fileLastLenStr= CSimpleStringA::Format("%d",fileLastLen-decOffSet).GetData();//计算出解密后总长度
- //最后一块
- int uploadLen = m_fsm->m_currUploadFile->fileLen-uploadedLen;
- unsigned char* contentEnd;
- if(uploadLen==0){
- //对零的长度特殊处理
- contentEnd = new unsigned char[1];
- memset(contentEnd,0,1);
- }else{
- contentEnd = new unsigned char[uploadLen];
- memset(contentEnd,0,uploadLen);
- }
-
- //Dbg("最后拷贝字节开始,文件[%s],起始位:%d,拷贝字节数:%d",m_fsm->m_currUploadFile->filePath.c_str(),uploadedLen,uploadLen);
- if(m_fsm->getFileContent(pFile,contentEnd,uploadedLen,uploadLen)){
- UploadDataRet respEnd ={0};
- if(uploadFileImpl(client,info,reqUrl,contentEnd,uploadLen,fileLastLenStr,respEnd)){
- delete[] contentEnd;
- if(respEnd.upload_finish){
- uploadSucc =true;//成功
- branchFilePath = respEnd.branchFilePath;//分行保存地址
- }else{
- uploadSucc =false;//失败
- }
- break;
- }else{
- delete[] contentEnd;
- errmsg=CSimpleStringA::Format("url=%s, uploadFileImpl end is fail",reqUrl.c_str()).GetData();
- break;//上传失败
- }
- }else{
- delete[] contentEnd;
- errmsg="uploadFile is fail , end getFileContent fail";
- break;//获取文件内存失败
- }
- }
- }
- if(uploadSucc){
- Dbg("uploadFile succ");
- }else{
- Dbg("uploadFile fail url=%s",reqUrl.c_str());
- }
- if(pFile){
- if(fclose(pFile)!=0){
- Dbg("uploadFile fail, close file fail ,url=%s",reqUrl.c_str());
- errmsg="uploadFile fail, close file fail";
- return false;
- }
- }
- return uploadSucc;
- }
- //上传文件:具体实现
- bool uploadFileImpl(IHttpFunc* client,CSystemStaticInfo info,string httpUrl,unsigned char* fileContent,long fileContentSize,string lastFileLength,UploadDataRet& uploadData){
- UploadFileReq qTempReq;
- UploadFileRet qTempRet;
- qTempRet.m_userCode ="";
- qTempRet.m_errMsg="";
-
- Json::Value rootReq;
- Json::FastWriter writer;
- rootReq["terminal_no"] = info.strTerminalID.GetData();
- rootReq["branch_no"] = m_fsm->m_currUploadFile->branch_no.c_str();
- rootReq["file_name"] = m_fsm->m_currUploadFile->fileName.c_str();
- rootReq["file_path"] = m_fsm->m_currUploadFile->fileUploadPath.c_str();
- rootReq["plan_id"] = m_fsm->m_currUploadFile->plan_id.c_str();
- rootReq["file_type"] = m_fsm->m_currUploadFile->file_type.c_str();
- //rootReq["origin_file_length"] = CSimpleStringA::Format("%d",m_fsm->m_currUploadFile->fileLen).GetData();
- rootReq["first_file_length"] = CSimpleStringA::Format("%d",m_fsm->m_currUploadFile->decFileLen).GetData();//加密后需要修改,服务端根据此长度做判断
- rootReq["last_file_length"] = lastFileLength.c_str();//加密后需要修改,最后一次长度跟服务端保存长度做对比
- rootReq["upload_length"] = CSimpleStringA::Format("%d",fileContentSize).GetData();
- //rootReq["first_sm3"] = firstMD5Str.c_str();
- //rootReq["last_sm3"] = lastMD5Str.c_str();
- rootReq["file_head_custom"] = m_fsm->m_currUploadFile->decHeadJson.c_str();//自定义解密的头json格式,可为空
- #ifdef RVC_OS_WIN
- rootReq["system_type"] = "W";//系统标记,代表路径分隔符
- #else
- rootReq["system_type"] = "L";//系统标记,代表路径分隔符
- #endif
- string jsonReq = writer.write(rootReq);
- qTempReq.url = httpUrl;
- qTempReq.paramContent = jsonReq;
- qTempReq.fileName = m_fsm->m_currUploadFile->fileName;
- qTempReq.fileContent = fileContent;
- qTempReq.fileContentSize= fileContentSize;
- qTempReq.m_timeOut = 5*60;//设置超时时间
- //qTempReq.m_printDbg=true;
- //调用上传接口
- //Dbg("测试上传接口:filelen=%d",fileContentSize);
- if(!client->UploadFileBlock(qTempReq,qTempRet)){
- Dbg("uploadFile http req fail ,url =%s ,fileName = %s",httpUrl.c_str(), m_fsm->m_currUploadFile->fileName.c_str());
- return false;
- }
- if(qTempRet.httpCode!=200){
- Dbg("uploadFile http req fail ,retHttpCode=%d, fileName = %s",qTempRet.httpCode, m_fsm->m_currUploadFile->fileName.c_str());
- return false;
- }
- //Dbg("qTempRet=%s",qTempRet.strBody.c_str());
- //解析回应报文
- Json::Reader reader;
- Json::Value rootRet;
- if (!reader.parse(qTempRet.strBody, rootRet, false))
- {
- Dbg("uploadFile http parse resp is fail,fileName = %s",m_fsm->m_currUploadFile->fileName.c_str());
- return false;//解析失败
- }
- uploadData.strBody = qTempRet.strBody;
- bool isSucc = rootRet["success"].asBool();
- if(isSucc){
- if(rootRet.isMember("data")){
- uploadData.uploaded_length= rootRet["data"]["uploaded_length"].asInt();
- uploadData.upload_finish = rootRet["data"]["upload_finish"].asBool();
- uploadData.branchFilePath = rootRet["data"]["branch_file_path"].asCString();
- return true;//返回值
- }else{
- Dbg("uploadFile http [success][data] is invaild , fileName = %s",m_fsm->m_currUploadFile->fileName.c_str());
- return false;//无data字段
- }
- }else{
- //失败
- Dbg("uploadFile http [success] is false, userCode[%s],errmsg[%s],fileName = %s",qTempRet.m_userCode.c_str(),qTempRet.m_errMsg.c_str(),m_fsm->m_currUploadFile->fileName.c_str());
- return false;
- }
- }
- //查询临时文件 post
- bool queryTempFile(IHttpFunc* client,CSystemStaticInfo info){
- UploadQueryTempFileReq qTempReq;
- UploadQueryTempFileRet qTempRet;
- qTempRet.m_userCode ="";
- qTempRet.m_errMsg="";
- Json::Value rootReq;
- Json::FastWriter writer;
- rootReq["terminal_no"] = info.strTerminalID.GetData();
- rootReq["plan_id"] = m_fsm->m_currUploadFile->plan_id.c_str();
- rootReq["file_path"] = m_fsm->m_currUploadFile->fileUploadPath.c_str();
- rootReq["file_type"] = m_fsm->m_currUploadFile->file_type.c_str();
- string jsonReq = writer.write(rootReq);
- qTempReq.m_reqStr = jsonReq;//请求参数
- //qTempReq.m_printDbg = true;
- qTempReq.m_url=m_fsm->m_centerUrl.queryTempFileUrl;//访问地址
- if(!client->Post(qTempReq, qTempRet)){
- Dbg("queryTempFile http req fail ,fileName = %s, err=%s",m_fsm->m_currUploadFile->filePath.c_str(),qTempRet.m_errMsg.c_str());
- return false;
- }
- //解析报文
- //Dbg("qTempRet=%s",qTempRet.m_retStr.c_str());
- //解析回应报文
- Json::Reader reader;
- Json::Value rootRet;
- if (!reader.parse(qTempRet.m_retStr, rootRet, false))
- {
- Dbg("queryTempFile parse resp is fail,fileName = %s",m_fsm->m_currUploadFile->filePath.c_str());
- m_fsm->m_uploadReport.queryTempFail++;//添加查询临时失败计数
- return false;//解析失败
- }
- bool isSucc = rootRet["success"].asBool();
- if(isSucc){
- if(rootRet.isMember("data")){
- if(rootRet["data"]["upload_flag"].asBool()){
- return true;//查询有
- }else{
- return false;//查询没有
- }
- }else{
- m_fsm->m_uploadReport.queryTempFail++;//添加查询临时失败计数
- return false;//无data字段
- }
- }else{
- Dbg("queryTempFile [success] is false, fileName = %s",m_fsm->m_currUploadFile->filePath.c_str());
- m_fsm->m_uploadReport.queryTempFail++;//添加查询临时失败计数
- return false;
-
- }
- }
- //登记文件流水状态 post
- bool updateState(IHttpFunc* client,CSystemStaticInfo info,bool isLanTerminal, string MD5str,string branchFilePath, string &errmsg){
- if(!isLanTerminal){
- return true;//行外默认是总行那边登记交易流水,终端不再发送交易流水登记,直接返回成功
- }
- UploadUpdateStateReq updateStateReq;
- UploadUpdateStateRet updateStateRet;
- updateStateRet.m_userCode="";
- updateStateRet.m_errMsg="";
- Json::Value rootReq;
- Json::FastWriter writer;
- rootReq["plan_id"] = m_fsm->m_currUploadFile->plan_id.c_str();
- rootReq["branch_no"] = m_fsm->m_currUploadFile->branch_no.c_str();
- rootReq["terminal_no"] = info.strTerminalID.GetData();
- rootReq["file_type"] = m_fsm->m_currUploadFile->file_type.c_str();
- rootReq["file_name"] = m_fsm->m_currUploadFile->fileName.c_str();
- rootReq["file_path"] = m_fsm->m_currUploadFile->fileUploadPath.c_str();
- rootReq["branch_file_path"] = branchFilePath.c_str();
- rootReq["file_length"] = CSimpleStringA::Format("%d",m_fsm->m_currUploadFile->fileLen).GetData();
- if(isLanTerminal){
- rootReq["status"] = "1";//分行
- }else{
- rootReq["status"] = "2";//总行
- }
- rootReq["upload_time"] = CSmallDateTime::GetNow().ToTimeString().GetData();
- rootReq["remark"] = "";
- rootReq["sm3"] = MD5str.c_str();
- string jsonReq = writer.write(rootReq);
- updateStateReq.m_reqStr = jsonReq;//请求参数
- updateStateReq.m_url=m_fsm->m_centerUrl.updateStateUrl;//访问地址
- if(!client->Post(updateStateReq, updateStateRet)){
- Dbg("updateState is fail, sysCode=%d , userCode = %s, errmsg = %s",updateStateRet.m_sysCode, updateStateRet.m_userCode.c_str(), updateStateRet.m_errMsg.c_str());
- errmsg = CSimpleStringA::Format("updateState is fail, sysCode=%d , userCode = %s, errmsg = %s",updateStateRet.m_sysCode, updateStateRet.m_userCode.c_str(), updateStateRet.m_errMsg.c_str());
- return false;
- }
- //解析报文
- //Dbg("updateStateRet=%s",updateStateRet.m_retStr.c_str());
- //解析回应报文
- Json::Reader reader;
- Json::Value rootRet;
- if (!reader.parse(updateStateRet.m_retStr, rootRet, false))
- {
- errmsg="return pack parse is fail";
- return false;//解析失败
- }
- bool isSucc = rootRet["success"].asBool();
- if(isSucc){
- if(rootRet.isMember("data")){
- if(rootRet["data"]["update_flag"].asBool()){
- return true;//登记成功
- }else{
- errmsg="return pack [data][update_flag] is not bool";
- return false;//登记失败
-
- }
- }else{
- errmsg="return pack [data] is null";
- return false;//无data字段
- }
- }else{
- errmsg="return pack [success] is false";
- return false;
- }
- }
- //登记失败文件流水 post
- bool addUploadFail(IHttpFunc* client,CSystemStaticInfo info,bool isLanTerminal,string operation,string errmsg){
- UploadUploadFailReq updateFailReq;
- UploadUploadFailRet updateFailRet;
- updateFailRet.m_userCode="";
- updateFailRet.m_errMsg="";
- Json::Value rootReq;
- Json::FastWriter writer;
- rootReq["plan_id"] = m_fsm->m_currUploadFile->plan_id.c_str();
- rootReq["branch_no"] = m_fsm->m_currUploadFile->branch_no.c_str();
- rootReq["terminal_no"] = info.strTerminalID.GetData();
- rootReq["file_name"] = m_fsm->m_currUploadFile->fileName.c_str();
- rootReq["file_path"] = m_fsm->m_currUploadFile->fileUploadPath.c_str();
- if(isLanTerminal){
- rootReq["stage"] = "1";//分行
- }else{
- rootReq["stage"] = "2";//总行
- }
- rootReq["operation"] = operation;
- rootReq["upload_time"] = CSmallDateTime::GetNow().ToTimeString().GetData();//2021-03-11 10:20:02
- rootReq["reason"] = errmsg.c_str();
- rootReq["remark"] = "";
- string jsonReq = writer.write(rootReq);
- updateFailReq.m_reqStr = jsonReq;//请求参数
- updateFailReq.m_url = m_fsm->m_centerUrl.addUploadFailUrl;//访问地址
- if(!client->Post(updateFailReq, updateFailRet)){
- Dbg("addUploadFail is fail, sysCode=%d , userCode = %s",updateFailRet.m_sysCode, updateFailRet.m_userCode.c_str());
- return false;
- }
- //解析报文
- //Dbg("updateFailRet=%s",updateFailRet.m_retStr.c_str());
- //解析回应报文
- Json::Reader reader;
- Json::Value rootRet;
- if (!reader.parse(updateFailRet.m_retStr, rootRet, false))
- {
- return false;//解析失败
- }
- bool isSucc = rootRet["success"].asBool();
- if(isSucc){
- if(rootRet.isMember("data")){
- if(rootRet["data"]["update_flag"].asBool()){
- return true;//登记成功
- }else{
- return false;//登记失败
- }
- }else{
- return false;//无data字段
- }
- }else{
- Dbg("addUploadFail [success] is false");
- return false;
- }
- }
-
- //上传后文件处理
- bool operateFile(string &errmsg){
- if(m_fsm->m_currUploadFile->after_deal_type=="D"){
- Dbg("delete file %s",m_fsm->m_currUploadFile->fileName.c_str());
- #ifdef RVC_OS_WIN
- if(remove(m_fsm->m_currUploadFile->filePath.c_str())==0){
- Dbg("delete file is ok");
- return true;
- }else{
- Dbg("delete file is error, errno=%d",GetLastError());
- errmsg = CSimpleStringA::Format("delete file is error, errno=%d",GetLastError());
- return false;
- }
- #else
- if (changeFileAtt(m_fsm->m_currUploadFile->filePath.c_str()) == 0) {
- if (remove(m_fsm->m_currUploadFile->filePath.c_str()) == 0) {
- Dbg("delete file is ok");
- return true;
- }
- else {
- Dbg("delete file is error, errno=%d", errno);
- errmsg = CSimpleStringA::Format("delete file is error, errno=%d", errno);
- return false;
- }
- }
- else {
- Dbg("delete file is error,changefileAtt fail, errno=%d", errno);
- errmsg = CSimpleStringA::Format("delete file is error,changefileAtt fail, errno=%d", errno);
- return false;
- }
- #endif
- }else if(m_fsm->m_currUploadFile->after_deal_type=="M"){
- string destPath="";
- string destDir="";
- //根据路径类型组成最后移动路径
- if(m_fsm->m_currUploadFile->path_type=="R"){
- #ifdef RVC_OS_WIN
- destPath = m_fsm->m_rootPath+"\\"+m_fsm->m_currUploadFile->after_deal_text+"\\"+m_fsm->m_currUploadFile->fileName;
- destDir = m_fsm->m_rootPath+"\\"+m_fsm->m_currUploadFile->after_deal_text;
- #else
- destPath = m_fsm->m_rootPath + SPLIT_SLASH_STR + m_fsm->m_currUploadFile->after_deal_text + SPLIT_SLASH_STR + m_fsm->m_currUploadFile->fileName;
- destDir = m_fsm->m_rootPath + SPLIT_SLASH_STR + m_fsm->m_currUploadFile->after_deal_text;
- #endif
- }else{
- #ifdef RVC_OS_WIN
- destPath = m_fsm->m_currUploadFile->after_deal_text+"\\"+m_fsm->m_currUploadFile->fileName;
- #else
- destPath = m_fsm->m_currUploadFile->after_deal_text + SPLIT_SLASH_STR + m_fsm->m_currUploadFile->fileName;
- #endif
- destDir = m_fsm->m_currUploadFile->after_deal_text;
- }
- string srcPath = m_fsm->m_currUploadFile->filePath;
- Dbg("move file %s to %s",srcPath.c_str(),destPath.c_str());
- //判断是否存在目标文件,如存在,则先删除
- #ifdef RVC_OS_WIN
- if(ExistsFileA(destPath.c_str())){
- if(remove(destPath.c_str())==0){
- Dbg("delete dest file is ok");
- }else{
- Dbg("delete dest file is error, errno=%d",GetLastError());
- errmsg = CSimpleStringA::Format("delete src file is error, errno=%d",GetLastError());
- return false;
- }
- }
- //判断移动目标文件夹是否存在,不存在,则创建
- if(!ExistsDirA(destDir.c_str())){
- if(!CreateDirRecursiveA(destDir.c_str())){
- Dbg("create dest dir [%s] is error, errno=%d",destDir.c_str(),GetLastError());
- errmsg = CSimpleStringA::Format("create dest dir [%s] is error, errno=%d",destDir.c_str(),GetLastError());
- return false;
- }
- }
- //移动文件
- int i;
- int tries = 10;
- bool isSucc = false;
- for (i = 0; i < tries; ++i) {
- if(rename(srcPath.c_str(),destPath.c_str())==0){
- Dbg("move ok! tries:%d", i);
- isSucc = true;
- break;
- } else {
- Dbg("move %s -> %s failed! errno:%d",srcPath.c_str(),destPath.c_str(), GetLastError());
- errmsg = CSimpleStringA::Format("move failed! destPath=[%s], errno:%d",destPath.c_str(), GetLastError());
- Sleep(1000);
- }
- }
- return isSucc;
- #else
- if (ExistsFileA(destPath.c_str())) {
- if (changeFileAtt(destPath.c_str()) == 0) {
- if (remove(destPath.c_str()) == 0) {
- Dbg("delete dest file is ok");
- }
- else {
- Dbg("delete dest file is error, errno=%d", errno);
- errmsg = CSimpleStringA::Format("delete src file is error, errno=%d", errno);
- return false;
- }
- }
- else {
- Dbg("delete dest file is error,changefileAtt fail ,errno=%d , file=%s ", errno, destPath.c_str());
- errmsg = CSimpleStringA::Format("delete src file is error, changefileAtt fail, errno=%d", errno);
- return false;
- }
- }
- //判断移动目标文件夹是否存在,不存在,则创建
- if (!ExistsDirA(destDir.c_str())) {
- if (!CreateDirRecursiveA(destDir.c_str())) {
- Dbg("create dest dir [%s] is error, errno=%d", destDir.c_str(), errno);
- errmsg = CSimpleStringA::Format("create dest dir [%s] is error, errno=%d", destDir.c_str(), errno);
- return false;
- }
- }
- //移动文件
- if (changeFileAtt(srcPath.c_str()) == 0)
- {
- int i;
- int tries = 10;
- bool isSucc = false;
- for (i = 0; i < tries; ++i) {
- //rename函数对于跨分支的目录存在拷贝不成功的情况,故采用先拷贝后删除的操作修复此bug
- if (fileutil_copy_file(destPath.c_str(), srcPath.c_str()) == 0) {
- if (remove(srcPath.c_str()) == 0) {
- Dbg("move ok! tries:%d", i);
- isSucc = true;
- break;
- }
- else {
- Dbg("move %s -> %s failed! delete src file fail, errno:%d", srcPath.c_str(), destPath.c_str(), errno);
- errmsg = CSimpleStringA::Format("move failed! delete src file fail, srcPath=[%s], errno:%d", srcPath.c_str(), errno);
- Sleep(1000);
- }
- }
- else {
- Dbg("move %s -> %s failed! copy src file fail, errno:%d", srcPath.c_str(), destPath.c_str(), errno);
- errmsg = CSimpleStringA::Format("move failed! copy src file fail, destPath=[%s], errno:%d", destPath.c_str(), errno);
- Sleep(1000);
- }
- }
- return isSucc;
- }
- else {
- Dbg("move %s -> %s failed! , changefileAtt fail,errno=%d", srcPath.c_str(), destPath.c_str(), errno);
- errmsg = CSimpleStringA::Format("move failed! changefileAtt fail, srcPath=[%s], errno:%d", srcPath.c_str(), errno);
- return false;
- }
- #endif
- }else if(m_fsm->m_currUploadFile->after_deal_type=="N"){
- return true;
- }else{
- Dbg("file [%s] invalid after_deal_type=%s",m_fsm->m_currUploadFile->fileName.c_str(),m_fsm->m_currUploadFile->after_deal_type.c_str());
- errmsg = CSimpleStringA::Format("invalid after_deal_type=%s",m_fsm->m_currUploadFile->after_deal_type.c_str());
- return false;
- }
- }
- };
- }
- 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') {
- m_isOffLine = true;
- PostEventFIFO(new FSMEvent(USER_EVT_JMP_ENABLE));
- } else {
- m_isOffLine = false;
- 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;
- Error = m_pEntity->GetFunction()->OpenConfig(Config_CenterSetting, spConfig);
-
- if(Error==Error_Succeed){
- //获取模式控制参数
- Error = spConfig->ReadConfigValueInt("upload", "CenterModel",m_centerModel);
- if (Error_Succeed != Error)
- {
- Dbg("get m_centerModel from CenterSetting.ini failed");
- return Error;
- }
- if(m_centerModel==(int)branchModel||m_centerModel==(int)mixModel){
- Dbg("model is branchModel or mixModel,CenterModel = %d",m_centerModel);
- CSmartPointer<IConfigInfo> spConfig_software;
- Error = m_pEntity->GetFunction()->OpenConfig(Config_Software, spConfig_software);
- Error = (ErrorCodeEnum)upload_create(&m_updir_list, m_pEntity->GetFunction().GetRawPointer(), spConfig_software.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);
- }
- */
- }
- }else if(m_centerModel==(int)centerModel){
- Dbg("model is centerModel, CenterModel = %d",m_centerModel);
- }else{
- Dbg("model is invalid ,CenterModel =%d",m_centerModel);
- return Error_Unexpect;
- }
- //增加总行接口Url参数
- CSimpleStringA str;
- Error = spConfig->ReadConfigValue("upload", "QueryPlanUrl", str);
- if (Error == Error_Succeed)
- {
- Dbg("[CenterUrl] QueryPlanUrl =%s",str.GetData());
- m_centerUrl.queryPlanUrl = str.GetData();
- }
- else
- {
- return Error;
- }
- Error = spConfig->ReadConfigValue("upload", "QueryTempFileUrl", str);
- if (Error == Error_Succeed)
- {
- Dbg("[CenterUrl] QueryTempFileUrl =%s",str.GetData());
- m_centerUrl.queryTempFileUrl = str.GetData();
- }
- else
- {
- return Error;
- }
- Error = spConfig->ReadConfigValue("upload", "UpdateStateUrl", str);
- if (Error == Error_Succeed)
- {
- Dbg("[CenterUrl] UpdateStateUrl =%s",str.GetData());
- m_centerUrl.updateStateUrl = str.GetData();
- }
- else
- {
- return Error;
- }
- Error = spConfig->ReadConfigValue("upload", "AddUploadFailUrl", str);
- if (Error == Error_Succeed)
- {
- Dbg("[CenterUrl] AddUploadFailUrl =%s",str.GetData());
- m_centerUrl.addUploadFailUrl = str.GetData();
- }
- else
- {
- return Error;
- }
- Error = spConfig->ReadConfigValue("upload", "EachUploadLen", str);
- if (Error == Error_Succeed)
- {
- Dbg("[CenterUrl] EachUploadLen =%s",str.GetData());
- if(str.IsNullOrEmpty()){
- m_eachUploadLen = UPLOAD_CONTENT_EACH_LEN;
- }else{
- m_eachUploadLen = atoi(str.GetData())*1024;
- }
- }
- else
- {
- return Error;
- }
- int uploadMaxNum = 0;
- Error = spConfig->ReadConfigValueInt("upload", "UploadMaxNum", uploadMaxNum);
- if (Error == Error_Succeed)
- {
- Dbg("[CenterUrl] UploadMaxNum =%d",uploadMaxNum);
- if(uploadMaxNum<=10){
- m_uploadMaxNum = 1000;
- }else{
- m_uploadMaxNum = uploadMaxNum;
- }
- }
- else
- {
- return Error;
- }
- //Error = spConfig->ReadConfigValue("upload", "UploadCenterUrl", str);
- //if (Error == Error_Succeed)
- //{
- // Dbg("[CenterUrl] UploadCenterUrl =%s",str.GetData());
- // m_centerUrl.uploadCenterUrl = str.GetData();
- //}
- //else
- //{
- // return Error;
- //}
- //获取系统文件根路径
- CSimpleStringA runPath ;
- Error =this->GetEntityBase()->GetFunction()->GetPath("Root",runPath);//例如:C:\\Run /opt/Run
- if(Error_Succeed == Error){
- if(!runPath.IsNullOrEmpty()){
- string runPathStr = runPath.GetData();
- int pos = runPathStr.find_last_of(SPLIT_SLASH);//修改为路径分隔符
- m_rootPath = runPathStr.substr(0,pos);
- Dbg("get Root path is %s",m_rootPath.c_str());
- }else{
- Dbg("get Root path is null");
- return Error_Unexpect;
- }
- }else{
- Dbg("get Root path is error");
- return Error_Unexpect;
- }
- }else{
- return Error;
- }
-
- /**交易受限功能代码废弃
- //初始化临界区
- InitializeCriticalSection(&m_cs);
- m_dBeginTime=0;
- m_dEndTime=0;
- m_iSpeed=0;
- */
-
- Error = m_pEntity->GetFunction()->RegistSysVarEvent("CallState", this);
-
- 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()
- {
- ScheduleTimer(8, 30000);
- }
- void UploadFSM::s1_on_exit()
- {
- CancelTimer(8);
- }
- unsigned int UploadFSM::s1_on_event(FSMEvent* event)
- {
- if (event->iEvt == EVT_TIMER) {
- if(m_isOffLine){
- PostEventLIFO(new FSMEvent(USER_EVT_JMP_ENABLE));
- }else{
- ScheduleTimer(8, 30000);//继续等待
- }
- }
- return 0;
- }
- void UploadFSM::s2_on_entry()
- {
- m_uploadModel = false;//默认是分行上传模式
- //清理上次扫描文件列表,扫描计划,分行ip列表
- if(m_uploadList!=NULL){
- list<UploadFileInfo*>::iterator iter ;
- for(iter=m_uploadList->begin();iter!=m_uploadList->end();++iter){
- delete *iter;
- }
- m_uploadList->clear();
- }
- if(m_uploadPlanList!=NULL){
- list<UploadPlan*>::iterator iter ;
- for(iter=m_uploadPlanList->begin();iter!=m_uploadPlanList->end();++iter){
- delete *iter;
- }
- m_uploadPlanList->clear();
- }
- m_branchIpList.clear();
- while(true){
- file_t* f = find_first_upload_file();
- if (f == NULL) {
- break;//链表为空
- }else{
- updir_del_file(f);//销毁对象
- }
- }
- //上传模式确定
- if(m_centerModel==(int)branchModel){
- Dbg("branchModel");
- m_uploadModel = false;//旧模式
- }else if(m_centerModel == (int)centerModel){
- Dbg("centerModel");
- //防止url无效,新模式直接停止上传 等待下一次
- if(m_centerUrl.addUploadFailUrl.empty()||m_centerUrl.queryPlanUrl.empty()||
- m_centerUrl.queryTempFileUrl.empty()||m_centerUrl.updateStateUrl.empty()){
- Dbg("centerModel mode ,centerUrl is null , wait for next time");
- ScheduleTimer(1, 120000);
- return ;
- }
- //新模式,查询策略
- if(queryPlan()){
- m_uploadModel = true;
- }else{
- //查询策略失败,等待下一次
- Dbg("queryPlan is fail , wait for next time");
- ScheduleTimer(1, 120000);
- return ;
- }
- }else if(m_centerModel == (int)mixModel){
- Dbg("mixModel");
- m_uploadModel = queryPlan();//新旧模式 查询策略进一步判断采用何种模式
- //防止url无效,url依然走旧模式
- if(m_uploadModel){
- if(m_centerUrl.addUploadFailUrl.empty()||m_centerUrl.queryPlanUrl.empty()||
- m_centerUrl.queryTempFileUrl.empty()||m_centerUrl.updateStateUrl.empty()){
- //走旧模式
- Dbg("mixModel mode ,centerUrl is null ,exec old model");
- m_uploadModel=false;
- }
- }
- }else{
- Dbg("m_centerModel is invalid");
- ScheduleTimer(1, 120000);
- return;
- }
- if(m_uploadModel){
- //新模式扫描
- PostEventLIFO(new FSMEvent(USER_EVT_JMP_NEW_SCAN));
- }else{
- //旧模式扫描
- PostEventLIFO(new FSMEvent(USER_EVT_JMP_OLD_SCAN));
- //旧模式扫描
- /**
- if (find_first_upload_file() == NULL) {
- upload_fresh(&m_updir_list);
- }
- if (find_first_upload_file() == NULL) {
- ScheduleTimer(1, 120000);
- } 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, 120000);
- } else {
- //把新增加的上传日期加入集合里面
- //交易受限功能代码废弃
- //insertUploadDate();
- PostEventLIFO(new FSMEvent(USER_EVT_JMP_CONNECT));
- }
- */
- m_uploadModel = false;//默认是分行上传模式
- //清理上次扫描文件列表,扫描计划,分行ip列表
- if(m_uploadList!=NULL){
- list<UploadFileInfo*>::iterator iter ;
- for(iter=m_uploadList->begin();iter!=m_uploadList->end();++iter){
- delete *iter;
- }
- m_uploadList->clear();
- }
- if(m_uploadPlanList!=NULL){
- list<UploadPlan*>::iterator iter ;
- for(iter=m_uploadPlanList->begin();iter!=m_uploadPlanList->end();++iter){
- delete *iter;
- }
- m_uploadPlanList->clear();
- }
- m_branchIpList.clear();
- while(true){
- file_t* f = find_first_upload_file();
- if (f == NULL) {
- break;//链表为空
- }else{
- updir_del_file(f);//销毁对象
- }
- }
-
- //上传模式确定
- if(m_centerModel==(int)branchModel){
- //旧模式
- Dbg("branchModel");
- m_uploadModel = false;
- }else if(m_centerModel == (int)centerModel){
- Dbg("centerModel");
- //防止url无效,新模式直接停止上传,等待下一次
- if(m_centerUrl.addUploadFailUrl.empty()||m_centerUrl.queryPlanUrl.empty()||
- m_centerUrl.queryTempFileUrl.empty()||m_centerUrl.updateStateUrl.empty()){
- Dbg("centerModel mode ,centerUrl is null , wait for next time");
- ScheduleTimer(1, 240000);
- return 0;
- }
- //新模式,查询策略
- if(queryPlan()){
- m_uploadModel = true;
- }else{
- //查询策略失败,等待下一次
- Dbg("queryPlan is fail , wait for next time");
- ScheduleTimer(1, 240000);
- return 0;
- }
- }else if(m_centerModel == (int)mixModel){
- Dbg("mixModel");
- //新旧模式 查询策略进一步判断采用何种模式
- m_uploadModel = queryPlan();
- //防止url无效,url依然走旧模式
- if(m_uploadModel){
- if(m_centerUrl.addUploadFailUrl.empty()||m_centerUrl.queryPlanUrl.empty()||
- m_centerUrl.queryTempFileUrl.empty()||m_centerUrl.updateStateUrl.empty()){
- //走旧模式
- Dbg("mixModel mode ,centerUrl is null ,exec old model");
- m_uploadModel=false;
- }
- }
- }else{
- Dbg("m_centerModel is invalid");
- ScheduleTimer(1, 240000);
- return 0;
- }
- if(m_uploadModel){
- //新模式扫描
- PostEventLIFO(new FSMEvent(USER_EVT_JMP_NEW_SCAN));
- }else{
- //旧模式扫描
- PostEventLIFO(new FSMEvent(USER_EVT_JMP_OLD_SCAN));
- }
-
- }else if (event->iEvt == USER_EVT_JMP_OLD_SCAN) {
- Dbg("enter old model upload");
- //旧模式
- if (find_first_upload_file() == NULL) {
- upload_fresh(&m_updir_list);
- }
- if (find_first_upload_file() == NULL) {
- Dbg("find_first_upload_file is 0,wait for 4 min retry scan");
- ScheduleTimer(1, 240000);
- } else {
- PostEventLIFO(new FSMEvent(USER_EVT_JMP_CONNECT));
- }
- }else if (event->iEvt == USER_EVT_JMP_NEW_SCAN) {
- Dbg("enter new model upload");
- //新模式
- if (m_uploadList->size() == 0) {
- //重新扫描文件
- scanFile();
- }
- if (m_uploadList->size() == 0) {
- Dbg("m_uploadList is 0,wait for 4 min retry scan");
- ScheduleTimer(1, 240000);
- } else {
- PostEventLIFO(new FSMEvent(USER_EVT_JMP_NEW_UPLOAD));//跳到新的上传状态
- }
- }
- return 0;
- }
- void UploadFSM::s3_on_entry()
- {
- if (!m_pConnection) {
- m_pConnection = new UploadConnection(m_pEntity, this);
- //if (!m_pConnection->Connect(m_server1, m_server1_port, 3)) {
- // m_pConnection->Close();
- // m_pConnection->DecRefCount();
- // m_pConnection = NULL;
- // if((m_server2!=NULL)&&(m_server2_port!=0)&&((m_server2 != m_server1) || (m_server2_port != m_server1_port)))
- // {
- // m_pConnection = new UploadConnection(m_pEntity, this);
- // if (!m_pConnection->Connect(m_server2, m_server2_port, 3)) {
- // m_pConnection->Close();
- // m_pConnection->DecRefCount();
- // m_pConnection = NULL;
- // }
- // }
- //}
- 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));
- }
- }
- 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->Connect(m_server1, m_server1_port, 3))
- {
- m_pConnection->Close();
- m_pConnection->DecRefCount();
- m_pConnection = NULL;
- if((m_server2!=NULL)&&(m_server2_port!=0)&&((m_server2 != m_server1) || (m_server2_port != m_server1_port)))
- {
- m_pConnection = new UploadConnection(m_pEntity, this);
- if (!m_pConnection->Connect(m_server2, m_server2_port, 3))
- {
- m_pConnection->Close();
- m_pConnection->DecRefCount();
- m_pConnection = NULL;
- }
- }
- }*/
- 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, 1000); // 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, 30000);
- }
- 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 30s");
- }
- return 0;
- }
- void UploadFSM::s7_on_entry()
- {
- //新上传模式
- m_uploadReport.sum = m_uploadList->size();
- m_uploadReport.succ = 0;
- m_uploadReport.fail = 0;
- m_uploadReport.uploadFail = 0;
- m_uploadReport.updateState = 0;
- m_uploadReport.decodeFail = 0;
- m_uploadReport.operateFileFail= 0;
- m_uploadReport.queryTempFail =0;
- m_uploadReport.isBreak =false;
- //获取一个新文件,发送上传请求线程
- if(m_uploadList->size()>0){
- m_currUploadFile = *(m_uploadList->begin());//首元素赋值给全局变量
- m_uploadList->pop_front();//去除首元素
- Task::SendFileTask* task = new Task::SendFileTask(this);
- ErrorCodeEnum rc = this->GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
- if (rc != Error_Succeed)
- {
- if(m_currUploadFile!=NULL){
- delete m_currUploadFile;
- m_currUploadFile=NULL;
- }
- LogError(Severity_Middle, rc, 0, "Post SendFileTask task to Thread is fail,wait 30s retry");
- ScheduleTimer(7, 30000);
- }
- }
- }
- void UploadFSM::s7_on_exit()
- {
- if(m_currUploadFile!=NULL){
- delete m_currUploadFile;
- m_currUploadFile=NULL;
- }
- CancelTimer(7);
- }
- unsigned int UploadFSM::s7_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_UPLOAD_SUCC) {
- m_uploadReport.succ++;//成功计数
- //获取新的上传文件,发送上传请求线程
- //判断是否已经需要退出
- //无文件,跳出此状态
- if(m_isOffLine){
- if(m_uploadList->size()>0){
- if(m_currUploadFile!=NULL){
- delete m_currUploadFile;
- m_currUploadFile=NULL;
- }
- m_currUploadFile = *(m_uploadList->begin());//首元素赋值给全局变量
- m_uploadList->pop_front();//去除首元素
- Task::SendFileTask* task = new Task::SendFileTask(this);
- ErrorCodeEnum rc = this->GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
- if (rc != Error_Succeed)
- {
- if(m_currUploadFile!=NULL){
- delete m_currUploadFile;
- m_currUploadFile=NULL;
- }
- m_uploadReport.isBreak =true;
- reportBatchMsg();
- LogError(Severity_Middle, rc, 0, "Post SendFileTask task to Thread is fail,wait 120s retry");
- ScheduleTimer(7, 120000);
- }
- }else{
- m_uploadReport.isBreak =false;
- reportBatchMsg();
- Dbg("NewUpload m_uploadList is empty,wait 120s retry scan");
- ScheduleTimer(7, 120000);// goto s2
- }
- }else{
- m_uploadReport.isBreak =true;
- reportBatchMsg();
- Dbg("NewUpload m_isOffLine is false");
- PostEventLIFO(new FSMEvent(USER_EVT_JMP_NEW_DISABLE));// goto s1
- }
-
- }else if (event->iEvt == USER_EVT_UPLOAD_FAIL) {
- m_uploadReport.fail++;//失败计数
- //获取新的上传文件,发送上传请求线程
- //判断是否已经需要退出
- //无文件,跳出此状态
- if(m_isOffLine){
- Sleep(3000);//上传失败,等待5秒再下一个
- if(m_uploadList->size()>0){
- if(m_currUploadFile!=NULL){
- delete m_currUploadFile;
- m_currUploadFile=NULL;
- }
- m_currUploadFile = *(m_uploadList->begin());//首元素赋值给全局变量
- m_uploadList->pop_front();//去除首元素
- Task::SendFileTask* task = new Task::SendFileTask(this);
- ErrorCodeEnum rc = this->GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
- if (rc != Error_Succeed)
- {
- if(m_currUploadFile!=NULL){
- delete m_currUploadFile;
- m_currUploadFile=NULL;
- }
- m_uploadReport.isBreak =true;
- reportBatchMsg();
- LogError(Severity_Middle, rc, 0, "Post SendFileTask task to Thread is fail,wait 120s retry");
- ScheduleTimer(7, 120000);
- }
- }else{
- m_uploadReport.isBreak =false;
- reportBatchMsg();
- Dbg("NewUpload m_uploadList is empty,wait 120s retry scan");
- ScheduleTimer(7, 120000);// goto s2
- }
- }else{
- m_uploadReport.isBreak =true;
- reportBatchMsg();
- Dbg("NewUpload m_isOffLine is false");
- PostEventLIFO(new FSMEvent(USER_EVT_JMP_NEW_DISABLE));// goto s1
- }
-
- }else if (event->iEvt == EVT_TIMER) {
- // goto s2
- Dbg("NewUpload ,jump to s2");
- }
-
- 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
-
- }
- //通过
- bool UploadFSM::queryPlan()
- {
- Dbg("queryPlan");
- IHttpFunc* client;
- client = create_http(HttpsLogCallBack);
- CSystemStaticInfo si;
- ErrorCodeEnum error = this->GetEntityBase()->GetFunction()->GetSystemStaticInfo(si);
- if(error!=Error_Succeed){
- return false;//获取失败,走旧模式
- }
- UploadQueryPlanReq planReq;
- UploadQueryPlanRet planRet;
- Json::Value rootReq;
- Json::FastWriter writer;
- rootReq["terminal_no"] = si.strTerminalID.GetData();
- string jsonReq = writer.write(rootReq);
-
- planReq.m_reqStr = jsonReq;
- //planReq.m_printDbg = true;
- planReq.m_url = m_centerUrl.queryPlanUrl.c_str();
- if(!client->Post(planReq, planRet)){
- //Dbg("queryPlan http req fail,url is %s",m_centerUrl.queryPlanUrl.c_str());
- LogWarn(Severity_Middle, Error_Unexpect, LOG_WARN_UPLOAD_QUERY_PLAN_ERROR, CSimpleStringA::Format("queryPlan http req fail,url is %s, err=%s",m_centerUrl.queryPlanUrl.c_str(),planRet.m_errMsg.c_str()).GetData());
- client->Destory();
- return false;//http错误,走旧模式
- }
- client->Destory();
- //Dbg("planRet=%s",planRet.m_retStr.c_str());
- //解析回应报文
- Json::Reader reader;
- Json::Value rootRet;
- if (!reader.parse(planRet.m_retStr, rootRet, false))
- {
- Dbg("queryPlan parse resp is fail");
- return false;//解析失败,走旧模式
- }
- bool isSucc = rootRet["success"].asBool();
- if(isSucc){
- if(rootRet.isMember("data")){
- if(rootRet["data"]["mode"].asBool()){
- //新模式
- string branchNO = rootRet["data"]["branch_no"].asString();
- //分行服务器列表:可能为空
- if(rootRet["data"]["upload_address"].isMember("upload_url")){
- for(int i =0;i<rootRet["data"]["upload_address"]["upload_url"].size();i++){
- string bUploadUrl = rootRet["data"]["upload_address"]["upload_url"][i].asString();
- m_branchIpList[i+1]=bUploadUrl;
- //Dbg("queryPlan return upload_url=%s",bUploadUrl.c_str());
- if(!bUploadUrl.empty()){
- Dbg("queryPlan return upload_url=%s",bUploadUrl.c_str());
- }else{
- Dbg("queryPlan return upload_url=");
- }
- }
- }else{
- Dbg("[data][upload_address][upload_url] is null");
- }
- string env = "";//上传服务器地址属于行内还是行外
- if(rootRet["data"]["upload_address"].isMember("env")){
- env = rootRet["data"]["upload_address"]["env"].asString();
- if(strcmp(env.c_str(),"DMZ")==0||strcmp(env.c_str(),"BIZ")==0){
- }else{
- //Dbg("[data][upload_address][env] is invaild [%s]",env.c_str());
- LogWarn(Severity_Middle, Error_Unexpect, LOG_WARN_UPLOAD_QUERY_PLAN_DATA_ERROR, CSimpleStringA::Format("queryPlan http req fail, return [data][upload_address][env] is invaild [%s]",env.c_str()).GetData());
- return false;//上传服务ip环境不正确,走旧模式
- }
- }else{
- //Dbg("[data][upload_address][env] is null");
- LogWarn(Severity_Middle, Error_Unexpect, LOG_WARN_UPLOAD_QUERY_PLAN_DATA_ERROR, CSimpleStringA::Format("queryPlan http req fail, return [data][upload_address][env] is null").GetData());
- return false;//上传服务ip环境为空,走旧模式
- }
- //扫描策略:可能为空
- for (int i =0;i <rootRet["data"]["plan_list"].size();i++){
- UploadPlan* p = new UploadPlan();
- p->plan_id = rootRet["data"]["plan_list"][i]["plan_id"].asString();
- p->branch_no = branchNO;//分行号
- p->plan_type = rootRet["data"]["plan_list"][i]["plan_type"].asString();
- p->file_type = rootRet["data"]["plan_list"][i]["file_type"].asString();
- p->path_type = rootRet["data"]["plan_list"][i]["path_type"].asString();
- #ifdef RVC_OS_WIN
- p->path_name = rootRet["data"]["plan_list"][i]["path_name"].asString();
- #else
- //转义路径里面windows反斜杠'\'为'/'
- //p->path_name = rootRet["data"]["plan_list"][i]["path_name"].asString();
- string pathName = rootRet["data"]["plan_list"][i]["path_name"].asString();
- std::replace(pathName.begin(), pathName.end(), '\\', SPLIT_SLASH);
- p->path_name = pathName.c_str();
- #endif
-
- p->file_name = rootRet["data"]["plan_list"][i]["file_name"].asString();
- p->file_regex = rootRet["data"]["plan_list"][i]["file_regex"].asString();
- p->before_deal_type = rootRet["data"]["plan_list"][i]["before_deal_type"].asString();
- p->after_deal_type = rootRet["data"]["plan_list"][i]["after_deal_type"].asString();
- #ifdef RVC_OS_WIN
- p->after_deal_text = rootRet["data"]["plan_list"][i]["after_deal_text"].asString();
- #else
- //转义路径里面windows反斜杠'\'为'/'
- //p->after_deal_text = rootRet["data"]["plan_list"][i]["after_deal_text"].asString();
- string afterDealText = rootRet["data"]["plan_list"][i]["after_deal_text"].asString();
- std::replace(afterDealText.begin(), afterDealText.end(), '\\', SPLIT_SLASH);
- p->after_deal_text = afterDealText.c_str();
- #endif
- p->file_limit = rootRet["data"]["plan_list"][i]["file_limit"].asInt();
- p->file_length_limit = rootRet["data"]["plan_list"][i]["file_length_limit"].asInt();//增加扫描文件最大长度限制
- if(p->file_length_limit==0){
- //默认扫描文件最大长度设置为200m
- p->file_length_limit = 200;
- }
- p->max_silent = rootRet["data"]["plan_list"][i]["max_silent"].asInt();
- p->upload_address_env = env;
- m_uploadPlanList->push_back(p);
- }
- //行内需要判断分行服务url地址(废弃用终端场所判断,统一由后台服务返回文件上传地址)
- //if(si.strSite.Compare("CMB.LIB",true)==0||si.strSite.Compare("CMB.SSB",true)==0){
- // if(m_branchIpList.size()==0){
- // return false;//告警分行服务ip为空,走旧模式
- // }
- //}
- if(m_branchIpList.size()==0){
- //Dbg("queryPlan return upload_url count is 0");
- LogWarn(Severity_Middle, Error_Unexpect, LOG_WARN_UPLOAD_QUERY_PLAN_DATA_ERROR, CSimpleStringA::Format("queryPlan http req fail, return upload_url count is 0, url is %s",m_centerUrl.queryPlanUrl.c_str()).GetData());
- return false;//上传服务ip为空,走旧模式
- }
-
- if(m_uploadPlanList->size()==0){
- Dbg("queryPlan return %d plan ",m_uploadPlanList->size());
- return true;//等待下一次请求,走新模式
- }
- return true;//明确走新模式
- }else{
- Dbg("queryPlan return [data].[mode] is false");
- return false;//明确走旧模式
- }
- }else{
- Dbg("queryPlan return data is null");
- return false;//无data字段,走旧模式
- }
- }else{
- Dbg("queryPlan return success is false");
- return false;//服务返回错误,走旧模式
- }
- }
- //通过
- void UploadFSM::scanFile()
- {
- list<UploadPlan*>::iterator iter;
- for(iter = m_uploadPlanList->begin();iter!=m_uploadPlanList->end();++iter){
- UploadPlan* up = *iter;
- //构建初始的扫描根路径
- string path="";
- if(up->path_type=="R"){
- #ifdef RVC_OS_WIN
- path = m_rootPath + "\\" + up->path_name;
- #else
- path = m_rootPath + SPLIT_SLASH_STR + up->path_name;
- //path = "/opt/" + up->path_name;//test
- #endif
- }else if(up->path_type=="A"){
- path=up->path_name;
- }else{
- Dbg("plan path_type is invalid path_type=%s",up->path_type.c_str());
- continue;
- }
- Dbg("planID=[%s], scanDir=[%s], filter=[%s], max_silent=[%d]",up->plan_id.c_str(),path.c_str(),up->file_name.c_str(),up->max_silent);
- scanDirfresh(up,path.c_str());
- }
- //加入文件超过阈值后告警
- if(m_uploadList->size()>m_uploadMaxNum){
- LogWarn(Severity_Middle, Error_Unexpect, LOG_WARN_UPLOAD_MAX_ERROR, CSimpleStringA::Format("upload list num(%d) is over max num",m_uploadList->size()).GetData());
- }
- }
- //通过
- void UploadFSM::scanDirfresh(UploadPlan* plan,const char *path)
- {
- //Dbg("scan dir=%s",path);
- array_header_t *arr_files = fileutil_get_sub_files2_a(path, plan->file_limit);
- if (arr_files) {
- int i;
- for (i = 0; i < arr_files->nelts; ++i){
- char *file_path = ARRAY_IDX(arr_files, i, char*);
- //Dbg("find file src path=%s",file_path);
- //得到文件名
- string fileName = _GetFileName(file_path);
- //Dbg("fileName=%s",fileName.c_str());
- //文件名通配符过滤
- CSimpleStringA strFileFilter = _GetFileName(plan->file_name.c_str());
- //Dbg("strFileFilter=[%s]",strFileFilter.GetData());
- if(!IsFileMatch(strFileFilter.GetData(),fileName.c_str())){
- continue;//继续下一个文件
- }
- //正则表达式过滤
- if(!plan->file_regex.empty()){
- //Dbg("file_regex=[%s]",plan->file_regex.c_str());
- regex e(plan->file_regex.c_str());
- if(!regex_match(fileName.c_str(),e,regex_constants::match_default)){
- continue;//继续下一个文件
- }
- }
- #ifdef RVC_OS_WIN
- //最后修改时间间隔过滤
- WIN32_FILE_ATTRIBUTE_DATA attr;
- if (!GetFileAttributesExA(file_path, GetFileExInfoStandard, &attr)) {
- Dbg("get local file attrr fail! file_path=%s",file_path);
- continue;//继续下一个文件
- }
- //判断文件是否超过最大扫描文件长度
- unsigned long limit = plan->file_length_limit*1024*1024;
- if(attr.nFileSizeLow>limit){
- CSimpleStringA warnStr = CSimpleStringA::Format("local file len (%u), over file_length_limit (%d)m, file_path=%s",attr.nFileSizeLow,plan->file_length_limit,file_path);
- LogWarn(Severity_Low, Error_Unexpect, LOG_WARN_UPLOAD_QUERY_FILE_OVER_LIMIT, warnStr.GetData());
- continue;//继续下一个文件
- }
- FILETIME fttmp;
- SYSTEMTIME filest;
- SYSTEMTIME st;
- if(!FileTimeToLocalFileTime(&attr.ftLastWriteTime,&fttmp)){
- continue;//继续下一个文件
- }
- if(!FileTimeToSystemTime(&fttmp,&filest)){
- continue;//继续下一个文件
- }
- GetLocalTime(&st);
- int nsecondsInterval = (st.wYear-filest.wYear)*32140800+(st.wMonth-filest.wMonth)*2678400+(st.wDay-filest.wDay)*86400+(st.wHour-filest.wHour)*3600+(st.wMinute-filest.wMinute)*60+(st.wSecond-filest.wSecond);
- if(nsecondsInterval<plan->max_silent){
- //Dbg("the time error! file_path=%s",file_path);
- continue;//继续下一个文件,时间不够
- }else{
- //时间已够,判断文件是否能独占锁
- if(!test_zero_ref(file_path)){
- continue;//继续下一个文件
- }
- }
- //得到文件长度
- unsigned long filelen = attr.nFileSizeLow;
- #else
- struct stat attr_of_src;
- if (lstat(file_path, &attr_of_src) != 0) {
- Dbg("get local file attrr fail! file_path=%s", file_path);
- continue;//继续下一个文件
- }
- //判断文件是否超过最大扫描文件长度
- unsigned long limit = plan->file_length_limit * 1024 * 1024;
- if (attr_of_src.st_size > limit) {
- CSimpleStringA warnStr = CSimpleStringA::Format("local file len (%u), over file_length_limit (%d)m, file_path=%s", attr_of_src.st_size, plan->file_length_limit, file_path);
- LogWarn(Severity_Low, Error_Unexpect, LOG_WARN_UPLOAD_QUERY_FILE_OVER_LIMIT, warnStr.GetData());
- continue;//继续下一个文件
- }
- time_t nowTime = time((time_t*)NULL);//当前时间
- if (nowTime > attr_of_src.st_mtime)
- {
- long diff = nowTime - attr_of_src.st_mtime;
- if (diff < plan->max_silent)
- {
- //Dbg("the time error! file_path=%s",file_path);
- continue;//继续下一个文件,时间不够
- }else{
- //时间已够,判断文件是否能独占锁
- if (!test_zero_ref(file_path)) {
- continue;//继续下一个文件
- }
- }
- }else {
- //Dbg("the time error! file_path=%s",file_path);
- continue;//继续下一个文件,时间不够
- }
- //得到文件长度
- unsigned long filelen = attr_of_src.st_size;
- #endif
- //添加扫描文件
- UploadFileInfo* upFile = new UploadFileInfo();
- upFile->fileName = fileName;
- upFile->filePath = file_path;
- if(upFile->filePath.length()>900){
- //路径太长,ECS存储有问题,截取路径,并加入模拟路径前缀:window S:\\ , linux /S/
- #ifdef RVC_OS_WIN
- upFile->fileUploadPath.append("S:\\");
- #else
- upFile->fileUploadPath.append("/S/");
- #endif
- upFile->fileUploadPath.append(fileName.c_str());
- //upFile->fileUploadRelativePath = "S:\\";
- }else{
- //直接赋值
- upFile->fileUploadPath = file_path;
- //upFile->fileUploadRelativePath = upFile->fileUploadPath.substr(0,upFile->fileUploadPath.length()-fileName.length());//截取文件名称的路径
- }
- upFile->BranchFilePath = file_path;
- upFile->fileLen = filelen;
- #ifdef RVC_OS_WIN
- upFile->editTime = CSimpleStringA::Format("%d-%d-%d %d:%d:%d",filest.wYear, filest.wMonth, filest.wDay, filest.wHour, filest.wMinute, filest.wSecond);
- #else
- tm fileTimeTm = { 0 };
- localtime_r(&attr_of_src.st_mtime, &fileTimeTm);
- upFile->editTime = CSimpleStringA::Format("%d-%d-%d %d:%d:%d", fileTimeTm.tm_year+1900, fileTimeTm.tm_mon+1, fileTimeTm.tm_mday, fileTimeTm.tm_hour, fileTimeTm.tm_min, fileTimeTm.tm_sec);
- #endif
- upFile->plan_id = plan->plan_id;
- upFile->branch_no = plan->branch_no;
- upFile->plan_type = plan->plan_type;
- upFile->file_type = plan->file_type;
- upFile->path_type = plan->path_type;
- upFile->path_name = plan->path_name;//因传回路径可能是windows路径斜杠,转义为linux斜杠
- upFile->file_name = plan->file_name;
- upFile->file_regex = plan->file_regex;
- upFile->before_deal_type = plan->before_deal_type;
- upFile->after_deal_type = plan->after_deal_type;
- upFile->after_deal_text = plan->after_deal_text;
- upFile->file_limit = plan->file_limit;
- upFile->file_length_limit = plan->file_length_limit;
- upFile->max_silent = plan->max_silent;
- upFile->upload_address_env = plan->upload_address_env;
- m_uploadList->push_back(upFile);
- Dbg("file %s lenth(%d) added!",file_path,filelen);
- }
- toolkit_array_free2(arr_files);
- }
- array_header_t *arr_dir = fileutil_get_sub_dirs_a(path);
- if (arr_dir) {
- int i;
- for (i = 0; i < arr_dir->nelts; ++i) {
- char *dir_path = ARRAY_IDX(arr_dir, i, char*);
- scanDirfresh(plan, dir_path);
- }
- }
- }
- void UploadFSM::HttpsLogCallBack(const char* logtxt)
- {
- Dbg("%s",logtxt);
- }
- bool UploadFSM::IsFileMatch(const char *pszFilter, const char *pszFileName)
- {
- const char *p1 = pszFilter;
- const char *p2 = pszFileName;
- if (p1 == NULL)
- return true;
- if (p2 == NULL)
- return false;
- if (*p1 ==NULL && *p2 ==NULL)
- return true;
- else if (*p1 == NULL)
- return false;
- else if (*p2 == NULL)
- {
- // 查找*p1是否全是*
- while (*p1 == '*')
- p1++;
- if (*p1 == NULL)
- return true;
- else
- return false;
- }
- if (*p1 != '*')
- {
- if (tolower(*p1) != tolower(*p2))
- return false;
- else
- return IsFileMatch(p1+1, p2+1);
- }
- else
- {
- while(*++p1 == '*');
- if (*p1 == NULL)
- return true;
- while (*p2)
- {
- while(tolower(*p1) != tolower(*p2) && *++p2);
- if (*p2 == NULL)
- return false;
- if(IsFileMatch(p1+1, p2+1))
- return true;
- p2++;
- }
- return false;
- }
- }
- ErrorCodeEnum UploadFSM::SM3FileToStr(CSimpleStringA strFilePath, CSimpleStringA &strSM3,bool isSub)
- {
- if(strFilePath.IsNullOrEmpty()){
- return Error_Null;
- }
- int nlen = strlen(strFilePath.GetData());
- char* pchar = new char[nlen+1];
- strcpy(pchar,strFilePath.GetData());
- BYTE md5[32]={0};
- try
- {
- if(SM3File(pchar,md5)){
- delete pchar;
- //获取16进制的字符串
- if(Error_Succeed!=SM3_Str(strSM3,md5,isSub)){
- Dbg("sm3国密转16进制字符串失败,file=%s",strFilePath.GetData());
- return Error_Unexpect;
- }
- return Error_Succeed;
- }else{
- delete pchar;
- Dbg("sm3国密加密失败,file=%s",strFilePath.GetData());
- return Error_Unexpect;
- }
- }
- catch (...)
- {
- delete pchar;
- Dbg("sm3国密加密异常失败,file=%s",strFilePath.GetData());
- return Error_Exception;
- }
- }
- ErrorCodeEnum UploadFSM::SM3_Str(CSimpleStringA &strSM3,BYTE * SM3Byte,bool isSub)
- {
- if(SM3Byte == NULL){
- return Error_Exception;
- }
- int SM3_len=64;
- if(isSub){
- SM3_len=32;
- }
- int i;
- char* file_SM3 = (char*)malloc((SM3_len + 1) * sizeof(char));
- if(file_SM3 == NULL)
- {
- fprintf(stderr, "SM3 malloc failed.\n");
- return Error_Exception;
- }
- memset(file_SM3, 0, (SM3_len + 1));
- if(SM3_len == 32)
- {
- for(i=0; i<16; i++)
- { //SM3截取中间16位字节,8-24位字节数,小写
- sprintf(&file_SM3[i*2], "%02x", SM3Byte[i+8]);
- }
- }
- else if(SM3_len == 64)
- {
- for(i=0; i<32; i++)
- {
- sprintf(&file_SM3[i*2], "%02x", SM3Byte[i]);
- }
- }
- strSM3=file_SM3;
- free(file_SM3);
- return Error_Succeed;
- }
- bool UploadFSM::getFileContent(FILE* pFile,unsigned char* content,int beginPos, int contentLen)
- {
- if(pFile==NULL||content==NULL){
- return false;
- }
- if(fseek(pFile,beginPos,SEEK_SET)!=0){
- return false;
- }
- unsigned char* pRead = content;//移动的指针
- int pRemaind = contentLen; //剩余字节数
- int numBytes = 0;
- while(!feof(pFile)&&!ferror(pFile)){
- if(pRemaind<=0){
- break;
- }
- numBytes=(int)fread(pRead,1,pRemaind,pFile);
- //Dbg("读取字节numBytes=%d",numBytes);
- if(numBytes>0){
- pRemaind -=numBytes;
- pRead +=numBytes;
- }
- }
- if(pRemaind==0){
- return true;
- }else{
- Dbg("getFileContent fail,pRemaind =%d",pRemaind);
- return false;
- }
- }
- bool UploadFSM::test_zero_ref(const char *path)
- {
- #ifdef RVC_OS_WIN
- HANDLE hFile = CreateFileA(path,
- GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); // try open
- if (hFile != INVALID_HANDLE_VALUE) {
- CloseHandle(hFile);
- return TRUE;
- } else {
- //添加获取句柄失败情况
- LogWarn(Severity_Low, Error_Unexpect, LOG_WARN_UPLOAD_SCANFILE_LOCK_ERROR, CSimpleStringA::Format("test_zero_ref is fail:%s",path).GetData());
- DWORD dwRet = GetLastError();
- return FALSE;
- }
- #else
- //后期改为linux真正的文件锁,现在来看,暂时用不着
- FILE* fp = fopen(path, "rb");
- if (fp != NULL) {
- fclose(fp);
- return true;
- }
- else
- {
- //Dbg("try open file fail :%s",path);
- LogWarn(Severity_Low, Error_Unexpect, LOG_WARN_UPLOAD_SCANFILE_LOCK_ERROR, CSimpleStringA::Format("check_zero_ref is fail:%s", path).GetData());
- return false;
- }
- #endif // RVC_OS_WIN
- }
- bool UploadFSM::GetFileLength(const char* filePath,long &fileLen)
- {
- FILE* file = fopen(filePath, "rb");
- if (file!=NULL)
- {
- #ifdef RVC_OS_WIN
- fileLen = filelength(fileno(file));
- fclose(file);
- return true;
- #else
- //取得当前文件流的读取位置
- int cur_pos = ftell(file);
- //将文件流的读取位置设为文件末尾
- if (fseek(file, SEEK_SET, SEEK_END)==0) {
- //获取文件末尾的读取位置,即文件大小
- fileLen = ftell(file);
- //将文件流的读取位置还原为原先的值
- fseek(file, cur_pos, SEEK_SET);
- fclose(file);
- return true;
- }
- else {
- Dbg("fseek file=%s is fail, err=%d", filePath,errno);
- fclose(file);
- return false;
- }
- #endif
-
- } else{
- fileLen = 0;
- return false;
- }
- }
- void UploadFSM::reportBatchMsg()
- {
- CSimpleStringA strReport;
- if(m_uploadReport.fail==0){
- strReport =CSimpleStringA::Format("batch=succ isbreak=%d sum=%d succ=%d fail=%d detail fail: uploadFail=%d updateState=%d operateFileFail=%d (decodeFail=%d queryTempFail=%d)",
- m_uploadReport.isBreak, m_uploadReport.sum, m_uploadReport.succ, m_uploadReport.fail, m_uploadReport.uploadFail, m_uploadReport.updateState,
- m_uploadReport.operateFileFail, m_uploadReport.decodeFail, m_uploadReport.queryTempFail);
- }else{
- strReport =CSimpleStringA::Format("batch=fail isbreak=%d sum=%d succ=%d fail=%d detail fail: uploadFail=%d updateState=%d operateFileFail=%d (decodeFail=%d queryTempFail=%d)",
- m_uploadReport.isBreak, m_uploadReport.sum, m_uploadReport.succ, m_uploadReport.fail, m_uploadReport.uploadFail, m_uploadReport.updateState,
- m_uploadReport.operateFileFail, m_uploadReport.decodeFail, m_uploadReport.queryTempFail);
- }
- LogWarn(Severity_Middle, Error_Unexpect, LOG_WARN_UPLOAD_FILE_REPORT, strReport.GetData());
- }
- /** 交易受限功能代码废弃
- 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;
- }
- */
|