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

📄 mainfrm.cpp

📁 非常好用的VC++源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	}

}

//Line Parse API
//parse a line and store all the fields to tollerInfo structure
/*
The structure for the logfile toller-based info line:

  FromIPAddr;
  -:Seperator
  (loginName,-);loginName is concerned with the FromIPAddr,
				-means the failure of this connection,this
				line will be bypassed.
  [Data+Time+TimeArea]
  "+(GET,PUT,POST)+webAddress+HTTP Version":
  resultcode:integer
  (traffic,-):traffic:the size of webpage or other services info download from
		remote server,- means the invalid operation,this line may be bypased.
*/
BOOL	CMainFrame::ParseLine(CString aLine)
{
	//Is there a timezone in the logfile information line
	CString sIPAddr,sSeperator,sLoginName;
	CString sDateTime,sTimeZone,sOperation,sResultCode,sTraffic;
	CString sTmp;

	sIPAddr=aLine.Left(aLine.Find(' '));
	sTmp=aLine.Right(aLine.GetLength()-aLine.Find(' ')-1);

	sSeperator=sTmp.Left(sTmp.Find(' '));
	sTmp=sTmp.Right(sTmp.GetLength()-sTmp.Find(' ')-1);

	sLoginName=sTmp.Left(sTmp.Find(' '));
	sTmp=sTmp.Right(sTmp.GetLength()-sTmp.Find(' ')-1);

	if(sLoginName=="-")return FALSE;//It is an invalid http request,we bypass it.

	sDateTime=sTmp.Left(sTmp.Find('"'));//Including the timezone information
	sTmp=sTmp.Right(sTmp.GetLength()-sTmp.Find('"')-1);

	//If we turn off the timezone,comment the following two lines
	/*sTimeZone=sTmp.Left(sTmp.Find(' '));
	sTmp=sTmp.Right(sTmp.GetLength()-sTmp.Find(' ')-1);
	*/

	sOperation=sTmp.Left(sTmp.Find('"'));
	sTmp=sTmp.Right(sTmp.GetLength()-sTmp.Find('"')-1);

	//trim the left leading space from the rest string
	sTmp.TrimLeft();
	sResultCode=sTmp.Left(sTmp.Find(' '));
	sTmp=sTmp.Right(sTmp.GetLength()-sTmp.Find(' ')-1);
	//result code starts with space character

	sTraffic=sTmp.Left(sTmp.Find(' '));
//	sTmp=sTmp.Right(sTmp.GetLength()-sTmp.Find(' ')-1);

	if(!IsOperationNeedToll(sOperation))return FALSE;

	int nTotal=0;
	int nSubTotal=0;
	char szTraffic[10];
	strcpy(szTraffic,sTraffic);

	nTotal=atoi(szTraffic);

	//if current item's timestamp is today,
	//store it into the nSubTotal
	//if(IsToday(sDateTime))//certainly it is today's information
	nSubTotal=nTotal;

	tollerInfo.Put(sLoginName,sIPAddr,nSubTotal,nTotal);

	//add current tollerInfo to the toller list.
	AddToTollerList(tollerInfo);
	return TRUE;
}

//
//is the operation need to be tolled into database
//
BOOL    CMainFrame::IsOperationNeedToll(CString sOperation)
{
	/*now we parse the operation string
	It is like the following form:
		GET http://www.njnet.edu.cn/www/backgrd/wall12.jpg HTTP/1.0"
	*/

	if(sOperation.Find("GET")==-1)return FALSE;
	//the operation is not the GET,
	//so we bypass it

	/*And now we analyze the operation address.
	There are two kinds of address:digital address and 
	alphabet address we should take into account.
	the address is delimited with two string:// or /
		//www.njnet.edu.cn/
		if it is an alphabet form,we should see if there is
		the string 'edu.cn' in it
		or
		//206.106.140.134/
		we tell it using the IsCERNAddr()
	*/
	
	CString sTarget,sTmp;
	//sTarget:the target IP Address of the server

	//we should minus 2 because the // has two chars
	sTmp=sOperation.Right(sOperation.GetLength()-sOperation.Find("//")-2);
	sTarget=sTmp.Left(sTmp.Find('/'));

	//if there is a space,like the form  "www.njupt.edu.cn HTTP1.0/"
	if(sTarget.Find(' ')!=-1)
	{
		sTarget=sTarget.Left(sTarget.Find(' '));
	}

	if(sTarget.Find(':')!=-1)
	{
		sTarget=sTarget.Left(sTarget.Find(':'));
	}

	if(sTarget.IsEmpty())return FALSE;

	char a=sTarget.GetAt(0);
		
	if(IsCharAlphaNumeric((TCHAR)a) && !IsCharAlpha((TCHAR)a))
	{
		//It is a form of numbers:206.106.140.134
		if(IsCERNAddr(sTarget))
		//If it is a CERNET IP Addr,needn't to be tolled.
			return FALSE;
	}

	/*else it is alphabet form like:
		www.microsoft.com
		www.njupt.edu.cn
		Is there a .edu.cn substring in the Target address
	*/
	sTarget.MakeLower();
	CString sSuffix;
	sSuffix=sTarget.Right(3);
	if(sSuffix==".cn")//.cn.net need to be tolled.
		return FALSE;//it is a CERNET IP Addr

	return TRUE;
}

//is the string specified by sTimeDate today?
BOOL	CMainFrame::IsToday(CString& sTimeDate)
{
	/*The timedate string has the following form:
		[05/Nov/1997:00:15:02 +0800]	
	*/
	CTime time = CTime::GetCurrentTime();
	CString strDate = time.Format(_T("%d/%b/%Y"));

	return(sTimeDate.Find(strDate)!=-1);
}

//
//is the user a special user(excluded from tolling),not used in toller system now
//
BOOL	CMainFrame::IsSpecialUser(CString loginName)
{
	CString item;
	POSITION pos;
	
	pos=SpecialUserList.GetHeadPosition();
	if(pos==NULL)
	{
		return FALSE;
	}

	/*
	item=SpecialUserList.GetAt(pos);
	if(item==loginName)return TRUE;
	*/

	while(pos!=NULL)
	{
		item=SpecialUserList.GetNext(pos);
		if(item==loginName)return TRUE;//find the loginname in
		//the special user list
	}	

	//traverse all the list items and not find the loginName
	return FALSE;
}

//is the digital form IP address a cernet ip addr?
//Is it a CERNET IP Address
BOOL	CMainFrame::IsCERNAddr(CString sIPAddr)
{
	POSITION pos;
	if((pos=CERNIPPrefixList.GetHeadPosition())==NULL)return FALSE;

	CString item;

	while(pos!=NULL)
	{
		item=CERNIPPrefixList.GetNext(pos);
		
		if(sIPAddr.Find(item)!=-1)return TRUE;
	}

	return FALSE;
}


//
//check if it is end of the toller month,if it is,
//we need to clear the toller.dbf
//and generate the monthly report.
//
void CMainFrame::CheckIfEndOfTollerMonth()
{
	CTime time=CTime::GetCurrentTime();

	int nDay=time.GetDay();
	
	if(nDay==m_nTollFromDate)
	{
		int nMonth=time.GetMonth();

		CString sMonth=NumToStr(nMonth);

		sMonth="c:\\toller\\database\\tolllistofmonth"+sMonth;
		sMonth+=".htm";

		ReportAsHTML(sMonth);

		//now it is the end of the toller month
		//we need to update the toller database.
		sDatabaseName="c:\\toller\\database\\toller.dbf";
	
		//clear content of the file
		CFile file;
		if(!file.Open(sDatabaseName,CFile::modeRead|CFile::modeCreate))
		{
			MessageBeep(0);
			return;
		}
		file.Close();

		//also clear and reset the toller buffer.
		FreeTollerList();
	}
}

//
//test the collect function
//
void CMainFrame::OnTestCollect() 
{
	// TODO: Add your command handler code here
	Collect();
}

//
//test Update database function
//
void CMainFrame::OnTestUpdatedatabase() 
{
	// TODO: Add your command handler code here
	UpdateTollerInfoDatabase();
	
}


//
//Init Internet Session for ftp connection
//
void CMainFrame::InitInternetSession()
{
	CString str;
	if (!str.LoadString(IDS_APPNAME))
		str = _T("AppUnknown");
	m_pInetSession = new CInternetSession(str, 1, PRE_CONFIG_INTERNET_ACCESS);

	// Alert the user if the internet session could
	// not be started and close app
	if (!m_pInetSession)
	{
		//AfxMessageBox(IDS_BAD_SESSION, MB_OK);
		MessageBeep(0);
	}
}


//
//connect to remote ftp server with userID and password
//
BOOL CMainFrame::ConnectFtpServer()
{
	// ConnectFtpServer provides some basic error-checking before 
	// passing a pointer to a FtpConnection (and possibly a path
	// to expand) to the tree control

	CString strFtpSite;
	CString strServerName;
	CString strObject;
	INTERNET_PORT nPort;
	DWORD dwServiceType;

	if (m_pFtpConnection != NULL)
		m_pFtpConnection->Close();

	delete m_pFtpConnection;

	m_pFtpConnection = NULL;

	strFtpSite=sFTPServerName;
	strFtpSite+=sLogFileFTPAddr;

	// check to see if this is a reasonable URL -- 
	// ie "ftp://servername/dirs" or just "servername/dirs"

	if (!AfxParseURL(strFtpSite, dwServiceType, strServerName, strObject, nPort))
	{
		// try adding the "ftp://" protocol
		CString strFtpURL = _T("ftp://");
		strFtpURL += strFtpSite;

		if (!AfxParseURL(strFtpURL, dwServiceType, strServerName, strObject, nPort))
		{
			Error(IDS_INVALID_URL);
			//m_FtpTreeCtl.PopulateTree();
			return FALSE;
		}
	}


	CWaitCursor cursor;	// this will automatically display a wait cursor

	//port number=21 for command transfer
	//port number=20 for data transfer

	nPort=21;

	// Now open an annonymous FTP connection to the server
	if ((dwServiceType == INTERNET_SERVICE_FTP) && !strServerName.IsEmpty())
	{
		try
		{
			m_pFtpConnection = m_pInetSession->GetFtpConnection(strServerName,
				m_sUserName,
				m_sPassword,
				nPort);
		}

		catch (CInternetException* pEx)
		{
			// catch errors from WinINet
			TCHAR szErr[1024];
			if (pEx->GetErrorMessage(szErr, 1024))
				Error(szErr);
			else
				Error(szErr);
			pEx->Delete();
			m_pFtpConnection = NULL;
		}
	}
	else
	{
		//AfxMessageBox(IDS_INVALID_URL, MB_OK);
		Error(IDS_INVALID_URL);
	}

	if (m_pFtpConnection != NULL)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

//从proxy server上获取以sLogFileFTPAddr为目录,sFileName为文件
//名的文件
//get file from ftp server
//sFileName:specify the logfilename like acess1101
BOOL CMainFrame::FtpGetFile(CString sFileName)
{
	CString sRemotePath(""),sLocalPath("");

	BOOL bReturn;
	bReturn=m_pFtpConnection->SetCurrentDirectory(sLogFileFTPAddr);
	/*sFTPServerName+":"+*/
	
	sRemotePath=sFileName;
	sLocalPath="c:\\toller\\proxylog\\access";
//	sLocalPath="access";

	BOOL bResult=m_pFtpConnection->GetFile(sRemotePath,sLocalPath,
		FALSE,//allow overwrite the logfile
		FILE_ATTRIBUTE_NORMAL,
		FTP_TRANSFER_TYPE_ASCII);
/*
	if(bResult!=0)
	{
		//AfxMessageBox("Download file succeeded!");
	}
	else
	{
		DWORD dwError;
		dwError=GetLastError();
		
		//AfxMessageBox("Error in download log file.");
	}
*/
	return bResult;
}

//
//close ftp connection from remote server
//
void CMainFrame::CloseFtpConnect()
{
	if (m_pFtpConnection != NULL)
	m_pFtpConnection->Close();

	delete m_pFtpConnection;

	m_pFtpConnection = NULL;
}

//
//make a log filename with date
//
CString CMainFrame::MakeLogFileName()
{
	CString sName;
	CTime time=CTime::GetCurrentTime();

	//int nMonth,nDate;
	CString sMonth,sDate,sAMPM;
	sMonth.Format("%d",time.GetMonth());
	sDate=NumToStr(time.GetDay());

	if(nServerUpdateLogAt<12)
		sAMPM="上午";
	else
		sAMPM="下午";
	
	sName="access."+sDate+sMonth+"月-"+
		  NumToStr(nServerUpdateLogAt)+
		  sAMPM;

	return sName;
}

//将数字n转化为字符串形式,如1转化为"1"
CString CMainFrame::NumToStr(int n)
{
	CString str;
	if(n<10)
	{
		str.Format("%d",n);
		str="0"+str;
	}
	else
	{
		str.Format("%d",n);
	}

	return str;
}


//
//error message handler
//
void CMainFrame::Error(UINT nError)
{
	CString sErr;
	sErr.LoadString(nError);

	StatusText(sErr);

}

//
//
//
void CMainFrame::Error(char* szError)
{
	CString sErr(szError);

	StatusText(sErr);
}
	
//
//test the fpt connect function
//
void CMainFrame::OnFtpConnect() 
{
	// TODO: Add your command handler code here
//	sFTPServerName="ftp.njupt.edu.cn";

	if(!ConnectFtpServer())
		AfxMessageBox("Fail to connect remote server.");
	else
		AfxMessageBox("Connect remote server succeeds!");

	CloseFtpConnect();
}

//
//test the query function
//
void CMainFrame::OnDebugQuery() 
{
	// TODO: Add your command handler code here
	CTollerInfo tollerInfo;
	if(Query("232014",tollerInfo))
	{
		CString sResult;
		sResult.Format("Month Fee=%d,\nToday Fee=%d",
			tollerInfo.nTotal,tollerInfo.nSubTotal);
		sResult+=CString(tollerInfo.szIPAddr);
		AfxMessageBox(sResult);

	}
}

//
//prompt report filename and format dialog,and g
//generate the report
//
void CMainFrame::OnSystemReport() 
{
	// TODO: Add your command handler code here
	CReportFormatDlg dialog;

	dialog.m_nReportFormat=1;//HTML Format
	dialog.m_sPath="c:\\toller\\database\\tolllist.htm";
	
	if(dialog.DoModal()==IDOK)
	{
	   int   nFormat=dialog.m_nReportFormat;
	   if(nFormat==0)
		ReportAsText(dialog.m_sPath);
	   else
		ReportAsHTML(dialog.m_sPath);
	}
}
//
//test download log file from proxy server function
//
void CMainFrame::OnDnloadLogfile() 
{
	// TODO: Add your command handler code here
	if(ConnectFtpServer())
	{
	//	CString sFileName="access";
		FtpGetFile(MakeLogFileName());
		CloseFtpConnect();
	}
}

void CMainFrame::OnManualUpdate() 
{
	// TODO: Add your command handler code here
	CTime time=CTime::GetCurrentTime();
	CString	strDate=time.Format(_T("%x"));

	
	//ftp to get log file from proxy server
	if(ConnectFtpServer())
	{
		//CString sFileName=MakeLogFileName();
		FtpGetFile(MakeLogFileName());
		CloseFtpConnect();
	}

	//another day's toller information,we must first clear subtotal information
	ClearSubTotal();

	Collect();
	UpdateTollerInfoDatabase();
	
	CString sDate=time.Format("%m%d");
	//generate everyday report
	CString strReportFileName;
	strReportFileName="c:\\toller\\report\\report"+sDate+".htm";
	ReportAsHTML(strReportFileName);	
	strReportFileName="c:\\toller\\report\\report"+sDate+".txt";
	ReportAsText(strReportFileName);
	sLastDate=strDate;//let lastDate=Current Date

	CWinApp* pApp=AfxGetApp();
	pApp->WriteProfileString("System","LastDate",sLastDate);
}

⌨️ 快捷键说明

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