UploadFSM.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. #include "stdafx.h"
  2. #include "UploadFSM.h"
  3. #include <thread>
  4. #include "upload.h"
  5. #include "libtoolkit/path.h"
  6. #ifdef RVC_OS_WIN
  7. #else
  8. #include <sys/stat.h>
  9. #include <sys/types.h>
  10. #include <fcntl.h>
  11. #include <time.h>
  12. #include <stdio.h>
  13. #include <cstdio>
  14. #include <iostream>
  15. #endif
  16. UploadFSM::UploadFSM() : m_pConnection(NULL)
  17. {
  18. }
  19. UploadFSM::~UploadFSM()
  20. {
  21. }
  22. void UploadFSM::OnStateTrans( int iSrcState, int iDstState )
  23. {
  24. Dbg("trans from %s to %s", GetStateName(iSrcState), GetStateName(iDstState));
  25. }
  26. void UploadFSM::OnSysVarEvent( const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName )
  27. {
  28. Dbg("rx sysvar %s from %s, %s to %s", pszKey, pszEntityName, pszOldValue, pszValue);
  29. if (pszValue[0] == 'O') {
  30. //PostEventFIFO(new FSMEvent(USER_EVT_JMP_ENABLE));
  31. LogWarn(Severity_High, Error_NotSupport, 0, "一轮测试期间不做任何上传操作!");
  32. PostEventFIFO(new FSMEvent(USER_EVT_JMP_DISABLE));
  33. } else {
  34. PostEventFIFO(new FSMEvent(USER_EVT_JMP_DISABLE));
  35. }
  36. }
  37. ErrorCodeEnum UploadFSM::OnInit()
  38. {
  39. ErrorCodeEnum Error;
  40. Error = LoadServerConfigFromCenterSetting();
  41. if (Error != Error_Succeed)
  42. {
  43. LogWarn(Severity_High, Error, 0, "从集中配置读取配置信息失败");
  44. return Error;
  45. }
  46. INIT_LIST_HEAD(&m_updir_list);
  47. m_check_dir = new list<CSimpleStringA>();
  48. m_uploadDateList = new list<CSimpleStringA>();
  49. {
  50. CSmartPointer<IEntityFunction> spFunction = m_pEntity->GetFunction();;
  51. CSmartPointer<IConfigInfo> spConfig;
  52. Error = spFunction->OpenConfig(Config_Run, spConfig);
  53. if (Error == Error_Succeed) {
  54. CSimpleStringA dateList ="";
  55. Error = spConfig->ReadConfigValue("uploadDate", "dateList", dateList);
  56. bool bError= false;
  57. if(Error == Error_Succeed){
  58. CAutoArray<CSimpleStringA> dList = dateList.Split(',');
  59. for(int i=0; i<dList.GetCount(); i++){
  60. //当读入的日期大于10天,则不加载后续的日期,防止后续日期字符串太长
  61. if(i>=10){
  62. break;
  63. }
  64. CSimpleStringA strDate = dList[i];
  65. //判断日期长度,只读入正确的日期参数,防止读入文件是错误的参数
  66. if(strDate.GetLength()==10){
  67. m_uploadDateList->push_back(strDate);
  68. Dbg("upload.ini uploadDate/dateList is: %s ",strDate.GetData());
  69. }else{
  70. Dbg("upload.ini uploadDate/dateList param is error : %s ",strDate.GetData());
  71. bError=true;
  72. }
  73. }
  74. //重置错误的参数文件
  75. if(bError){
  76. if(SaveUploadDate()==Error_Succeed){
  77. Dbg("upload.ini param reset is Success!");
  78. }else{
  79. Dbg("upload.ini param reset is fail!");
  80. }
  81. }
  82. }else{
  83. Dbg("read upload.ini param uploadDate/dateList fail!");
  84. return Error;
  85. }
  86. }else{
  87. Dbg("open runcfg upload.ini fail, please check out upload.ini!");
  88. return Error;
  89. }
  90. }
  91. CSimpleStringA checkStr="";
  92. {
  93. CSmartPointer<IConfigInfo> spConfig;
  94. m_pEntity->GetFunction()->OpenConfig(Config_Software, spConfig);
  95. Error = (ErrorCodeEnum)upload_create(&m_updir_list, m_pEntity->GetFunction().GetRawPointer(), spConfig.GetRawPointer(),checkStr);
  96. if (Error != Error_Succeed) {
  97. LOG_TRACE("load main config failed!");
  98. return Error;
  99. }else{
  100. Dbg("checkdirStr is %s",checkStr.GetData());
  101. char *p, *c;
  102. char checkType_str[1024];
  103. strcpy(checkType_str, (LPCSTR)checkStr);
  104. p = strtok_s(checkType_str, ", ", &c);
  105. while (p) {
  106. CSimpleStringA str = p;
  107. m_check_dir->push_back(str);
  108. Dbg("checkdir is %s",str.GetData());
  109. p = strtok_s(NULL, ", ", &c);
  110. }
  111. }
  112. }
  113. //初始化临界区
  114. InitializeCriticalSection(&m_cs);
  115. Error = m_pEntity->GetFunction()->RegistSysVarEvent("CallState", this);
  116. m_dBeginTime=0;
  117. m_dEndTime=0;
  118. m_iSpeed=0;
  119. //Error = Error_Succeed;
  120. if (Error == Error_Succeed) {
  121. AddStateHooker(this);
  122. }
  123. //......
  124. return Error;
  125. }
  126. ErrorCodeEnum UploadFSM::OnExit()
  127. {
  128. //释放临界区
  129. DeleteCriticalSection(&m_cs);
  130. m_uploadDateList->clear();
  131. m_pEntity->GetFunction()->UnregistSysVarEvent("CallState");
  132. return Error_Succeed;
  133. }
  134. void UploadFSM::s0_on_entry()
  135. {
  136. CSimpleStringA strValue;
  137. ErrorCodeEnum Error = m_pEntity->GetFunction()->GetSysVar("CallState", strValue);
  138. if (Error == Error_Succeed) {
  139. if (strValue[0] == 'O') { // only upload when CallState in offline state
  140. //PostEventLIFO(new FSMEvent(USER_EVT_JMP_ENABLE));
  141. PostEventLIFO(new FSMEvent(USER_EVT_JMP_DISABLE));
  142. LogWarn(Severity_High, Error_NotSupport, 0, "一轮测试期间不做任何上传操作!");
  143. } else {
  144. PostEventLIFO(new FSMEvent(USER_EVT_JMP_DISABLE));
  145. }
  146. }
  147. }
  148. void UploadFSM::s0_on_exit()
  149. {
  150. }
  151. unsigned int UploadFSM::s0_on_event( FSMEvent* event )
  152. {
  153. return 0;
  154. }
  155. void UploadFSM::s1_on_entry()
  156. {
  157. }
  158. void UploadFSM::s1_on_exit()
  159. {
  160. }
  161. unsigned int UploadFSM::s1_on_event(FSMEvent* event)
  162. {
  163. return 0;
  164. }
  165. void UploadFSM::s2_on_entry()
  166. {
  167. if (find_first_upload_file() == NULL) {
  168. upload_fresh(&m_updir_list);
  169. }
  170. if (find_first_upload_file() == NULL) {
  171. ScheduleTimer(1, 30000);
  172. } else {
  173. //把新增加的上传日期加入集合里面
  174. insertUploadDate();
  175. PostEventLIFO(new FSMEvent(USER_EVT_JMP_CONNECT));
  176. }
  177. }
  178. void UploadFSM::s2_on_exit()
  179. {
  180. CancelTimer(1);
  181. }
  182. unsigned int UploadFSM::s2_on_event(FSMEvent* event)
  183. {
  184. if (event->iEvt == EVT_TIMER) {
  185. if (find_first_upload_file() == NULL) {
  186. upload_fresh(&m_updir_list);
  187. }
  188. if (find_first_upload_file() == NULL) {
  189. ScheduleTimer(1, 10000);
  190. } else {
  191. //把新增加的上传日期加入集合里面
  192. insertUploadDate();
  193. PostEventLIFO(new FSMEvent(USER_EVT_JMP_CONNECT));
  194. }
  195. }
  196. return 0;
  197. }
  198. void UploadFSM::s3_on_entry()
  199. {
  200. if (!m_pConnection) {
  201. m_pConnection = new UploadConnection(m_pEntity, this);
  202. if(m_pConnection->ConnectFromCentralSetting() && m_pConnection->IsConnectionOK()){
  203. //创建成功
  204. }else{
  205. m_pConnection->Close();
  206. m_pConnection->DecRefCount();
  207. m_pConnection = NULL;
  208. }
  209. }
  210. if (!m_pConnection)
  211. {
  212. ScheduleTimer(2, 10000); // try 30 seconds later
  213. }
  214. else
  215. {
  216. PostEventLIFO(new FSMEvent(USER_EVT_JMP_UPLOAD));
  217. }
  218. }
  219. void UploadFSM::s3_on_exit()
  220. {
  221. CancelTimer(2);
  222. }
  223. unsigned int UploadFSM::s3_on_event(FSMEvent* event)
  224. {
  225. if (event->iEvt == EVT_TIMER) {
  226. m_pConnection = new UploadConnection(m_pEntity, this);
  227. if(m_pConnection->ConnectFromCentralSetting()&&m_pConnection->IsConnectionOK()){
  228. //创建成功
  229. }else{
  230. m_pConnection->Close();
  231. m_pConnection->DecRefCount();
  232. m_pConnection = NULL;
  233. }
  234. if (!m_pConnection)
  235. {
  236. ScheduleTimer(2, 30000); // try 30 seconds later
  237. }
  238. else {
  239. PostEventLIFO(new FSMEvent(USER_EVT_JMP_UPLOAD));
  240. }
  241. }
  242. return 0;
  243. }
  244. void UploadFSM::s4_on_entry()
  245. {
  246. m_uploading_file = find_first_upload_file();//获取要处理的文件
  247. #ifdef RVC_OS_WIN
  248. m_uploading_handle = INVALID_HANDLE_VALUE;//文件句柄
  249. #else
  250. m_uploading_handle = NULL;
  251. #endif // RVC_OS_WIN
  252. //开始统计时间
  253. m_dBeginTime=GetTickCount();//开始时间
  254. //Dbg("m_dBeginTime:%d",m_dBeginTime);
  255. m_pConnection->SendUpReq(m_uploading_file);//发送上传请求
  256. ScheduleTimer(4,30000);
  257. }
  258. void UploadFSM::s4_on_exit()
  259. {
  260. m_dBeginTime = 0;
  261. m_dEndTime = 0;
  262. m_iSpeed = 0;
  263. #ifdef RVC_OS_WIN
  264. if (m_uploading_handle != INVALID_HANDLE_VALUE) {
  265. CloseHandle(m_uploading_handle);
  266. m_uploading_handle = INVALID_HANDLE_VALUE;
  267. }
  268. #else
  269. if (m_uploading_handle != NULL) {
  270. fclose(m_uploading_handle);
  271. m_uploading_handle = NULL;
  272. }
  273. #endif // RVC_OS_WIN
  274. m_uploading_file = NULL;
  275. CancelTimer(4);
  276. }
  277. unsigned int UploadFSM::s4_on_event(FSMEvent* event)
  278. {
  279. CancelTimer(4);
  280. if (event->iEvt == USER_EVT_UPLOAD_ANS)
  281. { // .....
  282. UploadAnsEvent *ans = (UploadAnsEvent *)event;
  283. if (ans->m_reply.ResultCode == UploadCtlCode::Begin || ans->m_reply.ResultCode == UploadCtlCode::Continua)
  284. {
  285. m_uploading_block_id = ans->m_reply.BeginBlock;
  286. #ifdef RVC_OS_WIN
  287. m_uploading_handle = CreateFileA(m_uploading_file->path, GENERIC_READ,
  288. FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  289. if (m_uploading_handle == INVALID_HANDLE_VALUE)
  290. { // open file handle failed, we ignore this file
  291. //list_del(&m_uploading_file->entry);//从链表删除文件
  292. //upload_file_destroy(m_uploading_file);//销毁文件
  293. DWORD err = GetLastError();
  294. Dbg("USER_EVT_UPLOAD_ANS CreateFileA is error file name [%s] , GetLastError = %d", m_uploading_file->name, err);
  295. updir_del_file(m_uploading_file);//把文件从文件夹列表删除
  296. m_pConnection->Close();
  297. m_pConnection->DecRefCount();
  298. m_pConnection = NULL;
  299. return 1; // goto s5
  300. }
  301. else
  302. {
  303. memcpy(m_upload_id, ans->m_reply.UploadID, sizeof(m_upload_id));
  304. //当发送不成功,则把文件从列表删除,进行下一个文件传送
  305. if (!m_pConnection->SendBlockReq(m_uploading_file, m_uploading_handle, m_upload_id, m_uploading_block_id)) {
  306. updir_del_file(m_uploading_file);//把文件从文件夹列表删除
  307. m_pConnection->Close();
  308. m_pConnection->DecRefCount();
  309. m_pConnection = NULL;
  310. return 1; // goto s5
  311. }
  312. }
  313. #else
  314. m_uploading_handle = fopen(m_uploading_file->path, "rb");
  315. if (m_uploading_handle == NULL)
  316. {
  317. Dbg("USER_EVT_UPLOAD_ANS fopen is error file name [%s] ", m_uploading_file->name);
  318. updir_del_file(m_uploading_file);//把文件从文件夹列表删除
  319. m_pConnection->Close();
  320. m_pConnection->DecRefCount();
  321. m_pConnection = NULL;
  322. return 1; // goto s5
  323. }
  324. else
  325. {
  326. memcpy(m_upload_id, ans->m_reply.UploadID, sizeof(m_upload_id));
  327. //当发送不成功,则把文件从列表删除,进行下一个文件传送
  328. if (!m_pConnection->SendBlockReq(m_uploading_file, m_uploading_handle, m_upload_id, m_uploading_block_id)) {
  329. updir_del_file(m_uploading_file);//把文件从文件夹列表删除
  330. m_pConnection->Close();
  331. m_pConnection->DecRefCount();
  332. m_pConnection = NULL;
  333. return 1; // goto s5
  334. }
  335. }
  336. #endif // RVC_OS_WIN
  337. }
  338. else if (ans->m_reply.ResultCode == UploadCtlCode::Timeout)
  339. { //文件是否过期,不用再上传,现有配置是日志文件31天过期,受分行服务配置文件控制
  340. post_process();//后续本地处理此文件
  341. m_uploading_file = find_first_upload_file();//获取下一个文件
  342. if (m_uploading_file)
  343. {
  344. m_pConnection->SendUpReq(m_uploading_file);//发送上传文件指令
  345. }
  346. else
  347. {
  348. m_pConnection->Close();
  349. m_pConnection->DecRefCount();
  350. m_pConnection = NULL;
  351. return 1; // goto s5
  352. }
  353. }
  354. else
  355. {//(会有ErrorType错误)不做任务任何处理,仅仅把要处理的文件从列表移除
  356. //list_del(&m_uploading_file->entry);
  357. //upload_file_destroy(m_uploading_file);
  358. updir_del_file(m_uploading_file);//把文件从文件夹列表删除
  359. m_pConnection->Close();
  360. m_pConnection->DecRefCount();
  361. m_pConnection = NULL;
  362. return 1; // goto s5
  363. }
  364. }
  365. else if (event->iEvt == USER_EVT_BLOCK_ANS)
  366. {
  367. BlockAnsEvent *ans = (BlockAnsEvent*)event;
  368. if (ans->m_reply.ResultCode == UploadCtlCode::Continua)
  369. {
  370. m_dEndTime = GetTickCount();
  371. if(m_dEndTime-m_dBeginTime!=0){
  372. m_iSpeed = 1000*32/(m_dEndTime-m_dBeginTime);
  373. }else{
  374. }
  375. m_dBeginTime = GetTickCount();
  376. m_uploading_block_id++;
  377. //当发送不成功,则把文件从列表删除,进行下一个文件传送
  378. if(!m_pConnection->SendBlockReq(m_uploading_file, m_uploading_handle, m_upload_id, m_uploading_block_id)){
  379. updir_del_file(m_uploading_file);//把文件从文件夹列表删除
  380. m_pConnection->Close();
  381. m_pConnection->DecRefCount();
  382. m_pConnection = NULL;
  383. return 1; // goto s5
  384. }
  385. }
  386. else if (ans->m_reply.ResultCode == UploadCtlCode::ErrorBlock)
  387. { //不做任务任何处理,仅仅把要处理的文件从列表移除
  388. Dbg("rx error block reply, resend!");
  389. //m_uploading_block_id = ans->m_reply.
  390. //m_pConnection->SendBlockReq(m_uploading_file, m_uploading_handle, m_upload_id, m_uploading_block_id);
  391. //list_del(&m_uploading_file->entry);
  392. //upload_file_destroy(m_uploading_file);
  393. updir_del_file(m_uploading_file);//把文件从文件夹列表删除
  394. m_pConnection->Close();
  395. m_pConnection->DecRefCount();
  396. m_pConnection = NULL;
  397. return 1; // goto s5
  398. }
  399. else if (ans->m_reply.ResultCode == UploadCtlCode::Begin)
  400. { //重新传文件
  401. m_uploading_block_id = 0; // upload again
  402. if(!m_pConnection->SendBlockReq(m_uploading_file, m_uploading_handle, m_upload_id, m_uploading_block_id)){
  403. updir_del_file(m_uploading_file);//把文件从文件夹列表删除
  404. m_pConnection->Close();
  405. m_pConnection->DecRefCount();
  406. m_pConnection = NULL;
  407. return 1; // goto s5
  408. }
  409. }
  410. else if (ans->m_reply.ResultCode == UploadCtlCode::ErrorType)
  411. { // (不存在)server do not support this//不做任务任何处理,仅仅把要处理的文件从列表移除
  412. Dbg("server do not support this type!");
  413. //list_del(&m_uploading_file->entry);
  414. updir_del_file(m_uploading_file);//把文件从文件夹列表删除
  415. m_pConnection->Close();
  416. m_pConnection->DecRefCount();
  417. m_pConnection = NULL;
  418. //upload_file_destroy(m_uploading_file);
  419. //m_uploading_file = NULL;
  420. //CloseHandle(m_uploading_handle);
  421. //m_uploading_handle = INVALID_HANDLE_VALUE;
  422. return 1;
  423. }
  424. else if (ans->m_reply.ResultCode == UploadCtlCode::ErrorUploadId)
  425. { //(概率很小)不做任务任何处理,仅仅把要处理的文件从列表移除
  426. Dbg("server do not support this type!");
  427. //list_del(&m_uploading_file->entry);
  428. updir_del_file(m_uploading_file);//把文件从文件夹列表删除
  429. m_pConnection->Close();
  430. m_pConnection->DecRefCount();
  431. m_pConnection = NULL;
  432. //upload_file_destroy(m_uploading_file);
  433. //m_uploading_file = NULL;
  434. //CloseHandle(m_uploading_handle);
  435. //m_uploading_handle = INVALID_HANDLE_VALUE;
  436. return 1;
  437. }
  438. else if (ans->m_reply.ResultCode == UploadCtlCode::Finish)
  439. { //后续本地处理此文件
  440. m_dEndTime = GetTickCount();
  441. if(m_dEndTime-m_dBeginTime!=0){
  442. m_iSpeed = 1000*32/(m_dEndTime-m_dBeginTime);
  443. }else{
  444. }
  445. m_dBeginTime = GetTickCount();
  446. post_process();
  447. m_uploading_file = find_first_upload_file();
  448. if (m_uploading_file)
  449. {
  450. m_pConnection->SendUpReq(m_uploading_file);
  451. }
  452. else
  453. {
  454. m_pConnection->Close();
  455. m_pConnection->DecRefCount();
  456. m_pConnection = NULL;
  457. return 1; // goto s5
  458. }
  459. }
  460. else
  461. { //不做任务任何处理,仅仅把要处理的文件从列表移除
  462. Dbg("server do not support this file! ResultCode:%d", ans->m_reply.ResultCode);
  463. //list_del(&m_uploading_file->entry);
  464. updir_del_file(m_uploading_file);//把文件从文件夹列表删除
  465. m_pConnection->Close();
  466. m_pConnection->DecRefCount();
  467. m_pConnection = NULL;
  468. //upload_file_destroy(m_uploading_file);
  469. //m_uploading_file = NULL;
  470. //CloseHandle(m_uploading_handle);
  471. //m_uploading_handle = INVALID_HANDLE_VALUE;
  472. return 1; // goto s5
  473. }
  474. }
  475. else if (event->iEvt == USER_EVT_DISCONNECT)
  476. { // server disconnected
  477. m_pConnection->Close();
  478. m_pConnection->DecRefCount();
  479. m_pConnection = NULL;
  480. return 1; // goto s6
  481. }
  482. else if (event->iEvt == USER_EVT_JMP_DISABLE)
  483. { // aborted any way
  484. m_pConnection->Close();
  485. m_pConnection->DecRefCount();
  486. m_pConnection = NULL;
  487. }
  488. else if (event->iEvt == EVT_TIMER)
  489. {
  490. Dbg("SendUpReq timeout!!!");
  491. }
  492. return 0;
  493. }
  494. void UploadFSM::s5_on_entry()
  495. {
  496. ScheduleTimer(5, 500); // sleep for a short to collect OnClose and OnDisconnect event
  497. }
  498. void UploadFSM::s5_on_exit()
  499. {
  500. CancelTimer(5);
  501. }
  502. unsigned int UploadFSM::s5_on_event(FSMEvent* event)
  503. {
  504. if (event->iEvt == USER_EVT_DISCONNECT) {
  505. Dbg("rx disconnect evt at s5");
  506. // goto s2
  507. } else if (event->iEvt == EVT_TIMER) {
  508. // goto s2
  509. }
  510. return 0;
  511. }
  512. void UploadFSM::s6_on_entry()
  513. {
  514. ScheduleTimer(6, 10000);
  515. }
  516. void UploadFSM::s6_on_exit()
  517. {
  518. CancelTimer(6);
  519. }
  520. unsigned int UploadFSM::s6_on_event(FSMEvent* event)
  521. {
  522. if (event->iEvt == EVT_TIMER) {
  523. // goto s2
  524. Dbg("disconnect sleeping 10s");
  525. }
  526. return 0;
  527. }
  528. static ErrorCodeEnum ParseIPAddress( const char *str, CSimpleStringA &ip, int &port )
  529. {
  530. ErrorCodeEnum Error = Error_Unexpect;
  531. if (str) {
  532. char tmp1[32] = {};
  533. char tmp2[16] = {};
  534. sscanf(str, "%s %s", tmp1, tmp2);
  535. if (tmp1[0] != 0 && tmp2[0] != 0)
  536. {
  537. ip = tmp1;
  538. port = atoi(tmp2);
  539. Error = Error_Succeed;
  540. }
  541. else
  542. return Error;
  543. }
  544. return Error;
  545. }
  546. ErrorCodeEnum UploadFSM::LoadServerConfigFromCenterSetting()
  547. {
  548. CSmartPointer<IEntityFunction> spFunction = m_pEntity->GetFunction();
  549. CSmartPointer<IConfigInfo> spConfig;
  550. ErrorCodeEnum Error = spFunction->OpenConfig(Config_CenterSetting, spConfig);
  551. if (Error == Error_Succeed) {
  552. CSimpleStringA str;
  553. Error = spConfig->ReadConfigValue("Upload", "Server", str);
  554. if (Error == Error_Succeed)
  555. {
  556. Error = ParseIPAddress(str, m_server1, m_server1_port);
  557. }
  558. if (Error == Error_Succeed)
  559. {
  560. ErrorCodeEnum ErrorCode;
  561. ErrorCode = spConfig->ReadConfigValue("Upload", "Server_Backup", str);
  562. if (ErrorCode == Error_Succeed)
  563. {
  564. ErrorCode = ParseIPAddress(str, m_server2, m_server2_port);
  565. }
  566. if (ErrorCode!= Error_Succeed)
  567. {
  568. m_server2 = NULL;
  569. m_server2_port = 0;
  570. }
  571. }
  572. }
  573. return Error;
  574. }
  575. file_t * UploadFSM::find_first_upload_file()
  576. {
  577. upload_dir_t *pos;
  578. list_for_each_entry(pos, &m_updir_list, upload_dir_t, entry) {
  579. if (!list_empty(&pos->candidate_list)) {
  580. return list_first_entry(&pos->candidate_list, file_t, entry);
  581. }
  582. }
  583. return NULL;
  584. }
  585. void UploadFSM::post_process()
  586. {
  587. int flags = m_uploading_file->owner->flags;
  588. if (flags & UPLOAD_FLAG_AUTODELETE)
  589. {
  590. Dbg("delete file %s", m_uploading_file->name);
  591. #ifdef RVC_OS_WIN
  592. if (m_uploading_handle != INVALID_HANDLE_VALUE)
  593. {
  594. CloseHandle(m_uploading_handle);
  595. m_uploading_handle = INVALID_HANDLE_VALUE;
  596. }
  597. DWORD dwAttr = GetFileAttributesA(m_uploading_file->path);
  598. dwAttr &= ~FILE_ATTRIBUTE_READONLY;//去除只读属性
  599. SetFileAttributesA(m_uploading_file->path, dwAttr);
  600. if (DeleteFileA(m_uploading_file->path)) {
  601. Dbg("delete file is ok");
  602. }
  603. else {
  604. DWORD err = GetLastError();
  605. Dbg("delete file is error , GetLastError = %d", err);
  606. }
  607. #else
  608. if (m_uploading_handle != NULL) {
  609. fclose(m_uploading_handle);
  610. m_uploading_handle = NULL;
  611. }
  612. if (changeFileAtt(m_uploading_file->path)==0) {
  613. if (remove(m_uploading_file->path) == 0) {
  614. Dbg("delete file is ok");
  615. }
  616. else {
  617. Dbg("delete file is error,errno=%d , file=%s", errno, m_uploading_file->path);
  618. }
  619. }
  620. else {
  621. Dbg("delete file is error,changefileAtt fail ,errno=%d , file=%s ", errno,m_uploading_file->path);
  622. }
  623. #endif // RVC_OS_WIN
  624. }
  625. else if (flags & UPLOAD_FLAG_MOVEPATH)
  626. {
  627. char to_path[MAX_PATH];
  628. strcpy(to_path, m_uploading_file->owner->movepath);
  629. strcat(to_path, SPLIT_SLASH_STR);
  630. strcat(to_path, m_uploading_file->name);
  631. Dbg("move file %s to %s", m_uploading_file->path, to_path);
  632. #ifdef RVC_OS_WIN
  633. if (m_uploading_handle != INVALID_HANDLE_VALUE) {
  634. CloseHandle(m_uploading_handle);
  635. m_uploading_handle = INVALID_HANDLE_VALUE;
  636. }
  637. DWORD dwAttr = GetFileAttributesA(m_uploading_file->path);
  638. dwAttr &= ~FILE_ATTRIBUTE_READONLY;//一定要设置只读属性
  639. SetFileAttributesA(m_uploading_file->path, dwAttr);
  640. {
  641. int i;
  642. int tries = 10;
  643. for (i = 0; i < tries; ++i) {
  644. if (MoveFileExA(m_uploading_file->path, to_path, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) {
  645. Dbg("move ok! tries:%d", i);
  646. break;
  647. }
  648. else {
  649. Dbg("move %s -> %s failed! GetLastError:%d", m_uploading_file->path, to_path, GetLastError());
  650. Sleep(1000);
  651. }
  652. }
  653. }
  654. #else
  655. if (m_uploading_handle != NULL) {
  656. fclose(m_uploading_handle);
  657. m_uploading_handle = NULL;
  658. }
  659. if (changeFileAtt(m_uploading_file->path) == 0)
  660. {
  661. int i;
  662. int tries = 10;
  663. for (i = 0; i < tries; ++i) {
  664. if (rename(m_uploading_file->path, to_path) == 0) {
  665. Dbg("move ok! tries:%d", i);
  666. break;
  667. }
  668. else {
  669. Dbg("move %s -> %s failed! errno=%d", m_uploading_file->path, to_path,errno);
  670. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  671. }
  672. }
  673. }
  674. else
  675. {
  676. Dbg("move %s -> %s failed! , changefileAtt fail,errno=%d", m_uploading_file->path, to_path,errno);
  677. }
  678. #endif // RVC_OS_WIN
  679. }
  680. if (m_uploading_file)
  681. {
  682. //list_del(&m_uploading_file->entry);
  683. //upload_file_destroy(m_uploading_file);
  684. updir_del_file(m_uploading_file);//把文件从文件夹列表删除
  685. m_uploading_file = NULL;
  686. }
  687. #ifdef RVC_OS_WIN
  688. if (m_uploading_handle != INVALID_HANDLE_VALUE)
  689. {
  690. CloseHandle(m_uploading_handle);
  691. m_uploading_handle = INVALID_HANDLE_VALUE;
  692. }
  693. #else
  694. if (m_uploading_handle != NULL)
  695. {
  696. fclose(m_uploading_handle);
  697. m_uploading_handle = NULL;
  698. }
  699. #endif // RVC_OS_WIN
  700. }
  701. int UploadFSM::getCheckDirFile(int silentTime)
  702. {
  703. //把要检查的目录在要上传的目录里面查询,并搜索文件
  704. list<CSimpleStringA>::iterator iter;
  705. int sum =0;
  706. int count=0;
  707. for(iter=m_check_dir->begin();iter!=m_check_dir->end();++iter){
  708. CSimpleStringA checkstr= *iter;
  709. upload_dir_t *pos;
  710. list_for_each_entry(pos, &m_updir_list, upload_dir_t, entry)
  711. {
  712. if(strcmp(pos->name,checkstr.GetData())==0){
  713. int fileLen=0;//文件长度,长度是k
  714. count = check_dir_fresh(pos->path,pos->child_count_limitation,silentTime,fileLen);
  715. Dbg("交易文件夹:%s 还有未上传的文件个数 %d,文件总大小 %d k",pos->name,count,fileLen);
  716. sum +=count;
  717. }else{
  718. continue;;
  719. }
  720. }
  721. }
  722. return sum;
  723. }
  724. void UploadFSM::getUploadProgress(UploadProgress &progress)
  725. {
  726. int fileSumlen=0;
  727. if(m_iState==s0){
  728. progress.uploadState=0;
  729. progress.elapseTime="--:--:--";
  730. progress.uploadNumber=getUploadFileNum(fileSumlen);
  731. }else if(m_iState==s1){
  732. progress.uploadState=1;
  733. progress.elapseTime="--:--:--";
  734. progress.uploadNumber=getUploadFileNum(fileSumlen);
  735. }else if(m_iState==s2){
  736. progress.uploadState=2;
  737. progress.elapseTime="--:--:--";
  738. progress.uploadNumber=getUploadFileNum(fileSumlen);
  739. }else if(m_iState==s3){
  740. progress.uploadState=3;
  741. progress.elapseTime="--:--:--";
  742. progress.uploadNumber=getUploadFileNum(fileSumlen);
  743. }else if(m_iState==s4){
  744. progress.uploadState=4;
  745. progress.uploadNumber=getUploadFileNum(fileSumlen);
  746. //Dbg("总文件长度:%d",fileSumlen);
  747. if(m_iSpeed!=0){
  748. int loop = fileSumlen/m_iSpeed ;
  749. //Dbg("loop:%d",loop);
  750. CSimpleStringA hour=CSimpleStringA::Format("%d",loop/3600);
  751. //Dbg("hour:%s",hour);
  752. loop=loop%3600;
  753. CSimpleStringA ss = CSimpleStringA::Format("%02d",loop/60);
  754. //Dbg("ss:%s",ss);
  755. loop = loop%60;
  756. CSimpleStringA mm = CSimpleStringA::Format("%02d",loop);
  757. //Dbg("mm:%s",mm);
  758. CSimpleStringA strloop = hour+":"+ss+":"+mm;
  759. progress.elapseTime=strloop;//动态获取估计时间
  760. Dbg("总预估时间:%s",strloop.GetData());
  761. }else{
  762. progress.elapseTime="--:--:--";
  763. }
  764. }else if(m_iState==s5){
  765. progress.uploadState=5;
  766. progress.elapseTime="--:--:--";
  767. progress.uploadNumber=getUploadFileNum(fileSumlen);
  768. }else if(m_iState==s6){
  769. progress.uploadState=6;
  770. progress.elapseTime="--:--:--";
  771. progress.uploadNumber=getUploadFileNum(fileSumlen);
  772. }else{
  773. progress.uploadState=-1;
  774. progress.elapseTime="--:--:--";
  775. progress.uploadNumber=getUploadFileNum(fileSumlen);
  776. }
  777. }
  778. int UploadFSM::getUploadFileNum(int &fileSumlen)
  779. {
  780. int sum=0;
  781. upload_dir_t *pos;
  782. int count=0;
  783. list_for_each_entry(pos, &m_updir_list, upload_dir_t, entry)
  784. {
  785. int fileLen=0;//文件长度,长度是k
  786. count = check_dir_fresh(pos->path,pos->child_count_limitation,pos->silent_time,fileLen);
  787. if(count!=0){
  788. Dbg("文件夹:%s 未上传的文件个数 %d,文件大小 %d k",pos->name,count,fileLen);
  789. }
  790. sum += count;
  791. fileSumlen += fileLen;
  792. }
  793. Dbg("上传进度查询:未上传的文件总个数 %d,文件总大小 %d k",sum,fileSumlen);
  794. return sum;
  795. }
  796. bool UploadFSM::clearUploadDate()
  797. {
  798. bool ret = false;
  799. EnterCriticalSection(&m_cs);
  800. m_uploadDateList->clear();
  801. ErrorCodeEnum Error = SaveUploadDate();
  802. if(Error==Error_Succeed){
  803. ret = true;
  804. }else{
  805. ret = false;
  806. }
  807. LeaveCriticalSection(&m_cs);
  808. return ret;
  809. }
  810. ErrorCodeEnum UploadFSM::SaveUploadDate()
  811. {
  812. CSmartPointer<IEntityFunction> spFunction = m_pEntity->GetFunction();;
  813. CSmartPointer<IConfigInfo> spConfig;
  814. ErrorCodeEnum Error = spFunction->OpenConfig(Config_Run, spConfig);
  815. if (Error == Error_Succeed) {
  816. list<CSimpleStringA>::iterator iter;
  817. CSimpleStringA strDate="";
  818. for(iter=m_uploadDateList->begin();iter!=m_uploadDateList->end();++iter){
  819. CSimpleStringA date = (CSimpleStringA)*iter;
  820. strDate.Append(date);
  821. strDate.Append(",");
  822. }
  823. CSimpleStringA writeStr ="";
  824. if(strDate.GetLength()>0){
  825. writeStr = strDate.SubString(0,strDate.GetLength()-1);
  826. Dbg("prepare write uploadDate/dateList : %s",writeStr.GetData());
  827. Error = spConfig->WriteConfigValue("uploadDate", "dateList", writeStr.GetData());
  828. }else{
  829. Dbg("prepare write uploadDate/dateList : ");
  830. Error = spConfig->WriteConfigValue("uploadDate", "dateList", writeStr.GetData());
  831. }
  832. if(Error==Error_Succeed){
  833. Dbg("write uploadDate/dateList success: %s",writeStr.GetData());
  834. }else{
  835. Dbg("write uploadDate/dateList fail: %s error:%d",writeStr.GetData(),Error);
  836. }
  837. }else{
  838. Dbg("SaveUploadDate is fail , open runcfg/upload.ini fail!");
  839. }
  840. return Error;
  841. }
  842. ErrorCodeEnum UploadFSM::getUploadDate( CAutoArray<CSimpleStringA> &strList )
  843. {
  844. ErrorCodeEnum Error = Error_Succeed;
  845. EnterCriticalSection(&m_cs);
  846. //CSmartPointer<IEntityFunction> spFunction = m_pEntity->GetFunction();;
  847. //CSmartPointer<IConfigInfo> spConfig;
  848. //Error = spFunction->OpenConfig(Config_Run, spConfig);
  849. //if (Error == Error_Succeed) {
  850. // CSimpleStringA dateList ="";
  851. // Error = spConfig->ReadConfigValue("uploadDate", "dateList", dateList);
  852. // if(Error == Error_Succeed){
  853. // strList = dateList.Split(',');
  854. // }else{
  855. // Dbg("getUploadDate read upload.ini param uploadDate/dateList fail!");
  856. // }
  857. //}else{
  858. // Dbg("getUploadDate open runcfg upload.ini fail, please check out upload.ini!");
  859. //}
  860. //因运行时文件可能会有错误的日期,故从内存中读取
  861. strList.Init(m_uploadDateList->size());
  862. list<CSimpleStringA>::iterator iter;
  863. CSimpleStringA strDate="";
  864. int i=0;
  865. for(iter=m_uploadDateList->begin();iter!=m_uploadDateList->end();++iter){
  866. CSimpleStringA strDate = *iter;
  867. strList[i]=strDate;
  868. Dbg("getUploadDate strDate[%d]:%s",i,strDate.GetData());
  869. i++;
  870. }
  871. LeaveCriticalSection(&m_cs);
  872. return Error;
  873. }
  874. ErrorCodeEnum UploadFSM::insertUploadDate()
  875. {
  876. ErrorCodeEnum Error = Error_Succeed;
  877. EnterCriticalSection(&m_cs);
  878. list<CSimpleStringA>::iterator iter;
  879. for(iter=m_check_dir->begin();iter!=m_check_dir->end();++iter){
  880. CSimpleStringA checkstr= *iter;
  881. upload_dir_t *pos;
  882. list_for_each_entry(pos, &m_updir_list, upload_dir_t, entry)
  883. {
  884. if(strcmp(pos->name,checkstr.GetData())==0){
  885. if (!list_empty(&pos->candidate_list)) {
  886. file_t *fpos;
  887. list_for_each_entry(fpos,&pos->candidate_list,file_t,entry){
  888. #ifdef RVC_OS_WIN
  889. WIN32_FILE_ATTRIBUTE_DATA attr;
  890. FILETIME fttmp;
  891. SYSTEMTIME filest;
  892. if (GetFileAttributesExA(fpos->path, GetFileExInfoStandard, &attr))
  893. {
  894. FileTimeToLocalFileTime(&attr.ftLastWriteTime, &fttmp);
  895. FileTimeToSystemTime(&fttmp, &filest);
  896. CSimpleStringA strDate = CSimpleStringA::Format("%4.4d-%2.2d-%2.2d", filest.wYear, filest.wMonth, filest.wDay);
  897. if (!uploadDate_exist(strDate))
  898. {
  899. Dbg("insertUploadDate %s:%s", fpos->path, strDate.GetData());
  900. m_uploadDateList->push_back(strDate);
  901. if (SaveUploadDate() == Error_Succeed) {
  902. Dbg("insertUploadDate is success! %s:%s", fpos->path, strDate.GetData());
  903. }
  904. else {
  905. Dbg("insertUploadDate is fail%s:%s", fpos->path, strDate.GetData());
  906. //因文件写入失败,删除当前的日期,后续还有文件上传会继续添加进来
  907. m_uploadDateList->pop_back();
  908. }
  909. }
  910. }
  911. #else
  912. struct stat attr_of_src;
  913. if (lstat(fpos->path, &attr_of_src) == 0) {
  914. struct tm flocalTime = { 0 };
  915. localtime_r(&attr_of_src.st_mtime,&flocalTime);
  916. CSimpleStringA strDate = CSimpleStringA::Format("%4.4d-%2.2d-%2.2d", (1900 + flocalTime.tm_year), (1 + flocalTime.tm_mon), flocalTime.tm_mday);
  917. if (!uploadDate_exist(strDate))
  918. {
  919. Dbg("insertUploadDate %s:%s", fpos->path, strDate.GetData());
  920. m_uploadDateList->push_back(strDate);
  921. if (SaveUploadDate() == Error_Succeed) {
  922. Dbg("insertUploadDate is success! %s:%s", fpos->path, strDate.GetData());
  923. }
  924. else {
  925. Dbg("insertUploadDate is fail%s:%s", fpos->path, strDate.GetData());
  926. //因文件写入失败,删除当前的日期,后续还有文件上传会继续添加进来
  927. m_uploadDateList->pop_back();
  928. }
  929. }
  930. }
  931. else {
  932. Dbg("get file attr fail,file=%s", fpos->path);
  933. }
  934. #endif // RVC_OS_WIN
  935. }
  936. }
  937. }
  938. }
  939. }
  940. LeaveCriticalSection(&m_cs);
  941. return Error;
  942. }
  943. bool UploadFSM::uploadDate_exist( CSimpleStringA uploadDate )
  944. {
  945. bool ret = false;
  946. list<CSimpleStringA>::iterator iter;
  947. CSimpleStringA strDate="";
  948. for(iter=m_uploadDateList->begin();iter!=m_uploadDateList->end();++iter){
  949. if(*iter==uploadDate){
  950. ret=true;
  951. break;
  952. }
  953. }
  954. return ret;
  955. }