Files
HLStock/CheckServer/WebReloadThread.cpp
2013-07-20 23:25:43 +00:00

327 lines
8.5 KiB
C++

// WebReloadThread.cpp : implementation file
//
#include "stdafx.h"
#include "CheckServer.h"
#include "WebReloadThread.h"
#include "MainFrm.h"
//////////////////////////////////////////////////////////////////////
// MSHTML 파서
#include <comdef.h>
#include <mshtml.h>
#pragma warning(disable : 4146) //see Q231931 for explaintation
#import <mshtml.tlb> no_auto_exclude
//////////////////////////////////////////////////////////////////////
#include <afxsock.h> // MFC socket extensions
#include <afxinet.h>
#include <atlbase.h>
#pragma comment(lib,"Wininet.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CWebReloadThread
UINT CWebReloadThread::tm_CWebReloadThreadOnDoContinuous = ::RegisterWindowMessage( "CWebReloadThread OnDoContinuous" );
IMPLEMENT_DYNCREATE(CWebReloadThread, CWinThread)
CWebReloadThread::CWebReloadThread()
{
m_pCMainFrame = NULL;
m_hStopEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
m_strURL[0] = _T("");
m_strURL[1] = _T("");
m_strNews[0] = _T("");
m_strNews[1] = _T("");
m_bFirst[0] = TRUE;
m_bFirst[1] = TRUE;
m_strError = _T("");
}
CWebReloadThread::~CWebReloadThread()
{
m_pCMainFrame = NULL;
}
BOOL CWebReloadThread::InitInstance()
{
// TODO: perform and per-thread initialization here
AfxOleInit();
return TRUE;
}
int CWebReloadThread::ExitInstance()
{
// TODO: perform any per-thread cleanup here
return CWinThread::ExitInstance();
}
BEGIN_MESSAGE_MAP(CWebReloadThread, CWinThread)
//{{AFX_MSG_MAP(CWebReloadThread)
// NOTE - the ClassWizard will add and remove mapping macros here.
ON_REGISTERED_THREAD_MESSAGE( tm_CWebReloadThreadOnDoContinuous, OnDoContinuous )
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWebReloadThread message handlers
//DEL int CWebReloadThread::Run()
//DEL {
//DEL // TODO: Add your specialized code here and/or call the base class
//DEL
//DEL return CWinThread::Run();
//DEL }
//void CMyThread::OnDoTest( WPARAM wParam, LPARAM lParam )
//{
// ASSERT( m_pCheckServerView ); // make sure we have the dialog to send it back to.
// char* pchar = (char*) wParam;
// CString s;
// s.Format( "%d", atol( pchar ) + 1 );
// delete pchar;
// pchar = NULL;
// pchar = new char[ s.GetLength( ) + 1 ];
// strcpy( pchar, s );
//
// //m_pCheckServerView->TestCompleted( pchar );
//
// CStatic *p = (CStatic*)m_pCheckServerView->GetDlgItem(IDC_MY_TEXT);
// p->SetWindowText(pchar);
//}
void CWebReloadThread::OnDoContinuous( WPARAM wParam, LPARAM lParam )
{
ASSERT( m_pCMainFrame ); // make sure we have the dialog to send it back to.
char* pchar = (char*) wParam;
CString s;
s.Format( "%d\n", atol( pchar ) + 1 );
delete pchar;
TRACE( s );
m_strNews[0] = _T("");
m_strNews[1] = _T("");
m_bFirst[0] = TRUE;
m_bFirst[1] = TRUE;
char *location1;
char *location2;
LPTSTR pppp1 = m_strURL[0].GetBuffer(m_strURL[0].GetLength());
location1 = pppp1;
LPTSTR pppp2 = m_strURL[1].GetBuffer(m_strURL[1].GetLength());
location2 = pppp2;
if (_strnicmp(location1, "http://",7)==0 &&
_strnicmp(location2, "http://",7)==0)
{
HINTERNET hInternet=InternetOpen("Daemon", INTERNET_OPEN_TYPE_PRECONFIG , NULL, NULL, NULL);
if (hInternet)
{
while( ::WaitForSingleObject( m_hStopEvent, 100 ) == WAIT_TIMEOUT )
{
WebReload( hInternet, location1, m_bFirst[0], m_strNews[0] );
WebReload( hInternet, location2, m_bFirst[1], m_strNews[1] );
}
}
else
{
m_strError = "The sURL location must contain 'http://' string";
}
InternetCloseHandle( hInternet );
}
::ResetEvent( m_hStopEvent );
}
BOOL CWebReloadThread::WebReload( HINTERNET hInternet, char * szUrl, BOOL &bFirst, CString &strNews )
{
// HINTERNET file_handle=InternetOpenUrl( hInternet,
// szUrl, NULL, 0,
// INTERNET_FLAG_KEEP_CONNECTION & INTERNET_FLAG_DONT_CACHE & INTERNET_FLAG_RELOAD, 0);
HINTERNET file_handle=InternetOpenUrl(hInternet,
szUrl, NULL, 0,
INTERNET_FLAG_RELOAD, 0);
if (file_handle)
{
#ifdef _DEBUG
// Beep(200, 200);
#endif
memset(m_szPageData, NULL, sizeof(m_szPageData));
char *pBuff = m_szPageData;
DWORD bytes_read=0;
DWORD bytes_read_tot=0;
BOOL bSuccess = TRUE;
while(bSuccess)
{
bSuccess = InternetReadFile(file_handle, pBuff, 1024, &bytes_read);
if( bytes_read == 0 )
break;
bytes_read_tot += bytes_read;
if( bytes_read_tot < PAGE_READMAX )
pBuff += bytes_read;
}
m_szPageData[bytes_read_tot] = 0;
//in szPageData am &bytes_read bytes de pe internet
//tre sa bag szPageData in sBody
// FILE *fp;
// if ((fp=fopen("C:\\HTTPFILE.TXT","wb")) !=NULL)
// {
// fwrite(m_szPageData, bytes_read, 1, fp);
// fclose(fp);
// }
// CString strBody = CString (m_szPageData);
// strBody = strBody.Left(bytes_read_tot);
// long lll= strBody.GetLength();
PageParser( m_szPageData, bFirst, strNews );
//Sleep(0);
//AfxMessageBox("GetPage: "+ sURL + " "+ sBody);
}
else
{
m_strError = "Is not possible to connect to internet";
return FALSE;
}
::InternetCloseHandle( file_handle ) ;
return TRUE;
}
BOOL CWebReloadThread::PageParser(char * szBody, BOOL &bFirst, CString &strNews)
{
BOOL bRet = TRUE;
try
{
//declare our MSHTML variables and create a document
MSHTML::IHTMLDocument2Ptr pDoc;
MSHTML::IHTMLDocument3Ptr pDoc3;
MSHTML::IHTMLElementCollectionPtr pCollection;
MSHTML::IHTMLElementPtr pElement;
HRESULT hr = CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER,
IID_IHTMLDocument2, (void**)&pDoc);
if (SUCCEEDED(hr))
{
//put the code into SAFEARRAY and write it into document
SAFEARRAY* psa = SafeArrayCreateVector(VT_VARIANT, 0, 1);
VARIANT *param;
bstr_t bsData = (LPCTSTR)szBody;
hr = SafeArrayAccessData(psa, (LPVOID*)&param);
param->vt = VT_BSTR;
param->bstrVal = (BSTR)bsData;
hr = pDoc->write(psa);
hr = pDoc->close();
SafeArrayDestroy(psa);
//I'll use IHTMLDocument3 to retrieve tags. Note it is available only in IE5+
//If you don't want to use it, u can just run through all tags in HTML
//(IHTMLDocument2->all property)
pDoc3 = pDoc;
CString NewNews = "";
//display HREF parameter of every link (A tag) in ListBox
pCollection = pDoc3->getElementsByTagName(L"A");
for(long i=0; i<pCollection->length; i++)
{
pElement = pCollection->item(i, (long)0);
if(pElement != NULL)
{
//second parameter says that you want to get text inside attribute as is
bstr_t bstrTag = pElement->getAttribute("href", 2);
// m_wndLinksList.AddString( (LPCTSTR)bstrTag );
if( strncmp( (LPCTSTR)bstrTag, "javascript:viewCrp", 18 ) == 0 ) // 회사명.
{
CComBSTR bstrText;
pElement->get_innerText( &bstrText );
NewNews = bstrText;
NewNews += "@";
}
else if( strncmp( (LPCTSTR)bstrTag, "javascript:viewDoc", 18 ) == 0 ) // 공시내용.
{
if( NewNews != "" )
{
CComBSTR bstrText;
pElement->get_innerText( &bstrText );
NewNews += CString(bstrText);
if( bFirst )
{
bFirst = FALSE;
strNews = NewNews;
break;
}
#ifdef _DEBUG
TRACE2( "%s, %s\n", strNews, NewNews );
#endif
if( strNews != NewNews )
{
strNews = NewNews;
Beep(2000,200);
char * pchar = new char[ NewNews.GetLength( ) + 1 ];
strcpy( pchar, NewNews );
m_pCMainFrame->WebNewsFinded( pchar );
return TRUE;
}
}
return FALSE;
}
}
}
}
}
catch( _com_error e )
{
TRACE("CWebReloadThread::PageParser catch(_com_error)\n");
}
catch(...)
{
TRACE("CWebReloadThread::PageParser catch(...)\n");
}
return FALSE;
}
/*
<tr>
<td bgcolor="#F2F8F2" height="24" align="center">21:02</td>
<td height="24">&nbsp;</td>
<td height="24"><a HREF="javascript:viewCrp('hgngjklgih');" >한마음상호저축은행</a></td>
<td bgcolor="#F2F8F2" height="24">&nbsp;</td>
<td bgcolor="#F2F8F2" height="24"><A HREF="javascript:viewDoc(20040917000308);" ><font color = "FF5E06"> </font>감사보고서제출</A></td>
<td height="24">&nbsp;</td>
<td height="24">한마음상호저축은행</td>
<td bgcolor="#F2F8F2" align="center">2004.09.18</td>
<td align="center"></td>
</tr>
*/