mod_centersetting.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #ifndef RVC_MOD_CENTERSETTING_H__
  2. #define RVC_MOD_CENTERSETTING_H__
  3. #pragma once
  4. #include <map>
  5. #include <string>
  6. #include "CenterSetting_server_g.h"
  7. using namespace CenterSetting;
  8. #include "modVer.h"
  9. #include "CenterSettingConn.h"
  10. #include "SpTest.h"
  11. enum ConnectServerType
  12. {
  13. DEFAULT = 0,
  14. BACKUP = 1,
  15. PARAM = 2,
  16. DEBRANCH = 3
  17. };
  18. #define WEBURL_ENV_DEV 1
  19. #define WEBURL_ENV_ST 2
  20. #define WEBURL_ENV_UAT 3
  21. #define WEBURL_ENV_ALL 0
  22. #define WEBURL_CONFIG_CENTESETTING 0
  23. #define WEBURL_CONFIG_CUSTOM 1
  24. #define WEBURL_STATUS_PROVIDE 0
  25. #define WEBURL_STATUS_DEFAULT 1
  26. #define WEBURL_STATUS_USING 2
  27. #define WEBURL_STATUS_DEPRECATE 3
  28. #define WEBURL_ITEM_COUNT_MAX 100
  29. #define WEBURL_ITEM_INDEX_CENTERSETTING (WEBURL_ITEM_COUNT_MAX + 1)
  30. struct CustomWebUrlConfig
  31. {
  32. CSimpleStringA strFutureUrl;
  33. CSimpleStringA strAdUrl;
  34. CSimpleStringA strRemark;
  35. int useEnv;
  36. int configFrom;
  37. int curStatus;
  38. int index;
  39. bool IsValid()
  40. {
  41. return (!strFutureUrl.IsNullOrEmpty() && curStatus != WEBURL_STATUS_DEPRECATE);
  42. }
  43. };
  44. struct GrayLaunchReq : CHTTPReq{
  45. GrayLaunchReq(){m_timeOut = 5;}
  46. string m_terminal_no;
  47. string m_branch_no;
  48. string m_modular;
  49. virtual string ToJson(){
  50. char reqcontent[512];
  51. sprintf(reqcontent, "{\"terminal_no\":\"%s\",\"branch_no\":\"%s\",\"modular\":\"%s\"}", m_terminal_no.c_str(),
  52. m_branch_no.c_str(), m_modular.c_str());
  53. return reqcontent;
  54. }
  55. };
  56. struct GrayLaunchResponse : CHTTPRet{
  57. GrayLaunchResponse():m_result(false){}
  58. bool m_result;
  59. virtual bool Parse(string strData){
  60. Json::Value root;
  61. Json::Reader reader;
  62. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("GrayLaunchResponse.Parse");
  63. reader.parse(strData, root, false);
  64. if (root["data"].isBool()) {
  65. m_result = root["data"].asBool();
  66. }
  67. return true;
  68. }
  69. };
  70. static void HTTPLogCallback(const char* msg){}
  71. class CCenterSettingEntity : public CEntityBase, public ITimerListener, public ISysVarListener
  72. {
  73. public:
  74. CCenterSettingEntity() : /*m_dwLastSyncTime(0),*/
  75. m_pConnection(nullptr), m_CenterFileNotExists(false), m_nConnectFailCount(0), m_bUseBackupNow(false)
  76. , m_spDownloadCall(NULL), m_spDownloadCallEx(NULL){}
  77. virtual ~CCenterSettingEntity() {}
  78. virtual const char *GetEntityName() const { return "CenterSetting"; }
  79. const char* GetEntityVersion() const { return MODULE_VERSION_FULL; }
  80. virtual bool IsService()const{return true;}
  81. ON_ENTITYT_TEST()
  82. void OnPreStart(CAutoArray<CSimpleStringA> strArgs, CSmartPointer<ITransactionContext> pTransactionContext);
  83. virtual void OnStarted();
  84. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext);
  85. virtual void OnTimeout(DWORD dwTimerID);
  86. virtual CServerSessionBase *OnNewSession(const char* /*pszRemoteEntityName*/, const char * /*pszClass*/);
  87. ErrorCodeEnum GetSyncInfo(unsigned int &dwSyncTime, CSimpleStringA &strSyncHash, CSimpleStringA &strSyncFile);
  88. ErrorCodeEnum DownloadCenterSetting(SpReqAnsContext<CenterSettingService_Download_Req, CenterSettingService_Download_Ans>::Pointer sp);
  89. void OnPrePause(CSmartPointer<ITransactionContext> pTransactionContext);
  90. void EditWebUrl(SpReqAnsContext<CenterSettingService_EditWebUrl_Req, CenterSettingService_EditWebUrl_Ans>::Pointer ctx);
  91. void GetWebUrlList(SpReqAnsContext<CenterSettingService_GetWebUrlList_Req, CenterSettingService_GetWebUrlList_Ans>::Pointer ctx);
  92. void GetActiveCustomUrl(SpReqAnsContext<CenterSettingService_GetActiveCustomUrl_Req, CenterSettingService_GetActiveCustomUrl_Ans>::Pointer ctx);
  93. void EditTerminalBackupInfo(SpReqAnsContext<CenterSettingService_EditTerminalBackupInfo_Req, CenterSettingService_EditTerminalBackupInfo_Ans>::Pointer ctx);
  94. void GetTerminalBackupInfoList(SpReqAnsContext<CenterSettingService_GetTerminalBackupInfoList_Req, CenterSettingService_GetTerminalBackupInfoList_Ans>::Pointer ctx);
  95. void DownloadCenterFiles(SpReqAnsContext<CenterSettingService_DownloadCenterFiles_Req, CenterSettingService_DownloadCenterFiles_Ans>::Pointer ctx);
  96. private:
  97. ErrorCodeEnum BeginDownloadCenterSetting(LPCTSTR serverIP = NULL, int port = 0);
  98. ErrorCodeEnum EndDownloadCenterSetting(ErrorCodeEnum rc);
  99. bool ConnectToServiceWithType(ConnectServerType type, LPCTSTR serverIP = NULL, int port = 0);
  100. void DisconnectForSure();
  101. bool ParseIPAddress(const char *str, CSimpleStringA &ip, int &port);
  102. bool TryExtractSiteFromFileName(const char *pszPath, CSimpleStringA &strFileName, CSimpleStringA &strSite);
  103. ErrorCodeEnum GetWebUrlInfoFromCenterSettings(CustomWebUrlConfig& config);
  104. bool IsServerConnectedNow() const
  105. {
  106. return (m_pConnection != nullptr && m_pConnection->IsConnectionOK());
  107. }
  108. virtual void OnSysVarEvent(const char *pszKey,
  109. const char *pszValue, const char *pszOldValue, const char *pszEntityName);
  110. bool CheckServerIPIsExist();
  111. ErrorCodeEnum UpdateTerminalInfoAtChange(const CSimpleStringA& newTerminalInfo);
  112. ErrorCodeEnum ConfirmCenterSettingsFileAndUpdateIfNecessary(const CSimpleStringA& serverIP);
  113. bool IsParamCurrentUsed(const CSimpleStringA& strTerminalInfo, const CSimpleStringA& strServerIP, DWORD& outMask);
  114. ErrorCodeEnum GetParamCurrentUseing(CustomWebUrlConfig& config);
  115. bool IsDownloadPending()
  116. {
  117. if (m_spDownloadCall != NULL) {
  118. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Download Call Pending!");
  119. return true;
  120. }
  121. if (m_spDownloadCallEx != NULL) {
  122. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Download Call Ex Pending!");
  123. return true;
  124. }
  125. return false;
  126. }
  127. void ReturnAndClearDownloadContext(ErrorCodeEnum ec)
  128. {
  129. if (m_spDownloadCall != NULL) {
  130. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("DownloadCall return %s", SpStrError(ec));
  131. m_spDownloadCall->Answer(ec);
  132. m_spDownloadCall.Clear();
  133. m_spDownloadCall = NULL;
  134. }
  135. if (m_spDownloadCallEx != NULL) {
  136. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("DownloadCallEx return %s", SpStrError(ec));
  137. m_spDownloadCallEx->Answer(ec);
  138. m_spDownloadCallEx.Clear();
  139. m_spDownloadCallEx = NULL;
  140. }
  141. }
  142. void RemoveAllCenterSettingFiles();
  143. CSimpleStringA GetCenterSettingsACSTypeDownloadUrl();
  144. private:
  145. struct ConfigFileInfo
  146. {
  147. CSimpleStringA strFileHash;
  148. CSimpleStringA strFileName;
  149. DWORD dwSyncTime;
  150. };
  151. std::map<std::string, ConfigFileInfo> m_SyncFileInfo;
  152. CSimpleStringA m_strCurSiteExtName; // 当前场所集中配置扩展名:LAN或DMZ
  153. CSimpleStringA m_strRvcWebVersion; // 当前RVCWeb版本
  154. bool m_CenterFileNotExists;
  155. bool m_bUseBackupNow;
  156. int m_nConnectFailCount;
  157. CCenterSettingConn *m_pConnection;
  158. SpReqAnsContext<CenterSettingService_Download_Req, CenterSettingService_Download_Ans>::Pointer m_spDownloadCall;
  159. SpReqAnsContext<CenterSettingService_DownloadCenterFiles_Req, CenterSettingService_DownloadCenterFiles_Ans>::Pointer m_spDownloadCallEx;
  160. friend class CCenterSettingConn;
  161. };
  162. struct DownloadCenterSettingsTask : public ITaskSp
  163. {
  164. DownloadCenterSettingsTask(CCenterSettingEntity* entity) :pEntity(entity) {}
  165. void Process()
  166. {
  167. pEntity->DownloadCenterSetting(NULL);
  168. }
  169. CCenterSettingEntity* pEntity;
  170. };
  171. class CCenterSettingService : public CenterSettingService_ServerSessionBase
  172. {
  173. public:
  174. CCenterSettingService(CCenterSettingEntity *pEntity) :m_pEntity(pEntity){}
  175. virtual ~CCenterSettingService(){}
  176. virtual void Handle_Download(SpReqAnsContext<CenterSettingService_Download_Req, CenterSettingService_Download_Ans>::Pointer ctx)
  177. {
  178. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER)("%s: %s, %d", __FUNCTION__, ctx->Req.strAddr.GetData(), ctx->Req.nPort);
  179. if (ctx->Req.strAddr.IsNullOrEmpty() || ctx->Req.nPort < 0) {
  180. ctx->Answer(Error_Param);
  181. } else {
  182. m_pEntity->DownloadCenterSetting(ctx);
  183. }
  184. }
  185. virtual void Handle_GetSyncInfo(SpReqAnsContext<CenterSettingService_GetSyncInfo_Req, CenterSettingService_GetSyncInfo_Ans>::Pointer ctx)
  186. {
  187. auto rc = m_pEntity->GetSyncInfo(ctx->Ans.nSyncTime, ctx->Ans.strFileHash, ctx->Ans.strFileName);
  188. ctx->Answer(rc);
  189. }
  190. void Handle_EditWebUrl(SpReqAnsContext<CenterSettingService_EditWebUrl_Req, CenterSettingService_EditWebUrl_Ans>::Pointer ctx);
  191. void Handle_GetWebUrlList(SpReqAnsContext<CenterSettingService_GetWebUrlList_Req, CenterSettingService_GetWebUrlList_Ans>::Pointer ctx);
  192. void Handle_GetActiveCustomUrl(SpReqAnsContext<CenterSettingService_GetActiveCustomUrl_Req, CenterSettingService_GetActiveCustomUrl_Ans>::Pointer ctx);
  193. void Handle_EditTerminalBackupInfo(SpReqAnsContext<CenterSettingService_EditTerminalBackupInfo_Req, CenterSettingService_EditTerminalBackupInfo_Ans>::Pointer ctx);
  194. void Handle_GetTerminalBackupInfoList(SpReqAnsContext<CenterSettingService_GetTerminalBackupInfoList_Req, CenterSettingService_GetTerminalBackupInfoList_Ans>::Pointer ctx);
  195. void Handle_DownloadCenterFiles(SpReqAnsContext<CenterSettingService_DownloadCenterFiles_Req, CenterSettingService_DownloadCenterFiles_Ans>::Pointer ctx);
  196. private:
  197. CCenterSettingEntity *m_pEntity;
  198. };
  199. #endif /*RVC_MOD_CENTERSETTING_H__*/