Beispiel #1
0
	virtual bool OnInitialize()
	{
		uint32 nLength;
		const char* sVal = NULL;
		CTextAccess oAccess;
		CInitConfigSystem* pConfigSystem = CInitConfigSystem::GetInstance();

		//Register telnet command line interface
		CCmdSystem::GetInstance()->RegisterCmd("/Mdb", "Mdb<CR>:\r\n\t sql command line", MdbSqlCmdFunc);

		//Create Local Memory Database
		uint32 nMdbSvr = 0;
		if(pConfigSystem->OpenConfig(oAccess, "MdbService", true))
		{
			oAccess.OpenIdxVal();
			if(oAccess.Query())
			{
				sVal = oAccess.GetVal("MdbSvr", nLength);
				if(!sVal || sVal[nLength-1])
				{
					FocpLog(FOCP_LOG_ERROR, ("The config 'MdbService.ServerPort' is invalid"));
					return false;
				}
				nMdbSvr = CString::Atoi(sVal);
				sVal = oAccess.GetVal("MdbList", nLength);
			}
		}

		bool bInitializeMdbDataSource = false;
		bool bInitializeMdbStorageAttr = false;
		bool bCreateMdbReplicator = false;
		bool bConfigMdbDomain = false;

		if(sVal && sVal[0])
		{
			bool bCreated = false;
			char*pShift, *pDbName = (char*)sVal;
			while(pDbName)
			{
				pShift = (char*)CString::CharOfString(pDbName, ',');
				if(pShift)
					pShift[0] = 0;

				if(m_pMdbItf == NULL)
				{
					m_pMdbItf = new CLocalMdbItf;
					if(!m_pMdbItf->Valid())
					{
						if(pShift)
							pShift[0] = ',';
						return false;
					}
				}
				if(!m_pMdbItf->CreateMemoryDataBase(pDbName))
				{
					if(pShift)
						pShift[0] = ',';
					return false;
				}
				bCreated = true;
				CCmdSession oSession;
				char sDbfScript[FOCP_MAX_PATH];
				const char* sHome = CFilePathInfo::GetInstance()->GetHome();
				StringPrint(sDbfScript, "%s/dbf/%s.Create.sql", sHome, pDbName);
				oSession.ProcessScript(sDbfScript);

				pDbName = pShift;
				if(pDbName)
				{
					pDbName[0] = ',';
					++pDbName;
				}
			}

			if(bCreated)//创建了本地数据库
			{
				if(pConfigSystem->OpenConfig(oAccess, "MdbStorage", true))
				{
					oAccess.OpenIdxVal();
					while(oAccess.Query())
					{
						sVal = oAccess.GetVal("MdbName", nLength);
						if(!sVal || sVal[nLength-1])
						{
							FocpLog(FOCP_LOG_ERROR, ("The config 'MdbStorage.MdbName' is invalid"));
							return false;
						}
						CString oMdbName(sVal);
						sVal = oAccess.GetVal("CacheName", nLength);
						if(!sVal || sVal[nLength-1])
						{
							FocpLog(FOCP_LOG_ERROR, ("The config 'MdbStorage.CacheName' is invalid"));
							return false;
						}
						CString oMdbTabName(sVal);
						sVal = oAccess.GetVal("TableName", nLength);
						if(sVal && sVal[nLength-1])
						{
							FocpLog(FOCP_LOG_ERROR, ("The config 'MdbStorage.TableName' is invalid"));
							return false;
						}
						CString oDbTabName(sVal?sVal:(char*)"");
						sVal = oAccess.GetVal("LoadWhere", nLength);
						if(sVal && sVal[nLength-1])
						{
							FocpLog(FOCP_LOG_ERROR, ("The config 'MdbStorage.LoadWhere' is invalid"));
							return false;
						}
						CString oLoadWhere(sVal?sVal:"");
						sVal = oAccess.GetVal("StorageWhere", nLength);
						if(sVal && sVal[nLength-1])
						{
							FocpLog(FOCP_LOG_ERROR, ("The config 'MdbStorage.StorageWhere' is invalid"));
							return false;
						}
						CString oStorageWhere(sVal?sVal:(char*)"");
						sVal = oAccess.GetVal("CacheWhere", nLength);
						if(sVal && sVal[nLength-1])
						{
							FocpLog(FOCP_LOG_ERROR, ("The config 'MdbStorage.CacheWhere' is invalid"));
							return false;
						}
						CString oCacheWhere(sVal?sVal:(char*)"");
						sVal = oAccess.GetVal("StorageIdx", nLength);
						if(!sVal || sVal[nLength-1])
						{
							FocpLog(FOCP_LOG_ERROR, ("The config 'MdbStorage.StorageIdx' is invalid"));
							return false;
						}
						CString oStorageIdx(sVal);
						sVal = oAccess.GetVal("FieldList", nLength);
						if(sVal && sVal[nLength-1])
						{
							FocpLog(FOCP_LOG_ERROR, ("The config 'MdbStorage.FieldList' is invalid"));
							return false;
						}
						CString oFieldList(sVal?sVal:(char*)"");
						if(m_pStoItf == NULL)
						{
							m_pStoItf = new CMdbStoItf;
							if(!m_pStoItf->Valid())
								return false;
						}
						if(!m_pStoItf->InitializeMdbStorageAttr(oMdbName.GetStr(), oMdbTabName.GetStr(),
																oDbTabName.GetStr(), oLoadWhere.GetStr(), oStorageWhere.GetStr(), oCacheWhere.GetStr(), 
																oStorageIdx.GetStr(), oFieldList.GetStr()))
							return false;
						bInitializeMdbStorageAttr = true;
					}
				}

				if(pConfigSystem->OpenConfig(oAccess, "MdbReplication", true))
				{
					oAccess.OpenIdxVal();
					while(oAccess.Query())
					{
						sVal = oAccess.GetVal("MdbName", nLength);
						if(!sVal || sVal[nLength-1])
						{
							FocpLog(FOCP_LOG_ERROR, ("The config 'MdbReplication.MdbName' is invalid"));
							return false;
						}
						CString oDbName(sVal);
						sVal = oAccess.GetVal("TableName", nLength);
						if(!sVal || sVal[nLength-1])
						{
							FocpLog(FOCP_LOG_ERROR, ("The config 'MdbReplication.TableName' is invalid"));
							return false;
						}
						CString oTabName(sVal);
						sVal = oAccess.GetVal("ReplicateTable", nLength);
						if(!sVal || sVal[nLength-1])
						{
							FocpLog(FOCP_LOG_ERROR, ("The config 'MdbReplication.ReplicateTable' is invalid"));
							return false;
						}
						uint32 bTableReplicative = CString::Atoi(sVal);
						sVal = oAccess.GetVal("WithoutFields", nLength);
						if(!sVal || sVal[nLength-1])
						{
							FocpLog(FOCP_LOG_ERROR, ("The config 'MdbReplication.WithoutFields' is invalid"));
							return false;
						}
						uint32 bWithout = CString::Atoi(sVal);
						sVal = oAccess.GetVal("Fields", nLength);
						if(sVal && sVal[nLength-1])
						{
							FocpLog(FOCP_LOG_ERROR, ("The config 'MdbStorage.Fields' is invalid"));
							return false;
						}
						CString oFields(sVal?sVal:(char*)"");
						sVal = oAccess.GetVal("Where", nLength);
						if(sVal && sVal[nLength-1])
						{
							FocpLog(FOCP_LOG_ERROR, ("The config 'MdbStorage.Where' is invalid"));
							return false;
						}
						CString oWhere(sVal?sVal:(char*)"");
						sVal = oAccess.GetVal("TransferIdx", nLength);
						if(!sVal || sVal[nLength-1])
						{
							FocpLog(FOCP_LOG_ERROR, ("The config 'MdbReplication.TransferIdx' is invalid"));
							return false;
						}
						if(!m_pRepItf)
						{
							m_pRepItf = new CMdbRepItf;
							if(!m_pRepItf->Valid())
								return false;
						}
						if(!m_pRepItf->CreateMdbReplicator(oDbName.GetStr(), oTabName.GetStr(), sVal, bTableReplicative?true:false, bWithout?true:false, oFields.GetStr(), oWhere.GetStr()))
							return false;
						bCreateMdbReplicator = true;
					}
				}

				if( (bCreateMdbReplicator || bInitializeMdbStorageAttr) && pConfigSystem->OpenConfig(oAccess, "MdbDomain", true))
				{
					oAccess.OpenIdxVal();
					while(oAccess.Query())
					{
						sVal = oAccess.GetVal("MdbDomain", nLength);
						if(!sVal || sVal[nLength-1])
						{
							FocpLog(FOCP_LOG_ERROR, ("The config 'MdbDomain.MdbDomain' is invalid"));
							return false;
						}
						uint32 nDomain = CString::Atoi(sVal);
						sVal = oAccess.GetVal("MdbName", nLength);
						if(!sVal || sVal[nLength-1])
						{
							FocpLog(FOCP_LOG_ERROR, ("The config 'MdbDomain.MdbName' is invalid"));
							return false;
						}
						if(bCreateMdbReplicator)
						{
							if(!m_pRepItf->ConfigMdbDomain(sVal, nDomain))
								return false;
							bConfigMdbDomain = true;
						}
						if(bInitializeMdbStorageAttr)
						{
							CString oMdbName(sVal);
							sVal = oAccess.GetVal("OdbcSource", nLength);
							if(sVal && sVal[nLength-1])
							{
								FocpLog(FOCP_LOG_ERROR, ("The config 'MdbDomain.OdbcSource' is invalid"));
								return false;
							}
							if(!sVal)
								continue;
							CString oOdbcDsn, oOdbcUser, oOdbcPasswd;
							if(!GetOdbcSourceInfo(sVal, oOdbcDsn, oOdbcUser, oOdbcPasswd))
							{
								FocpLog(FOCP_LOG_ERROR, ("The config 'MdbDomain.OdbcSource' is invalid"));
								return false;
							}
							sVal = oAccess.GetVal("EventDb", nLength);
							if(sVal && sVal[nLength-1])
							{
								FocpLog(FOCP_LOG_ERROR, ("The config 'MdbDomain.EventDb' is invalid"));
								return false;
							}
							CString oEventDb(sVal?sVal:"");
							sVal = oAccess.GetVal("EventTable", nLength);
							if(sVal && sVal[nLength-1])
							{
								FocpLog(FOCP_LOG_ERROR, ("The config 'MdbDomain.EventTable' is invalid"));
								return false;
							}
							CString oEventTable(sVal?sVal:"");
							uint32 nSupportStorage = 0;
							sVal = oAccess.GetVal("SupportStorage", nLength);
							if(sVal && sVal[nLength-1])
							{
								FocpLog(FOCP_LOG_ERROR, ("The config 'MdbDomain.SupportStorage' is invalid"));
								return false;
							}
							if(sVal)
								nSupportStorage = CString::Atoi(sVal);
							if(!m_pStoItf->InitializeMdbDataSource(oMdbName.GetStr(), nDomain, (nSupportStorage!=0),
																   oOdbcDsn.GetStr(), oOdbcUser.GetStr(), oOdbcPasswd.GetStr(),
																   oEventDb.GetStr(), oEventTable.GetStr()))
								return false;
							bInitializeMdbDataSource = true;
						}
					}
					if(bConfigMdbDomain && !m_pRepItf->RegisterMdbTransfer())
						return false;
				}
			}
		}

		//Create Remote Memory Database
		if(pConfigSystem->OpenConfig(oAccess, "RemoteMdb", true))
		{
			CString oServerAddr;
			uint32 nServerPort;
			const char* sDbList;
			oAccess.OpenIdxVal();
			while(oAccess.Query())
			{
				sVal = oAccess.GetVal("ServerPort", nLength);
				if(!sVal || sVal[nLength-1])
				{
					FocpLog(FOCP_LOG_ERROR, ("The config 'RemoteMdb.ServerPort' is invalid"));
					return false;
				}
				nServerPort = CString::Atoi(sVal);
				if(!nServerPort || nServerPort > 65535)
				{
					FocpLog(FOCP_LOG_ERROR, ("The config 'RemoteMdb.ServerPort' is invalid"));
					return false;
				}
				sVal = oAccess.GetVal("ServerAddr", nLength);
				if(!sVal || !sVal[0] || sVal[nLength-1])
				{
					FocpLog(FOCP_LOG_ERROR, ("The config 'RemoteMdb.ServerAddr' is invalid"));
					return false;
				}
				oServerAddr = sVal;
				sVal = oAccess.GetVal("MdbList", nLength);
				if(sVal && sVal[nLength-1])
				{
					FocpLog(FOCP_LOG_ERROR, ("The config 'RemoteMdb.MdbList' is invalid"));
					return false;
				}
				sDbList = sVal;

				if(m_pRdbItf == NULL)
				{
					m_pRdbItf = new CRemoteMdbItf;
					if(!m_pRdbItf->Valid())
						return false;
				}

				void* pRdbList = m_pRdbItf->CreateMdbClient(sDbList, oServerAddr.GetStr(), (uint16)nServerPort);
				if(pRdbList == NULL)
					return false;
				m_oMdbClients.Insert((char*)pRdbList);
			}
		}

		// Create Mdb Server
		if(nMdbSvr)
		{
			CService* pService = CServiceManager::GetInstance()->QueryService("AcmService");
			if(!pService)
			{
				FocpLog(FOCP_LOG_ERROR, ("MdbService need AcmService for MdbSvrModule, but there isn't it"));
				return false;
			}
			if(!Wait(pService, FOCP_SERVICE_INITIALIZED))
				return false;
			if(!pService->HaveAbility(ACM_TCPSVR_ABILITY))
			{
				FocpLog(FOCP_LOG_ERROR, ("MdbService need AcmService's tcp server ability for MdbSvrModule, but there isn't it"));
				return false;
			}
			m_pSvrItf = new CMdbServerItf;
			if(!m_pSvrItf->Valid())
				return false;
		}

		if(!bConfigMdbDomain && m_pRepItf)
		{
			m_pRepItf->CleanupMdbReplication();
			delete m_pStoItf;
			m_pStoItf = NULL;
		}

		if(!bInitializeMdbDataSource && m_pStoItf)
		{
			m_pStoItf->CleanupMdbStorage();
			delete m_pStoItf;
			m_pStoItf = NULL;
		}

		const char* sCreateCode = GetEnvVar("CreateMdbCode");
		if(sCreateCode && sCreateCode[0] == '1')
			CMdb::CreateCppCode();

		return true;
	}