CenterSettingConn.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. #include "stdafx.h"
  2. #include "CenterSettingConn.h"
  3. #include "Event.h"
  4. #include <map>
  5. #include <regex>
  6. using namespace std;
  7. using std::regex;
  8. #include "mod_centersetting.h"
  9. #include "fileutil.h"
  10. #include "iniutil.h"
  11. #if defined(RVC_OS_WIN)
  12. #pragma comment(lib, "crypt32.lib")
  13. #endif //RVC_OS_WIN
  14. #ifdef WITH_CPPRESTSDK
  15. #include "CommEntityRestful.hpp"
  16. #else
  17. #define SAFE_DELETE_HTTPCLIENT(obj) \
  18. do{if(obj != NULL) { obj->Destory(); obj = NULL; }}while(false)
  19. #endif
  20. bool CenterSettingResponse::Parse(string strData)
  21. {
  22. bSuccess = false;
  23. errInfo.clear();
  24. Json::Value root;
  25. Json::Reader reader;
  26. if (!reader.parse(strData, root, false)) {
  27. errInfo = "parse json string failed: ";
  28. errInfo += strData;
  29. return false;
  30. }
  31. struct CommResponseJson : public MicroServices::API::CommResponse {
  32. JSONCONVERT2OBJECT_MEMEBER_REGISTER(success, errorCode, returnCode, errorMsg, message)
  33. JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER("success", "code", "return_code", "error_msg", "message")
  34. } reponseStatus;
  35. if (!Json2Object(reponseStatus, root)) {
  36. errInfo = "Json2Object failed";
  37. return false;
  38. }
  39. if (!reponseStatus.IsOperatedOK()) {
  40. errInfo = reponseStatus.WhatError();
  41. return false;
  42. }
  43. bSuccess = Json2Object(centerSyncInfo, root["data"]);
  44. if (bSuccess) {
  45. centerSyncInfo.biz.mapSiteName = "LAN";
  46. centerSyncInfo.dmz.mapSiteName = "DMZ";
  47. } else {
  48. errInfo = "parse json data node failed: ";
  49. }
  50. return bSuccess;
  51. }
  52. ////////////////////////////////////////////////////////////////////////////////////
  53. CenterSettingsMicroServiceHelper::CenterSettingsMicroServiceHelper(CEntityBase* pEntity)
  54. :m_pEntity(pEntity), m_bGrayLaunch(false),m_strGrayLaunchUrl(true)
  55. ,m_strCallRouteBranchNo(true),m_strLastTime(true)
  56. ,m_strTerminalList(true),m_strMode(true)
  57. {
  58. m_pEntity->GetFunction()->GetSystemStaticInfo(mTerminalInfo);
  59. InitCenterSettingsInfoFromConfig();
  60. }
  61. void CenterSettingsMicroServiceHelper::InitCenterSettingsInfoFromConfig()
  62. {
  63. LOG_FUNCTION();
  64. CSmartPointer<IConfigInfo> centerConfig;
  65. m_pEntity->GetFunction()->OpenConfig(Config_CenterSetting, centerConfig);
  66. centerConfig->ReadConfigValue("Common", "GrayLaunchUrl", m_strGrayLaunchUrl);
  67. centerConfig->ReadConfigValue("Initializer", "SubBankNo", m_strCallRouteBranchNo);
  68. centerConfig->ReadConfigValue("CenterSetting", "CenterConfigUrl", m_strCenterSettingUrl);
  69. centerConfig->ReadConfigValue("CenterSetting", "LastUpdateTime", m_strLastTime);
  70. centerConfig->ReadConfigValue("CenterSetting", "Mode", m_strMode);
  71. centerConfig->ReadConfigValue("CenterSetting", "TerminalList", m_strTerminalList);
  72. }
  73. ErrorCodeEnum CenterSettingsMicroServiceHelper::GetCenterSetting(CSimpleStringA& strErrMsg)
  74. {
  75. LOG_FUNCTION();
  76. #ifdef WITH_CPPRESTSDK
  77. struct GetCenterSettingsReqJson
  78. {
  79. std::string terminalNo;
  80. std::string lastUpdateTimie;
  81. JSONCONVERT2OBJECT_MEMEBER_REGISTER(terminalNo, lastUpdateTimie)
  82. JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER("terminal_no", "last_update_time")
  83. } getCenterSettingsReqJson;
  84. HttpClientResponseResult result;
  85. HttpClientRequestConfig config(HttpRequestMethod::POST, m_strCenterSettingUrl.GetData());
  86. getCenterSettingsReqJson.lastUpdateTimie = m_strLastTime;
  87. getCenterSettingsReqJson.terminalNo = mTerminalInfo.strTerminalID;
  88. SP::Module::Restful::FulfillRequestJsonBody(&config, getCenterSettingsReqJson);
  89. RestfulClient client = RestfulClient::getInstance();
  90. config.PreDo();
  91. client.Do(&config, &result);
  92. if (result.ResponseOK()) {
  93. SP::Module::Restful::CommResponseJson responseStatus;
  94. SP::Module::Restful::GetStatusFromDebranchResponse(result.content, responseStatus);
  95. if (!responseStatus.IsOperatedOK()) {
  96. strErrMsg = responseStatus.WhatError().c_str();
  97. } else {
  98. CenterSettingFileJson getCenterSettingsAns;
  99. if (!SP::Module::Restful::ExtractDataFromDebranchResponse(result.content, getCenterSettingsAns)) {
  100. strErrMsg = "parse json data node failed: ";
  101. } else {
  102. getCenterSettingsAns.biz.mapSiteName = "LAN";
  103. getCenterSettingsAns.dmz.mapSiteName = "DMZ";
  104. if ((mTerminalInfo.strSite.Compare("cmb.LIB", true) == 0 && getCenterSettingsAns.biz.lastUpdateTime == getCenterSettingsReqJson.lastUpdateTimie)
  105. || (mTerminalInfo.strSite.Compare("cmb.FLB", true) == 0 && getCenterSettingsAns.dmz.lastUpdateTime == getCenterSettingsReqJson.lastUpdateTimie)) {//行内
  106. Dbg("Centersetting not changed.");
  107. strErrMsg = "Centersetting not any changed";
  108. return Error_Succeed;
  109. }
  110. CenterSettingConfigJson& cur = getCenterSettingsAns.dmz;
  111. if (mTerminalInfo.strSite.Compare("cmb.LIB", true) == 0) {
  112. cur = getCenterSettingsAns.biz;
  113. }
  114. Dbg("local last update time(%s) vs server last update time(%s)", getCenterSettingsReqJson.lastUpdateTimie.c_str(), cur.lastUpdateTime.c_str());
  115. SyncConfigAndRenameCenterFile(cur);
  116. LogEvent(Severity_Middle, EVENT_MOD_CENTERSETTING_UPDATE,
  117. CSimpleString::Format("Update CenterSetting.%s.ini.", cur.mapSiteName.c_str()));
  118. strErrMsg = "Centersetting has changed";
  119. return Error_Succeed;
  120. }
  121. }
  122. } else {
  123. strErrMsg = result.WhatError().c_str();
  124. }
  125. #else
  126. CenterSettingReq req;
  127. req.m_url = m_strCenterSettingUrl;
  128. req.m_last_update_time = m_strLastTime;
  129. req.m_terminal_no = mTerminalInfo.strTerminalID;
  130. Dbg("request url: %s", req.m_url.c_str());
  131. CenterSettingResponse response;
  132. IHttpFunc* pHttpFunc = create_http(HTTPLogCallback);
  133. bool ret = pHttpFunc->Post(req, response);
  134. pHttpFunc->Destory();
  135. if (ret) {
  136. if (response.bSuccess) {
  137. if ((mTerminalInfo.strSite.Compare("cmb.LIB", true) == 0 && response.centerSyncInfo.biz.lastUpdateTime == req.m_last_update_time)
  138. || (mTerminalInfo.strSite.Compare("cmb.FLB", true) == 0 && response.centerSyncInfo.dmz.lastUpdateTime == req.m_last_update_time)) {//行内
  139. Dbg("Centersetting not changed.");
  140. strErrMsg = "Centersetting not any changed";
  141. return Error_Succeed;
  142. }
  143. CenterSettingConfigJson& cur = response.centerSyncInfo.dmz;
  144. if (mTerminalInfo.strSite.Compare("cmb.LIB", true) == 0) {
  145. cur = response.centerSyncInfo.biz;
  146. }
  147. Dbg("local last update time(%s) vs server last update time(%s)", cur.lastUpdateTime.c_str(), req.m_last_update_time.c_str());
  148. SyncConfigAndRenameCenterFile(cur);
  149. LogEvent(Severity_Middle, EVENT_MOD_CENTERSETTING_UPDATE,
  150. CSimpleString::Format("Update CenterSetting.%s.ini.", cur.mapSiteName.c_str()));
  151. strErrMsg = "Centersetting has changed";
  152. return Error_Succeed;
  153. } else {
  154. strErrMsg = response.errInfo.c_str();
  155. }
  156. } else {
  157. strErrMsg = CSimpleStringA::Format("post [%s] failed!", req.m_url.c_str());
  158. }
  159. #endif //WITH_CPPRESTSDK
  160. return Error_Unexpect;
  161. }
  162. void CenterSettingsMicroServiceHelper::SyncConfigAndRenameCenterFile(const CenterSettingConfigJson& configOfOneBranch)
  163. {
  164. LOG_FUNCTION();
  165. auto pFunc = m_pEntity->GetFunction();
  166. CSimpleStringA strPath;
  167. pFunc->GetPath("Cfg", strPath);
  168. ///**TODO(Gifur@9/16/2021): GetPath("CenterSetting") 通过此接口来获取集中配置路径地址 */
  169. CSimpleStringA fileName = CSimpleStringA::Format("CenterSetting.%s.ini", configOfOneBranch.mapSiteName.c_str());
  170. Dbg("fileName:%s", fileName.GetData());
  171. CSimpleStringA strCurFile = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (const char*)strPath, (const char*)fileName);
  172. CSimpleString strRunInfoPath;
  173. ///**TODO(Gifur@9/16/2021): GetPath("RunCfg") implement! */
  174. pFunc->GetPath("RunInfo", strRunInfoPath);
  175. strRunInfoPath += SPLIT_SLASH_STR;
  176. strRunInfoPath += "runcfg";
  177. CSimpleStringA strNewPath = strRunInfoPath + SPLIT_SLASH_STR;
  178. CSimpleStringA strNewFile = strNewPath + fileName + ".new";
  179. for (int i = 0; i < configOfOneBranch.config.size(); i++) {
  180. const string& module = configOfOneBranch.config[i].moudle;
  181. const string& name = configOfOneBranch.config[i].name;
  182. const string& value = configOfOneBranch.config[i].value;
  183. inifile_write_str(strNewFile, module.c_str(), name.c_str(), value.c_str());
  184. }
  185. inifile_write_str(strNewFile, "CenterSetting", "ProvideByHeadBank", "1");
  186. //将服务的修改时间同步到本地,避免每次都去更新
  187. inifile_write_str(strNewFile, "CenterSetting", "LastUpdateTime", configOfOneBranch.lastUpdateTime.c_str());
  188. // 替换当前配置文件
  189. fileutil_copy_file(strCurFile, strNewFile);
  190. fileutil_delete_file(strNewFile);
  191. Dbg("save config %s succ", fileName.GetData());
  192. }
  193. /////////////////////////////////////////////////////////////////////////
  194. CCenterSettingConn::CCenterSettingConn(CEntityBase *pEntity)
  195. : SpSecureClient(pEntity)
  196. {
  197. }
  198. CCenterSettingConn::~CCenterSettingConn(void)
  199. {
  200. }
  201. void CCenterSettingConn::OnDisconnect()
  202. {
  203. Dbg("connection disconnected");
  204. }
  205. void CCenterSettingConn::OnPkgAnswer(const CSmartPointer<IPackage> &pRecvPkg)
  206. {
  207. string serviceCode = pRecvPkg->GetServiceCode();
  208. auto pEntity = (CCenterSettingEntity*) m_pEntity;
  209. if (serviceCode == "PollSM")
  210. {
  211. auto rc = OnEndPollConfig(pRecvPkg);
  212. if (rc == Error_NotExist) {
  213. CSimpleStringA strFilePath;
  214. pEntity->GetFunction()->GetPath("CenterSetting", strFilePath);
  215. if (ExistsFileA(strFilePath) && 0 < pEntity->m_SyncFileInfo.size()) {
  216. Dbg("maybe local centersetting file is new as the server side one or there are no any centersettings files under server side");
  217. rc = Error_Succeed; // = Error_AlreadyExist;
  218. }
  219. }
  220. // 通知实体轮询结束
  221. pEntity->EndDownloadCenterSetting(rc);
  222. }
  223. else
  224. {
  225. LOG_TRACE("unknown service code: %s", serviceCode.c_str());
  226. }
  227. }
  228. bool CCenterSettingConn::ParseIPAddress(const char *str, CSimpleStringA &ip, int &port)
  229. {
  230. if (str)
  231. {
  232. char tmp1[32] = {};
  233. char tmp2[16] = {};
  234. sscanf(str, "%s %s", tmp1, tmp2);
  235. if (tmp1[0] != 0 && tmp2[0] != 0)
  236. {
  237. ip = tmp1;
  238. port = atoi(tmp2);
  239. return true;
  240. }
  241. }
  242. return false;
  243. }
  244. bool CCenterSettingConn::IsServiceAvailable(const char *pszServiceAddr)
  245. {
  246. CSimpleStringA strServer;
  247. int nPort;
  248. if (! ParseIPAddress(pszServiceAddr, strServer, nPort))
  249. return false;
  250. SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  251. if (s == INVALID_SOCKET)
  252. return false;
  253. sockaddr_in addr = {0};
  254. addr.sin_family = AF_INET;
  255. addr.sin_addr.s_addr = inet_addr(strServer);
  256. addr.sin_port = htons(nPort);
  257. bool bRet = (connect(s, (SOCKADDR*)&addr, sizeof(addr)) != SOCKET_ERROR);
  258. closesocket(s);
  259. return bRet;
  260. }
  261. // 上送多个请求结构,对应多个配置
  262. ErrorCodeEnum CCenterSettingConn::BeginPollConfig(const CSystemStaticInfo& info)
  263. {
  264. assert(IsConnectionOK());
  265. auto pEntity = (CCenterSettingEntity *)m_pEntity;
  266. auto pFunc = pEntity->GetFunction();
  267. CSimpleStringA strPath;
  268. pFunc->GetPath("Cfg", strPath);
  269. ClearillegalCenterSettingsFile(strPath);
  270. auto pPackage = CreateNewPackage("PollSM");
  271. const int nArrayNum = pEntity->m_SyncFileInfo.size();
  272. if (nArrayNum> 0)
  273. {
  274. PollSMR *pReq = new PollSMR[nArrayNum];
  275. memset(pReq, 0, sizeof(PollSMR)*nArrayNum);
  276. int nIndex = 0;
  277. for (auto it = pEntity->m_SyncFileInfo.begin(); it != pEntity->m_SyncFileInfo.end(); it++)
  278. {
  279. auto &fileInfo = it->second;
  280. #ifdef RVC_OS_WIN
  281. strncpy_s(pReq[nIndex].TerminalNo, sizeof(pReq[nIndex].TerminalNo), info.strTerminalID, _TRUNCATE);
  282. strncpy_s(pReq[nIndex].MachineType, sizeof(pReq[nIndex].MachineType), info.strMachineType, _TRUNCATE);
  283. strncpy_s(pReq[nIndex].Sites, sizeof(pReq[nIndex].Sites), it->first.c_str(), _TRUNCATE);
  284. strncpy_s(pReq[nIndex].LastHash, sizeof(pReq[nIndex].LastHash), fileInfo.strFileHash, _TRUNCATE);
  285. #else
  286. strncpy(pReq[nIndex].TerminalNo, info.strTerminalID, strlen(info.strTerminalID));
  287. strncpy(pReq[nIndex].MachineType, info.strMachineType, strlen(info.strMachineType));
  288. strncpy(pReq[nIndex].Sites, it->first.c_str(), it->first.length());
  289. strncpy(pReq[nIndex].LastHash, fileInfo.strFileHash, strlen(fileInfo.strFileHash));
  290. #endif
  291. nIndex++;
  292. }
  293. pPackage->AddStruct("PollSMR", false, false, (BYTE*) pReq, sizeof(PollSMR)*nArrayNum, nArrayNum);
  294. delete[] pReq;
  295. }
  296. else
  297. {
  298. PollSMR req = {};
  299. #ifdef RVC_OS_WIN
  300. strncpy_s(req.TerminalNo, sizeof(req.TerminalNo), info.strTerminalID, _TRUNCATE);
  301. strncpy_s(req.MachineType, sizeof(req.MachineType), info.strMachineType, _TRUNCATE);
  302. strncpy_s(req.Sites, sizeof(req.Sites), info.strSite, _TRUNCATE);
  303. #else
  304. strncpy(req.TerminalNo, info.strTerminalID, strlen(info.strTerminalID));
  305. strncpy(req.MachineType, info.strMachineType, strlen(info.strMachineType));
  306. strncpy(req.Sites, info.strSite, strlen(info.strSite));
  307. #endif
  308. pPackage->AddStruct("PollSMR", false, false, (BYTE*)&req, sizeof(PollSMR));
  309. }
  310. SendPackage(pPackage);
  311. return Error_Succeed;
  312. }
  313. ErrorCodeEnum CCenterSettingConn::OnEndPollConfig(const CSmartPointer<IPackage> &pRecvPkg)
  314. {
  315. auto pEntity = (CCenterSettingEntity*)m_pEntity;
  316. DWORD dwSysCode, dwUserCode;
  317. string strErrMsg;
  318. bool bInitMKSuc = false;
  319. if (pRecvPkg->GetErrMsg(dwSysCode, dwUserCode, strErrMsg))
  320. {
  321. Dbg("poll center config fail, sysCode=%d, usrCode=%d, %s", dwSysCode, dwUserCode, strErrMsg.c_str());
  322. return Error_Unexpect;
  323. }
  324. int nIndex = 1;
  325. while (true)
  326. {
  327. //1.获取包条目结构和内容
  328. CSimpleStringA strCurStructName = CSimpleStringA::Format("PollSMA%d", nIndex);
  329. int nRetLen = pRecvPkg->GetStructLen(strCurStructName);
  330. if (nRetLen == -1) {
  331. Dbg("Get %s pkg struct failed !", strCurStructName.GetData());
  332. break;
  333. }
  334. nIndex++;
  335. BYTE *pBuf = new BYTE[nRetLen];
  336. memset(pBuf, 0, nRetLen);
  337. int nArrayNum(0);
  338. int nBufLen = nRetLen;
  339. bool bSuc = pRecvPkg->GetStructData(strCurStructName, pBuf, &nBufLen, &nArrayNum);
  340. assert(bSuc && nRetLen == nBufLen);
  341. PollSMA*pRet = (PollSMA*)pBuf;
  342. Dbg("center config changed, hash: %s, file: %s", (const char*)pRet->FileHash, (const char*)pRet->FileName);
  343. // 2. 校验Hash是否匹配,以校验内容完整性
  344. BYTE sm3[32] = {};
  345. if (!SM3Hash((BYTE*)pRet->FileContent, nRetLen - sizeof(PollSMA), sm3)) {
  346. Dbg("compute sm3 hash fail");
  347. delete[] pBuf;
  348. return Error_CheckSum;
  349. }
  350. CSimpleStringA strHash;
  351. for (int i = 0; i < 32; i++) {
  352. strHash += CSimpleStringA::Format("%02x", sm3[i]);
  353. }
  354. if (stricmp(strHash, pRet->FileHash) != 0) {
  355. Dbg("computed hash [%s] != return hash [%s]", (const char*)strHash, pRet->FileHash);
  356. delete[] pBuf;
  357. return Error_CheckSum;
  358. }
  359. auto pFunc = m_pEntity->GetFunction();
  360. //3. 将结构内容信息存在在临时的集中配置文件中
  361. CSimpleString strRunInfoPath;
  362. ///**TODO(Gifur@9/16/2021): GetPath("RunCfg") implement! */
  363. pFunc->GetPath("RunInfo", strRunInfoPath);
  364. strRunInfoPath += SPLIT_SLASH_STR;
  365. strRunInfoPath += "runcfg";
  366. CSimpleStringA strNewPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (const char*)strRunInfoPath, (const char*)pRet->FileName);
  367. // 保存新配置文件
  368. CSimpleStringA strNewFile = strNewPath + ".new";
  369. auto hFile = fopen(strNewFile, "wb");
  370. int nLen = fwrite(pRet->FileContent, 1, nRetLen - sizeof(PollSMA), hFile);
  371. fflush(hFile);
  372. fclose(hFile);
  373. if (nLen < nRetLen - sizeof(PollSMA)) {
  374. Dbg("save %s fail, fwrite len: %d, act len: %d", pRet->FileName, nLen, nRetLen - sizeof(PollSMA));
  375. fileutil_delete_file(strNewFile);
  376. delete[] pBuf;
  377. return Error_IO;
  378. }
  379. // 4. 检测新集中配置地址是否可用
  380. char tmp[32] = {};
  381. if(inifile_read_str_s("CenterSetting", "Server", "", tmp, 32, strNewFile) < 10)
  382. {
  383. Dbg("read center setting service addr from new file %s fail", (const char*)strNewFile);
  384. fileutil_delete_file(strNewFile);
  385. delete[] pBuf;
  386. return Error_NotConfig;
  387. }
  388. else if (!IsServiceAvailable(tmp)) {
  389. Dbg("the new centersetting server ip[%s] cannot be connectable!!!", tmp);
  390. }
  391. // 5. 读出当前集中配置服务地址并写入到新的集中配置文件中
  392. CSimpleStringA strPath;
  393. pFunc->GetPath("Cfg", strPath);
  394. CSimpleStringA strCurFile = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (const char*)strPath, (const char*)pRet->FileName);
  395. memset(tmp, 0, 32);
  396. if (10 > inifile_read_str_s("CenterSetting", "Server", "", tmp, 32, strCurFile)) {
  397. Dbg("read center setting service addr from exist file %s fail: (%d)", strCurFile.GetData(), GetLastError());
  398. }
  399. // 写入同步信息
  400. inifile_write_str(strNewFile, "Main", "LastServer", tmp);
  401. DWORD dwSyncTime = CSmallDateTime::GetNow();
  402. memset(tmp, 0, sizeof(tmp));
  403. sprintf_s(tmp, sizeof(tmp), "0x%X", dwSyncTime);
  404. inifile_write_str(strNewFile, "Main", "SyncTime", tmp);
  405. inifile_write_str(strNewFile, "Main", "SyncHash", pRet->FileHash);
  406. inifile_write_str(strNewFile, "Main", "SyncFile", pRet->FileName);
  407. // 6. 备份原配置文件
  408. #if defined(RVC_OS_WIN)
  409. SYSTEMTIME st = {};
  410. GetLocalTime(&st);
  411. CSimpleStringA strBackFile = CSimpleStringA::Format("%s.%04d%02d%02d%02d%02d", (const char*)strCurFile,
  412. st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute);
  413. #else
  414. time_t t = time(NULL);
  415. tm* t1 = localtime(&t);
  416. CSimpleStringA strBackFile = CSimpleStringA::Format("%s.%04d%02d%02d%02d%02d", (const char*)strCurFile,
  417. t1->tm_year + 1900, t1->tm_mon + 1, t1->tm_mday, t1->tm_hour, t1->tm_min);
  418. #endif //RVC_OS_WIN
  419. fileutil_copy_file(strBackFile, strCurFile);
  420. // 7.替换当前配置文件,并删除临时文件
  421. fileutil_copy_file(strCurFile, strNewFile);
  422. fileutil_delete_file(strNewFile);
  423. Dbg("save config %s succ", (const char*)pRet->FileName);
  424. // 8. 更新内存中的同步信息
  425. CCenterSettingEntity::ConfigFileInfo info;
  426. info.dwSyncTime = dwSyncTime;
  427. info.strFileHash = pRet->FileHash;
  428. info.strFileName = pRet->FileName;
  429. CSimpleStringA str1, str2;
  430. if (pEntity->TryExtractSiteFromFileName(info.strFileName, str1, str2)) {
  431. pEntity->m_SyncFileInfo[(const char*)str2] = info;
  432. }
  433. delete[] pBuf;
  434. pBuf = NULL;
  435. } // end while
  436. if (nIndex > 1)
  437. {
  438. LogEvent(Severity_Middle, EVENT_MOD_CENTERSETTING_DOWNOK, "同步集中配置文件成功!");
  439. // 检查RvcWebVersion是否有变化
  440. CSimpleStringA strFilePath;
  441. pEntity->GetFunction()->GetPath("CenterSetting", strFilePath);
  442. // 缓存当前RVCWeb版本
  443. char szVersion[64] = {};
  444. inifile_read_str_s("IEBrowser", "RvcWebVersion", "", szVersion, 64, strFilePath);
  445. if (pEntity->m_strRvcWebVersion.IsNullOrEmpty() || strcmp(szVersion, pEntity->m_strRvcWebVersion) != 0)
  446. {
  447. Dbg("rvc web version changed, last: [%s], current: [%s]", (const char*)pEntity->m_strRvcWebVersion, szVersion);
  448. pEntity->m_strRvcWebVersion = szVersion;
  449. LogEvent(Severity_Middle, EVENT_MOD_CENTERSETTING_RVCWEBVER_CHANGE, "RVCWeb version has updated !");
  450. }
  451. return Error_Succeed;
  452. } else {
  453. return Error_NotExist;
  454. }
  455. }
  456. bool CCenterSettingConn::ClearillegalCenterSettingsFile(CSimpleStringA strCfgDirPath)
  457. {
  458. LOG_FUNCTION();
  459. array_header_t* arr = fileutil_get_sub_files_a(strCfgDirPath);
  460. if (arr) {
  461. for (int i = 0; i < arr->nelts; ++i) {
  462. char* path = (char*)ARRAY_IDX(arr, i, char*);
  463. regex reg("((DMZ)|(LAN))[^(\.ini)]");
  464. if (regex_search(path, reg)) {
  465. Dbg("delete: %s", path);
  466. fileutil_delete_file(path);
  467. }
  468. }
  469. toolkit_array_free2(arr);
  470. }
  471. return true;
  472. }