📄 webspeeddlg.cpp
字号:
// WebSpeedDlg.cpp : implementation file
//
#include "stdafx.h"
#include "WebSpeed.h"
#include "WebSpeedDlg.h"
#include "CRobot.h"
#include "CRobotInternet.h"
#include "CRobotDatabase.h"
#include <afxmt.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// **********************
// * *
// * Global Variables *
// * *
// **********************
#define ACCESS_COUNT 3
#define MAX_URLS 100
int nURLs;
CString sURL[MAX_URLS];
int nGoodAccesses[MAX_URLS];
int nBadAccesses[MAX_URLS];
double dAvgAccessTime[MAX_URLS];
double dBestAccessTime[MAX_URLS];
double dWorstAccessTime[MAX_URLS];
double dTotalAccessTime[MAX_URLS];
int nOrder[MAX_URLS];
int nActiveThreads = 0;
int nOrderNo = 0;
CCriticalSection cs;
CString sNow;
// **************** Global function
// * *
// * AccessSite *
// * *
// ****************
// Description: Thread procedure to access a site and measure time
//
// Inputs: pParam - address of sURL[] array element
UINT AccessSite(LPVOID pParam)
{
// Get URL index number that function AfxBeginThread passed
int u = *((int*) pParam);
// Initialize statistics for this site
nGoodAccesses[u] = 0;
nBadAccesses[u] = 0;
dAvgAccessTime[u] = -1;
dBestAccessTime[u] = -1;
dWorstAccessTime[u] = -1;
dTotalAccessTime[u] = 0.0;
CRobotInternet internet;
int nResult;
CString sErrMsg;
CString sHTML;
DWORD tStart, tFinish;
double access_time = 0.0;
double elapsed_time = 0.0;
// Main loop to repeatedly access site and gather
// timing information
for (int nTry = 0; nTry < ACCESS_COUNT; nTry++)
{
tStart = GetTickCount();
if (internet.httpGet(sURL[u], sHTML, nResult, sErrMsg))
{
tFinish = GetTickCount();
access_time += (double(tFinish - tStart) / 1000.0);
elapsed_time += access_time;
cs.Lock();
nGoodAccesses[u]++;
if (dBestAccessTime[u] == -1
|| access_time < dBestAccessTime[u])
dBestAccessTime[u] = access_time;
if (dWorstAccessTime[u] == -1
|| access_time > dWorstAccessTime[u])
dWorstAccessTime[u] = access_time;
cs.Unlock();
} // End if
else
nBadAccesses[u]++;
} // End for
// Compute and store average access time and total access time
cs.Lock();
if (nGoodAccesses[u] > 0)
dAvgAccessTime[u] = (elapsed_time) / nGoodAccesses[u];
else
dAvgAccessTime[u] = -1;
dTotalAccessTime[u] = elapsed_time;
// Store ranking order number if no access failures occurred
if (nBadAccesses[u] == 0)
{
nOrderNo++;
nOrder[u] = nOrderNo;
} // End if
// Decrease active thread count and return (terminate this thread)
nActiveThreads--;
cs.Unlock();
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWebSpeedDlg dialog
CWebSpeedDlg::CWebSpeedDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWebSpeedDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWebSpeedDlg)
m_db = FALSE;
m_html = FALSE;
m_repeat = FALSE;
m_status = _T("");
m_urls = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CWebSpeedDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWebSpeedDlg)
DDX_Control(pDX, IDC_URLS, m_urls_ctl);
DDX_Check(pDX, IDC_DB, m_db);
DDX_Check(pDX, IDC_HTML, m_html);
DDX_Check(pDX, IDC_REPEAT, m_repeat);
DDX_Text(pDX, IDC_STATUS, m_status);
DDX_CBString(pDX, IDC_URLS, m_urls);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWebSpeedDlg, CDialog)
//{{AFX_MSG_MAP(CWebSpeedDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_REMOVE, OnRemove)
ON_BN_CLICKED(IDC_RUN, OnRun)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_STOP, OnStop)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWebSpeedDlg message handlers
BOOL CWebSpeedDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING,
IDM_ABOUTBOX,
strAboutMenu);
}
}
// Set the icon for this dialog.
// The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
nURLs = 0;
LoadURLs();
bRunning = false;
m_html = true;
m_status = "Ready";
UpdateData(false);
GetDlgItem(IDC_URLS)->SetFocus();
return false; //return TRUE unless you set the focus to a control
}
void CWebSpeedDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CWebSpeedDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CWebSpeedDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
// ***********
// * *
// * OnAdd *
// * *
// ***********
// Description: Message handler for the Add button.
// Adds the entered item to the URL list.
void CWebSpeedDlg::OnAdd()
{
if (nURLs >= MAX_URLS)
{
MessageBox("The URL list is full. You must delete a URL "
"before you can enter any new ones.",
"Out Of Memory",
MB_ICONEXCLAMATION);
return;
} // End if
CWaitCursor wc;
UpdateData(true);
sURL[nURLs] = m_urls;
nURLs++;
m_urls_ctl.AddString(m_urls);
m_urls = "";
UpdateData(false);
m_urls_ctl.SetFocus();
}
// **************
// * *
// * OnRemove *
// * *
// **************
// Description: Message handler for the Remove button.
// Removes the selected item from the URL list.
void CWebSpeedDlg::OnRemove()
{
CWaitCursor wc;
UpdateData(true);
for (int u = 0; u < nURLs; u++)
{
if (sURL[u] == m_urls)
{
for (int u2 = u; u2 < nURLs - 1; u2++)
sURL[u2] = sURL[u2 + 1];
u = nURLs;
nURLs--;
}
} // End for
m_urls_ctl.DeleteString(m_urls_ctl.GetCurSel());
m_urls = "";
UpdateData(false);
m_urls_ctl.SetFocus();
}
// ***********
// * *
// * OnRun *
// * *
// ***********
// Description: Message handler for the Run button.
// Saves URL list by calling SaveURLs().
// Checks site by calling ScanSites().
// If check box for repetition is checked,
// sets timer to repeat scan in 15 minutes.
void CWebSpeedDlg::OnRun()
{
UpdateData(true);
if (nURLs < 1)
{
MessageBox("No URLs have been defined.\n\n"
"At least one URL must be defined "
"before you can Run.",
"No URLs Defined",
MB_ICONEXCLAMATION);
GetDlgItem(IDC_URLS)->SetFocus();
return;
} // End if
CWaitCursor wc;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -