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