mod_centersetting.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "Event.h"
  4. #include "mod_centersetting.h"
  5. #include "publicFunExport.h"
  6. #include "array.h"
  7. #include "fileutil.h"
  8. #include "iniutil.h"
  9. #include "array.h"
  10. #include "EventCode.h"
  11. #include "SpIni.h"
  12. #include "SpUtility.h"
  13. #include "CommEntitySettings.hpp"
  14. #include "CommEntityHelpAgge.hpp"
  15. #include <regex>
  16. #include "microservices/api_comm.h"
  17. using namespace std;
  18. #define DOWNLOAD_CENTERSETTINGS_TIMER_ID 1
  19. #define DOWNLOAD_CENTERSETTINGS_TIMER_INTERVAL (5 * 60 * 1000)
  20. CServerSessionBase *CCenterSettingEntity::OnNewSession(const char* /*pszRemoteEntityName*/, const char * /*pszClass*/)
  21. {
  22. return new CCenterSettingService(this);
  23. }
  24. void CCenterSettingEntity::OnPreStart(CAutoArray<CSimpleStringA> strArgs, CSmartPointer<ITransactionContext> pTransactionContext)
  25. {
  26. ErrorCodeEnum result(Error_Succeed);
  27. if (!SP::Module::Comm::Settings::IsInConfigScheduleMode(this)) {
  28. CSimpleStringA strFilePath;
  29. GetFunction()->GetPath("CenterSetting", strFilePath);
  30. if (!ExistsFileA(strFilePath)) {
  31. std::map<std::string, std::string> srcData;
  32. srcData.insert(std::make_pair("reason", "empty"));
  33. srcData.insert(std::make_pair("errmsg", "集中配置文件不存在,请先下载集中配置再重启应用!"));
  34. srcData.insert(std::make_pair("rebootTime", ""));
  35. auto ret = generateJsonStr(srcData);
  36. if (ret.first) {
  37. LogNotify(Notify_Error, Error_InvalidState, EVENT_CENTERSETTING_NOTIFY_LACK_OF_CENTER_CONFIG, ret.second.c_str());
  38. m_CenterFileNotExists = true;
  39. }
  40. }
  41. }
  42. pTransactionContext->SendAnswer(result);
  43. }
  44. void CCenterSettingEntity::OnStarted()
  45. {
  46. if (SP::Module::Comm::Settings::IsInConfigScheduleMode(this)) {
  47. Dbg("in config mode, do nothing about centersetting sync");
  48. return;
  49. }
  50. CSimpleStringA strPath;
  51. GetFunction()->GetPath("Cfg", strPath);
  52. auto arr = fileutil_get_sub_files(strPath);
  53. if (arr != NULL)
  54. {
  55. for (int i = 0; i < arr->nelts; ++i)
  56. {
  57. ConfigFileInfo info = {};
  58. char *pszFile = ARRAY_IDX(arr, i, char*);
  59. CSimpleStringA strSite;
  60. if (TryExtractSiteFromFileName(pszFile, info.strFileName, strSite))
  61. {
  62. char szTmp[80];
  63. inifile_read_str_s("Main", "SyncHash", "", szTmp, 80, pszFile);
  64. info.strFileHash = szTmp;
  65. inifile_read_str_s("Main", "SyncTime", "", szTmp, 80, pszFile);
  66. DWORD nValue;
  67. sscanf(szTmp, "0x%X", &nValue);
  68. info.dwSyncTime = nValue;
  69. m_SyncFileInfo[(const char*)strSite] = info;
  70. }
  71. }
  72. toolkit_array_free2(arr);
  73. }
  74. CSimpleStringA strFilePath;
  75. GetFunction()->GetPath("CenterSetting", strFilePath);
  76. CSimpleStringA strNoUsed;
  77. TryExtractSiteFromFileName(strFilePath, strNoUsed, m_strCurSiteExtName);
  78. // 缓存当前RVCWeb版本
  79. char szVersion[64] = {};
  80. inifile_read_str_s("IEBrowser", "RvcWebVersion", "", szVersion, 64, strFilePath);
  81. m_strRvcWebVersion = szVersion;
  82. // 立刻同步配置
  83. if (!m_CenterFileNotExists) { /** 让用户主动去处理*/
  84. GetFunction()->PostEntityTaskFIFO(new DownloadCenterSettingsTask(this));
  85. }
  86. //订阅首页状态
  87. GetFunction()->RegistSysVarEvent("UIState", this);
  88. // 启动轮询定时器 5min
  89. if (!m_CenterFileNotExists) { /** 让用户主动去处理*/
  90. GetFunction()->SetTimer(DOWNLOAD_CENTERSETTINGS_TIMER_ID, this, DOWNLOAD_CENTERSETTINGS_TIMER_INTERVAL);
  91. }
  92. }
  93. void CCenterSettingEntity::OnTimeout(DWORD dwTimerID)
  94. {
  95. BeginDownloadCenterSetting(GetCenterSettingsACSTypeDownloadUrl());
  96. }
  97. void CCenterSettingEntity::OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  98. {
  99. DisconnectForSure();
  100. GetFunction()->KillTimer(DOWNLOAD_CENTERSETTINGS_TIMER_ID);
  101. pTransactionContext->SendAnswer(Error_Succeed);
  102. }
  103. ErrorCodeEnum CCenterSettingEntity::BeginDownloadCenterSetting(LPCTSTR serverIP, int port)
  104. {
  105. LOG_FUNCTION();
  106. CSimpleStringA strServiceLink(serverIP == NULL ? "" : serverIP);
  107. if (SP::Module::Ulitily::IsValidUrl(strServiceLink)) {
  108. Dbg("download centersettings file from head branch micro service!");
  109. CenterSettingsMicroServiceHelper microHelper(this);
  110. if (strServiceLink.IndexOf(URLPATH_CENTERSETTING_UPDATE_CONFIG) == -1) {
  111. microHelper.SetServicesUrl(strServiceLink + URLPATH_CENTERSETTING_UPDATE_CONFIG);
  112. } else {
  113. microHelper.SetServicesUrl(strServiceLink);
  114. }
  115. CSimpleStringA strLastErrInfo(true);
  116. const ErrorCodeEnum rc = microHelper.GetCenterSetting(strLastErrInfo);
  117. if (rc != Error_Succeed) {
  118. LogWarn(Severity_Middle, rc, ERR_MOD_CENTERSETTING_CONNECT_FAILED,
  119. CSimpleStringA::Format("Get centersetting failed:%s", strLastErrInfo.GetData()));
  120. } else {
  121. if (SP::Module::Comm::Settings::IsUsingSubBranchServerConfig(this)) {
  122. LogWarn(Severity_Middle, Error_Debug, ERR_MOD_CENTERSETTING_CHANGE_FROM_SUB2HEADBRANCH, "CenterSetting Source Change: from branch to head");
  123. } else if (!SP::Module::Comm::Settings::IsUsingHeadBranchServices(this)) {
  124. LogWarn(Severity_Middle, Error_Debug, ERR_MOD_CENTERSETTING_CHANGE_FROM_NONE2HEADBRANCH, "CenterSetting Source Change: from unknown to head");
  125. }
  126. SP::Module::Comm::Settings::StoreHeadBranchServicesMode(this, TRUE);
  127. LogWarn(Severity_Middle, Error_Succeed, EVENT_MOD_CENTERSETTING_CONNECT_SUCC,
  128. CSimpleStringA::Format("Get centersetting succ: %s", strLastErrInfo.GetData()));
  129. }
  130. ReturnAndClearDownloadContext(rc);
  131. return rc;
  132. } else if (SP::Module::Ulitily::CheckIPv4(strServiceLink.GetData()) && port > 0) {
  133. /*指定分行服务IP和端口下载集中配置文件*/
  134. if (!ConnectToServiceWithType(ConnectServerType::PARAM, serverIP, port)) {
  135. return Error_NetBroken;
  136. } else {
  137. m_nConnectFailCount = 0;
  138. LOG_ASSERT(IsServerConnectedNow());
  139. CSystemStaticInfo info;
  140. ErrorCodeEnum rc = GetFunction()->GetSystemStaticInfo(info);
  141. rc = m_pConnection->BeginPollConfig(info);
  142. return rc;
  143. }
  144. } else if (strServiceLink.IsNullOrEmpty() && port == 0) { //计时器同步
  145. if (SP::Module::Comm::Settings::IsUsingHeadBranchServices(this)) {
  146. CenterSettingsMicroServiceHelper microHelper(this);
  147. if (microHelper.IsConfigureValid()) {
  148. CSimpleStringA strLastErrInfo(true);
  149. const ErrorCodeEnum rc = microHelper.GetCenterSetting(strLastErrInfo);
  150. if (rc != Error_Succeed) {
  151. LogWarn(Severity_Middle, rc, ERR_MOD_CENTERSETTING_CONNECT_FAILED,
  152. CSimpleStringA::Format("Get centersetting failed:%s", strLastErrInfo.GetData()));
  153. } else {
  154. LogWarn(Severity_Middle, Error_Succeed, EVENT_MOD_CENTERSETTING_CONNECT_SUCC,
  155. CSimpleStringA::Format("Get centersetting succ: %s", strLastErrInfo.GetData()));
  156. }
  157. ReturnAndClearDownloadContext(rc);
  158. return rc;
  159. } else {
  160. LogWarn(Severity_Middle, Error_DataCheck, ERR_MOD_CENTERSETTING_FILE_LEGALITY_UNEXPECTED, "CenterSetting file is illegality!");
  161. ReturnAndClearDownloadContext(Error_DataCheck);
  162. return Error_DataCheck;
  163. }
  164. } else {
  165. if (!ConnectToServiceWithType(ConnectServerType::DEFAULT)) {
  166. return Error_NetBroken;
  167. } else {
  168. m_nConnectFailCount = 0;
  169. LOG_ASSERT(IsServerConnectedNow());
  170. CSystemStaticInfo info;
  171. ErrorCodeEnum rc = GetFunction()->GetSystemStaticInfo(info);
  172. rc = m_pConnection->BeginPollConfig(info);
  173. return rc;
  174. }
  175. }
  176. } else {
  177. LogWarn(Severity_Middle, Error_Param, 0, CSimpleStringA::Format("%s,%d", strServiceLink.GetData(), port));
  178. ReturnAndClearDownloadContext(Error_Param);
  179. return Error_Param;
  180. }
  181. }
  182. ErrorCodeEnum CCenterSettingEntity::EndDownloadCenterSetting(ErrorCodeEnum rc)
  183. {
  184. // 备份地址只使用短连接,用于应急切换,使用后立即释放,仍尝试使用新地址
  185. //if (m_bUseBackupNow) // xkm@20160803: 改为短连接
  186. DisconnectForSure();
  187. if (rc == Error_Succeed) {
  188. if (SP::Module::Comm::Settings::IsUsingHeadBranchServices(this)) {
  189. LogWarn(Severity_Middle, Error_Debug, ERR_MOD_CENTERSETTING_CHANGE_FROM_HEAD2SUBBRANCH, "CenterSetting Source Change: from head to branch");
  190. } else if (!SP::Module::Comm::Settings::IsUsingSubBranchServerConfig(this)) {
  191. LogWarn(Severity_Middle, Error_Debug, ERR_MOD_CENTERSETTING_CHANGE_FROM_NONE2SUBBRANCH, "CenterSetting Source Change: from unknown to branch");
  192. }
  193. SP::Module::Comm::Settings::StoreHeadBranchServicesMode(this, FALSE);
  194. LogEvent(Severity_Middle, EVENT_MOD_CENTERSETTING_DOWNOK, "集中配置同步成功");
  195. }
  196. ReturnAndClearDownloadContext(rc);
  197. return rc;
  198. }
  199. ErrorCodeEnum CCenterSettingEntity::DownloadCenterSetting(SpReqAnsContext<CenterSettingService_Download_Req, CenterSettingService_Download_Ans>::Pointer sp)
  200. {
  201. if (IsDownloadPending())
  202. {
  203. Dbg("last download call not complet");
  204. if (sp != NULL) {
  205. sp->Answer(Error_Duplication);
  206. }
  207. return Error_Duplication;
  208. }
  209. m_spDownloadCall = sp;
  210. ErrorCodeEnum error = (sp != NULL ? BeginDownloadCenterSetting(sp->Req.strAddr, sp->Req.nPort)
  211. : BeginDownloadCenterSetting(GetCenterSettingsACSTypeDownloadUrl()));
  212. if (error == Error_NetBroken) { // 如果返回特定的错误码 Error_NetBroken 则立即返回到上下文调用层
  213. m_spDownloadCall = nullptr;
  214. if (sp != NULL) {
  215. sp->Answer(Error_Unexpect);
  216. }
  217. return error;
  218. }
  219. return Error_Succeed;
  220. }
  221. void CCenterSettingEntity::OnPrePause(CSmartPointer<ITransactionContext> pTransactionContext)
  222. {
  223. LOG_ASSERT(SP::Module::Ulitily::IsValidUrl("http://centerconfig.paasst.cmbchina.cn/api/terminal/center/config"));
  224. LOG_ASSERT(SP::Module::Ulitily::IsValidUrl("https://centerconfig.paasst.cmbchina.cn/api/terminal/center/config"));
  225. LOG_ASSERT(SP::Module::Ulitily::IsValidUrl("https://rvcgateway.paas.cmbchina.cn"));
  226. LOG_ASSERT(SP::Module::Ulitily::IsValidUrl("http://rvcgateway.paas.cmbchina.cn"));
  227. LOG_ASSERT(!SP::Module::Ulitily::IsValidUrl("99.1.100.215"));
  228. LOG_ASSERT(!SP::Module::Ulitily::IsValidUrl("rvcgateway.paas.cmbchina.cn"));
  229. LOG_ASSERT(SP::Module::Ulitily::CheckIPv4("99.1.100.215"));
  230. LOG_ASSERT(!SP::Module::Ulitily::CheckIPv4(""));
  231. LOG_ASSERT(!SP::Module::Ulitily::CheckIPv4("99"));
  232. LOG_ASSERT(!SP::Module::Ulitily::CheckIPv4("1.1"));
  233. LOG_ASSERT(!SP::Module::Ulitily::CheckIPv4("1.1.2"));
  234. LOG_ASSERT(SP::Module::Ulitily::CheckIPv4("1.1.2.3"));
  235. LOG_ASSERT(SP::Module::Ulitily::CheckIPv4("255.255.255.255"));
  236. LOG_ASSERT(!SP::Module::Ulitily::CheckIPv4("1.01.2.3"));
  237. LOG_ASSERT(!SP::Module::Ulitily::CheckIPv4("1.501.2.3"));
  238. LOG_ASSERT(!SP::Module::Ulitily::CheckIPv4("1.2.256.3"));
  239. LOG_ASSERT(!SP::Module::Ulitily::CheckIPv4("1.2.3.256"));
  240. LOG_ASSERT(!SP::Module::Ulitily::CheckIPv4("256.2.3.255"));
  241. LOG_ASSERT(!SP::Module::Ulitily::CheckIPv4("1. 1.2.3"));
  242. LOG_ASSERT(!SP::Module::Ulitily::CheckIPv4(" 1.1.2.3"));
  243. LOG_ASSERT(!SP::Module::Ulitily::CheckIPv4("1.1.2.3 "));
  244. pTransactionContext->SendAnswer(Error_Succeed);
  245. }
  246. void CCenterSettingEntity::EditWebUrl(SpReqAnsContext<CenterSettingService_EditWebUrl_Req, CenterSettingService_EditWebUrl_Ans>::Pointer ctx)
  247. {
  248. ErrorCodeEnum result(Error_Unexpect);
  249. ctx->Ans.result = 0;
  250. ctx->Ans.msg = "";
  251. switch (ctx->Req.operation) {
  252. case 1: //new
  253. {
  254. CSmartPointer<IConfigInfo> pConfig;
  255. GetFunction()->OpenConfig(Config_Cache, pConfig);
  256. int count(0);
  257. bool newCreate = true;
  258. pConfig->ReadConfigValueInt("CustomWebUrl", "Count", count);
  259. int newIndex(count + 1);
  260. for (int i = 1; i <= count; ++i) {
  261. CustomWebUrlConfig item;
  262. CSimpleStringA strSection = CSimpleStringA::Format("CustomWebUrl%d", i);
  263. pConfig->ReadConfigValue(strSection, "FultureUrl", item.strFutureUrl);
  264. pConfig->ReadConfigValue(strSection, "AdUrl", item.strAdUrl);
  265. pConfig->ReadConfigValue(strSection, "Remark", item.strRemark);
  266. pConfig->ReadConfigValueInt(strSection, "Env", item.useEnv);
  267. pConfig->ReadConfigValueInt(strSection, "Status", item.curStatus);
  268. if (!item.IsValid()) {
  269. newIndex = i;
  270. newCreate = false;
  271. break;
  272. }
  273. }
  274. CSimpleStringA strNewSection = CSimpleStringA::Format("CustomWebUrl%d", newIndex);
  275. pConfig->WriteConfigValue(strNewSection, "FultureUrl", ctx->Req.futureUrl);
  276. pConfig->WriteConfigValue(strNewSection, "AdUrl", ctx->Req.adUrl);
  277. CSimpleStringA strRemark(ctx->Req.remark);
  278. if (strRemark.IsNullOrEmpty()) {
  279. if (ctx->Req.env == 1) strRemark = "DEV:";
  280. else if(ctx->Req.env == 2) strRemark = "ST:";
  281. if (ctx->Req.env == 3) strRemark = "UAT:";
  282. strRemark += ctx->Req.futureUrl;
  283. }
  284. pConfig->WriteConfigValue(strNewSection, "Remark", strRemark);
  285. pConfig->WriteConfigValueInt(strNewSection, "Env", ctx->Req.env);
  286. pConfig->WriteConfigValueInt(strNewSection, "Status", WEBURL_STATUS_PROVIDE);
  287. if (ctx->Req.setDefault) {
  288. pConfig->WriteConfigValueInt("CustomWebUrl", "Current", newIndex);
  289. }
  290. if (newCreate) {
  291. pConfig->WriteConfigValueInt("CustomWebUrl", "Count", newIndex);
  292. }
  293. ctx->Ans.addition = newIndex;
  294. result = Error_Succeed;
  295. }
  296. break;
  297. case 2: //delete
  298. {
  299. if (ctx->Req.index >= WEBURL_ITEM_INDEX_CENTERSETTING) {
  300. result = Error_NoPrivilege;
  301. } else {
  302. CSmartPointer<IConfigInfo> pConfig;
  303. GetFunction()->OpenConfig(Config_Cache, pConfig);
  304. int oldStatus(0);
  305. CSimpleStringA strSection = CSimpleStringA::Format("CustomWebUrl%d", ctx->Req.index);
  306. pConfig->ReadConfigValueInt(strSection, "Status", oldStatus);
  307. pConfig->WriteConfigValueInt(strSection, "Status", WEBURL_STATUS_DEPRECATE);
  308. int currentUsing(0);
  309. pConfig->ReadConfigValueInt("CustomWebUrl", "Current", currentUsing);
  310. if (ctx->Req.index == currentUsing) {
  311. pConfig->WriteConfigValueInt("CustomWebUrl", "Current", 0);
  312. }
  313. result = Error_Succeed;
  314. }
  315. }
  316. break;
  317. case 3: //update
  318. {
  319. if (ctx->Req.index >= WEBURL_ITEM_INDEX_CENTERSETTING) { //如果当前是集中配置文件
  320. CSmartPointer<IConfigInfo> pConfig;
  321. GetFunction()->OpenConfig(Config_Cache, pConfig);
  322. int currentUsing(0);
  323. pConfig->ReadConfigValueInt("CustomWebUrl", "Current", currentUsing);
  324. if (currentUsing != 0 && ctx->Req.setDefault) {
  325. pConfig->WriteConfigValueInt("CustomWebUrl", "Current", 0);
  326. result = Error_Succeed;
  327. } else {
  328. result = Error_NoPrivilege;
  329. }
  330. break;
  331. }
  332. CSmartPointer<IConfigInfo> pConfig;
  333. GetFunction()->OpenConfig(Config_Cache, pConfig);
  334. CSimpleStringA strExistedSection = CSimpleStringA::Format("CustomWebUrl%d", ctx->Req.index);
  335. pConfig->WriteConfigValue(strExistedSection, "FultureUrl", ctx->Req.futureUrl);
  336. pConfig->WriteConfigValue(strExistedSection, "AdUrl", ctx->Req.adUrl);
  337. CSimpleStringA strRemark(ctx->Req.remark);
  338. if (strRemark.IsNullOrEmpty()) {
  339. if (ctx->Req.env == 1) strRemark = "DEV:";
  340. else if (ctx->Req.env == 2) strRemark = "ST:";
  341. if (ctx->Req.env == 3) strRemark = "UAT:";
  342. strRemark += ctx->Req.futureUrl;
  343. }
  344. pConfig->WriteConfigValue(strExistedSection, "Remark", strRemark);
  345. pConfig->WriteConfigValueInt(strExistedSection, "Env", ctx->Req.env);
  346. pConfig->WriteConfigValueInt(strExistedSection, "Status", WEBURL_STATUS_PROVIDE);
  347. if (ctx->Req.setDefault) {
  348. pConfig->WriteConfigValueInt("CustomWebUrl", "Current", ctx->Req.index);
  349. }
  350. ctx->Ans.addition = ctx->Req.index;
  351. result = Error_Succeed;
  352. }
  353. break;
  354. default:
  355. result = Error_NotSupport;
  356. break;
  357. }
  358. ctx->Answer(result);
  359. return;
  360. }
  361. void CCenterSettingEntity::GetWebUrlList(SpReqAnsContext<CenterSettingService_GetWebUrlList_Req, CenterSettingService_GetWebUrlList_Ans>::Pointer ctx)
  362. {
  363. ErrorCodeEnum result(Error_Succeed);
  364. CSmartPointer<IConfigInfo> pConfig;
  365. GetFunction()->OpenConfig(Config_Cache, pConfig);
  366. int cnt(0), curIndex(0), realCnt(0);
  367. bool hasDefaultFromCustom(false);
  368. pConfig->ReadConfigValueInt("CustomWebUrl", "Count", cnt);
  369. int currentUsing(0);
  370. pConfig->ReadConfigValueInt("CustomWebUrl", "Current", currentUsing);
  371. CAutoArray< CustomWebUrlConfig> configs(cnt);
  372. Dbg("custom count: %d", cnt);
  373. for (int i = 1; i <= cnt; ++i) {
  374. CustomWebUrlConfig& item = configs[i - 1];
  375. item.configFrom = WEBURL_CONFIG_CUSTOM;
  376. item.index = i;
  377. CSimpleStringA strSection = CSimpleStringA::Format("CustomWebUrl%d", i);
  378. pConfig->ReadConfigValue(strSection, "FultureUrl", item.strFutureUrl);
  379. pConfig->ReadConfigValue(strSection, "AdUrl", item.strAdUrl);
  380. pConfig->ReadConfigValue(strSection, "Remark", item.strRemark);
  381. pConfig->ReadConfigValueInt(strSection, "Env", item.useEnv);
  382. pConfig->ReadConfigValueInt(strSection, "Status", item.curStatus);
  383. if (item.IsValid()) {
  384. realCnt++;
  385. if (currentUsing == i) {
  386. hasDefaultFromCustom = true;
  387. item.curStatus = WEBURL_STATUS_DEFAULT;
  388. } else {
  389. item.curStatus = WEBURL_STATUS_PROVIDE;
  390. }
  391. }
  392. }
  393. CustomWebUrlConfig defaultOne;
  394. ErrorCodeEnum resultFromFetch = GetWebUrlInfoFromCenterSettings(defaultOne);
  395. if (resultFromFetch == Error_Succeed) {
  396. Dbg("add centersettings config");
  397. realCnt += 1;
  398. if (!hasDefaultFromCustom) {
  399. defaultOne.curStatus = WEBURL_STATUS_DEFAULT;
  400. }
  401. }
  402. Dbg("total count: %d", realCnt);
  403. ctx->Ans.index.Init(realCnt);
  404. ctx->Ans.futureUrl.Init(realCnt);
  405. ctx->Ans.adUrl.Init(realCnt);
  406. ctx->Ans.remark.Init(realCnt);
  407. ctx->Ans.env.Init(realCnt);
  408. ctx->Ans.type.Init(realCnt);
  409. ctx->Ans.status.Init(realCnt);
  410. if (resultFromFetch == Error_Succeed) {
  411. ctx->Ans.index[curIndex] = WEBURL_ITEM_INDEX_CENTERSETTING;
  412. ctx->Ans.futureUrl[curIndex] = defaultOne.strFutureUrl;
  413. ctx->Ans.adUrl[curIndex] = defaultOne.strAdUrl;
  414. ctx->Ans.remark[curIndex] = defaultOne.strRemark;
  415. ctx->Ans.env[curIndex] = defaultOne.useEnv;
  416. ctx->Ans.type[curIndex] = defaultOne.configFrom;
  417. ctx->Ans.status[curIndex] = defaultOne.curStatus;
  418. curIndex++;
  419. }
  420. for (int i = 0; i < configs.GetCount(); ++i) {
  421. CustomWebUrlConfig& item = configs[i];
  422. if (item.IsValid()) {
  423. ctx->Ans.index[curIndex] = item.index;
  424. ctx->Ans.futureUrl[curIndex] = item.strFutureUrl;
  425. ctx->Ans.adUrl[curIndex] = item.strAdUrl;
  426. ctx->Ans.remark[curIndex] = item.strRemark;
  427. ctx->Ans.env[curIndex] = item.useEnv;
  428. ctx->Ans.type[curIndex] = item.configFrom;
  429. ctx->Ans.status[curIndex] = item.curStatus;
  430. curIndex++;
  431. }
  432. }
  433. ctx->Answer(result);
  434. }
  435. void CCenterSettingEntity::GetActiveCustomUrl(
  436. SpReqAnsContext<CenterSettingService_GetActiveCustomUrl_Req, CenterSettingService_GetActiveCustomUrl_Ans>::Pointer ctx)
  437. {
  438. ErrorCodeEnum result(Error_Succeed);
  439. CSmartPointer<IConfigInfo> pConfig;
  440. GetFunction()->OpenConfig(Config_Cache, pConfig);
  441. int cnt(0), currentUsing(0);
  442. pConfig->ReadConfigValueInt("CustomWebUrl", "Count", cnt);
  443. pConfig->ReadConfigValueInt("CustomWebUrl", "Current", currentUsing);
  444. if (cnt == 0 || currentUsing == 0) {
  445. result = Error_NotConfig;
  446. } else {
  447. CSimpleStringA strSection = CSimpleStringA::Format("CustomWebUrl%d", currentUsing);
  448. pConfig->ReadConfigValue(strSection, "FultureUrl", ctx->Ans.fultureUrl);
  449. pConfig->ReadConfigValue(strSection, "AdUrl", ctx->Ans.adUrl);
  450. if (!ctx->Ans.fultureUrl.IsNullOrEmpty()) {
  451. result = Error_Succeed;
  452. } else {
  453. result = Error_CheckSum;
  454. }
  455. }
  456. ctx->Answer(result);
  457. return;
  458. }
  459. void CCenterSettingEntity::EditTerminalBackupInfo(SpReqAnsContext<CenterSettingService_EditTerminalBackupInfo_Req, CenterSettingService_EditTerminalBackupInfo_Ans>::Pointer ctx)
  460. {
  461. ErrorCodeEnum result(Error_Succeed);
  462. CSimpleStringA errMsg(true);
  463. ErrorCodeEnum tmpResult(Error_Succeed);
  464. switch (ctx->Req.operation) {
  465. case 1: //new
  466. {
  467. CSmartPointer<IConfigInfo> pConfig;
  468. GetFunction()->OpenConfig(Config_Cache, pConfig);
  469. int count(0);
  470. bool newCreate = true;
  471. pConfig->ReadConfigValueInt("TerminalBackup", "Count", count);
  472. int newIndex(count + 1);
  473. for (int i = 1; i <= count; ++i) {
  474. CustomWebUrlConfig item;
  475. CSimpleStringA strSection = CSimpleStringA::Format("TerminalBackup%d", i);
  476. pConfig->ReadConfigValue(strSection, "TerminalNo", item.strFutureUrl);
  477. pConfig->ReadConfigValue(strSection, "ServerIP", item.strAdUrl);
  478. pConfig->ReadConfigValue(strSection, "Remark", item.strRemark);
  479. pConfig->ReadConfigValueInt(strSection, "Env", item.useEnv);
  480. pConfig->ReadConfigValueInt(strSection, "Status", item.curStatus);
  481. if (!item.IsValid()) {
  482. newIndex = i;
  483. newCreate = false;
  484. break;
  485. }
  486. }
  487. CSimpleStringA strNewSection = CSimpleStringA::Format("TerminalBackup%d", newIndex);
  488. pConfig->WriteConfigValue(strNewSection, "TerminalNo", ctx->Req.terminalNo);
  489. pConfig->WriteConfigValue(strNewSection, "ServerIP", ctx->Req.branchIP);
  490. CSimpleStringA strRemark(ctx->Req.remark);
  491. if (strRemark.IsNullOrEmpty()) {
  492. if (ctx->Req.env == 1) strRemark = "DEV:";
  493. else if (ctx->Req.env == 2) strRemark = "ST:";
  494. if (ctx->Req.env == 3) strRemark = "UAT:";
  495. strRemark += ctx->Req.terminalNo;
  496. }
  497. pConfig->WriteConfigValue(strNewSection, "Remark", strRemark);
  498. pConfig->WriteConfigValueInt(strNewSection, "Env", ctx->Req.env);
  499. pConfig->WriteConfigValueInt(strNewSection, "Status", WEBURL_STATUS_PROVIDE);
  500. if (ctx->Req.setDefault) {
  501. tmpResult = UpdateTerminalInfoAtChange(ctx->Req.terminalNo);
  502. if (!(tmpResult == Error_Succeed || tmpResult == Error_AlreadyExist)) {
  503. pConfig->WriteConfigValueInt(strNewSection, "Status", WEBURL_STATUS_DEPRECATE);
  504. errMsg = CSimpleStringA::Format("更新终端号信息失败!");
  505. break;
  506. }
  507. }
  508. if (newCreate) {
  509. pConfig->WriteConfigValueInt("TerminalBackup", "Count", newIndex);
  510. }
  511. ctx->Ans.addition = (int)ConfirmCenterSettingsFileAndUpdateIfNecessary(ctx->Req.branchIP);
  512. result = Error_Succeed;
  513. }
  514. break;
  515. case 2: //delete
  516. {
  517. CSmartPointer<IConfigInfo> pConfig;
  518. GetFunction()->OpenConfig(Config_Cache, pConfig);
  519. CSimpleStringA strSection = CSimpleStringA::Format("TerminalBackup%d", ctx->Req.index);
  520. int oldStatus(0);
  521. pConfig->ReadConfigValueInt(strSection, "Status", oldStatus);
  522. pConfig->WriteConfigValueInt(strSection, "Status", WEBURL_STATUS_DEPRECATE);
  523. }
  524. break;
  525. case 3: //update
  526. {
  527. CSmartPointer<IConfigInfo> pConfig;
  528. GetFunction()->OpenConfig(Config_Cache, pConfig);
  529. CSimpleStringA strExistedSection = CSimpleStringA::Format("TerminalBackup%d", ctx->Req.index);
  530. if (ctx->Req.setDefault) {
  531. tmpResult = UpdateTerminalInfoAtChange(ctx->Req.terminalNo);
  532. if (!(tmpResult == Error_Succeed || tmpResult == Error_AlreadyExist)) {
  533. errMsg = CSimpleStringA::Format("更新终端号信息失败!");
  534. break;
  535. }
  536. }
  537. pConfig->WriteConfigValue(strExistedSection, "TerminalNo", ctx->Req.terminalNo);
  538. pConfig->WriteConfigValue(strExistedSection, "ServerIP", ctx->Req.branchIP);
  539. CSimpleStringA strRemark(ctx->Req.remark);
  540. if (strRemark.IsNullOrEmpty()) {
  541. if (ctx->Req.env == 1) strRemark = "DEV:";
  542. else if (ctx->Req.env == 2) strRemark = "ST:";
  543. if (ctx->Req.env == 3) strRemark = "UAT:";
  544. strRemark += ctx->Req.terminalNo;
  545. }
  546. pConfig->WriteConfigValue(strExistedSection, "Remark", strRemark);
  547. pConfig->WriteConfigValueInt(strExistedSection, "Env", ctx->Req.env);
  548. pConfig->WriteConfigValueInt(strExistedSection, "Status", WEBURL_STATUS_PROVIDE);
  549. ctx->Ans.addition = (int)ConfirmCenterSettingsFileAndUpdateIfNecessary(ctx->Req.branchIP);
  550. result = Error_Succeed;
  551. }
  552. break;
  553. default:
  554. result = Error_NotSupport;
  555. break;
  556. }
  557. ctx->Ans.result = tmpResult;
  558. ctx->Ans.msg = errMsg;
  559. ctx->Answer(result);
  560. return;
  561. }
  562. void CCenterSettingEntity::GetTerminalBackupInfoList(SpReqAnsContext<CenterSettingService_GetTerminalBackupInfoList_Req, CenterSettingService_GetTerminalBackupInfoList_Ans>::Pointer ctx)
  563. {
  564. ErrorCodeEnum result(Error_Succeed);
  565. CSmartPointer<IConfigInfo> pConfig;
  566. GetFunction()->OpenConfig(Config_Cache, pConfig);
  567. int cnt(0), curIndex(0), realCnt(0);
  568. bool hasDefaultFromCustom(false);
  569. pConfig->ReadConfigValueInt("TerminalBackup", "Count", cnt);
  570. CAutoArray< CustomWebUrlConfig> configs(cnt);
  571. Dbg("custom count: %d", cnt);
  572. for (int i = 1; i <= cnt; ++i) {
  573. CustomWebUrlConfig& item = configs[i - 1];
  574. item.configFrom = WEBURL_CONFIG_CUSTOM;
  575. item.index = i;
  576. CSimpleStringA strSection = CSimpleStringA::Format("TerminalBackup%d", i);
  577. pConfig->ReadConfigValue(strSection, "TerminalNo", item.strFutureUrl);
  578. pConfig->ReadConfigValue(strSection, "ServerIP", item.strAdUrl);
  579. pConfig->ReadConfigValue(strSection, "Remark", item.strRemark);
  580. pConfig->ReadConfigValueInt(strSection, "Env", item.useEnv);
  581. pConfig->ReadConfigValueInt(strSection, "Status", item.curStatus);
  582. if (item.IsValid()) {
  583. realCnt++;
  584. DWORD dwMask(0);
  585. if (IsParamCurrentUsed(item.strFutureUrl, item.strAdUrl, dwMask)) {
  586. hasDefaultFromCustom = true;
  587. item.curStatus = WEBURL_STATUS_DEFAULT;
  588. } else {
  589. item.curStatus = WEBURL_STATUS_PROVIDE;
  590. }
  591. }
  592. }
  593. ErrorCodeEnum resultFromFetch(Error_NotInit);
  594. CustomWebUrlConfig defaultOne;
  595. if (!hasDefaultFromCustom) {
  596. resultFromFetch = GetParamCurrentUseing(defaultOne);
  597. if (resultFromFetch == Error_Succeed) {
  598. Dbg("add root config");
  599. realCnt += 1;
  600. defaultOne.curStatus = WEBURL_STATUS_DEFAULT;
  601. }
  602. }
  603. Dbg("total count: %d", realCnt);
  604. ctx->Ans.index.Init(realCnt);
  605. ctx->Ans.terminalNo.Init(realCnt);
  606. ctx->Ans.branchIP.Init(realCnt);
  607. ctx->Ans.remark.Init(realCnt);
  608. ctx->Ans.env.Init(realCnt);
  609. ctx->Ans.status.Init(realCnt);
  610. if (resultFromFetch == Error_Succeed) {
  611. ctx->Ans.index[curIndex] = defaultOne.index;
  612. ctx->Ans.terminalNo[curIndex] = defaultOne.strFutureUrl;
  613. ctx->Ans.branchIP[curIndex] = defaultOne.strAdUrl;
  614. ctx->Ans.remark[curIndex] = defaultOne.strRemark;
  615. ctx->Ans.env[curIndex] = defaultOne.useEnv;
  616. ctx->Ans.status[curIndex] = defaultOne.curStatus;
  617. Dbg("index: %d", ctx->Ans.index[curIndex]);
  618. Dbg("terminalNo: %s", ctx->Ans.terminalNo[curIndex].GetData());
  619. Dbg("branchIP: %s", ctx->Ans.branchIP[curIndex].GetData());
  620. Dbg("remark: %s", ctx->Ans.remark[curIndex].GetData());
  621. Dbg("env: %d", ctx->Ans.env[curIndex]);
  622. Dbg("status: %d", ctx->Ans.status[curIndex]);
  623. curIndex++;
  624. }
  625. for (int i = 0; i < configs.GetCount(); ++i) {
  626. CustomWebUrlConfig& item = configs[i];
  627. if (item.IsValid()) {
  628. ctx->Ans.index[curIndex] = item.index;
  629. ctx->Ans.terminalNo[curIndex] = item.strFutureUrl;
  630. ctx->Ans.branchIP[curIndex] = item.strAdUrl;
  631. ctx->Ans.remark[curIndex] = item.strRemark;
  632. ctx->Ans.env[curIndex] = item.useEnv;
  633. ctx->Ans.status[curIndex] = item.curStatus;
  634. curIndex++;
  635. }
  636. }
  637. ctx->Answer(result);
  638. }
  639. void CCenterSettingEntity::DownloadCenterFiles(
  640. SpReqAnsContext<CenterSettingService_DownloadCenterFiles_Req, CenterSettingService_DownloadCenterFiles_Ans>::Pointer ctx)
  641. {
  642. if (IsDownloadPending()) {
  643. Dbg("%s: last download call not complet", __FUNCTION__);
  644. ctx->Answer(Error_Duplication);
  645. return;
  646. }
  647. if (ctx->Req.strAddr == NULL || strlen(ctx->Req.strAddr) == 0 || ctx->Req.nPort <= 0) {
  648. ctx->Answer(Error_Param);
  649. return;
  650. }
  651. m_spDownloadCallEx = ctx;
  652. DisconnectForSure();
  653. m_pConnection = new CCenterSettingConn(this);
  654. if (!m_pConnection->Connect(ctx->Req.strAddr, ctx->Req.nPort, 3)) {
  655. DisconnectForSure();
  656. ReturnAndClearDownloadContext(Error_NetBroken);
  657. } else {
  658. Dbg("custom download from server %s:%d", ctx->Req.strAddr.GetData(), ctx->Req.nPort, 3);
  659. if (ctx->Req.deleteIfExist) {
  660. Dbg("Remove CenterSettings file!");
  661. RemoveAllCenterSettingFiles();
  662. }
  663. CSystemStaticInfo info;
  664. GetFunction()->GetSystemStaticInfo(info);
  665. if (!ctx->Req.additional1.IsNullOrEmpty()) {
  666. Dbg("Render TeminalNo from %s to %s", info.strTerminalID.GetData(), ctx->Req.additional1.GetData());
  667. info.strTerminalID = ctx->Req.additional1;
  668. }
  669. /** 该接口永远返回成功,所以没有做判断 [Gifur@2021127]*/
  670. ErrorCodeEnum ret = m_pConnection->BeginPollConfig(info);
  671. }
  672. }
  673. ErrorCodeEnum CCenterSettingEntity::GetSyncInfo(unsigned int& dwSyncTime, CSimpleStringA& strSyncHash, CSimpleStringA& strSyncFile)
  674. {
  675. if (m_SyncFileInfo.find((const char*)m_strCurSiteExtName) != m_SyncFileInfo.end())
  676. {
  677. auto info = m_SyncFileInfo[(const char*)m_strCurSiteExtName];
  678. dwSyncTime = info.dwSyncTime;
  679. strSyncFile = info.strFileName;
  680. strSyncHash = info.strFileHash;
  681. return Error_Succeed;
  682. }
  683. return Error_NotExist;
  684. }
  685. bool CCenterSettingEntity::ParseIPAddress(const char *str, CSimpleStringA &ip, int &port)
  686. {
  687. if (str)
  688. {
  689. char tmp1[32] = {};
  690. char tmp2[16] = {};
  691. sscanf(str, "%s %s", tmp1, tmp2);
  692. if (tmp1[0] != 0 && tmp2[0] != 0)
  693. {
  694. ip = tmp1;
  695. port = atoi(tmp2);
  696. return true;
  697. }
  698. }
  699. return false;
  700. }
  701. bool CCenterSettingEntity::TryExtractSiteFromFileName(
  702. const char *pszPath, CSimpleStringA &strFileName, CSimpleStringA &strSite)
  703. {
  704. const regex re("(CenterSetting\.(.+)\.ini$)");
  705. cmatch result;
  706. if (!regex_search(pszPath, result, re))
  707. return false;
  708. strFileName = result[1].str().c_str();
  709. strSite = result[2].str().c_str();
  710. return true;
  711. }
  712. ErrorCodeEnum CCenterSettingEntity::GetWebUrlInfoFromCenterSettings(CustomWebUrlConfig& config)
  713. {
  714. CSmartPointer<IConfigInfo> spCerConfig;
  715. ErrorCodeEnum err = GetFunction()->OpenConfig(Config_CenterSetting, spCerConfig);
  716. SpIniMappingTable table;
  717. CSimpleStringA entityName("Chromium");
  718. // clean cache every time
  719. table.AddEntryString(entityName, "UserMgrUrlFulture", config.strFutureUrl, "");
  720. table.AddEntryString(entityName, "UserMgrAd", config.strAdUrl, "");
  721. err = table.Load(spCerConfig);
  722. if (err == Error_Succeed) {
  723. config.configFrom = WEBURL_CONFIG_CENTESETTING;
  724. config.curStatus = WEBURL_STATUS_PROVIDE;
  725. config.useEnv = WEBURL_ENV_ALL;
  726. config.strRemark = "Default Config From CenterSettings";
  727. }
  728. if (config.strFutureUrl.IsNullOrEmpty()) {
  729. err = Error_NotConfig;
  730. }
  731. return err;
  732. }
  733. bool CCenterSettingEntity::ConnectToServiceWithType(ConnectServerType type, LPCTSTR serverIP, int port)
  734. {
  735. LOG_FUNCTION();
  736. Dbg("connect type:%d, %s::%d", type, serverIP, port);
  737. if (type == ConnectServerType::DEFAULT && IsServerConnectedNow()) {
  738. Dbg("Connection has been built before.");
  739. return true;
  740. }
  741. DisconnectForSure();
  742. m_pConnection = new CCenterSettingConn(this);
  743. m_bUseBackupNow = (type == ConnectServerType::BACKUP);
  744. if (m_bUseBackupNow)
  745. {
  746. CSmartPointer<IConfigInfo> pConfig;
  747. auto rc = GetFunction()->OpenConfig(Config_CenterSetting, pConfig);
  748. assert(rc == Error_Succeed);
  749. CSimpleStringA strLastServer;
  750. rc = pConfig->ReadConfigValue("Main", "LastServer", strLastServer);
  751. if (rc != Error_Succeed || strLastServer.IsNullOrEmpty())
  752. {
  753. Dbg("read last server from center setting fail");
  754. return false;
  755. }
  756. CSimpleStringA strIP;
  757. int nPort(0);
  758. if (!ParseIPAddress(strLastServer, strIP, nPort))
  759. {
  760. Dbg("parse last server [%s] fail", (const char*)strLastServer);
  761. return false;
  762. }
  763. return m_pConnection->Connect(strIP, nPort, 3);
  764. } else if(type == ConnectServerType::PARAM)
  765. {
  766. Dbg("custom download from server %s:%d", serverIP, port);
  767. return m_pConnection->Connect(serverIP, port, 3);
  768. }
  769. return m_pConnection->ConnectFromCentralSetting();
  770. }
  771. void CCenterSettingEntity::DisconnectForSure()
  772. {
  773. if (m_pConnection !=NULL)
  774. {
  775. if (m_pConnection->IsConnectionOK())
  776. m_pConnection->Close();
  777. m_pConnection->DecRefCount();
  778. m_pConnection = NULL;
  779. }
  780. }
  781. void CCenterSettingEntity::OnSysVarEvent(const char* pszKey,
  782. const char* pszValue, const char* pszOldValue, const char* pszEntityName)
  783. {
  784. if ((_strnicmp(pszKey, "UIState", strlen("UIState")) == 0)) {
  785. if (_strnicmp(pszValue, "M", strlen("M")) == 0) {
  786. Dbg("enter main page");
  787. CSimpleStringA strCurFile;
  788. GetFunction()->GetPath("CenterSetting", strCurFile);
  789. CSimpleStringA strBackFile = CSimpleStringA::Format("%s.backup", strCurFile.GetData());
  790. if (!fileutil_copy_file(strBackFile, strCurFile)) {
  791. Dbg("Copy curFile to backFile failed: %d", GetLastError());
  792. }
  793. GetFunction()->UnregistSysVarEvent("UIState");
  794. } else {
  795. Dbg("not main page[%s]", pszValue);
  796. }
  797. }
  798. }
  799. bool CCenterSettingEntity::CheckServerIPIsExist()
  800. {
  801. LOG_FUNCTION();
  802. CSmartPointer<IConfigInfo> pConfig;
  803. auto rc = GetFunction()->OpenConfig(Config_CenterSetting, pConfig);
  804. assert(rc == Error_Succeed);
  805. CSimpleStringA strServer;
  806. rc = pConfig->ReadConfigValue("CenterSetting", "Server", strServer);
  807. if (rc != Error_Succeed || strServer.IsNullOrEmpty()) {
  808. Dbg("read strServer from center setting fail.");
  809. Dbg("use backup center setting...");
  810. CSimpleStringA strBackFile, strCurFile;
  811. GetFunction()->GetPath("CenterSetting", strCurFile);
  812. strBackFile = CSimpleStringA::Format("%s.backup", strCurFile.GetData());
  813. if(-1 == fileutil_copy_file(strCurFile, strBackFile)) {
  814. Dbg("Copy backFile to curFile failed: %d", GetLastError());
  815. return false;
  816. } else {
  817. rc = pConfig->ReadConfigValue("CenterSetting", "Server", strServer);
  818. if (rc != Error_Succeed || strServer.IsNullOrEmpty()) {
  819. return false;
  820. }
  821. }
  822. }
  823. return true;
  824. }
  825. ErrorCodeEnum CCenterSettingEntity::UpdateTerminalInfoAtChange(const CSimpleStringA& newTerminalInfo)
  826. {
  827. ErrorCodeEnum result(Error_Succeed);
  828. CSystemStaticInfo sysInfo;
  829. result = GetFunction()->GetSystemStaticInfo(sysInfo);
  830. if (result == Error_Succeed) {
  831. if (sysInfo.strTerminalID == newTerminalInfo) {
  832. result = Error_AlreadyExist;
  833. } else {
  834. CSimpleStringA strTemp = CSimpleStringA::Format("更新终端号信息:%s -> %s", sysInfo.strTerminalID.GetData(), newTerminalInfo.GetData());
  835. LogWarn(Severity_High, Error_Debug, EVENT_CENTERSETTING_CHANGE_ROOT_TERMINALNO, strTemp);
  836. CSimpleStringA strRootCfgPath, rootPath;
  837. ErrorCodeEnum ec = GetFunction()->GetPath("HardwareCfg", strRootCfgPath);
  838. rootPath = strRootCfgPath;
  839. rootPath += SPLIT_SLASH_STR;
  840. rootPath += "root.ini";
  841. inifile_write_str(rootPath, "Terminal", "TerminalNo", newTerminalInfo);
  842. inifile_write_str(rootPath, "Terminal", "LastTerminalNo", sysInfo.strTerminalID);
  843. char tmp[32] = {'\0'};
  844. inifile_read_str_s("Terminal", "TerminalNo", "", tmp, 31, rootPath);
  845. CSimpleStringA strConfirmTerminalNo(tmp);
  846. if (strConfirmTerminalNo != newTerminalInfo) {
  847. Dbg("Update TeminalNo failed: read returned: %s, write into: %s", strConfirmTerminalNo.GetData(), newTerminalInfo.GetData());
  848. result = Error_FailVerify;
  849. }
  850. }
  851. }
  852. return result;
  853. }
  854. bool CCenterSettingEntity::IsParamCurrentUsed(const CSimpleStringA& strTerminalInfo, const CSimpleStringA& strServerIP, DWORD& outMask)
  855. {
  856. bool result(true);
  857. outMask = 0;
  858. CSystemStaticInfo sysInfo;
  859. GetFunction()->GetSystemStaticInfo(sysInfo);
  860. if (sysInfo.strTerminalID.IsNullOrEmpty() || sysInfo.strTerminalID != strTerminalInfo) {
  861. result = false;
  862. } else {
  863. outMask |= 0x1;
  864. }
  865. CSmartPointer<IConfigInfo> pConfig;
  866. CSimpleStringA strServer;
  867. GetFunction()->OpenConfig(Config_CenterSetting, pConfig);
  868. pConfig->ReadConfigValue("CenterSetting", "Server", strServer);
  869. CAutoArray<CSimpleStringA> ipPorts = strServer.Split(' ');
  870. if (strServer.IsNullOrEmpty() || ipPorts.GetCount() != 2 || ipPorts[0] != strServerIP) {
  871. result = false;
  872. } else {
  873. outMask |= 0x2;
  874. }
  875. return result;
  876. }
  877. ErrorCodeEnum CCenterSettingEntity::GetParamCurrentUseing(CustomWebUrlConfig& config)
  878. {
  879. ErrorCodeEnum result(Error_Succeed);
  880. CSystemStaticInfo sysInfo;
  881. result = GetFunction()->GetSystemStaticInfo(sysInfo);
  882. config.strFutureUrl = sysInfo.strTerminalID;
  883. CSmartPointer<IConfigInfo> pConfig;
  884. CSimpleStringA strServer;
  885. result = GetFunction()->OpenConfig(Config_CenterSetting, pConfig);
  886. result = pConfig->ReadConfigValue("CenterSetting", "Server", strServer);
  887. CAutoArray<CSimpleStringA> ipPorts = strServer.Split(' ');
  888. if (ipPorts.GetCount() == 2) {
  889. config.strAdUrl = ipPorts[0];
  890. } else {
  891. config.strAdUrl = "";
  892. }
  893. config.index = WEBURL_ITEM_INDEX_CENTERSETTING;
  894. config.configFrom = WEBURL_CONFIG_CENTESETTING;
  895. config.curStatus = WEBURL_STATUS_PROVIDE;
  896. config.useEnv = WEBURL_ENV_ALL;
  897. config.strRemark = "root.ini&CenterSetting";
  898. return result;
  899. }
  900. void CCenterSettingEntity::RemoveAllCenterSettingFiles()
  901. {
  902. CSimpleStringA strEntityCfgPath;
  903. GetFunction()->GetPath("Cfg", strEntityCfgPath);
  904. array_header_t* subs = fileutil_get_sub_files_a(strEntityCfgPath);
  905. if (subs) {
  906. regex_t reg;
  907. CSimpleStringA pattern = "CenterSetting\.[a-zA-Z0-9_\\(\\)\\-]*\.ini";
  908. int ret = regcomp(&reg, pattern, REG_EXTENDED | REG_NOSUB);
  909. if (ret) {
  910. char ebuff[256];
  911. regerror(ret, &reg, ebuff, 256);
  912. Dbg("regex failed: %s", ebuff);
  913. } else {
  914. Dbg("pattern: %s", pattern.GetData());
  915. for (int i = 0; i < subs->nelts; ++i) {
  916. char* filenamePath = ARRAY_IDX(subs, i, char*);
  917. char* filename = &filenamePath[strEntityCfgPath.GetLength() + 1];
  918. ret = regexec(&reg, filename, 0, NULL, 0);
  919. if (0 == ret) {
  920. Dbg("filename %s matched and remove it.", filename);
  921. fileutil_delete_file(filenamePath);
  922. }
  923. }
  924. }
  925. toolkit_array_free2(subs);
  926. }
  927. CCenterSettingConn::ClearillegalCenterSettingsFile(strEntityCfgPath);
  928. }
  929. CSimpleStringA CCenterSettingEntity::GetCenterSettingsACSTypeDownloadUrl()
  930. {
  931. CSimpleStringA result(true);
  932. CSmartPointer<IConfigInfo> pConfig;
  933. GetFunction()->OpenConfig(Config_CenterSetting, pConfig);
  934. pConfig->ReadConfigValue("CenterSetting", "CenterConfigUrl", result);
  935. if (result.IsNullOrEmpty()) {
  936. do
  937. {
  938. CSimpleStringA strFilePath;
  939. GetFunction()->GetPath("CenterSetting", strFilePath);
  940. if (ExistsFileA(strFilePath)) {
  941. LogWarn(Severity_Middle, Error_DataCheck, ERR_MOD_CENTERSETTING_FILE_LEGALITY_UNEXPECTED, "[CenterSetting]CenterConfigUrl empty!");
  942. }
  943. } while (false);
  944. CSmartPointer<IConfigInfo> pConfigShell;
  945. GetFunction()->OpenConfig(Config_Shell, pConfigShell);
  946. CSimpleStringA section("CenterSettingsDownloadUrl");
  947. #ifndef UNDER_PRODUCT_ENV
  948. section.Append("_ST");
  949. #endif
  950. pConfigShell->ReadConfigValue(section, "LAN", result);
  951. }
  952. return result;
  953. }
  954. ErrorCodeEnum CCenterSettingEntity::ConfirmCenterSettingsFileAndUpdateIfNecessary(const CSimpleStringA& serverIP)
  955. {
  956. CSmartPointer<IConfigInfo> pConfig;
  957. CSimpleStringA strServer;
  958. ErrorCodeEnum result = GetFunction()->OpenConfig(Config_CenterSetting, pConfig);
  959. result = pConfig->ReadConfigValue("CenterSetting", "Server", strServer);
  960. CAutoArray<CSimpleStringA> ipPorts = strServer.Split(' ');
  961. if (strServer.IsNullOrEmpty() || ipPorts.GetCount() !=2 || ipPorts[0] != serverIP) {
  962. result = Error_FailVerify;
  963. }
  964. return result;
  965. }
  966. void CCenterSettingService::Handle_EditWebUrl(SpReqAnsContext<CenterSettingService_EditWebUrl_Req, CenterSettingService_EditWebUrl_Ans>::Pointer ctx)
  967. {
  968. LOG_FUNCTION();
  969. m_pEntity->EditWebUrl(ctx);
  970. }
  971. void CCenterSettingService::Handle_GetWebUrlList(SpReqAnsContext<CenterSettingService_GetWebUrlList_Req, CenterSettingService_GetWebUrlList_Ans>::Pointer ctx)
  972. {
  973. LOG_FUNCTION();
  974. m_pEntity->GetWebUrlList(ctx);
  975. }
  976. void CCenterSettingService::Handle_GetActiveCustomUrl(SpReqAnsContext<CenterSettingService_GetActiveCustomUrl_Req, CenterSettingService_GetActiveCustomUrl_Ans>::Pointer ctx)
  977. {
  978. LOG_FUNCTION();
  979. m_pEntity->GetActiveCustomUrl(ctx);
  980. }
  981. void CCenterSettingService::Handle_EditTerminalBackupInfo(SpReqAnsContext<CenterSettingService_EditTerminalBackupInfo_Req, CenterSettingService_EditTerminalBackupInfo_Ans>::Pointer ctx)
  982. {
  983. LOG_FUNCTION();
  984. m_pEntity->EditTerminalBackupInfo(ctx);
  985. }
  986. void CCenterSettingService::Handle_GetTerminalBackupInfoList(SpReqAnsContext<CenterSettingService_GetTerminalBackupInfoList_Req, CenterSettingService_GetTerminalBackupInfoList_Ans>::Pointer ctx)
  987. {
  988. LOG_FUNCTION();
  989. m_pEntity->GetTerminalBackupInfoList(ctx);
  990. }
  991. void CCenterSettingService::Handle_DownloadCenterFiles(SpReqAnsContext<CenterSettingService_DownloadCenterFiles_Req, CenterSettingService_DownloadCenterFiles_Ans>::Pointer ctx)
  992. {
  993. LOG_FUNCTION();
  994. m_pEntity->DownloadCenterFiles(ctx);
  995. }
  996. SP_BEGIN_ENTITY_MAP()
  997. SP_ENTITY(CCenterSettingEntity)
  998. SP_END_ENTITY_MAP()