UploadFSM.cpp 67 KB

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