UploadFSM.cpp 29 KB

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