mod_centersetting.cpp 42 KB

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