UploadFSM.cpp 67 KB

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