Эх сурвалжийг харах

#IQRV #comment [Module][CenterSetting]处理主动下载集中配置的问题

gifur 4 жил өмнө
parent
commit
77ad1bc437

+ 1 - 0
Module/mod_CenterSetting/ChangeLog

@@ -1,2 +1,3 @@
+* 处理主动下载集中配置的问题(廖桂发,2020年12月26日)
 * 2020年10月27日 合并国密版本的代码
 * 2020-7-3 调通与分行服务进行通信的功能,双活机制中用到

+ 40 - 19
Module/mod_CenterSetting/mod_centersetting.cpp

@@ -62,14 +62,12 @@ CServerSessionBase *CCenterSettingEntity::OnNewSession(const char* /*pszRemoteEn
 	 m_strRvcWebVersion = szVersion;
 
 	 // 立刻同步配置
-	 //BeginDownloadCenterSetting();
 	 pFunc->PostEntityTaskFIFO(new DownloadCenterSettingsTask(this));
 
 	 // 启动轮询定时器 5min
 	 GetFunction()->SetTimer(DOWNLOAD_CENTERSETTINGS_TIMER_ID, this, DOWNLOAD_CENTERSETTINGS_TIMER_INTERVAL);
  }
 
-
  void CCenterSettingEntity::OnTimeout(DWORD dwTimerID)
  {
 	 BeginDownloadCenterSetting();
@@ -82,23 +80,30 @@ CServerSessionBase *CCenterSettingEntity::OnNewSession(const char* /*pszRemoteEn
 	 pTransactionContext->SendAnswer(Error_Succeed);
  }
 
- ErrorCodeEnum CCenterSettingEntity::BeginDownloadCenterSetting()
+ ErrorCodeEnum CCenterSettingEntity::BeginDownloadCenterSetting(LPCTSTR serverIP, int port)
  {
-	 if (!SecureClientConnect(false))
-	 {
-		 m_nConnectFailCount++;
-		 if (m_nConnectFailCount < 20)
-			 return Error_NetBroken;
+	 LOG_FUNCTION();
 
-		 // 10分钟连接不上,切换到备用地址		 
-		 Dbg("connect fail more than 10 min, now use backup config");
-		 if (!SecureClientConnect(true))
+	 if (serverIP != NULL && strlen(serverIP) > 0 && port > 0) {
+		 if (!SecureClientConnect(ConnectServerType::PARAM, serverIP, port)) {
 			 return Error_NetBroken;
+		 }
+	 } else {
+         if (!SecureClientConnect(ConnectServerType::DEFAULT)) {
+             m_nConnectFailCount++;
+             if (m_nConnectFailCount < 20)
+                 return Error_NetBroken;
+
+             // 10分钟连接不上,切换到备用地址		 
+             Dbg("connect fail more than 10 min, now use backup config");
+             if (!SecureClientConnect(ConnectServerType::BACKUP))
+                 return Error_NetBroken;
+         }
 	 }
 
 	 m_nConnectFailCount = 0;
 	 //Dbg("begin poll center setting");
-	 assert(m_pConnection != NULL && m_pConnection->IsConnectionOK());
+	 LOG_ASSERT(IsServerConnectedNow());
 	 auto rc = m_pConnection->BeginPollConfig();
 	 return rc;
  }
@@ -133,7 +138,15 @@ CServerSessionBase *CCenterSettingEntity::OnNewSession(const char* /*pszRemoteEn
 	 }
 
 	 m_spDownloadCall = sp;
-	 BeginDownloadCenterSetting();
+	 ErrorCodeEnum error = (sp != NULL ? BeginDownloadCenterSetting(sp->Req.strAddr, sp->Req.nPort) : BeginDownloadCenterSetting());
+	 if (error == Error_NetBroken) {
+		 m_spDownloadCall = nullptr;
+		 if (sp != NULL) {
+             sp->Answer(Error_Unexpect);
+		 }
+         return error;
+	 }
+
 	 return Error_Succeed;
  }
 
@@ -183,17 +196,21 @@ CServerSessionBase *CCenterSettingEntity::OnNewSession(const char* /*pszRemoteEn
 	 return true;
  }
 
- bool CCenterSettingEntity::SecureClientConnect(bool bUseBackup)
+ bool CCenterSettingEntity::SecureClientConnect(ConnectServerType type, LPCTSTR serverIP, int port)
  {
-	 if (m_pConnection!=NULL && m_pConnection->IsConnectionOK())
+	 LOG_FUNCTION();
+	 Dbg("%d, %s, %d", type, serverIP, port);
+
+	 if (type == ConnectServerType::DEFAULT && IsServerConnectedNow())
 		 return true;
 
 	 SecureClientRelease();
-	 m_bUseBackupNow = bUseBackup;
+
+	 m_bUseBackupNow = (type == ConnectServerType::BACKUP);
 
 	 m_pConnection = new CCenterSettingConn(this);
 	 
-	 if (bUseBackup)
+	 if (m_bUseBackupNow)
 	 {
 		 CSmartPointer<IConfigInfo> pConfig;
 		 auto rc = GetFunction()->OpenConfig(Config_CenterSetting, pConfig);
@@ -216,9 +233,13 @@ CServerSessionBase *CCenterSettingEntity::OnNewSession(const char* /*pszRemoteEn
 		 }
 
 		 return m_pConnection->Connect(strIP, nPort, 3);
+
+	 } else if(type == ConnectServerType::PARAM) 
+	 {
+		 Dbg("custom download from server %s:%d", serverIP, port);
+		 return m_pConnection->Connect(serverIP, port, 3);
 	 }
-	 else
-		 return m_pConnection->ConnectFromCentralSetting();
+	 return m_pConnection->ConnectFromCentralSetting();
  }
 
  void CCenterSettingEntity::SecureClientRelease()

+ 21 - 4
Module/mod_CenterSetting/mod_centersetting.h

@@ -13,6 +13,13 @@ using namespace CenterSetting;
 #include "CenterSettingConn.h"
 #include "SpTest.h"
 
+enum ConnectServerType
+{
+	DEFAULT = 0,
+	BACKUP = 1,
+	PARAM = 2
+};
+
 class CCenterSettingEntity : public CEntityBase, public ITimerListener
 {
 public:
@@ -35,14 +42,19 @@ public:
 	ErrorCodeEnum DownloadCenterSetting(SpReqAnsContext<CenterSettingService_Download_Req, CenterSettingService_Download_Ans>::Pointer sp);
 
 private:
-	ErrorCodeEnum BeginDownloadCenterSetting();
+	ErrorCodeEnum BeginDownloadCenterSetting(LPCTSTR serverIP = NULL, int port = 0);
 	ErrorCodeEnum EndDownloadCenterSetting(ErrorCodeEnum rc);
 	
-	bool SecureClientConnect(bool bUseBackup);
+	bool SecureClientConnect(ConnectServerType type, LPCTSTR serverIP = NULL, int port = 0);
 	void SecureClientRelease();
 	bool ParseIPAddress(const char *str, CSimpleStringA &ip, int &port);
 	bool TryExtractSiteFromFileName(const char *pszPath, CSimpleStringA &strFileName, CSimpleStringA &strSite);
 	
+	bool IsServerConnectedNow() const
+	{
+		return (m_pConnection != nullptr && m_pConnection->IsConnectionOK());
+	}
+
 private:
 	struct ConfigFileInfo
 	{
@@ -67,7 +79,7 @@ struct DownloadCenterSettingsTask : public ITaskSp
 	DownloadCenterSettingsTask(CCenterSettingEntity* entity) :pEntity(entity) {}
 	void Process()
 	{
-		pEntity->DownloadCenterSetting(nullptr);
+		pEntity->DownloadCenterSetting(NULL);
 	}
 	CCenterSettingEntity* pEntity;
 };
@@ -80,7 +92,12 @@ public:
 
 	virtual void Handle_Download(SpReqAnsContext<CenterSettingService_Download_Req, CenterSettingService_Download_Ans>::Pointer ctx)
 	{
-		m_pEntity->DownloadCenterSetting(ctx);
+		Dbg("%s: %s, %d", __FUNCTION__, ctx->Req.strAddr.GetData(), ctx->Req.nPort);
+		if (ctx->Req.strAddr.IsNullOrEmpty() || ctx->Req.nPort <= 0) {
+			ctx->Answer(Error_Param);
+		} else {
+            m_pEntity->DownloadCenterSetting(ctx);
+		}
 	}
 
 	virtual void Handle_GetSyncInfo(SpReqAnsContext<CenterSettingService_GetSyncInfo_Req, CenterSettingService_GetSyncInfo_Ans>::Pointer ctx)