UploadFSM.cpp 28 KB

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