⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webspeeddlg.cpp

📁 Visual C++自动查询和智能代理程序设计书籍源码-WebSpeed
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	bRunning = true;

	GetDlgItem(IDC_RUN)->EnableWindow(false);
	GetDlgItem(IDC_STOP)->EnableWindow(true);

	SaveURLs();

	ScanSites();

	// If Repeat check box is checked, and the user has not
	// interrupted the run, set the timer for 15 minutes
	
	if (bRunning && m_repeat)
		SetTimer(1, 15 * 60 * 1000, NULL);
	else
		OnStop();	
}

// *************
// *           *
// *  OnTimer  *
// *           *
// *************
// Description:	Message handler for WM_TIMER.
//				Turns off timer, scans sites, and turns
//				timer back on.

void CWebSpeedDlg::OnTimer(UINT nIDEvent) 
{
	KillTimer(1);
	ScanSites();
	if (bRunning && m_repeat)
		SetTimer(1, 15 * 60 * 1000, NULL);
	CDialog::OnTimer(nIDEvent);
}

// ************
// *          *
// *  OnStop  *
// *          *
// ************
// Description:	Message handler for the Stop button.
//				Turns off timer.

void CWebSpeedDlg::OnStop() 
{
	KillTimer(1);
	UpdateData(true);
	bRunning = false;
	GetDlgItem(IDC_STOP)->EnableWindow(false);
	GetDlgItem(IDC_RUN)->EnableWindow(true);
	m_status = "Ready";
	UpdateData(false);
}

// ***************
// *             *
// *  ScanSites  *
// *             *
// ***************
// Description:	Scans URLs by launching individual threads and
//				then waiting for them to complete processing

void CWebSpeedDlg::ScanSites()
{
	CWaitCursor wc;
	m_status = "Scanning sites";
	UpdateData(false);

	CTime tNow = CTime::GetCurrentTime();
	sNow.Format("%02d/%02d/%04d %02d:%02d",
				tNow.GetMonth(),
				tNow.GetDay(),
				tNow.GetYear(),
				tNow.GetHour(),
				tNow.GetMinute());

	// Launch threads to scan sites

	cs.Lock();
	nActiveThreads = nURLs;
	nOrderNo = 0;
	cs.Unlock();

	int ID[MAX_URLS];
	for (int u = 0; u < nURLs; u++)
	{
		cs.Lock();
		dAvgAccessTime[u] = -1;
		dTotalAccessTime[u] = 0;
		dBestAccessTime[u] = -1;
		dWorstAccessTime[u] = -1;
		nOrder[u] = -1;
		ID[u] = u;
		cs.Unlock();
		AfxBeginThread(AccessSite, &ID[u]);
	} // End for

	// Wait for threads to complete

	BOOL bActive = true;
	while(bActive)
	{
		cs.Lock();
		m_status.Format("Scanning (%d threads active)",
						nActiveThreads);
		if (nActiveThreads == 0)
			bActive = false;
		cs.Unlock();
		UpdateData(false);
		if (bActive)
			Sleep(250);
	} // End while

	if (m_html)
	{
		m_status = "Generating HTML page";
		UpdateData(false);
		OutputToHtml();
		ShellExecute(NULL,
					 "open",
					 "WebSpeed.htm",
					 NULL,
					 NULL,
					 SW_SHOWNORMAL);
	} // End if

	if (m_db)
	{
		m_status = "Updating database";
		UpdateData(false);
		OutputToDatabase();
	} // End if

	if (bRunning)
		m_status = "Sleeping";
	else
		m_status = "Finished";
	UpdateData(false);
}

// ******************
// *                *
// *  OutputToHtml  *
// *                *
// ******************
// Description:	Outputs site measurement results to Webspeed.htm

void CWebSpeedDlg::OutputToHtml()
{
	CStdioFile html;
	CString sLine;

	html.Open("webspeed.htm",
			  CFile::modeCreate | CFile::modeWrite);
	html.WriteString("<HTML>\n");
	html.WriteString("<HEAD>\n");
	html.WriteString("<TITLE>Web Speed</TITLE>\n");
	html.WriteString("</HEAD>\n");
	html.WriteString("<BODY>\n");
	html.WriteString("<H1>Web Speed Measurements</H1>\n");
	html.WriteString("Site speed measurements made at "
					 + sNow + "<P>\n");
	html.WriteString("<TABLE BORDER=1>\n");
	html.WriteString("<TR><TH><BR>Ranking</TH>\n");
	html.WriteString("<TH><BR>URL</TH>\n");
	html.WriteString("<TH>Successful<BR>Accesses</TH>\n");
	html.WriteString("<TH>Failed<BR>Accesses</TH>\n");
	html.WriteString("<TH>Average<BR>Access Time</TH>\n");
	html.WriteString("<TH>Best<BR>Access Time</TH>\n");
	html.WriteString("<TH>Worst<BR>Access Time</TH>\n");
	html.WriteString("<TH>Total<BR>Access Time</TH>\n");
	html.WriteString("</TR>\n");
	
	// Output sites in access time order

	for (int o = 1; o <= nURLs + 1; o++)
	{
		int nOrd = o;
		if (nOrd > nURLs)
			nOrd = -1;
		for (int u = 0; u < nURLs; u++)
		{
			if (nOrder[u] == nOrd)
			{
				// Ranking

				if (nOrd == -1)
					sLine = "N/A";
				else
					sLine.Format("%d.", nOrder[u]);
				html.WriteString("<TR><TD ALIGN=RIGHT>"
								 + sLine + "</TD>\n");
				
				// URL

				CString sFullURL = sURL[u];
				if (sFullURL.Left(5) != "http:")
					sFullURL = "http://" + sFullURL;
				html.WriteString("<TD><A HREF=\"" + sFullURL + "\">"
								 + sFullURL + "</A></TD>\n");

				// Good accesses

				sLine.Format("<TD ALIGN=RIGHT>%d</TD>\n",
							 nGoodAccesses[u]);
				html.WriteString(sLine);

				// Bad accesses

				sLine.Format("<TD ALIGN=RIGHT>%d</TD>\n",
							 nBadAccesses[u]);
				html.WriteString(sLine);

				// Average access time

				if (nBadAccesses[u] > 0)
					sLine = "N/A";
				else
					sLine.Format("<TD ALIGN=RIGHT>%0.2f secs</TD>\n",
								 dAvgAccessTime[u]);
				html.WriteString(sLine);

				// Best access time

				if (nBadAccesses[u] > 0)
					sLine = "N/A";
				else
					sLine.Format("<TD ALIGN=RIGHT>%0.2f secs</TD>\n",
								 dBestAccessTime[u]);
				html.WriteString(sLine);

				// Worst access time

				if (nBadAccesses[u] > 0)
					sLine = "N/A";
				else
					sLine.Format("<TD ALIGN=RIGHT>%0.2f secs</TD>\n",
								 dWorstAccessTime[u]);
				html.WriteString(sLine);

				// Total access time

				if (nBadAccesses[u] > 0)
					sLine = "N/A";
				else
					sLine.Format("<TD ALIGN=RIGHT>%0.2f secs</TD>\n",
								 dTotalAccessTime[u]);
				html.WriteString(sLine);

				html.WriteString("</TR>\n");
			} // End if
		} // End for u
	} // End for o
	html.WriteString("<TABLE>\n");
	sLine.Format("<P>(Each URL accessed %d times)", ACCESS_COUNT);
	html.WriteString(sLine + "\n");
	html.WriteString("</BODY>\n");
	html.WriteString("</HTML>\n");
	html.Close();
}

// **********************
// *                    *
// *  OutputToDatabase  *
// *                    *
// **********************
// Description:	Output site measurement results to database.
//				ODBC data source name (DSN): WebSpeed
//				Table name: History

void CWebSpeedDlg::OutputToDatabase()
{
	CRobotDatabase db;
	if (!db.Open("WebSpeed", "History", "", ""))
	{
		m_status = "ERROR: Unable to open WebSpeed database";
		UpdateData(false);
		return;
	} // End if

	m_status = "Updating database";
	UpdateData(false);

	// Define database record structure

	db.m_nFields = 11;

	db.m_sFieldName[0] = "When";
	db.m_sFieldName[1] = "URL";
	db.m_sFieldName[2] = "Sites";
	db.m_sFieldName[3] = "Ranking";
	db.m_sFieldName[4] = "Accesses";
	db.m_sFieldName[5] = "GoodAccesses";
	db.m_sFieldName[6] = "FailedAccesses";
	db.m_sFieldName[7] = "AvgAccessTime";
	db.m_sFieldName[8] = "BestAccessTime";
	db.m_sFieldName[9] = "WorstAccessTime";
	db.m_sFieldName[10] = "TotalAccessTime";

	db.m_bKeyField[0] = true;	db.m_bNumeric[0] = false;
	db.m_bKeyField[1] = true;	db.m_bNumeric[1] = false;
	db.m_bKeyField[2] = false;	db.m_bNumeric[2] = true;
	db.m_bKeyField[3] = false;	db.m_bNumeric[3] = true;
	db.m_bKeyField[4] = false;	db.m_bNumeric[4] = true;
	db.m_bKeyField[5] = false;	db.m_bNumeric[5] = true;
	db.m_bKeyField[6] = false;	db.m_bNumeric[6] = true;
	db.m_bKeyField[7] = false;	db.m_bNumeric[7] = true;
	db.m_bKeyField[8] = false;	db.m_bNumeric[8] = true;
	db.m_bKeyField[9] = false;	db.m_bNumeric[9] = true;
	db.m_bKeyField[10] = false;	db.m_bNumeric[10] = true;

	// Add a record for each site

	for (int u = 0; u < nURLs; u++)
	{
		db.m_sFieldValue[0] = sNow;
		db.m_sFieldValue[1] = sURL[u];
		db.m_sFieldValue[2].Format("%d", nURLs);
		db.m_sFieldValue[3].Format("%d", nOrder[u]);
		db.m_sFieldValue[4].Format("%d", ACCESS_COUNT);
		db.m_sFieldValue[5].Format("%d", nGoodAccesses[u]);
		db.m_sFieldValue[6].Format("%d", nBadAccesses[u]);
		db.m_sFieldValue[7].Format("%0.2f", dAvgAccessTime[u]);
		db.m_sFieldValue[8].Format("%0.2f", dBestAccessTime[u]);
		db.m_sFieldValue[9].Format("%0.2f", dAvgAccessTime[u]);
		db.m_sFieldValue[10].Format("%0.2f", dTotalAccessTime[u]);
	
		if (!db.AddRecord())
			MessageBox("Error: " + db.m_sErrMsg + "\n\nQuery: "
							+ db.m_sQuery, 
					   "Record Addition Failed",
					   MB_ICONEXCLAMATION);
	} // End for u
	db.Close();
}

// **************
// *            *
// *  LoadURLs  *
// *            *
// **************
// Description:	Loads URL list from previous session.
//				If Webspeed.url file exists, loads URLs from it
//				(one URL per line).
//
// Outputs:		nURLs ..... number of URLs
//				sURL[] .... contains the actual URLs

void CWebSpeedDlg::LoadURLs()
{
	CStdioFile file;
	CString sLine;

	nURLs = 0;
	m_urls_ctl.ResetContent();

	if (file.Open("WebSpeed.url", CFile::modeRead))
	{
		while (file.ReadString(sLine))
		{
			sURL[nURLs] = sLine;
			nURLs++;
			m_urls_ctl.AddString(sLine);
		} // End while
		file.Close();
	} // End if
}

// **************
// *            *
// *  SaveURLs  *
// *            *
// **************
// Description:	Saves URL list for the sake of future sessions.
//				Creates file Webspeed.url and writes 
//				one URL per line to it.
//
// Inputs:		nURLs ..... number of URLs
//				sURL[] .... contains the actual URLs

void CWebSpeedDlg::SaveURLs()
{
	CStdioFile file;

	file.Open("WebSpeed.url", CFile::modeCreate | CFile::modeWrite);
	for (int u = 0; u < nURLs; u++)
		file.WriteString(sURL[u] + "\n");
	file.Close();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -