コード例 #1
0
ファイル: dbtask.cpp プロジェクト: windyting126/simpleserver
int CDBTask::InitMySqlDb(int iIndex)
{
	try
	{
		CMysql * pMysql = m_vMysql[iIndex];
		std::string strMySqlHost;
	    std::string strMySqlUser;
	    std::string strMySqlPass;

	    ((CPRServer*)(CMyserver::GetInstance()))->GetMyConf().GetResourceDBInfo(strMySqlHost, strMySqlUser, strMySqlPass, iIndex);
		if (!pMysql->IfConnected(strMySqlHost))
		{

			pMysql->Init();
			my_bool bReconnect = 1;
			pMysql->SetOptions(MYSQL_OPT_RECONNECT, &bReconnect);
			pMysql->Connect(strMySqlHost, strMySqlUser, strMySqlPass, 20);
		}
	}
	catch (CMysqlException &e)
	{
		WriteRunInfo::WriteError("[CDBTask::InitMySqlDb] %s",e.GetErrMsg());
		return -1;
	}

	return 0;
}
コード例 #2
0
int CInsertTools::LoadPeers()
{
	CMysql Mysql;
	char szSqlBuf[1024];
	struct timespec tm1, tm2;

	try
	{
		Mysql.Init();
		Mysql.Connect(m_conf.GetConfItem().vecDbItems[0].strIp, m_conf.GetConfItem().vecDbItems[0].strUserName, m_conf.GetConfItem().vecDbItems[0].strPasswd);
		snprintf(szSqlBuf, sizeof(szSqlBuf), "select * from %s.%s;", m_conf.GetConfItem().strDbName.c_str(), ONLINE_PEER_TABLE.c_str());
		Mysql.Query(szSqlBuf);

		clock_gettime(CLOCK_MONOTONIC, &tm1);
		int nIndex = 0;
		long lUseTime = 0;
		int nSleepTime = (int)((1000 / (float)m_conf.GetConfItem().nSpeedPerSec) * 0.85 * 100 * 1000);
		string strPeerId;
		while(Mysql.FetchRow())
		{
			strPeerId = Mysql.GetField("fpeerid");

			CInsertPeerCmd reqCmd(strPeerId);
			reqCmd.Process();
			TransMsg(reqCmd.GetBuf(), reqCmd.GetBufLen(), 0, MACHINE_TYPE, ONLINE_SYN);
			nIndex ++;
			if(nIndex % 100 == 0)
			{
				usleep(nSleepTime);
			}
			if(nIndex >= m_conf.GetConfItem().nSpeedPerSec)
			{
				clock_gettime(CLOCK_MONOTONIC, &tm2);
				lUseTime =  (tm2.tv_sec - tm1.tv_sec) * 1000000000 + (tm2.tv_nsec - tm1.tv_nsec);
				lUseTime = lUseTime / 1000;
				WRITE_ALL_LOG(DEBUG_LOG_TYPE, "send %d peer info, use time %ld microsec", nIndex, lUseTime);
				nIndex = 0;
				lUseTime = 0;
				clock_gettime(CLOCK_MONOTONIC, &tm1);
			}
		}
		Mysql.FreeResult();
	}
	catch (CMysqlException& e)
	{
		WriteRunInfo::WriteError("CInsertTools::LoadPeers, sql %s, error %s", szSqlBuf, e.GetErrMsg());
		return -1;
	}
	return 0;
}
コード例 #3
0
int CInsertTools::LoadRc()
{
	CMysql Mysql;
	char szSqlBuf[1024];
	string strPeerId;
	long long llFileSize;
	string strGcid;
	int nCapability;
	unsigned char nDownloadScale;
	struct timespec tm1, tm2;
	int nIndex = 0;
	long lUseTime = 0;
	int nSleepTime = (int)((1000 / (float)m_conf.GetConfItem().nSpeedPerSec) * 0.85 * 100 * 1000);
	clock_gettime(CLOCK_MONOTONIC, &tm1);

	for (int i = 0; i < m_conf.GetConfItem().nDbNum; i ++)
	{
		try
		{
			Mysql.Init();
			Mysql.Connect(m_conf.GetConfItem().vecDbItems[i].strIp, m_conf.GetConfItem().vecDbItems[i].strUserName, m_conf.GetConfItem().vecDbItems[i].strPasswd);

			for (int j = 0; j < m_conf.GetConfItem().nTableNum; j ++)
			{
				stringstream strTableName;
				strTableName << RC_PEER_TABLE;
				strTableName << j;
				snprintf(szSqlBuf, sizeof(szSqlBuf), "select * from %s.%s;", m_conf.GetConfItem().strDbName.c_str(), strTableName.str().c_str());
				strTableName.clear();
				Mysql.Query(szSqlBuf);

				while(Mysql.FetchRow())
				{
					strGcid = Mysql.GetField("fcid");
					strPeerId = Mysql.GetField("fpeerid");
					llFileSize = strtoul(Mysql.GetField("ffilesize"), 0, 10);
					nCapability = strtoul(Mysql.GetField("fcapability"), 0, 10);
					nDownloadScale = strtoul(Mysql.GetField("downpercent"), 0, 10);

					CInsertRcCmd reqCmd(strGcid, strPeerId, llFileSize, nCapability, nDownloadScale);
					reqCmd.Process();
					TransMsg(reqCmd.GetBuf(), reqCmd.GetBufLen(), 0, MACHINE_TYPE, CONNECT_SERVER_COMMON);

					nIndex ++;
					if(nIndex % 100 == 0)
					{
						usleep(nSleepTime);
					}
					if(nIndex >= m_conf.GetConfItem().nSpeedPerSec)
					{
						clock_gettime(CLOCK_MONOTONIC, &tm2);
						lUseTime =  (tm2.tv_sec - tm1.tv_sec) * 1000000000 + (tm2.tv_nsec - tm1.tv_nsec);
						lUseTime = lUseTime / 1000;
						WRITE_ALL_LOG(DEBUG_LOG_TYPE, "send %d rc info, use time %ld microsec", nIndex, lUseTime);
						nIndex = 0;
						lUseTime = 0;
						clock_gettime(CLOCK_MONOTONIC, &tm1);
					}
				}
				Mysql.FreeResult();
			}

			for (int j = 0; j < m_conf.GetConfItem().nTableNum; j ++)
			{
				stringstream strTableName;
				strTableName << RC_PEER_TEMP_TABLE;
				strTableName << j;
				snprintf(szSqlBuf, sizeof(szSqlBuf), "select * from %s.%s;", m_conf.GetConfItem().strDbName.c_str(), strTableName.str().c_str());
				strTableName.clear();
				Mysql.Query(szSqlBuf);

				while(Mysql.FetchRow())
				{
					strGcid = Mysql.GetField("fcid");
					strPeerId = Mysql.GetField("fpeerid");
					llFileSize = strtoul(Mysql.GetField("ffilesize"), 0, 10);
					nCapability = strtoul(Mysql.GetField("fcapability"), 0, 10);
					nDownloadScale = strtoul(Mysql.GetField("downpercent"), 0, 10);

					CInsertRcCmd reqCmd(strGcid, strPeerId, llFileSize, nCapability, nDownloadScale);
					reqCmd.Process();
					TransMsg(reqCmd.GetBuf(), reqCmd.GetBufLen(), 0, MACHINE_TYPE, CONNECT_SERVER_COMMON);

					nIndex ++;
					if(nIndex % 100 == 0)
					{
						usleep(nSleepTime);
					}
					if(nIndex >= m_conf.GetConfItem().nSpeedPerSec)
					{
						clock_gettime(CLOCK_MONOTONIC, &tm2);
						lUseTime =  (tm2.tv_sec - tm1.tv_sec) * 1000000000 + (tm2.tv_nsec - tm1.tv_nsec);
						lUseTime = lUseTime / 1000;
						WRITE_ALL_LOG(DEBUG_LOG_TYPE, "send %d rc temp info, use time %ld microsec", nIndex, lUseTime);
						nIndex = 0;
						lUseTime = 0;
						clock_gettime(CLOCK_MONOTONIC, &tm1);
					}
				}
				Mysql.FreeResult();
			}
		}
		catch (CMysqlException& e)
		{
			WriteRunInfo::WriteError("CInsertTools::LoadPeers, sql %s, error %s", szSqlBuf, e.GetErrMsg());
			return -1;
		}
	}
	
	return 0;
}