UploadFSM.cpp 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  1. #include "stdafx.h"
  2. #include "UploadFSM.h"
  3. #include <regex>
  4. #include <fileutil.h>
  5. #include <array.h>
  6. #ifdef RVC_OS_WIN
  7. #include <cstdlib>
  8. #include <ctime>
  9. #include "EventCode.h"
  10. #else
  11. #include <thread>
  12. #include "upload.h"
  13. #include "path.h"
  14. #include <sys/stat.h>
  15. #include <sys/types.h>
  16. #include <fcntl.h>
  17. #include <time.h>
  18. #include <stdio.h>
  19. #include <cstdio>
  20. #include <iostream>
  21. #include <errno.h>
  22. #include "EventCode.h"
  23. #endif
  24. #define UPLOAD_CONTENT_EACH_LEN 512*1024
  25. #ifdef DEVOPS_ON_ST /*DevOps流水线编译,ST环境*/
  26. #define QUERY_PLAN_URL "https://accessproxy-service.paasst.cmbchina.cn/uploadservice/api/plan/query"
  27. #define QUERY_TEMP_FILE_URL "https://accessproxy-service.paasst.cmbchina.cn/uploadservice/api/upload/query/status"
  28. #define UPDATE_STATE_URL "https://accessproxy-service.paasst.cmbchina.cn/uploadservice/api/upload/update/status"
  29. #define ADD_UPLOAD_FAIL_URL "https://accessproxy-service.paasst.cmbchina.cn/uploadservice/api/upload/fail"
  30. #elif defined(DEVOPS_ON_UAT)/*DevOps流水线编译,UAT环境*/
  31. #define QUERY_PLAN_URL "https://uploadservice.paasuat.cmbchina.cn/api/plan/query"
  32. #define QUERY_TEMP_FILE_URL "https://uploadservice.paasuat.cmbchina.cn/api/upload/query/status"
  33. #define UPDATE_STATE_URL "https://uploadservice.paasuat.cmbchina.cn/api/upload/update/status"
  34. #define ADD_UPLOAD_FAIL_URL "https://uploadservice.paasuat.cmbchina.cn/api/upload/fail"
  35. #elif defined(DEVOPS_ON_PRD)/*DevOps流水线编译,PRD环境*/
  36. #define QUERY_PLAN_URL "https://uploadservice.paas.cmbchina.cn/api/plan/query"
  37. #define QUERY_TEMP_FILE_URL "https://uploadservice.paas.cmbchina.cn/api/upload/query/status"
  38. #define UPDATE_STATE_URL "https://uploadservice.paas.cmbchina.cn/api/upload/update/status"
  39. #define ADD_UPLOAD_FAIL_URL "https://uploadservice.paas.cmbchina.cn/api/upload/fail"
  40. #else/*DevOps流水线编译,Dev环境&本地编译等非DevOps环境*/
  41. #define QUERY_PLAN_URL "https://accessproxy-service.paasst.cmbchina.cn/uploadservice/api/plan/query"
  42. #define QUERY_TEMP_FILE_URL "https://accessproxy-service.paasst.cmbchina.cn/uploadservice/api/upload/query/status"
  43. #define UPDATE_STATE_URL "https://accessproxy-service.paasst.cmbchina.cn/uploadservice/api/upload/update/status"
  44. #define ADD_UPLOAD_FAIL_URL "https://accessproxy-service.paasst.cmbchina.cn/uploadservice/api/upload/fail"
  45. #endif
  46. UploadFSM::UploadFSM() : m_currUploadFile(NULL)
  47. {
  48. m_uploadList = new list<UploadFileInfo*>();
  49. m_uploadPlanList = new list<UploadPlan *>();
  50. m_overLenFileList = new list<string>();
  51. m_eachUploadLen=UPLOAD_CONTENT_EACH_LEN;
  52. m_uploadMaxNum = 200;
  53. m_isOffLine=true;//初始离线状态为true
  54. m_printPlan = true;//初始化为打印
  55. }
  56. UploadFSM::~UploadFSM()
  57. {
  58. }
  59. namespace Task
  60. {
  61. struct SendFileTask : public ITaskSp
  62. {
  63. UploadFSM* m_fsm;
  64. explicit SendFileTask(UploadFSM* e) : m_fsm(e) {}
  65. void Process(){
  66. //判断是分行url请求还是总行url请求
  67. //判断是临时文件还是永久文件
  68. //循环上传
  69. //登记上传流水或者失败流水
  70. //根据最终上传结果返回成功还是失败
  71. CSystemStaticInfo info;
  72. auto rc = m_fsm->GetEntityBase()->GetFunction()->GetSystemStaticInfo(info);
  73. if (rc != Error_Succeed)
  74. {
  75. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("SendFileTask")("get CSystemStaticInfo fail,file = %s",m_fsm->m_currUploadFile->filePath.c_str());
  76. m_fsm->PostEventFIFO(new FSMEvent(USER_EVT_UPLOAD_FAIL));//失败发送
  77. return;
  78. }
  79. bool isLanTerminal=true;//行内行外标志
  80. if(strcmp(m_fsm->m_currUploadFile->upload_address_env.c_str(),"BIZ")==0){
  81. isLanTerminal=true;
  82. }else{
  83. isLanTerminal=false;
  84. }
  85. IHttpFunc* client;
  86. client = create_http(m_fsm->HttpsLogCallBack);
  87. bool hasSend=false;
  88. if(m_fsm->m_currUploadFile->plan_type=="T"){
  89. //查询失败统一需要上传
  90. if(queryTempFile(client,info)){
  91. hasSend=true;
  92. }
  93. }
  94. //是否需要上传文件
  95. bool sendOk = false;
  96. if(!hasSend){
  97. string errmsg ="";
  98. if(isLanTerminal){
  99. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("SendFileTask")("上传文件到分行:%s",m_fsm->m_currUploadFile->filePath.c_str());
  100. }else{
  101. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("SendFileTask")("上传文件到总行:%s",m_fsm->m_currUploadFile->filePath.c_str());
  102. }
  103. string MD5str = ""; //返回的MD5值
  104. string branchFilePath = ""; //返回的分行保存路径值
  105. //修改为重试上传机制,防止上传当中网络抖动,避免等待下次上传
  106. bool isUploadSucc=false;
  107. for (int i =0 ; i<2;i++ )
  108. {
  109. errmsg="";//每次上传前清空错误信息,只记录最后一次的
  110. if(uploadFile(client,info,MD5str,branchFilePath,errmsg)){
  111. isUploadSucc = true;
  112. break;
  113. }
  114. Sleep(3000);//等待3秒下次传送
  115. }
  116. if(isUploadSucc){
  117. if(updateState(client,info,isLanTerminal,MD5str,branchFilePath,errmsg)){
  118. if(operateFile(errmsg)){
  119. sendOk = true;//成功
  120. }else{
  121. m_fsm->m_uploadReport.operateFileFail++;//添加操作失败计数
  122. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("SendFileTask")("operateFile fail,file = %s",m_fsm->m_currUploadFile->filePath.c_str());
  123. addUploadFail(client,info,isLanTerminal,"2",errmsg);//操作失败
  124. }
  125. }else{
  126. m_fsm->m_uploadReport.updateState++;//添加登记失败计数
  127. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("SendFileTask")("updateState fail,file = %s",m_fsm->m_currUploadFile->filePath.c_str());
  128. addUploadFail(client,info,isLanTerminal,"1",errmsg);//登记失败
  129. }
  130. }else{
  131. m_fsm->m_uploadReport.uploadFail++;//添加上传失败计数
  132. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("SendFileTask")("uploadFile fail,file = %s",m_fsm->m_currUploadFile->filePath.c_str());
  133. addUploadFail(client,info,isLanTerminal,"1",errmsg);//上传失败
  134. }
  135. }else{
  136. sendOk=true;
  137. }
  138. client->Destory();
  139. if(sendOk){
  140. m_fsm->PostEventFIFO(new FSMEvent(USER_EVT_UPLOAD_SUCC));//成功发送
  141. }else{
  142. m_fsm->PostEventFIFO(new FSMEvent(USER_EVT_UPLOAD_FAIL));//失败发送
  143. }
  144. }
  145. //上传文件:断点续传,多次调用
  146. bool uploadFile(IHttpFunc* client,CSystemStaticInfo info,string &MD5str,string &branchFilePath, string &errmsg){
  147. string reqUrl;
  148. unsigned seed;
  149. seed = time(0);
  150. srand(seed);
  151. if(m_fsm->m_branchIpList.size()>0){
  152. int branchSeq = (rand() % m_fsm->m_branchIpList.size()) + 1;
  153. map<int,string>::iterator it = m_fsm->m_branchIpList.find(branchSeq);
  154. if(it != m_fsm->m_branchIpList.end())
  155. {
  156. reqUrl = it->second;
  157. }else{
  158. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("uploadFile is fail ,branchIp[%d] is null",branchSeq);
  159. errmsg = CSimpleStringA::Format("uploadFile is fail ,branchIp[%d] is null",branchSeq).GetData();
  160. return false;
  161. }
  162. }else{
  163. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("uploadFile is fail ,branchIp is null");
  164. errmsg = "uploadFile is fail ,branchIp is null";
  165. return false;
  166. }
  167. //获取服务器文件长度
  168. unsigned char* contentFirst = new unsigned char[1];
  169. memset(contentFirst,0,1);
  170. UploadDataRet respFirst ={0};
  171. bool is_finish =false; //是否续传
  172. if(!uploadFileImpl(client,info,reqUrl,contentFirst,0,"",respFirst)){
  173. delete[] contentFirst;
  174. errmsg=CSimpleStringA::Format("url=%s, uploadFileImpl first is fail",reqUrl.c_str()).GetData();
  175. return false;//返回失败
  176. }else{
  177. delete[] contentFirst;
  178. }
  179. long uploadedLen = respFirst.uploaded_length;//服务器返回已上传的长度
  180. if(uploadedLen>m_fsm->m_currUploadFile->fileLen){
  181. //服务器返回长度大于终端文件长度
  182. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("uploadFile is fail, server filelen [%d] is over terminal filelen [%d]",uploadedLen,m_fsm->m_currUploadFile->fileLen);
  183. errmsg=CSimpleStringA::Format("uploadFile is fail, server filelen [%d] is over terminal filelen [%d]",uploadedLen,m_fsm->m_currUploadFile->fileLen).GetData();
  184. return false;
  185. }
  186. if (uploadedLen < 0) {
  187. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("uploadFile uploadedLen beginPos < 0");
  188. errmsg = CSimpleStringA::Format("uploadFile is fail, server filelen [%d] is <0", uploadedLen).GetData();
  189. return false;
  190. }
  191. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("文件[%s] 开始传送位置 uploadedLen = %d, file len=%d",m_fsm->m_currUploadFile->filePath.c_str(), uploadedLen, m_fsm->m_currUploadFile->fileLen);
  192. //循环传送数据块
  193. FILE* pFile =fopen(m_fsm->m_currUploadFile->filePath.c_str(),"rb");
  194. if(pFile==NULL){
  195. //读取文件失败;
  196. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("uploadFile is fail, file open fail");
  197. errmsg="uploadFile is fail, file open fail";
  198. return false;
  199. }
  200. //获取首次文件hash
  201. CSimpleStringA firstSM3Str = "";
  202. if (Error_Succeed == m_fsm->SM3FileToStr(m_fsm->m_currUploadFile->filePath.c_str(), firstSM3Str, false)) {
  203. m_fsm->m_currUploadFile->firstMD5Str = firstSM3Str.GetData();
  204. m_fsm->m_currUploadFile->lastMD5Str = "";
  205. }
  206. else {
  207. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("uploadFile is fail, get file first SM3 hash fail");
  208. errmsg = "uploadFile is fail , get file first SM3 hash fail";
  209. return false;
  210. }
  211. bool uploadSucc= false;
  212. while(true){
  213. //续传
  214. if((m_fsm->m_currUploadFile->fileLen-uploadedLen)>m_fsm->m_eachUploadLen){
  215. int uploadLen = m_fsm->m_eachUploadLen;//传送数据长度
  216. unsigned char* content = new unsigned char[uploadLen];
  217. memset(content,0,uploadLen);
  218. //从文件拷贝字节
  219. if(m_fsm->getFileContent(pFile,content,uploadedLen,uploadLen)){
  220. UploadDataRet resp ={0};
  221. if(uploadFileImpl(client,info,reqUrl,content,uploadLen,"",resp)){
  222. delete[] content;
  223. uploadedLen +=uploadLen;//改变已上传长度
  224. }else{
  225. delete[] content;
  226. errmsg=CSimpleStringA::Format("url=%s, uploadFileImpl continue is fail",reqUrl.c_str()).GetData();
  227. break;//上传失败
  228. }
  229. }else{
  230. delete[] content;
  231. errmsg="uploadFile is fail , getFileContent fail";
  232. break;//获取文件内存失败
  233. }
  234. }else{
  235. //获取最后一次传送时文件长度
  236. CSimpleStringA filepath = m_fsm->m_currUploadFile->filePath.c_str();
  237. long fileLastLen = 0;
  238. if(!m_fsm->GetFileLength(filepath.GetData(),fileLastLen)){
  239. errmsg="uploadFile is fail , end get file len fail";
  240. break;//计算sm3出错
  241. }
  242. string fileLastLenStr= CSimpleStringA::Format("%d",fileLastLen).GetData();//计算出解密后总长度
  243. //获取最后文件hash
  244. CSimpleStringA lastSM3Str = "";
  245. if(Error_Succeed == m_fsm->SM3FileToStr(filepath.GetData(), lastSM3Str, false)) {
  246. m_fsm->m_currUploadFile->lastMD5Str = lastSM3Str.GetData();
  247. }
  248. else {
  249. errmsg = "uploadFile is fail , get file last SM3 hash fail";
  250. break;//计算sm3出错
  251. }
  252. //最后一块
  253. int uploadLen = m_fsm->m_currUploadFile->fileLen-uploadedLen;
  254. unsigned char* contentEnd;
  255. if(uploadLen==0){
  256. //对零的长度特殊处理
  257. contentEnd = new unsigned char[1];
  258. memset(contentEnd,0,1);
  259. }else{
  260. contentEnd = new unsigned char[uploadLen];
  261. memset(contentEnd,0,uploadLen);
  262. }
  263. if(m_fsm->getFileContent(pFile,contentEnd,uploadedLen,uploadLen)){
  264. UploadDataRet respEnd ={0};
  265. if(uploadFileImpl(client,info,reqUrl,contentEnd,uploadLen,fileLastLenStr,respEnd)){
  266. delete[] contentEnd;
  267. if(respEnd.upload_finish){
  268. uploadSucc =true;//成功
  269. branchFilePath = respEnd.branchFilePath;//分行保存地址
  270. }else{
  271. uploadSucc =false;//失败
  272. }
  273. break;
  274. }else{
  275. delete[] contentEnd;
  276. errmsg=CSimpleStringA::Format("url=%s, uploadFileImpl end is fail",reqUrl.c_str()).GetData();
  277. break;//上传失败
  278. }
  279. }else{
  280. delete[] contentEnd;
  281. errmsg="uploadFile is fail , end getFileContent fail";
  282. break;//获取文件内存失败
  283. }
  284. }
  285. }
  286. if(uploadSucc){
  287. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("uploadFile succ");
  288. }else{
  289. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("uploadFile fail url=%s",reqUrl.c_str());
  290. }
  291. if(pFile){
  292. if(fclose(pFile)!=0){
  293. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("uploadFile fail, close file fail ,url=%s",reqUrl.c_str());
  294. errmsg="uploadFile fail, close file fail";
  295. return false;
  296. }
  297. }
  298. return uploadSucc;
  299. }
  300. //上传文件:具体实现
  301. bool uploadFileImpl(IHttpFunc* client,CSystemStaticInfo info,string httpUrl,unsigned char* fileContent,long fileContentSize,string lastFileLength,UploadDataRet& uploadData){
  302. UploadFileReq qTempReq;
  303. UploadFileRet qTempRet;
  304. qTempRet.m_userCode ="";
  305. qTempRet.m_errMsg="";
  306. Json::Value rootReq;
  307. Json::FastWriter writer;
  308. rootReq["terminal_no"] = info.strTerminalID.GetData();
  309. rootReq["branch_no"] = m_fsm->m_currUploadFile->branch_no.c_str();
  310. rootReq["file_name"] = m_fsm->m_currUploadFile->fileName.c_str();
  311. rootReq["file_path"] = m_fsm->m_currUploadFile->fileUploadPath.c_str();
  312. rootReq["plan_id"] = m_fsm->m_currUploadFile->plan_id.c_str();
  313. rootReq["file_type"] = m_fsm->m_currUploadFile->file_type.c_str();
  314. //rootReq["origin_file_length"] = CSimpleStringA::Format("%d",m_fsm->m_currUploadFile->fileLen).GetData();
  315. rootReq["first_file_length"] = CSimpleStringA::Format("%d",m_fsm->m_currUploadFile->fileLen).GetData();//加密后需要修改,服务端根据此长度做判断
  316. rootReq["last_file_length"] = lastFileLength.c_str();//加密后需要修改,最后一次长度跟服务端保存长度做对比
  317. rootReq["upload_length"] = CSimpleStringA::Format("%d",fileContentSize).GetData();
  318. rootReq["first_sm3"] = m_fsm->m_currUploadFile->firstMD5Str.c_str();
  319. rootReq["last_sm3"] = m_fsm->m_currUploadFile->lastMD5Str.c_str();
  320. rootReq["file_head_custom"] = "";//自定义解密的头json格式,可为空
  321. #ifdef RVC_OS_WIN
  322. rootReq["system_type"] = "W";//系统标记,代表路径分隔符
  323. #else
  324. rootReq["system_type"] = "L";//系统标记,代表路径分隔符
  325. #endif
  326. string jsonReq = writer.write(rootReq);
  327. qTempReq.url = httpUrl;
  328. qTempReq.paramContent = jsonReq;
  329. qTempReq.fileName = m_fsm->m_currUploadFile->fileName;
  330. qTempReq.fileContent = fileContent;
  331. qTempReq.fileContentSize= fileContentSize;
  332. qTempReq.m_timeOut = 5*60;//设置超时时间
  333. //qTempReq.m_printDbg=true;
  334. //调用上传接口
  335. //最后一次上传时,文件hash和长度告警
  336. if (m_fsm->m_currUploadFile->lastMD5Str.length() != 0) {
  337. LogWarn(Severity_Low, Error_Unexpect, LOG_WARN_UPLOAD_FILE_INFO, CSimpleStringA::Format("upload %s file size is %s byte,sm3 digest is %s.", m_fsm->m_currUploadFile->fileName.c_str(), lastFileLength.c_str(), m_fsm->m_currUploadFile->lastMD5Str.c_str()).GetData());
  338. }
  339. PROCESS_LINK_CONTEXT("LR0402104UploadFile")
  340. if (!client->UploadFileBlock(qTempReq, qTempRet, &nextLink)) {
  341. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("uploadFile http req fail ,url =%s ,fileName = %s",httpUrl.c_str(), m_fsm->m_currUploadFile->fileName.c_str());
  342. return false;
  343. }
  344. if(qTempRet.httpCode!=200){
  345. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("uploadFile http req fail ,url =%s ,retHttpCode=%d, fileName = %s",httpUrl.c_str(), qTempRet.httpCode, m_fsm->m_currUploadFile->fileName.c_str());
  346. return false;
  347. }
  348. //解析回应报文
  349. Json::Reader reader;
  350. Json::Value rootRet;
  351. if (!reader.parse(qTempRet.strBody, rootRet, false))
  352. {
  353. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("uploadFile http parse resp is fail,fileName = %s",m_fsm->m_currUploadFile->fileName.c_str());
  354. return false;//解析失败
  355. }
  356. uploadData.strBody = qTempRet.strBody;
  357. bool isSucc = rootRet["success"].asBool();
  358. if(isSucc){
  359. if(rootRet.isMember("data")){
  360. uploadData.uploaded_length= rootRet["data"]["uploaded_length"].asInt();
  361. uploadData.upload_finish = rootRet["data"]["upload_finish"].asBool();
  362. uploadData.branchFilePath = rootRet["data"]["branch_file_path"].asCString();
  363. return true;//返回值
  364. }else{
  365. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("uploadFile http [success][data] is invaild ,url =%s, fileName = %s",httpUrl.c_str(),m_fsm->m_currUploadFile->fileName.c_str());
  366. return false;//无data字段
  367. }
  368. }else{
  369. //失败
  370. if (m_fsm->m_currUploadFile->lastMD5Str.length() == 0) {
  371. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("uploadFile http [success] is false, url[%s],userCode[%s],errmsg[%s],fileName = %s", httpUrl.c_str(), qTempRet.m_userCode.c_str(), qTempRet.m_errMsg.c_str(), m_fsm->m_currUploadFile->fileName.c_str());
  372. }
  373. else {
  374. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("uploadFile http [success] is false, url[%s],userCode[%s],errmsg[%s],fileName = %s,first_sm3 =[%s],last_sm3=[%s]", httpUrl.c_str(), qTempRet.m_userCode.c_str(), qTempRet.m_errMsg.c_str(), m_fsm->m_currUploadFile->fileName.c_str(), m_fsm->m_currUploadFile->firstMD5Str.c_str(), m_fsm->m_currUploadFile->lastMD5Str.c_str());
  375. }
  376. return false;
  377. }
  378. }
  379. //查询临时文件 post
  380. bool queryTempFile(IHttpFunc* client,CSystemStaticInfo info){
  381. UploadQueryTempFileReq qTempReq;
  382. UploadQueryTempFileRet qTempRet;
  383. qTempRet.m_userCode ="";
  384. qTempRet.m_errMsg="";
  385. Json::Value rootReq;
  386. Json::FastWriter writer;
  387. rootReq["terminal_no"] = info.strTerminalID.GetData();
  388. rootReq["plan_id"] = m_fsm->m_currUploadFile->plan_id.c_str();
  389. rootReq["file_path"] = m_fsm->m_currUploadFile->fileUploadPath.c_str();
  390. rootReq["file_type"] = m_fsm->m_currUploadFile->file_type.c_str();
  391. string jsonReq = writer.write(rootReq);
  392. qTempReq.m_reqStr = jsonReq;//请求参数
  393. //qTempReq.m_printDbg = true;
  394. qTempReq.m_url=m_fsm->m_centerUrl.queryTempFileUrl;//访问地址
  395. PROCESS_LINK_CONTEXT("LR0402104QueryTempFile")
  396. if(!client->Post(qTempReq, qTempRet, &nextLink)){
  397. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("queryTempFile http req fail ,fileName = %s, err=%s",m_fsm->m_currUploadFile->filePath.c_str(),qTempRet.m_errMsg.c_str());
  398. return false;
  399. }
  400. //解析报文
  401. //解析回应报文
  402. Json::Reader reader;
  403. Json::Value rootRet;
  404. if (!reader.parse(qTempRet.m_retStr, rootRet, false))
  405. {
  406. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("queryTempFile parse resp is fail,fileName = %s",m_fsm->m_currUploadFile->filePath.c_str());
  407. m_fsm->m_uploadReport.queryTempFail++;//添加查询临时失败计数
  408. return false;//解析失败
  409. }
  410. bool isSucc = rootRet["success"].asBool();
  411. if(isSucc){
  412. if(rootRet.isMember("data")){
  413. if(rootRet["data"]["upload_flag"].asBool()){
  414. return true;//查询有
  415. }else{
  416. return false;//查询没有
  417. }
  418. }else{
  419. m_fsm->m_uploadReport.queryTempFail++;//添加查询临时失败计数
  420. return false;//无data字段
  421. }
  422. }else{
  423. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("queryTempFile [success] is false, fileName = %s",m_fsm->m_currUploadFile->filePath.c_str());
  424. m_fsm->m_uploadReport.queryTempFail++;//添加查询临时失败计数
  425. return false;
  426. }
  427. }
  428. //登记文件流水状态 post
  429. bool updateState(IHttpFunc* client,CSystemStaticInfo info,bool isLanTerminal, string MD5str,string branchFilePath, string &errmsg){
  430. if(!isLanTerminal){
  431. return true;//行外默认是总行那边登记交易流水,终端不再发送交易流水登记,直接返回成功
  432. }
  433. UploadUpdateStateReq updateStateReq;
  434. UploadUpdateStateRet updateStateRet;
  435. updateStateRet.m_userCode="";
  436. updateStateRet.m_errMsg="";
  437. Json::Value rootReq;
  438. Json::FastWriter writer;
  439. rootReq["plan_id"] = m_fsm->m_currUploadFile->plan_id.c_str();
  440. rootReq["branch_no"] = m_fsm->m_currUploadFile->branch_no.c_str();
  441. rootReq["terminal_no"] = info.strTerminalID.GetData();
  442. rootReq["file_type"] = m_fsm->m_currUploadFile->file_type.c_str();
  443. rootReq["file_name"] = m_fsm->m_currUploadFile->fileName.c_str();
  444. rootReq["file_path"] = m_fsm->m_currUploadFile->fileUploadPath.c_str();
  445. rootReq["branch_file_path"] = branchFilePath.c_str();
  446. rootReq["file_length"] = CSimpleStringA::Format("%d",m_fsm->m_currUploadFile->fileLen).GetData();
  447. if(isLanTerminal){
  448. rootReq["status"] = "1";//分行
  449. }else{
  450. rootReq["status"] = "2";//总行
  451. }
  452. rootReq["upload_time"] = CSmallDateTime::GetNow().ToTimeString().GetData();
  453. rootReq["remark"] = "";
  454. rootReq["sm3"] = MD5str.c_str();
  455. string jsonReq = writer.write(rootReq);
  456. updateStateReq.m_reqStr = jsonReq;//请求参数
  457. updateStateReq.m_url=m_fsm->m_centerUrl.updateStateUrl;//访问地址
  458. PROCESS_LINK_CONTEXT("LR0402104UpdateState")
  459. if(!client->Post(updateStateReq, updateStateRet, &nextLink)){
  460. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("updateState is fail, userCode = %s, errmsg = %s",updateStateRet.m_userCode.c_str(), updateStateRet.m_errMsg.c_str());
  461. errmsg = CSimpleStringA::Format("updateState is fail, userCode = %s, errmsg = %s",updateStateRet.m_userCode.c_str(), updateStateRet.m_errMsg.c_str());
  462. return false;
  463. }
  464. //解析报文
  465. //解析回应报文
  466. Json::Reader reader;
  467. Json::Value rootRet;
  468. if (!reader.parse(updateStateRet.m_retStr, rootRet, false))
  469. {
  470. errmsg="return pack parse is fail";
  471. return false;//解析失败
  472. }
  473. bool isSucc = rootRet["success"].asBool();
  474. if(isSucc){
  475. if(rootRet.isMember("data")){
  476. if(rootRet["data"]["update_flag"].asBool()){
  477. return true;//登记成功
  478. }else{
  479. errmsg="return pack [data][update_flag] is not bool";
  480. return false;//登记失败
  481. }
  482. }else{
  483. errmsg="return pack [data] is null";
  484. return false;//无data字段
  485. }
  486. }else{
  487. errmsg="return pack [success] is false";
  488. return false;
  489. }
  490. }
  491. //登记失败文件流水 post
  492. bool addUploadFail(IHttpFunc* client,CSystemStaticInfo info,bool isLanTerminal,string operation,string errmsg){
  493. UploadUploadFailReq updateFailReq;
  494. UploadUploadFailRet updateFailRet;
  495. updateFailRet.m_userCode="";
  496. updateFailRet.m_errMsg="";
  497. Json::Value rootReq;
  498. Json::FastWriter writer;
  499. rootReq["plan_id"] = m_fsm->m_currUploadFile->plan_id.c_str();
  500. rootReq["branch_no"] = m_fsm->m_currUploadFile->branch_no.c_str();
  501. rootReq["terminal_no"] = info.strTerminalID.GetData();
  502. rootReq["file_name"] = m_fsm->m_currUploadFile->fileName.c_str();
  503. rootReq["file_path"] = m_fsm->m_currUploadFile->fileUploadPath.c_str();
  504. if(isLanTerminal){
  505. rootReq["stage"] = "1";//分行
  506. }else{
  507. rootReq["stage"] = "2";//总行
  508. }
  509. rootReq["operation"] = operation;
  510. rootReq["upload_time"] = CSmallDateTime::GetNow().ToTimeString().GetData();//2021-03-11 10:20:02
  511. rootReq["reason"] = errmsg.c_str();
  512. rootReq["remark"] = "";
  513. string jsonReq = writer.write(rootReq);
  514. updateFailReq.m_reqStr = jsonReq;//请求参数
  515. updateFailReq.m_url = m_fsm->m_centerUrl.addUploadFailUrl;//访问地址
  516. PROCESS_LINK_CONTEXT("LR0402104UpdateFail")
  517. if(!client->Post(updateFailReq, updateFailRet, &nextLink)){
  518. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("addUploadFail is fail,userCode = %s",updateFailRet.m_userCode.c_str());
  519. return false;
  520. }
  521. //解析报文
  522. //解析回应报文
  523. Json::Reader reader;
  524. Json::Value rootRet;
  525. if (!reader.parse(updateFailRet.m_retStr, rootRet, false))
  526. {
  527. return false;//解析失败
  528. }
  529. bool isSucc = rootRet["success"].asBool();
  530. if(isSucc){
  531. if(rootRet.isMember("data")){
  532. if(rootRet["data"]["update_flag"].asBool()){
  533. return true;//登记成功
  534. }else{
  535. return false;//登记失败
  536. }
  537. }else{
  538. return false;//无data字段
  539. }
  540. }else{
  541. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("addUploadFail [success] is false");
  542. return false;
  543. }
  544. }
  545. //上传后文件处理
  546. bool operateFile(string &errmsg){
  547. if(m_fsm->m_currUploadFile->after_deal_type=="D"){
  548. #ifdef RVC_OS_WIN
  549. if(remove(m_fsm->m_currUploadFile->filePath.c_str())==0){
  550. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete file is ok, file =%s",m_fsm->m_currUploadFile->filePath.c_str());
  551. return true;
  552. }else{
  553. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete file is error, file =%s, errno=%d",m_fsm->m_currUploadFile->filePath.c_str(),(int)GetLastError());
  554. errmsg = CSimpleStringA::Format("delete file is error, file =%s, errno=%d",m_fsm->m_currUploadFile->filePath.c_str(),(int)GetLastError());
  555. return false;
  556. }
  557. #else
  558. if (changeFileAtt(m_fsm->m_currUploadFile->filePath.c_str()) == 0) {
  559. if (remove(m_fsm->m_currUploadFile->filePath.c_str()) == 0) {
  560. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete file is ok, file =%s",m_fsm->m_currUploadFile->filePath.c_str());
  561. return true;
  562. }
  563. else {
  564. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete file is error,file =%s, errno=%d",m_fsm->m_currUploadFile->filePath.c_str(), errno);
  565. errmsg = CSimpleStringA::Format("delete file is error, file =%s, errno=%d",m_fsm->m_currUploadFile->filePath.c_str(), errno);
  566. return false;
  567. }
  568. }
  569. else {
  570. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete file is error,changefileAtt fail,file =%s, errno=%d",m_fsm->m_currUploadFile->filePath.c_str(), errno);
  571. errmsg = CSimpleStringA::Format("delete file is error,changefileAtt fail,file =%s, errno=%d",m_fsm->m_currUploadFile->filePath.c_str(), errno);
  572. return false;
  573. }
  574. #endif
  575. }else if(m_fsm->m_currUploadFile->after_deal_type=="M"){
  576. string destPath="";
  577. string destDir="";
  578. //根据路径类型组成最后移动路径
  579. if(m_fsm->m_currUploadFile->path_type=="R"){
  580. //TODO: CrossPlaform [Gifur@2025730]
  581. #ifdef RVC_OS_WIN
  582. destPath = m_fsm->m_rootPath+"\\"+m_fsm->m_currUploadFile->after_deal_text+"\\"+m_fsm->m_currUploadFile->fileName;
  583. destDir = m_fsm->m_rootPath+"\\"+m_fsm->m_currUploadFile->after_deal_text;
  584. #else
  585. destPath = m_fsm->m_rootPath + SPLIT_SLASH_STR + m_fsm->m_currUploadFile->after_deal_text + SPLIT_SLASH_STR + m_fsm->m_currUploadFile->fileName;
  586. destDir = m_fsm->m_rootPath + SPLIT_SLASH_STR + m_fsm->m_currUploadFile->after_deal_text;
  587. #endif
  588. }else{
  589. //TODO: CrossPlaform [Gifur@2025730]
  590. #ifdef RVC_OS_WIN
  591. destPath = m_fsm->m_currUploadFile->after_deal_text+"\\"+m_fsm->m_currUploadFile->fileName;
  592. #else
  593. destPath = m_fsm->m_currUploadFile->after_deal_text + SPLIT_SLASH_STR + m_fsm->m_currUploadFile->fileName;
  594. #endif
  595. destDir = m_fsm->m_currUploadFile->after_deal_text;
  596. }
  597. string srcPath = m_fsm->m_currUploadFile->filePath;
  598. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("move file %s to %s",srcPath.c_str(),destPath.c_str());
  599. //判断是否存在目标文件,如存在,则先删除
  600. #ifdef RVC_OS_WIN
  601. //TODO: CrossPlaform [Gifur@2025730]
  602. if(ExistsFileA(destPath.c_str())){
  603. if(remove(destPath.c_str())==0){
  604. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete dest file is ok [%s]",destPath.c_str());
  605. }else{
  606. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete dest file is error, errno=%d, [%s]",(int)GetLastError(),destPath.c_str());
  607. errmsg = CSimpleStringA::Format("delete dest file is error, errno=%d, [%s]",(int)GetLastError(),destPath.c_str());
  608. return false;
  609. }
  610. }
  611. //判断移动目标文件夹是否存在,不存在,则创建
  612. if(!ExistsDirA(destDir.c_str())){
  613. if(!CreateDirRecursiveA(destDir.c_str())){
  614. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create dest dir [%s] is error, errno=%d",destDir.c_str(),GetLastError());
  615. errmsg = CSimpleStringA::Format("create dest dir [%s] is error, errno=%d",destDir.c_str(),GetLastError());
  616. return false;
  617. }
  618. }
  619. //移动文件
  620. int i;
  621. int tries = 10;
  622. bool isSucc = false;
  623. for (i = 0; i < tries; ++i) {
  624. if(rename(srcPath.c_str(),destPath.c_str())==0){
  625. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("move file ok! tries:%d, [%s] to [%s] ", i, srcPath.c_str(),destPath.c_str());
  626. isSucc = true;
  627. break;
  628. } else {
  629. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("move file failed! errno:%d, [%s] to [%s] ", (int)GetLastError(), srcPath.c_str(),destPath.c_str());
  630. errmsg = CSimpleStringA::Format("move file failed! errno:%d, [%s] to [%s] ", (int)GetLastError(), srcPath.c_str(),destPath.c_str());
  631. Sleep(1000);
  632. }
  633. }
  634. return isSucc;
  635. #else
  636. if (ExistsFileA(destPath.c_str())) {
  637. if (changeFileAtt(destPath.c_str()) == 0) {
  638. if (remove(destPath.c_str()) == 0) {
  639. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete dest file is ok [%s]",destPath.c_str());
  640. }
  641. else {
  642. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete dest file is error, errno=%d, [%s]", errno, destPath.c_str());
  643. errmsg = CSimpleStringA::Format("delete src file is error, errno=%d, [%s]", errno, destPath.c_str());
  644. return false;
  645. }
  646. }
  647. else {
  648. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete dest file is error,changefileAtt fail ,errno=%d , file=%s ", errno, destPath.c_str());
  649. errmsg = CSimpleStringA::Format("delete src file is error, changefileAtt fail, errno=%d, file=%s", errno, destPath.c_str());
  650. return false;
  651. }
  652. }
  653. //判断移动目标文件夹是否存在,不存在,则创建
  654. if (!ExistsDirA(destDir.c_str())) {
  655. if (!CreateDirRecursiveA(destDir.c_str())) {
  656. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create dest dir [%s] is error, errno=%d", destDir.c_str(), errno);
  657. errmsg = CSimpleStringA::Format("create dest dir [%s] is error, errno=%d", destDir.c_str(), errno);
  658. return false;
  659. }
  660. }
  661. //移动文件
  662. if (changeFileAtt(srcPath.c_str()) == 0)
  663. {
  664. int i;
  665. int tries = 10;
  666. bool isSucc = false;
  667. for (i = 0; i < tries; ++i) {
  668. //rename函数对于跨分支的目录存在拷贝不成功的情况,故采用先拷贝后删除的操作修复此bug
  669. if (fileutil_copy_file(destPath.c_str(), srcPath.c_str()) == 0) {
  670. if (remove(srcPath.c_str()) == 0) {
  671. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("move file ok! tries:%d, [%s] to [%s] ", i, srcPath.c_str(),destPath.c_str());
  672. isSucc = true;
  673. break;
  674. }
  675. else {
  676. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("move file failed! errno:%d, [%s] to [%s] ", errno, srcPath.c_str(),destPath.c_str());
  677. errmsg = CSimpleStringA::Format("move file failed! errno:%d, [%s] to [%s] ", errno, srcPath.c_str(),destPath.c_str());
  678. Sleep(1000);
  679. }
  680. }
  681. else {
  682. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("move file failed! copy src file fail, errno:%d, [%s] to [%s] ", errno, srcPath.c_str(),destPath.c_str());
  683. errmsg = CSimpleStringA::Format("move file failed! copy src file fail, errno:%d, [%s] to [%s] ", errno, srcPath.c_str(),destPath.c_str());
  684. Sleep(1000);
  685. }
  686. }
  687. return isSucc;
  688. }
  689. else {
  690. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("move file failed! changefileAtt fail, errno:%d, [%s] to [%s] ", errno, srcPath.c_str(),destPath.c_str());
  691. errmsg = CSimpleStringA::Format("move file failed! changefileAtt fail, errno:%d, [%s] to [%s] ", errno, srcPath.c_str(),destPath.c_str());
  692. return false;
  693. }
  694. #endif
  695. }else if(m_fsm->m_currUploadFile->after_deal_type=="N"){
  696. return true;
  697. }else{
  698. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("file [%s] invalid after_deal_type=%s",m_fsm->m_currUploadFile->fileName.c_str(),m_fsm->m_currUploadFile->after_deal_type.c_str());
  699. errmsg = CSimpleStringA::Format("invalid after_deal_type=%s",m_fsm->m_currUploadFile->after_deal_type.c_str());
  700. return false;
  701. }
  702. }
  703. };
  704. }
  705. void UploadFSM::OnStateTrans( int iSrcState, int iDstState )
  706. {
  707. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("trans from %s to %s", GetStateName(iSrcState), GetStateName(iDstState));
  708. }
  709. void UploadFSM::OnSysVarEvent( const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName )
  710. {
  711. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("rx sysvar %s from %s, %s to %s", pszKey, pszEntityName, pszOldValue, pszValue);
  712. if (pszValue[0] == 'O') {
  713. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("rx sysvar %s from %s, %s to %s", pszKey, pszEntityName, pszOldValue, pszValue);
  714. m_isOffLine = true;
  715. PostEventFIFO(new FSMEvent(USER_EVT_JMP_ENABLE));
  716. } else {
  717. if (m_isOffLine) {
  718. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("rx sysvar %s from %s, %s to %s", pszKey, pszEntityName, pszOldValue, pszValue);
  719. }
  720. m_isOffLine = false;
  721. PostEventFIFO(new FSMEvent(USER_EVT_JMP_DISABLE));
  722. }
  723. }
  724. ErrorCodeEnum UploadFSM::OnInit()
  725. {
  726. ErrorCodeEnum Error;
  727. CSimpleStringA checkStr="";
  728. CSmartPointer<IConfigInfo> spConfig;
  729. Error = m_pEntity->GetFunction()->OpenConfig(Config_CenterSetting, spConfig);
  730. if(Error==Error_Succeed){
  731. //获取模式控制参数
  732. m_centerModel = (int)centerModel;//强制总行模式
  733. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("model is centerModel, CenterModel = %d", m_centerModel);
  734. m_centerUrl.queryPlanUrl = QUERY_PLAN_URL;
  735. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("init read queryPlanUrl=%s", m_centerUrl.queryPlanUrl.c_str());
  736. m_centerUrl.queryTempFileUrl = QUERY_TEMP_FILE_URL;
  737. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("init read queryTempFileUrl=%s", m_centerUrl.queryTempFileUrl.c_str());
  738. m_centerUrl.updateStateUrl = UPDATE_STATE_URL;
  739. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("init read updateStateUrl=%s", m_centerUrl.updateStateUrl.c_str());
  740. m_centerUrl.addUploadFailUrl = ADD_UPLOAD_FAIL_URL;
  741. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("init read addUploadFailUrl=%s", m_centerUrl.addUploadFailUrl.c_str());
  742. //增加总行接口Url参数
  743. CSimpleStringA str="";
  744. //Error = spConfig->ReadConfigValue("upload", "QueryPlanUrl", str);
  745. //if (Error == Error_Succeed)
  746. //{
  747. // DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("[CenterUrl] QueryPlanUrl =%s",str.GetData());
  748. // m_centerUrl.queryPlanUrl = str.GetData();
  749. //}
  750. //else
  751. //{
  752. // return Error;
  753. //}
  754. //Error = spConfig->ReadConfigValue("upload", "QueryTempFileUrl", str);
  755. //if (Error == Error_Succeed)
  756. //{
  757. // DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("[CenterUrl] QueryTempFileUrl =%s",str.GetData());
  758. // m_centerUrl.queryTempFileUrl = str.GetData();
  759. //}
  760. //else
  761. //{
  762. // return Error;
  763. //}
  764. //Error = spConfig->ReadConfigValue("upload", "UpdateStateUrl", str);
  765. //if (Error == Error_Succeed)
  766. //{
  767. // DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("[CenterUrl] UpdateStateUrl =%s",str.GetData());
  768. // m_centerUrl.updateStateUrl = str.GetData();
  769. //}
  770. //else
  771. //{
  772. // return Error;
  773. //}
  774. //Error = spConfig->ReadConfigValue("upload", "AddUploadFailUrl", str);
  775. //if (Error == Error_Succeed)
  776. //{
  777. // DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("[CenterUrl] AddUploadFailUrl =%s",str.GetData());
  778. // m_centerUrl.addUploadFailUrl = str.GetData();
  779. //}
  780. //else
  781. //{
  782. // return Error;
  783. //}
  784. Error = spConfig->ReadConfigValue("upload", "EachUploadLen", str);
  785. if (Error == Error_Succeed)
  786. {
  787. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("[CenterUrl] EachUploadLen =%s",str.GetData());
  788. if(str.IsNullOrEmpty()){
  789. m_eachUploadLen = UPLOAD_CONTENT_EACH_LEN;
  790. }else{
  791. m_eachUploadLen = atoi(str.GetData())*1024;
  792. }
  793. }
  794. else
  795. {
  796. return Error;
  797. }
  798. int uploadMaxNum = 0;
  799. Error = spConfig->ReadConfigValueInt("upload", "UploadMaxNum", uploadMaxNum);
  800. if (Error == Error_Succeed)
  801. {
  802. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("[CenterUrl] UploadMaxNum =%d",uploadMaxNum);
  803. if(uploadMaxNum<=10){
  804. m_uploadMaxNum = 200;
  805. }else{
  806. m_uploadMaxNum = uploadMaxNum;
  807. }
  808. }
  809. else
  810. {
  811. return Error;
  812. }
  813. //获取系统文件根路径
  814. CSimpleStringA runPath ;
  815. Error =this->GetEntityBase()->GetFunction()->GetPath("rvc",runPath);//例如:C:\\Run /opt/Run
  816. if(Error_Succeed == Error){
  817. if(!runPath.IsNullOrEmpty()){
  818. string runPathStr = runPath.GetData();
  819. //TODO: CrossPlaform [Gifur@2025730]
  820. #ifdef RVC_OS_WIN
  821. int pos = runPathStr.find_last_of('\\');
  822. #else
  823. int pos = runPathStr.find_last_of(SPLIT_SLASH);//修改为路径分隔符
  824. #endif
  825. m_rootPath = runPathStr.substr(0,pos);
  826. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("get rvc path is %s",m_rootPath.c_str());
  827. }else{
  828. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("get rvc path is null");
  829. return Error_Unexpect;
  830. }
  831. }else{
  832. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("get rvc path is error");
  833. return Error_Unexpect;
  834. }
  835. }else{
  836. return Error;
  837. }
  838. Error = m_pEntity->GetFunction()->RegistSysVarEvent("CallState", this);
  839. if (Error == Error_Succeed) {
  840. AddStateHooker(this);
  841. }
  842. //......
  843. return Error;
  844. }
  845. ErrorCodeEnum UploadFSM::OnExit()
  846. {
  847. m_pEntity->GetFunction()->UnregistSysVarEvent("CallState");
  848. return Error_Succeed;
  849. }
  850. void UploadFSM::s0_on_entry()
  851. {
  852. CSimpleStringA strValue;
  853. ErrorCodeEnum Error = m_pEntity->GetFunction()->GetSysVar("CallState", strValue);
  854. if (Error == Error_Succeed) {
  855. if (strValue[0] == 'O') { // only upload when CallState in offline state
  856. PostEventLIFO(new FSMEvent(USER_EVT_JMP_ENABLE));
  857. } else {
  858. PostEventLIFO(new FSMEvent(USER_EVT_JMP_DISABLE));
  859. }
  860. }
  861. }
  862. void UploadFSM::s0_on_exit()
  863. {
  864. }
  865. unsigned int UploadFSM::s0_on_event( FSMEvent* event )
  866. {
  867. return 0;
  868. }
  869. void UploadFSM::s1_on_entry()
  870. {
  871. ScheduleTimer(8, 30000);
  872. }
  873. void UploadFSM::s1_on_exit()
  874. {
  875. CancelTimer(8);
  876. }
  877. unsigned int UploadFSM::s1_on_event(FSMEvent* event)
  878. {
  879. if (event->iEvt == EVT_TIMER) {
  880. if(m_isOffLine){
  881. PostEventLIFO(new FSMEvent(USER_EVT_JMP_ENABLE));
  882. }else{
  883. ScheduleTimer(8, 30000);//继续等待
  884. }
  885. }
  886. return 0;
  887. }
  888. void UploadFSM::s2_on_entry()
  889. {
  890. m_uploadModel = true;//默认总行上传模式
  891. //清理上次扫描文件列表,扫描计划,分行ip列表
  892. if(m_uploadList!=NULL){
  893. list<UploadFileInfo*>::iterator iter ;
  894. for(iter=m_uploadList->begin();iter!=m_uploadList->end();++iter){
  895. delete *iter;
  896. }
  897. m_uploadList->clear();
  898. }
  899. if(m_uploadPlanList!=NULL){
  900. list<UploadPlan*>::iterator iter ;
  901. for(iter=m_uploadPlanList->begin();iter!=m_uploadPlanList->end();++iter){
  902. delete *iter;
  903. }
  904. m_uploadPlanList->clear();
  905. }
  906. m_branchIpList.clear();
  907. if(m_centerModel == (int)centerModel){
  908. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("centerModel");
  909. //防止url无效,新模式直接停止上传 等待下一次
  910. if(m_centerUrl.addUploadFailUrl.empty()||m_centerUrl.queryPlanUrl.empty()||
  911. m_centerUrl.queryTempFileUrl.empty()||m_centerUrl.updateStateUrl.empty()){
  912. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("centerModel mode ,centerUrl is null , wait for next time");
  913. ScheduleTimer(1, 120000);
  914. return ;
  915. }
  916. //新模式,查询策略
  917. if(queryPlan()){
  918. m_uploadModel = true;
  919. }else{
  920. //查询策略失败,等待下一次
  921. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("queryPlan is fail , wait for next time");
  922. ScheduleTimer(1, 120000);
  923. return ;
  924. }
  925. }else{
  926. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("m_centerModel is invalid");
  927. ScheduleTimer(1, 120000);
  928. return;
  929. }
  930. if(m_uploadModel){
  931. //新模式扫描
  932. PostEventLIFO(new FSMEvent(USER_EVT_JMP_NEW_SCAN));
  933. }else{
  934. //旧模式扫描
  935. PostEventLIFO(new FSMEvent(USER_EVT_JMP_OLD_SCAN));
  936. }
  937. }
  938. void UploadFSM::s2_on_exit()
  939. {
  940. CancelTimer(1);
  941. }
  942. unsigned int UploadFSM::s2_on_event(FSMEvent* event)
  943. {
  944. if (event->iEvt == EVT_TIMER) {
  945. m_uploadModel = true;//默认是分行上传模式
  946. //清理上次扫描文件列表,扫描计划,分行ip列表
  947. if(m_uploadList!=NULL){
  948. list<UploadFileInfo*>::iterator iter ;
  949. for(iter=m_uploadList->begin();iter!=m_uploadList->end();++iter){
  950. delete *iter;
  951. }
  952. m_uploadList->clear();
  953. }
  954. if(m_uploadPlanList!=NULL){
  955. list<UploadPlan*>::iterator iter ;
  956. for(iter=m_uploadPlanList->begin();iter!=m_uploadPlanList->end();++iter){
  957. delete *iter;
  958. }
  959. m_uploadPlanList->clear();
  960. }
  961. m_branchIpList.clear();
  962. //上传模式确定
  963. if(m_centerModel == (int)centerModel){
  964. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("centerModel");
  965. //防止url无效,新模式直接停止上传,等待下一次
  966. if(m_centerUrl.addUploadFailUrl.empty()||m_centerUrl.queryPlanUrl.empty()||
  967. m_centerUrl.queryTempFileUrl.empty()||m_centerUrl.updateStateUrl.empty()){
  968. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("centerModel mode ,centerUrl is null , wait for next time");
  969. ScheduleTimer(1, 240000);
  970. return 0;
  971. }
  972. //新模式,查询策略
  973. if(queryPlan()){
  974. m_uploadModel = true;
  975. }else{
  976. //查询策略失败,等待下一次
  977. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("queryPlan is fail , wait for next time");
  978. ScheduleTimer(1, 240000);
  979. return 0;
  980. }
  981. }else{
  982. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("m_centerModel is invalid");
  983. ScheduleTimer(1, 240000);
  984. return 0;
  985. }
  986. if(m_uploadModel){
  987. //新模式扫描
  988. PostEventLIFO(new FSMEvent(USER_EVT_JMP_NEW_SCAN));
  989. }else{
  990. //旧模式扫描
  991. PostEventLIFO(new FSMEvent(USER_EVT_JMP_OLD_SCAN));
  992. }
  993. }else if (event->iEvt == USER_EVT_JMP_NEW_SCAN) {
  994. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("enter new model upload");
  995. //新模式
  996. if (m_uploadList->size() == 0) {
  997. //重新扫描文件
  998. scanFile();
  999. }
  1000. if (m_uploadList->size() == 0) {
  1001. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("m_uploadList is 0,wait for 8 min retry scan");
  1002. ScheduleTimer(1, 480000);
  1003. } else {
  1004. PostEventLIFO(new FSMEvent(USER_EVT_JMP_NEW_UPLOAD));//跳到新的上传状态
  1005. }
  1006. }
  1007. return 0;
  1008. }
  1009. void UploadFSM::s3_on_entry()
  1010. {
  1011. }
  1012. void UploadFSM::s3_on_exit()
  1013. {
  1014. }
  1015. unsigned int UploadFSM::s3_on_event(FSMEvent* event)
  1016. {
  1017. return 0;
  1018. }
  1019. void UploadFSM::s4_on_entry()
  1020. {
  1021. }
  1022. void UploadFSM::s4_on_exit()
  1023. {
  1024. }
  1025. unsigned int UploadFSM::s4_on_event(FSMEvent* event)
  1026. {
  1027. return 0;
  1028. }
  1029. void UploadFSM::s5_on_entry()
  1030. {
  1031. }
  1032. void UploadFSM::s5_on_exit()
  1033. {
  1034. }
  1035. unsigned int UploadFSM::s5_on_event(FSMEvent* event)
  1036. {
  1037. return 0;
  1038. }
  1039. void UploadFSM::s6_on_entry()
  1040. {
  1041. }
  1042. void UploadFSM::s6_on_exit()
  1043. {
  1044. }
  1045. unsigned int UploadFSM::s6_on_event(FSMEvent* event)
  1046. {
  1047. return 0;
  1048. }
  1049. void UploadFSM::s7_on_entry()
  1050. {
  1051. //新上传模式
  1052. m_uploadReport.sum = m_uploadList->size();
  1053. m_uploadReport.succ = 0;
  1054. m_uploadReport.fail = 0;
  1055. m_uploadReport.uploadFail = 0;
  1056. m_uploadReport.updateState = 0;
  1057. m_uploadReport.decodeFail = 0;
  1058. m_uploadReport.operateFileFail= 0;
  1059. m_uploadReport.queryTempFail =0;
  1060. m_uploadReport.isBreak =false;
  1061. //获取一个新文件,发送上传请求线程
  1062. if(m_uploadList->size()>0){
  1063. m_currUploadFile = *(m_uploadList->begin());//首元素赋值给全局变量
  1064. m_uploadList->pop_front();//去除首元素
  1065. Task::SendFileTask* task = new Task::SendFileTask(this);
  1066. ErrorCodeEnum rc = this->GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  1067. if (rc != Error_Succeed)
  1068. {
  1069. if(m_currUploadFile!=NULL){
  1070. delete m_currUploadFile;
  1071. m_currUploadFile=NULL;
  1072. }
  1073. LogError(Severity_Middle, rc, 0, "Post SendFileTask task to Thread is fail,wait 30s retry");
  1074. ScheduleTimer(7, 30000);
  1075. }
  1076. }
  1077. }
  1078. void UploadFSM::s7_on_exit()
  1079. {
  1080. if(m_currUploadFile!=NULL){
  1081. delete m_currUploadFile;
  1082. m_currUploadFile=NULL;
  1083. }
  1084. CancelTimer(7);
  1085. }
  1086. unsigned int UploadFSM::s7_on_event(FSMEvent* event)
  1087. {
  1088. if (event->iEvt == USER_EVT_UPLOAD_SUCC) {
  1089. m_uploadReport.succ++;//成功计数
  1090. //获取新的上传文件,发送上传请求线程
  1091. //判断是否已经需要退出
  1092. //无文件,跳出此状态
  1093. if(m_isOffLine){
  1094. if(m_uploadList->size()>0){
  1095. if(m_currUploadFile!=NULL){
  1096. delete m_currUploadFile;
  1097. m_currUploadFile=NULL;
  1098. }
  1099. m_currUploadFile = *(m_uploadList->begin());//首元素赋值给全局变量
  1100. m_uploadList->pop_front();//去除首元素
  1101. Task::SendFileTask* task = new Task::SendFileTask(this);
  1102. ErrorCodeEnum rc = this->GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  1103. if (rc != Error_Succeed)
  1104. {
  1105. if(m_currUploadFile!=NULL){
  1106. delete m_currUploadFile;
  1107. m_currUploadFile=NULL;
  1108. }
  1109. m_uploadReport.isBreak =true;
  1110. reportBatchMsg();
  1111. LogError(Severity_Middle, rc, 0, "Post SendFileTask task to Thread is fail,wait 240s retry scan");
  1112. ScheduleTimer(7, 240000);
  1113. }
  1114. }else{
  1115. m_uploadReport.isBreak =false;
  1116. reportBatchMsg();
  1117. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("NewUpload m_uploadList is empty,wait 240s retry scan");
  1118. ScheduleTimer(7, 240000);// goto s2
  1119. }
  1120. }else{
  1121. m_uploadReport.isBreak =true;
  1122. reportBatchMsg();
  1123. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("NewUpload m_isOffLine is false,jump s1");
  1124. PostEventLIFO(new FSMEvent(USER_EVT_JMP_NEW_DISABLE));// goto s1
  1125. }
  1126. }else if (event->iEvt == USER_EVT_UPLOAD_FAIL) {
  1127. m_uploadReport.fail++;//失败计数
  1128. //获取新的上传文件,发送上传请求线程
  1129. //判断是否已经需要退出
  1130. //无文件,跳出此状态
  1131. if(m_isOffLine){
  1132. Sleep(3000);//上传失败,等待3秒再下一个
  1133. if(m_uploadList->size()>0){
  1134. if(m_currUploadFile!=NULL){
  1135. delete m_currUploadFile;
  1136. m_currUploadFile=NULL;
  1137. }
  1138. m_currUploadFile = *(m_uploadList->begin());//首元素赋值给全局变量
  1139. m_uploadList->pop_front();//去除首元素
  1140. Task::SendFileTask* task = new Task::SendFileTask(this);
  1141. ErrorCodeEnum rc = this->GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  1142. if (rc != Error_Succeed)
  1143. {
  1144. if(m_currUploadFile!=NULL){
  1145. delete m_currUploadFile;
  1146. m_currUploadFile=NULL;
  1147. }
  1148. m_uploadReport.isBreak =true;
  1149. reportBatchMsg();
  1150. LogError(Severity_Middle, rc, 0, "Post SendFileTask task to Thread is fail,wait 240s retry scan");
  1151. ScheduleTimer(7, 240000);
  1152. }
  1153. }else{
  1154. m_uploadReport.isBreak =false;
  1155. reportBatchMsg();
  1156. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("NewUpload m_uploadList is empty,wait 240s retry scan");
  1157. ScheduleTimer(7, 240000);// goto s2
  1158. }
  1159. }else{
  1160. m_uploadReport.isBreak =true;
  1161. reportBatchMsg();
  1162. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("NewUpload m_isOffLine is false,jump s1");
  1163. PostEventLIFO(new FSMEvent(USER_EVT_JMP_NEW_DISABLE));// goto s1
  1164. }
  1165. }else if (event->iEvt == EVT_TIMER) {
  1166. // goto s2
  1167. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("NewUpload ,jump to s2");
  1168. }
  1169. return 0;
  1170. }
  1171. //通过
  1172. bool UploadFSM::queryPlan()
  1173. {
  1174. IHttpFunc* client;
  1175. client = create_http(HttpsLogCallBack);
  1176. CSystemStaticInfo si;
  1177. ErrorCodeEnum error = this->GetEntityBase()->GetFunction()->GetSystemStaticInfo(si);
  1178. if(error!=Error_Succeed){
  1179. return false;//获取失败,走旧模式
  1180. }
  1181. UploadQueryPlanReq planReq;
  1182. UploadQueryPlanRet planRet;
  1183. Json::Value rootReq;
  1184. Json::FastWriter writer;
  1185. rootReq["terminal_no"] = si.strTerminalID.GetData();
  1186. #ifdef RVC_OS_WIN
  1187. rootReq["os_type"] = "WIN";
  1188. #else
  1189. rootReq["os_type"] = "UOS";
  1190. #endif
  1191. string jsonReq = writer.write(rootReq);
  1192. planReq.m_reqStr = jsonReq;
  1193. //planReq.m_printDbg = true;
  1194. planReq.m_url = m_centerUrl.queryPlanUrl.c_str();
  1195. PROCESS_LINK_CONTEXT("LR0402104QueryPlan")
  1196. if(!client->Post(planReq, planRet, &nextLink)){
  1197. 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());
  1198. client->Destory();
  1199. return false;//http错误,走旧模式
  1200. }
  1201. client->Destory();
  1202. //解析回应报文
  1203. Json::Reader reader;
  1204. Json::Value rootRet;
  1205. if (!reader.parse(planRet.m_retStr, rootRet, false))
  1206. {
  1207. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("queryPlan parse resp is fail");
  1208. return false;//解析失败,走旧模式
  1209. }
  1210. bool isSucc = rootRet["success"].asBool();
  1211. if(isSucc){
  1212. if(rootRet.isMember("data")){
  1213. if(rootRet["data"]["mode"].asBool()){
  1214. //新模式
  1215. string branchNO = rootRet["data"]["branch_no"].asString();
  1216. //分行服务器列表:可能为空
  1217. if(rootRet["data"]["upload_address"].isMember("upload_url")){
  1218. for(int i =0;i<rootRet["data"]["upload_address"]["upload_url"].size();i++){
  1219. string bUploadUrl = rootRet["data"]["upload_address"]["upload_url"][i].asString();
  1220. m_branchIpList[i+1]=bUploadUrl;
  1221. if(!bUploadUrl.empty()){
  1222. // DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("queryPlan return upload_url=%s",bUploadUrl.c_str());
  1223. }else{
  1224. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("queryPlan return upload_url=");
  1225. }
  1226. }
  1227. }else{
  1228. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("[data][upload_address][upload_url] is null");
  1229. }
  1230. string env = "";//上传服务器地址属于行内还是行外
  1231. if(rootRet["data"]["upload_address"].isMember("env")){
  1232. env = rootRet["data"]["upload_address"]["env"].asString();
  1233. if(strcmp(env.c_str(),"DMZ")==0||strcmp(env.c_str(),"BIZ")==0){
  1234. }else{
  1235. 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());
  1236. return false;//上传服务ip环境不正确,走旧模式
  1237. }
  1238. }else{
  1239. 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());
  1240. return false;//上传服务ip环境为空,走旧模式
  1241. }
  1242. //扫描策略:可能为空
  1243. for (int i =0;i <rootRet["data"]["plan_list"].size();i++){
  1244. UploadPlan* p = new UploadPlan();
  1245. p->plan_id = rootRet["data"]["plan_list"][i]["plan_id"].asString();
  1246. p->branch_no = branchNO;//分行号
  1247. p->plan_type = rootRet["data"]["plan_list"][i]["plan_type"].asString();
  1248. p->file_type = rootRet["data"]["plan_list"][i]["file_type"].asString();
  1249. p->path_type = rootRet["data"]["plan_list"][i]["path_type"].asString();
  1250. #ifdef RVC_OS_WIN
  1251. p->path_name = rootRet["data"]["plan_list"][i]["path_name"].asString();
  1252. #else
  1253. //转义路径里面windows反斜杠'\'为'/'
  1254. //p->path_name = rootRet["data"]["plan_list"][i]["path_name"].asString();
  1255. string pathName = rootRet["data"]["plan_list"][i]["path_name"].asString();
  1256. std::replace(pathName.begin(), pathName.end(), '\\', SPLIT_SLASH);
  1257. p->path_name = pathName.c_str();
  1258. #endif
  1259. p->file_name = rootRet["data"]["plan_list"][i]["file_name"].asString();
  1260. p->file_regex = rootRet["data"]["plan_list"][i]["file_regex"].asString();
  1261. p->before_deal_type = rootRet["data"]["plan_list"][i]["before_deal_type"].asString();
  1262. p->after_deal_type = rootRet["data"]["plan_list"][i]["after_deal_type"].asString();
  1263. #ifdef RVC_OS_WIN
  1264. p->after_deal_text = rootRet["data"]["plan_list"][i]["after_deal_text"].asString();
  1265. #else
  1266. //转义路径里面windows反斜杠'\'为'/'
  1267. //p->after_deal_text = rootRet["data"]["plan_list"][i]["after_deal_text"].asString();
  1268. string afterDealText = rootRet["data"]["plan_list"][i]["after_deal_text"].asString();
  1269. std::replace(afterDealText.begin(), afterDealText.end(), '\\', SPLIT_SLASH);
  1270. p->after_deal_text = afterDealText.c_str();
  1271. #endif
  1272. p->file_limit = rootRet["data"]["plan_list"][i]["file_limit"].asInt();
  1273. if (p->file_limit <= 0) {
  1274. //默认文件最大扫描个数为1000
  1275. p->file_length_limit = 1000;
  1276. }
  1277. p->file_length_limit = rootRet["data"]["plan_list"][i]["file_length_limit"].asInt();//增加扫描文件最大长度限制
  1278. if(p->file_length_limit==0){
  1279. //默认扫描文件最大长度设置为200m
  1280. p->file_length_limit = 200;
  1281. }
  1282. p->max_silent = rootRet["data"]["plan_list"][i]["max_silent"].asInt();
  1283. if (p->max_silent <= 0) {
  1284. //默认文件流逝时间间隔为900s
  1285. p->file_length_limit = 900;
  1286. }
  1287. p->upload_address_env = env;
  1288. m_uploadPlanList->push_back(p);
  1289. }
  1290. if(m_branchIpList.size()==0){
  1291. 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());
  1292. return false;//上传服务ip为空,走旧模式
  1293. }
  1294. if(m_uploadPlanList->size()==0){
  1295. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("queryPlan return %d plan ",m_uploadPlanList->size());
  1296. return true;//等待下一次请求,走新模式
  1297. }
  1298. return true;//明确走新模式
  1299. }else{
  1300. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("queryPlan return [data].[mode] is false");
  1301. return false;//明确走旧模式
  1302. }
  1303. }else{
  1304. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("queryPlan return data is null");
  1305. return false;//无data字段,走旧模式
  1306. }
  1307. }else{
  1308. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("queryPlan return success is false");
  1309. return false;//服务返回错误,走旧模式
  1310. }
  1311. }
  1312. //通过
  1313. void UploadFSM::scanFile()
  1314. {
  1315. list<UploadPlan*>::iterator iter;
  1316. for(iter = m_uploadPlanList->begin();iter!=m_uploadPlanList->end();++iter){
  1317. UploadPlan* up = *iter;
  1318. //构建初始的扫描根路径
  1319. string path="";
  1320. if(up->path_type=="R"){
  1321. //TODO: CrossPlaform [Gifur@2025730]
  1322. #ifdef RVC_OS_WIN
  1323. path = m_rootPath + "\\" + up->path_name;
  1324. #else
  1325. path = m_rootPath + SPLIT_SLASH_STR + up->path_name;
  1326. #endif
  1327. }else if(up->path_type=="A"){
  1328. path=up->path_name;
  1329. }else{
  1330. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("plan path_type is invalid path_type=%s",up->path_type.c_str());
  1331. continue;
  1332. }
  1333. if(m_printPlan){
  1334. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("planID=[%s], scanDir=[%s], filter=[%s], file_limit=[%d], max_silent=[%d]",up->plan_id.c_str(),path.c_str(),up->file_name.c_str(),up->file_limit,up->max_silent);
  1335. }
  1336. scanDirfresh(up,path.c_str());
  1337. }
  1338. m_printPlan=false;//只打印一次,设置为不打印
  1339. //加入文件超过阈值后告警
  1340. if(m_uploadList->size()>m_uploadMaxNum){
  1341. LogWarn(Severity_Middle, Error_Unexpect, LOG_WARN_UPLOAD_MAX_ERROR, CSimpleStringA::Format("upload list num(%d) is over max num",m_uploadList->size()).GetData());
  1342. }
  1343. }
  1344. //通过
  1345. void UploadFSM::scanDirfresh(UploadPlan* plan,const char *path)
  1346. {
  1347. array_header_t *arr_files = fileutil_get_sub_files2_a(path, plan->file_limit);
  1348. if (arr_files) {
  1349. int i;
  1350. for (i = 0; i < arr_files->nelts; ++i){
  1351. char *file_path = ARRAY_IDX(arr_files, i, char*);
  1352. //得到文件名
  1353. string fileName = _GetFileName(file_path);
  1354. //文件名通配符过滤
  1355. CSimpleStringA strFileFilter = _GetFileName(plan->file_name.c_str());
  1356. if(!IsFileMatch(strFileFilter.GetData(),fileName.c_str())){
  1357. continue;//继续下一个文件
  1358. }
  1359. //正则表达式过滤
  1360. if(!plan->file_regex.empty()){
  1361. regex e(plan->file_regex.c_str());
  1362. if(!regex_match(fileName.c_str(),e,regex_constants::match_default)){
  1363. continue;//继续下一个文件
  1364. }
  1365. }
  1366. #ifdef RVC_OS_WIN
  1367. //最后修改时间间隔过滤
  1368. WIN32_FILE_ATTRIBUTE_DATA attr;
  1369. if (!GetFileAttributesExA(file_path, GetFileExInfoStandard, &attr)) {
  1370. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get local file attrr fail! file_path=%s",file_path);
  1371. continue;//继续下一个文件
  1372. }
  1373. //判断文件是否超过最大扫描文件长度
  1374. unsigned long limit = plan->file_length_limit*1024*1024;
  1375. if(attr.nFileSizeLow>limit){
  1376. if (!findOverLenWarn(file_path)) {
  1377. //未找到则告警
  1378. 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);
  1379. LogWarn(Severity_Low, Error_Unexpect, LOG_WARN_UPLOAD_QUERY_FILE_OVER_LIMIT, warnStr.GetData());
  1380. }
  1381. continue;//继续下一个文件
  1382. }
  1383. FILETIME fttmp;
  1384. SYSTEMTIME filest;
  1385. SYSTEMTIME st;
  1386. if(!FileTimeToLocalFileTime(&attr.ftLastWriteTime,&fttmp)){
  1387. continue;//继续下一个文件
  1388. }
  1389. if(!FileTimeToSystemTime(&fttmp,&filest)){
  1390. continue;//继续下一个文件
  1391. }
  1392. GetLocalTime(&st);
  1393. 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);
  1394. if(nsecondsInterval<plan->max_silent){
  1395. continue;//继续下一个文件,时间不够
  1396. }else{
  1397. //时间已够,判断文件是否能独占锁
  1398. if(!test_zero_ref(file_path)){
  1399. continue;//继续下一个文件
  1400. }
  1401. }
  1402. //得到文件长度
  1403. unsigned long filelen = attr.nFileSizeLow;
  1404. #else
  1405. struct stat attr_of_src;
  1406. if (lstat(file_path, &attr_of_src) != 0) {
  1407. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get local file attrr fail! file_path=%s", file_path);
  1408. continue;//继续下一个文件
  1409. }
  1410. //判断文件是否超过最大扫描文件长度
  1411. unsigned long limit = plan->file_length_limit * 1024 * 1024;
  1412. if (attr_of_src.st_size > limit) {
  1413. 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);
  1414. LogWarn(Severity_Low, Error_Unexpect, LOG_WARN_UPLOAD_QUERY_FILE_OVER_LIMIT, warnStr.GetData());
  1415. continue;//继续下一个文件
  1416. }
  1417. time_t nowTime = time((time_t*)NULL);//当前时间
  1418. if (nowTime > attr_of_src.st_mtime)
  1419. {
  1420. long diff = nowTime - attr_of_src.st_mtime;
  1421. if (diff < plan->max_silent)
  1422. {
  1423. continue;//继续下一个文件,时间不够
  1424. }else{
  1425. //时间已够,判断文件是否能独占锁
  1426. if (!test_zero_ref(file_path)) {
  1427. continue;//继续下一个文件
  1428. }
  1429. }
  1430. }else {
  1431. continue;//继续下一个文件,时间不够
  1432. }
  1433. //得到文件长度
  1434. unsigned long filelen = attr_of_src.st_size;
  1435. #endif
  1436. //添加扫描文件
  1437. UploadFileInfo* upFile = new UploadFileInfo();
  1438. upFile->firstMD5Str = "";
  1439. upFile->lastMD5Str = "";
  1440. upFile->fileName = fileName;
  1441. upFile->filePath = file_path;
  1442. if(upFile->filePath.length()>900){
  1443. //路径太长,ECS存储有问题,截取路径,并加入模拟路径前缀:window S:\\ , linux /S/
  1444. #ifdef RVC_OS_WIN
  1445. upFile->fileUploadPath.append("S:\\");
  1446. #else
  1447. upFile->fileUploadPath.append("/S/");
  1448. #endif
  1449. upFile->fileUploadPath.append(fileName.c_str());
  1450. //upFile->fileUploadRelativePath = "S:\\";
  1451. }else{
  1452. //直接赋值
  1453. upFile->fileUploadPath = file_path;
  1454. //upFile->fileUploadRelativePath = upFile->fileUploadPath.substr(0,upFile->fileUploadPath.length()-fileName.length());//截取文件名称的路径
  1455. }
  1456. upFile->BranchFilePath = file_path;
  1457. upFile->fileLen = filelen;
  1458. #ifdef RVC_OS_WIN
  1459. upFile->editTime = CSimpleStringA::Format("%d-%d-%d %d:%d:%d",filest.wYear, filest.wMonth, filest.wDay, filest.wHour, filest.wMinute, filest.wSecond);
  1460. #else
  1461. tm fileTimeTm = { 0 };
  1462. localtime_r(&attr_of_src.st_mtime, &fileTimeTm);
  1463. 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);
  1464. #endif
  1465. upFile->plan_id = plan->plan_id;
  1466. upFile->branch_no = plan->branch_no;
  1467. upFile->plan_type = plan->plan_type;
  1468. upFile->file_type = plan->file_type;
  1469. upFile->path_type = plan->path_type;
  1470. upFile->path_name = plan->path_name;//因传回路径可能是windows路径斜杠,转义为linux斜杠
  1471. upFile->file_name = plan->file_name;
  1472. upFile->file_regex = plan->file_regex;
  1473. upFile->before_deal_type = plan->before_deal_type;
  1474. upFile->after_deal_type = plan->after_deal_type;
  1475. upFile->after_deal_text = plan->after_deal_text;
  1476. upFile->file_limit = plan->file_limit;
  1477. upFile->file_length_limit = plan->file_length_limit;
  1478. upFile->max_silent = plan->max_silent;
  1479. upFile->upload_address_env = plan->upload_address_env;
  1480. m_uploadList->push_back(upFile);
  1481. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("file %s lenth(%d) added!",file_path,filelen);
  1482. }
  1483. toolkit_array_free2(arr_files);
  1484. }
  1485. array_header_t *arr_dir = fileutil_get_sub_dirs_a(path);
  1486. if (arr_dir) {
  1487. int i;
  1488. for (i = 0; i < arr_dir->nelts; ++i) {
  1489. char *dir_path = ARRAY_IDX(arr_dir, i, char*);
  1490. scanDirfresh(plan, dir_path);
  1491. }
  1492. }
  1493. }
  1494. void UploadFSM::HttpsLogCallBack(const char* logtxt)
  1495. {
  1496. if(logtxt!=NULL){
  1497. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("http dbg: %s",logtxt);
  1498. }
  1499. }
  1500. bool UploadFSM::IsFileMatch(const char *pszFilter, const char *pszFileName)
  1501. {
  1502. const char *p1 = pszFilter;
  1503. const char *p2 = pszFileName;
  1504. if (p1 == NULL)
  1505. return true;
  1506. if (p2 == NULL)
  1507. return false;
  1508. if (*p1 ==NULL && *p2 ==NULL)
  1509. return true;
  1510. else if (*p1 == NULL)
  1511. return false;
  1512. else if (*p2 == NULL)
  1513. {
  1514. // 查找*p1是否全是*
  1515. while(*p1 == '*')
  1516. p1++;
  1517. if (*p1 == NULL)
  1518. return true;
  1519. else
  1520. return false;
  1521. }
  1522. if (*p1 != '*')
  1523. {
  1524. if (tolower(*p1) != tolower(*p2))
  1525. return false;
  1526. else
  1527. return IsFileMatch(p1+1, p2+1);
  1528. }
  1529. else
  1530. {
  1531. while(*++p1 == '*');
  1532. if (*p1 == NULL)
  1533. return true;
  1534. while (*p2)
  1535. {
  1536. while(tolower(*p1) != tolower(*p2) && *++p2);
  1537. if (*p2 == NULL)
  1538. return false;
  1539. if(IsFileMatch(p1+1, p2+1))
  1540. return true;
  1541. p2++;
  1542. }
  1543. return false;
  1544. }
  1545. }
  1546. ErrorCodeEnum UploadFSM::SM3FileToStr(CSimpleStringA strFilePath, CSimpleStringA &strSM3,bool isSub)
  1547. {
  1548. if(strFilePath.IsNullOrEmpty()){
  1549. return Error_Null;
  1550. }
  1551. int nlen = strlen(strFilePath.GetData());
  1552. char* pchar = new char[nlen+1];
  1553. strcpy(pchar,strFilePath.GetData());
  1554. BYTE md5[32]={0};
  1555. try
  1556. {
  1557. if(SM3File(pchar,md5)){
  1558. delete pchar;
  1559. //获取16进制的字符串
  1560. if(Error_Succeed!=SM3_Str(strSM3,md5,isSub)){
  1561. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("sm3国密转16进制字符串失败,file=%s",strFilePath.GetData());
  1562. return Error_Unexpect;
  1563. }
  1564. return Error_Succeed;
  1565. }else{
  1566. delete pchar;
  1567. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("sm3国密加密失败,file=%s",strFilePath.GetData());
  1568. return Error_Unexpect;
  1569. }
  1570. }
  1571. catch (...)
  1572. {
  1573. delete pchar;
  1574. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("sm3国密加密异常失败,file=%s",strFilePath.GetData());
  1575. return Error_Exception;
  1576. }
  1577. }
  1578. ErrorCodeEnum UploadFSM::SM3_Str(CSimpleStringA &strSM3,BYTE * SM3Byte,bool isSub)
  1579. {
  1580. if(SM3Byte == NULL){
  1581. return Error_Exception;
  1582. }
  1583. int SM3_len=64;
  1584. if(isSub){
  1585. SM3_len=32;
  1586. }
  1587. int i;
  1588. char* file_SM3 = (char*)malloc((SM3_len + 1) * sizeof(char));
  1589. if(file_SM3 == NULL)
  1590. {
  1591. fprintf(stderr, "SM3 malloc failed.\n");
  1592. return Error_Exception;
  1593. }
  1594. memset(file_SM3, 0, (SM3_len + 1));
  1595. if(SM3_len == 32)
  1596. {
  1597. for(i=0; i<16; i++)
  1598. { //SM3截取中间16位字节,8-24位字节数,小写
  1599. sprintf(&file_SM3[i*2], "%02x", SM3Byte[i+8]);
  1600. }
  1601. }
  1602. else if(SM3_len == 64)
  1603. {
  1604. for(i=0; i<32; i++)
  1605. {
  1606. sprintf(&file_SM3[i*2], "%02x", SM3Byte[i]);
  1607. }
  1608. }
  1609. strSM3=file_SM3;
  1610. free(file_SM3);
  1611. return Error_Succeed;
  1612. }
  1613. bool UploadFSM::getFileContent(FILE* pFile,unsigned char* content,int beginPos, int contentLen)
  1614. {
  1615. if(pFile==NULL||content==NULL){
  1616. return false;
  1617. }
  1618. #ifdef RVC_OS_WIN
  1619. clearerr(pFile);//尝试先复位错误指针
  1620. #endif
  1621. if(fseek(pFile,beginPos,SEEK_SET)!=0){
  1622. //TODO: CrossPlaform [Gifur@2025730]
  1623. #ifdef RVC_OS_WIN
  1624. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("getFileContent fail,fseek beginPos =%d is error =%d",beginPos,(int)GetLastError());
  1625. #else
  1626. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("getFileContent fail,fseek beginPos =%d is error =%d",beginPos, errno);
  1627. #endif
  1628. return false;
  1629. }
  1630. unsigned char* pRead = content;//移动的指针
  1631. int pRemaind = contentLen; //剩余字节数
  1632. int numBytes = 0;
  1633. while(!feof(pFile)&&!ferror(pFile)){
  1634. if(pRemaind<=0){
  1635. break;
  1636. }
  1637. numBytes=(int)fread(pRead,1,pRemaind,pFile);
  1638. if(numBytes>0){
  1639. pRemaind -=numBytes;
  1640. pRead +=numBytes;
  1641. }
  1642. }
  1643. if(pRemaind==0){
  1644. return true;
  1645. }else{
  1646. //TODO: CrossPlaform [Gifur@2025730]
  1647. #ifdef RVC_OS_WIN
  1648. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("getFileContent fail,pRemaind =%d, error=%d",pRemaind,(int)GetLastError());
  1649. #else
  1650. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("getFileContent fail,pRemaind =%d, error=%d",pRemaind, errno);
  1651. #endif
  1652. return false;
  1653. }
  1654. }
  1655. bool UploadFSM::test_zero_ref(const char *path)
  1656. {
  1657. #ifdef RVC_OS_WIN
  1658. HANDLE hFile = CreateFileA(path,
  1659. GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); // try open
  1660. if (hFile != INVALID_HANDLE_VALUE) {
  1661. CloseHandle(hFile);
  1662. return TRUE;
  1663. } else {
  1664. //添加获取句柄失败情况
  1665. LogWarn(Severity_Low, Error_Unexpect, LOG_WARN_UPLOAD_SCANFILE_LOCK_ERROR, CSimpleStringA::Format("test_zero_ref is fail:%s",path).GetData());
  1666. DWORD dwRet = GetLastError();
  1667. return FALSE;
  1668. }
  1669. #else
  1670. //后期改为linux真正的文件锁,现在来看,暂时用不着
  1671. FILE* fp = fopen(path, "rb");
  1672. if (fp != NULL) {
  1673. fclose(fp);
  1674. return true;
  1675. }
  1676. else
  1677. {
  1678. LogWarn(Severity_Low, Error_Unexpect, LOG_WARN_UPLOAD_SCANFILE_LOCK_ERROR, CSimpleStringA::Format("check_zero_ref is fail:%s", path).GetData());
  1679. return false;
  1680. }
  1681. #endif // RVC_OS_WIN
  1682. }
  1683. //TODO: CrossPlaform [Gifur@2025730]
  1684. bool UploadFSM::GetFileLength(const char* filePath,long &fileLen)
  1685. {
  1686. FILE* file = fopen(filePath, "rb");
  1687. if (file!=NULL)
  1688. {
  1689. #ifdef RVC_OS_WIN
  1690. fileLen = filelength(fileno(file));
  1691. fclose(file);
  1692. return true;
  1693. #else
  1694. //取得当前文件流的读取位置
  1695. int cur_pos = ftell(file);
  1696. //将文件流的读取位置设为文件末尾
  1697. if (fseek(file, SEEK_SET, SEEK_END)==0) {
  1698. //获取文件末尾的读取位置,即文件大小
  1699. fileLen = ftell(file);
  1700. //将文件流的读取位置还原为原先的值
  1701. fseek(file, cur_pos, SEEK_SET);
  1702. fclose(file);
  1703. return true;
  1704. }
  1705. else {
  1706. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("fseek file=%s is fail, err=%d", filePath,errno);
  1707. fclose(file);
  1708. return false;
  1709. }
  1710. #endif
  1711. } else{
  1712. fileLen = 0;
  1713. return false;
  1714. }
  1715. }
  1716. void UploadFSM::reportBatchMsg()
  1717. {
  1718. CSimpleStringA strReport;
  1719. if(m_uploadReport.fail==0){
  1720. 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)",
  1721. m_uploadReport.isBreak, m_uploadReport.sum, m_uploadReport.succ, m_uploadReport.fail, m_uploadReport.uploadFail, m_uploadReport.updateState,
  1722. m_uploadReport.operateFileFail, m_uploadReport.decodeFail, m_uploadReport.queryTempFail);
  1723. LogWarn(Severity_Low, Error_Unexpect, LOG_WARN_UPLOAD_FILE_REPORT, strReport.GetData());
  1724. }else{
  1725. 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)",
  1726. m_uploadReport.isBreak, m_uploadReport.sum, m_uploadReport.succ, m_uploadReport.fail, m_uploadReport.uploadFail, m_uploadReport.updateState,
  1727. m_uploadReport.operateFileFail, m_uploadReport.decodeFail, m_uploadReport.queryTempFail);
  1728. LogWarn(Severity_Middle, Error_Unexpect, LOG_WARN_UPLOAD_FILE_REPORT, strReport.GetData());
  1729. }
  1730. //LogWarn(Severity_Middle, Error_Unexpect, LOG_WARN_UPLOAD_FILE_REPORT, strReport.GetData());
  1731. }
  1732. bool UploadFSM::findOverLenWarn(const char* fileName) {
  1733. if (m_overLenFileList != NULL) {
  1734. bool isfind = false;
  1735. list<string>::iterator iter;
  1736. for (iter = m_overLenFileList->begin(); iter != m_overLenFileList->end(); ++iter) {
  1737. string fileStr = *iter;
  1738. if (strcmp(fileName, fileStr.c_str()) == 0) {
  1739. isfind = true;
  1740. break;
  1741. }
  1742. }
  1743. if (!isfind) {
  1744. //未找到,加入队列
  1745. m_overLenFileList->push_back(fileName);
  1746. }
  1747. return isfind;
  1748. }
  1749. return false;
  1750. }