UploadFSM.cpp 67 KB

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