Old fashion design for PC-104
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

1157 lines
35 KiB

//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright © 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 09/11/2003
// PROJECT:
// SUBSYSTEM:
// FILE: LgReportProvider.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of LgReportProvider.cpp
//----------------------------------------------------------------------------
#include "stdafx.h"
#include "LgReportProvider.h"
CLgReportProvider::CLgReportProvider()
{
_tcsncpy( m_pRootPath, _T("\\REPORT"), MAX_PATH );
memset( &m_Storage, 0, sizeof(m_Storage) );
memset( &m_Loader, 0, sizeof(m_Loader) );
m_bPendingReset = FALSE;
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgReportProvider::CLgReportProvider") );
}
CLgReportProvider::~CLgReportProvider()
{
LoaderProviderToClientLinkP::Unlink();
StorageProviderToClientLinkSP::Unlink();
Destroy();
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgReportProvider::~CLgReportProvider") );
}
BOOL CLgReportProvider::Create( LPCTSTR pProviderName, LPCTSTR pPathRoot, INT nMaxStorageDays )
{
if( !CLgReportLoaderProvider::Create( pProviderName ) )
return FALSE;
_tcsncpy( m_pRootPath, pPathRoot, MAX_PATH );
m_nMaxStorageDays = nMaxStorageDays;
if( !DoWriteReset() )
return FALSE;
return Start( FALSE );
}
void CLgReportProvider::Destroy()
{
Stop( TRUE );
CLgReportLoaderClient::Destroy();
}
E_REPORT_RESULT CLgReportProvider::DoReadSite( REPORT_HANDLE rHandle, PREPORT_POSITION pPosition, PREPORT_PATH ppPath, INT nCount, PINT pRead )
{
return ReadSite( rHandle, pPosition, ppPath, nCount, pRead );
}
E_REPORT_RESULT CLgReportProvider::DoReadSiteDate( REPORT_HANDLE rHandle, PREPORT_POSITION pPosition, PREPORT_DATE pDate, INT nCount, PINT pRead )
{
// lock handles
CLgLocker Lock( GetHandleLock() );
// get handle info
REPORT_HANDLE_INFO *pHandleInfo = GetHandleInfo( rHandle );
// if provider handle is valid then pass it through
if( pHandleInfo->rProviderHandle != REPORT_INVALID_HANDLE_VALUE )
return ReadSiteDate( rHandle, pPosition, pDate, nCount, pRead );
TCHAR pSearchPath[MAX_PATH];
_tcsncpy( pSearchPath, m_pRootPath, MAX_PATH );
::PathAppend( pSearchPath, _T("*.*") );
// prImage errors on upper levels
*pRead = 0;
// end of search
BOOL bEnd = TRUE;
WIN32_FIND_DATA ffd;
HANDLE hFindFile = ::FindFirstFile( pSearchPath, &ffd );
if( hFindFile != INVALID_HANDLE_VALUE )
{
REPORT_POSITION pos = 0;
while( TRUE )
{
INT nYear, nMonth, nDay;
if( ( ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) == FILE_ATTRIBUTE_DIRECTORY &&
ffd.cFileName[0] != _T('.') &&
_stscanf( ffd.cFileName, PATH_DATE_FORMAT, &nYear, &nMonth, &nDay ) == 3 )
{
if( pos++ >= *pPosition )
{
// mark that it is not the end of search
bEnd = FALSE;
// store value
PREPORT_DATE pDatePtr = pDate + (*pRead)++;
pDatePtr->bYear = nYear;
pDatePtr->bMonth = nMonth;
pDatePtr->bDay = nDay;
// decrease number
if( !--nCount )
break;
}
}
// get next data
if( !::FindNextFile( hFindFile, &ffd ) )
{
bEnd = TRUE;
break;
}
}
// close handle
::FindClose( hFindFile );
}
// update position
*pPosition = *pPosition + *pRead;
return bEnd ? RR_RESULT_SUCCESS : RR_RESULT_SUCCESS_PENDING;
}
E_REPORT_RESULT CLgReportProvider::DoReadSiteDateTime( REPORT_HANDLE rHandle, PREPORT_POSITION pPosition, PREPORT_DATE_TIME pDateTime, INT nCount, PINT pRead )
{
// lock handles
CLgLocker Lock( GetHandleLock() );
// get handle info
REPORT_HANDLE_INFO *pHandleInfo = GetHandleInfo( rHandle );
// if provider handle is valid then pass it through
if( pHandleInfo->rProviderHandle != REPORT_INVALID_HANDLE_VALUE )
return ReadSiteDateTime( rHandle, pPosition, pDateTime, nCount, pRead );
TCHAR pSearchPath[MAX_PATH];
BuildPath( pSearchPath, NULL, &pHandleInfo->rDateTime );
::PathAppend( pSearchPath, _T("*.*") );
// prImage errors on upper levels
*pRead = 0;
// end of search
BOOL bEnd = TRUE;
WIN32_FIND_DATA ffd;
HANDLE hFindFile = ::FindFirstFile( pSearchPath, &ffd );
if( hFindFile != INVALID_HANDLE_VALUE )
{
REPORT_POSITION pos = 0;
while( TRUE )
{
INT nHour, nMinute, nSecond;
if( ( ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) == FILE_ATTRIBUTE_DIRECTORY &&
ffd.cFileName[0] != _T('.') &&
_stscanf( ffd.cFileName, PATH_TIME_FORMAT, &nHour, &nMinute, &nSecond ) == 3 )
{
if( pos++ >= *pPosition )
{
// mark that it is not the end of search
bEnd = FALSE;
// store value
PREPORT_DATE_TIME pDateTimePtr = pDateTime + (*pRead)++;
pDateTimePtr->bYear = pHandleInfo->rDateTime.bYear;
pDateTimePtr->bMonth = pHandleInfo->rDateTime.bMonth;
pDateTimePtr->bDay = pHandleInfo->rDateTime.bDay;
pDateTimePtr->bHour = nHour;
pDateTimePtr->bMinute = nMinute;
pDateTimePtr->bSecond = nSecond;
// decrease number
if( !--nCount )
break;
}
}
// get next data
if( !::FindNextFile( hFindFile, &ffd ) )
{
bEnd = TRUE;
break;
}
}
// close handle
::FindClose( hFindFile );
}
// update position
*pPosition = *pPosition + *pRead;
return bEnd ? RR_RESULT_SUCCESS : RR_RESULT_SUCCESS_PENDING;
}
E_REPORT_RESULT CLgReportProvider::DoReadSiteDateTimeBackground( REPORT_HANDLE rHandle, PREPORT_IMAGE pImage )
{
// lock handles
CLgLocker Lock( GetHandleLock() );
// get handle info
REPORT_HANDLE_INFO *pHandleInfo = GetHandleInfo( rHandle );
// if provider handle is valid then pass it through
if( pHandleInfo->rProviderHandle != REPORT_INVALID_HANDLE_VALUE )
return ReadSiteDateTimeBackground( rHandle, pImage );
// create path
TCHAR pFullPath[MAX_PATH];
BuildPathFull( pFullPath, pHandleInfo->rPath, &pHandleInfo->rDateTime );
::PathAppend( pFullPath, PATH_BACKGROUND );
return pImage->Load( pFullPath ) ? RR_RESULT_SUCCESS : RR_RESULT_FAILURE;
}
E_REPORT_RESULT CLgReportProvider::DoReadSiteDateTimeFilterEvent( REPORT_HANDLE rHandle, PREPORT_POSITION pPosition, PREPORT_FILTER_EVENT pFilterEvent, INT nCount, PINT pRead )
{
// lock handles
CLgLocker Lock( GetHandleLock() );
// get handle info
REPORT_HANDLE_INFO *pHandleInfo = GetHandleInfo( rHandle );
// if provider handle is valid then pass it through
if( pHandleInfo->rProviderHandle != REPORT_INVALID_HANDLE_VALUE )
return ReadSiteDateTimeFilterEvent( rHandle, pPosition, pFilterEvent, nCount, pRead );
// create path
TCHAR pFullPath[MAX_PATH];
BuildPathFull( pFullPath, pHandleInfo->rPath, &pHandleInfo->rDateTime );
::PathAppend( pFullPath, PATH_FILTER_EVENT );
// reset counter
*pRead = 0;
HANDLE hFile = ::CreateFile( pFullPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
if( hFile == INVALID_HANDLE_VALUE )
return RR_RESULT_FAILURE;
// end of read
E_REPORT_RESULT eResult = RR_RESULT_SUCCESS_PENDING;
// read data
LARGE_INTEGER liPos;
liPos.QuadPart = *pPosition * sizeof( REPORT_FILTER_EVENT );
if( ::SetFilePointer( hFile, liPos.LowPart, &liPos.HighPart, FILE_BEGIN ) != INVALID_SET_FILE_POINTER )
{
DWORD dwRead = 0UL;
DWORD dwToRead = sizeof(REPORT_FILTER_EVENT) * nCount;
if( !::ReadFile( hFile, pFilterEvent, dwToRead, &dwRead, NULL ) || dwToRead != dwRead )
eResult = RR_RESULT_SUCCESS;
*pRead = dwRead / sizeof(REPORT_FILTER_EVENT);
}
else
eResult = RR_RESULT_SUCCESS;
// update position
*pPosition = *pPosition + *pRead;
::CloseHandle( hFile );
return eResult;
}
E_REPORT_RESULT CLgReportProvider::DoReadSiteDateTimeFilterSource( REPORT_HANDLE rHandle, PREPORT_POSITION pPosition, PREPORT_FILTER_SOURCE pFilterSource, INT nCount, PINT pRead )
{
// lock handles
CLgLocker Lock( GetHandleLock() );
// get handle info
REPORT_HANDLE_INFO *pHandleInfo = GetHandleInfo( rHandle );
// if provider handle is valid then pass it through
if( pHandleInfo->rProviderHandle != REPORT_INVALID_HANDLE_VALUE )
return ReadSiteDateTimeFilterSource( rHandle, pPosition, pFilterSource, nCount, pRead );
// create path
TCHAR pFullPath[MAX_PATH];
BuildPathFull( pFullPath, pHandleInfo->rPath, &pHandleInfo->rDateTime );
::PathAppend( pFullPath, PATH_FILTER_SOURCE );
// reset counter
*pRead = 0;
HANDLE hFile = ::CreateFile( pFullPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
if( hFile == INVALID_HANDLE_VALUE )
return RR_RESULT_FAILURE;
// end of read
E_REPORT_RESULT eResult = RR_RESULT_SUCCESS_PENDING;
// read data
LARGE_INTEGER liPos;
liPos.QuadPart = *pPosition * sizeof( REPORT_FILTER_SOURCE );
if( ::SetFilePointer( hFile, liPos.LowPart, &liPos.HighPart, FILE_BEGIN ) != INVALID_SET_FILE_POINTER )
{
DWORD dwRead = 0UL;
DWORD dwToRead = sizeof(REPORT_FILTER_SOURCE) * nCount;
if( !::ReadFile( hFile, pFilterSource, dwToRead, &dwRead, NULL ) || dwToRead != dwRead )
eResult = RR_RESULT_SUCCESS;
*pRead = dwRead / sizeof(REPORT_FILTER_SOURCE);
}
else
eResult = RR_RESULT_SUCCESS;
// update position
*pPosition = *pPosition + *pRead;
::CloseHandle( hFile );
return eResult;
}
E_REPORT_RESULT CLgReportProvider::DoReadSiteDateTimeEvent( REPORT_HANDLE rHandle, PREPORT_POSITION pPosition, PREPORT_EVENT pEvent, INT nCount, PINT pRead )
{
// lock handles
CLgLocker Lock( GetHandleLock() );
// get handle info
REPORT_HANDLE_INFO *pHandleInfo = GetHandleInfo( rHandle );
// if provider handle is valid then pass it through
if( pHandleInfo->rProviderHandle != REPORT_INVALID_HANDLE_VALUE )
return ReadSiteDateTimeEvent( rHandle, pPosition, pEvent, nCount, pRead );
// create path
TCHAR pFullPath[MAX_PATH];
BuildPathFull( pFullPath, pHandleInfo->rPath, &pHandleInfo->rDateTime );
::PathAppend( pFullPath, PATH_EVENT );
// reset counter
*pRead = 0;
HANDLE hFile = ::CreateFile( pFullPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
if( hFile == INVALID_HANDLE_VALUE )
return RR_RESULT_FAILURE;
// end of read
E_REPORT_RESULT eResult = RR_RESULT_SUCCESS_PENDING;
// read data
LARGE_INTEGER liPos;
liPos.QuadPart = *pPosition * sizeof( REPORT_EVENT );
if( ::SetFilePointer( hFile, liPos.LowPart, &liPos.HighPart, FILE_BEGIN ) != INVALID_SET_FILE_POINTER )
{
DWORD dwRead = 0UL;
DWORD dwToRead = sizeof(REPORT_EVENT) * nCount;
if( !::ReadFile( hFile, pEvent, dwToRead, &dwRead, NULL ) || dwToRead != dwRead )
eResult = RR_RESULT_SUCCESS;
*pRead = dwRead / sizeof(REPORT_EVENT);
}
else
eResult = RR_RESULT_SUCCESS;
// update position
*pPosition = *pPosition + *pRead;
::CloseHandle( hFile );
return eResult;
}
E_REPORT_RESULT CLgReportProvider::DoReadSiteDateTimeImage( REPORT_HANDLE rHandle, PREPORT_POSITION pPosition, PREPORT_IMAGE pImage )
{
// lock handles
CLgLocker Lock( GetHandleLock() );
// get handle info
REPORT_HANDLE_INFO *pHandleInfo = GetHandleInfo( rHandle );
// if provider handle is valid then pass it through
if( pHandleInfo->rProviderHandle != REPORT_INVALID_HANDLE_VALUE )
return ReadSiteDateTimeImage( rHandle, pPosition, pImage );
// create path
TCHAR pFullPath[MAX_PATH];
BuildPathFull( pFullPath, pHandleInfo->rPath, &pHandleInfo->rDateTime );
// create image path
TCHAR pImagePath[MAX_PATH];
_sntprintf( pImagePath, MAX_PATH, PATH_IMAGE_FULL_FORMAT, pFullPath, (DWORD)( *pPosition / IMAGES_IN_BLOCK ), (DWORD)( *pPosition % IMAGES_IN_BLOCK ) );
return pImage->Load( pImagePath ) ? RR_RESULT_SUCCESS : RR_RESULT_FAILURE;
}
/*
E_REPORT_RESULT CLgReportProvider::DoReadSite( REPORT_PATH pPath, PREPORT_POSITION pPosition, PREPORT_PATH ppPathChild, INT nCount, PINT pRead )
{
return CLgReportLoaderClient::ReadSite( pPath, pPosition, ppPathChild, nCount, pRead );
}
E_REPORT_RESULT CLgReportProvider::DoReadSiteDate( REPORT_PATH pPath, PREPORT_POSITION pPosition, PREPORT_DATE pDate, INT nCount, PINT pRead )
{
// NOTE: should be changed: first of all should be checked cache !!!
if( pPath && *pPath )
return CLgReportLoaderClient::ReadSiteDate( pPath, pPosition, pDate, nCount, pRead );
TCHAR pSearchPath[MAX_PATH];
_tcsncpy( pSearchPath, m_pRootPath, MAX_PATH );
::PathAppend( pSearchPath, _T("*.*") );
// prImage errors on upper levels
*pRead = 0;
// end of search
BOOL bEnd = TRUE;
WIN32_FIND_DATA ffd;
HANDLE hFindFile = ::FindFirstFile( pSearchPath, &ffd );
if( hFindFile != INVALID_HANDLE_VALUE )
{
REPORT_POSITION pos = 0;
while( TRUE )
{
INT nYear, nMonth, nDay;
if( ( ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) == FILE_ATTRIBUTE_DIRECTORY &&
ffd.cFileName[0] != _T('.') &&
_stscanf( ffd.cFileName, PATH_DATE_FORMAT, &nYear, &nMonth, &nDay ) == 3 )
{
if( pos++ >= *pPosition )
{
// mark that it is not the end of search
bEnd = FALSE;
// store value
PREPORT_DATE pDatePtr = pDate + (*pRead)++;
pDatePtr->bYear = nYear;
pDatePtr->bMonth = nMonth;
pDatePtr->bDay = nDay;
// decrease number
if( !--nCount )
break;
}
}
// get next data
if( !::FindNextFile( hFindFile, &ffd ) )
{
bEnd = TRUE;
break;
}
}
// close handle
::FindClose( hFindFile );
}
// update position
*pPosition = *pPosition + *pRead;
return bEnd ? RR_RESULT_SUCCESS : RR_RESULT_SUCCESS_PENDING;
}
E_REPORT_RESULT CLgReportProvider::DoReadSiteDateTime( REPORT_PATH pPath, PREPORT_DATE pDate, PREPORT_POSITION pPosition, PREPORT_DATE_TIME pDateTime, INT nCount, PINT pRead )
{
// NOTE: should be changed: first of all should be checked cache !!!
if( pPath && *pPath )
return CLgReportLoaderClient::ReadSiteDateTime( pPath, pDate, pPosition, pDateTime, nCount, pRead );
TCHAR pSearchPath[MAX_PATH];
BuildPath( pSearchPath, NULL, pDate );
::PathAppend( pSearchPath, _T("*.*") );
// prImage errors on upper levels
*pRead = 0;
// end of search
BOOL bEnd = TRUE;
WIN32_FIND_DATA ffd;
HANDLE hFindFile = ::FindFirstFile( pSearchPath, &ffd );
if( hFindFile != INVALID_HANDLE_VALUE )
{
REPORT_POSITION pos = 0;
while( TRUE )
{
INT nHour, nMinute, nSecond;
if( ( ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) == FILE_ATTRIBUTE_DIRECTORY &&
ffd.cFileName[0] != _T('.') &&
_stscanf( ffd.cFileName, PATH_TIME_FORMAT, &nHour, &nMinute, &nSecond ) == 3 )
{
if( pos++ >= *pPosition )
{
// mark that it is not the end of search
bEnd = FALSE;
// store value
PREPORT_DATE_TIME pDateTimePtr = pDateTime + (*pRead)++;
pDateTimePtr->bYear = pDate->bYear;
pDateTimePtr->bMonth = pDate->bMonth;
pDateTimePtr->bDay = pDate->bDay;
pDateTimePtr->bHour = nHour;
pDateTimePtr->bMinute = nMinute;
pDateTimePtr->bSecond = nSecond;
// decrease number
if( !--nCount )
break;
}
}
// get next data
if( !::FindNextFile( hFindFile, &ffd ) )
{
bEnd = TRUE;
break;
}
}
// close handle
::FindClose( hFindFile );
}
// update position
*pPosition = *pPosition + *pRead;
return bEnd ? RR_RESULT_SUCCESS : RR_RESULT_SUCCESS_PENDING;
}
E_REPORT_RESULT CLgReportProvider::DoReadSiteDateTimeBackground( REPORT_PATH pPath, PREPORT_DATE_TIME pDateTime, PREPORT_IMAGE pImage )
{
// NOTE: should be changed: first of all should be checked cache !!!
if( pPath && *pPath )
return CLgReportLoaderClient::ReadSiteDateTimeBackground( pPath, pDateTime, pImage );
// create path
TCHAR pFullPath[MAX_PATH];
BuildPathFull( pFullPath, pPath, pDateTime );
::PathAppend( pFullPath, PATH_BACKGROUND );
return pImage->Load( pFullPath ) ? RR_RESULT_SUCCESS : RR_RESULT_FAILURE;
}
E_REPORT_RESULT CLgReportProvider::DoReadSiteDateTimeFilterEvent( REPORT_PATH pPath, PREPORT_DATE_TIME pDateTime, PREPORT_POSITION pPosition, PREPORT_FILTER_EVENT pFilterEvent, INT nCount, PINT pRead )
{
// NOTE: should be changed: first of all should be checked cache !!!
if( pPath && *pPath )
return CLgReportLoaderClient::ReadSiteDateTimeFilterEvent( pPath, pDateTime, pPosition, pFilterEvent, nCount, pRead );
// create path
TCHAR pFullPath[MAX_PATH];
BuildPathFull( pFullPath, pPath, pDateTime );
::PathAppend( pFullPath, PATH_FILTER_EVENT );
// reset counter
*pRead = 0;
HANDLE hFile = ::CreateFile( pFullPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
if( hFile == INVALID_HANDLE_VALUE )
return RR_RESULT_FAILURE;
// end of read
E_REPORT_RESULT eResult = RR_RESULT_SUCCESS_PENDING;
// read data
LARGE_INTEGER liPos;
liPos.QuadPart = *pPosition * sizeof( REPORT_FILTER_EVENT );
if( ::SetFilePointer( hFile, liPos.LowPart, &liPos.HighPart, FILE_BEGIN ) != INVALID_SET_FILE_POINTER )
{
DWORD dwRead = 0UL;
DWORD dwToRead = sizeof(REPORT_FILTER_EVENT) * nCount;
if( !::ReadFile( hFile, pFilterEvent, dwToRead, &dwRead, NULL ) || dwToRead != dwRead )
eResult = RR_RESULT_SUCCESS;
*pRead = dwRead / sizeof(REPORT_FILTER_EVENT);
}
else
eResult = RR_RESULT_SUCCESS;
// update position
*pPosition = *pPosition + *pRead;
::CloseHandle( hFile );
return eResult;
}
E_REPORT_RESULT CLgReportProvider::DoReadSiteDateTimeFilterSource( REPORT_PATH pPath, PREPORT_DATE_TIME pDateTime, PREPORT_POSITION pPosition, PREPORT_FILTER_SOURCE pFilterSource, INT nCount, PINT pRead )
{
// NOTE: should be changed: first of all should be checked cache !!!
if( pPath && *pPath )
return CLgReportLoaderClient::ReadSiteDateTimeFilterSource( pPath, pDateTime, pPosition, pFilterSource, nCount, pRead );
// create path
TCHAR pFullPath[MAX_PATH];
BuildPathFull( pFullPath, pPath, pDateTime );
::PathAppend( pFullPath, PATH_FILTER_SOURCE );
// reset counter
*pRead = 0;
HANDLE hFile = ::CreateFile( pFullPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
if( hFile == INVALID_HANDLE_VALUE )
return RR_RESULT_FAILURE;
// end of read
E_REPORT_RESULT eResult = RR_RESULT_SUCCESS_PENDING;
// read data
LARGE_INTEGER liPos;
liPos.QuadPart = *pPosition * sizeof( REPORT_FILTER_SOURCE );
if( ::SetFilePointer( hFile, liPos.LowPart, &liPos.HighPart, FILE_BEGIN ) != INVALID_SET_FILE_POINTER )
{
DWORD dwRead = 0UL;
DWORD dwToRead = sizeof(REPORT_FILTER_SOURCE) * nCount;
if( !::ReadFile( hFile, pFilterSource, dwToRead, &dwRead, NULL ) || dwToRead != dwRead )
eResult = RR_RESULT_SUCCESS;
*pRead = dwRead / sizeof(REPORT_FILTER_SOURCE);
}
else
eResult = RR_RESULT_SUCCESS;
// update position
*pPosition = *pPosition + *pRead;
::CloseHandle( hFile );
return eResult;
}
E_REPORT_RESULT CLgReportProvider::DoReadSiteDateTimeEvent( REPORT_PATH pPath, PREPORT_DATE_TIME pDateTime, PREPORT_POSITION pPosition, PREPORT_EVENT pEvent, INT nCount, PINT pRead )
{
// NOTE: should be changed: first of all should be checked cache !!!
if( pPath && *pPath )
return CLgReportLoaderClient::ReadSiteDateTimeEvent( pPath, pDateTime, pPosition, pEvent, nCount, pRead );
// create path
TCHAR pFullPath[MAX_PATH];
BuildPathFull( pFullPath, pPath, pDateTime );
::PathAppend( pFullPath, PATH_EVENT );
// reset counter
*pRead = 0;
HANDLE hFile = ::CreateFile( pFullPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
if( hFile == INVALID_HANDLE_VALUE )
return RR_RESULT_FAILURE;
// end of read
E_REPORT_RESULT eResult = RR_RESULT_SUCCESS_PENDING;
// read data
LARGE_INTEGER liPos;
liPos.QuadPart = *pPosition * sizeof( REPORT_EVENT );
if( ::SetFilePointer( hFile, liPos.LowPart, &liPos.HighPart, FILE_BEGIN ) != INVALID_SET_FILE_POINTER )
{
DWORD dwRead = 0UL;
DWORD dwToRead = sizeof(REPORT_EVENT) * nCount;
if( !::ReadFile( hFile, pEvent, dwToRead, &dwRead, NULL ) || dwToRead != dwRead )
eResult = RR_RESULT_SUCCESS;
*pRead = dwRead / sizeof(REPORT_EVENT);
}
else
eResult = RR_RESULT_SUCCESS;
// update position
*pPosition = *pPosition + *pRead;
::CloseHandle( hFile );
return eResult;
}
E_REPORT_RESULT CLgReportProvider::DoReadSiteDateTimeImage( REPORT_PATH pPath, PREPORT_DATE_TIME pDateTime, PREPORT_POSITION pPosition, PREPORT_IMAGE pImage, INT nCount, PINT pRead )
{
// NOTE: should be changed: first of all should be checked cache !!!
if( pPath && *pPath )
return CLgReportLoaderClient::ReadSiteDateTimeImage( pPath, pDateTime, pPosition, pImage, nCount, pRead );
// create path
TCHAR pFullPath[MAX_PATH];
BuildPathFull( pFullPath, pPath, pDateTime );
// create image path
TCHAR pImagePath[MAX_PATH];
_sntprintf( pImagePath, MAX_PATH, PATH_IMAGE_FULL_FORMAT, pFullPath, (DWORD)( *pPosition / IMAGES_IN_BLOCK ), (DWORD)( *pPosition % IMAGES_IN_BLOCK ) );
return ( *pRead = pImage->Load( pImagePath ) ? 1 : 0 ) ? RR_RESULT_SUCCESS : RR_RESULT_FAILURE;
}
*/
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CLgReportProvider::DoReset()
{
// prepare new names
::GetLocalTime( &m_Storage.stReportTime );
// convert to our format
m_Storage.rdtReportTime.bYear = (BYTE)( m_Storage.stReportTime.wYear - 2000 );
m_Storage.rdtReportTime.bMonth = (BYTE)m_Storage.stReportTime.wMonth;
m_Storage.rdtReportTime.bDay = (BYTE)m_Storage.stReportTime.wDay;
m_Storage.rdtReportTime.bHour = (BYTE)m_Storage.stReportTime.wHour;
m_Storage.rdtReportTime.bMinute = (BYTE)m_Storage.stReportTime.wMinute;
m_Storage.rdtReportTime.bSecond = (BYTE)m_Storage.stReportTime.wSecond;
m_bPendingReset = TRUE;
}
void CLgReportProvider::CheckReset()
{
if( !m_bPendingReset )
return;
m_bPendingReset = FALSE;
// build pathes
TCHAR pDateTime[MAX_PATH];
_sntprintf( pDateTime, MAX_PATH, PATH_DATE_FORMAT _T("\\") PATH_TIME_FORMAT, m_Storage.rdtReportTime.bYear, m_Storage.rdtReportTime.bMonth, m_Storage.rdtReportTime.bDay, m_Storage.rdtReportTime.bHour, m_Storage.rdtReportTime.bMinute, m_Storage.rdtReportTime.bSecond );
TCHAR pPath[MAX_PATH];
_tcsncpy( pPath, m_pRootPath, MAX_PATH );
::PathAppend( pPath, pDateTime );
strcpy( m_Storage.pPathBackground, pPath );
::PathAppend( m_Storage.pPathBackground, PATH_BACKGROUND );
strcpy( m_Storage.pPathFilterSource, pPath );
::PathAppend( m_Storage.pPathFilterSource, PATH_FILTER_SOURCE );
strcpy( m_Storage.pPathFilterEvent, pPath );
::PathAppend( m_Storage.pPathFilterEvent, PATH_FILTER_EVENT );
strcpy( m_Storage.pPathEvent, pPath );
::PathAppend( m_Storage.pPathEvent, PATH_EVENT );
strcpy( m_Storage.pPathImage, pPath );
::PathAppend( m_Storage.pPathImage, PATH_IMAGE );
// create path
LgCreateDirectory( pPath );
// reset storage positions
m_Storage.nEventPosition = 0;
m_Storage.nImageIndex = 0;
m_Storage.nImageFolderIndex = -1;
// call initialize procedure create site
CLgLocker Lock( &StorageProviderToClientLinkSP::GetLock() );
if( StorageProviderToClientLinkSP::GetLinkThis() )
StorageProviderToClientLinkSP::GetLinkThis()->OnCreateSite();
// notify loader provider about changes
OnChange( RRT_SITE_DATE_TIME, NULL, &m_Storage.rdtReportTime );
}
BOOL CLgReportProvider::DoWriteReset()
{
DoReset();
// CheckReset();
return TRUE;
}
BOOL CLgReportProvider::DoWriteBackground( PREPORT_IMAGE pImage )
{
CheckReset();
if( !pImage->Save( m_Storage.pPathBackground ) )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportProvider::DoWriteBackground"), _T("Can't store jpeg to <%s>"), (LPCTSTR)m_Storage.pPathBackground );
return FALSE;
}
// notify loader provider about changes
OnChange( RRT_SITE_DATE_TIME_BACKGROUND, NULL, &m_Storage.rdtReportTime );
return TRUE;
}
BOOL CLgReportProvider::DoWriteFilterSource( PREPORT_FILTER_SOURCE pFilterSource )
{
CheckReset();
FILE *pFile = fopen( m_Storage.pPathFilterSource, _T("ab+") );
if( !pFile )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportProvider::DoWriteFilterSource"), _T("Can't open <%s>"), m_Storage.pPathFilterSource );
return FALSE;
}
if( fwrite( pFilterSource, sizeof(REPORT_FILTER_SOURCE), 1, pFile ) != 1 )
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportProvider::DoWriteFilterSource"), _T("Can't write filter info") );
fclose( pFile );
// notify loader provider about changes
OnChange( RRT_SITE_DATE_TIME_FILTER_SOURCE, NULL, &m_Storage.rdtReportTime );
return TRUE;
}
BOOL CLgReportProvider::DoWriteFilterEvent( PREPORT_FILTER_EVENT pFilterEvent )
{
CheckReset();
FILE *pFile = fopen( m_Storage.pPathFilterEvent, _T("ab+") );
if( !pFile )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportProvider::DoWriteFilterEvent"), _T("Can't open <%s>"), m_Storage.pPathFilterEvent );
return FALSE;
}
if( fwrite( pFilterEvent, sizeof(REPORT_FILTER_EVENT), 1, pFile ) != 1 )
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportProvider::DoWriteFilterEvent"), _T("Can't write filter info") );
fclose( pFile );
// notify loader provider about changes
OnChange( RRT_SITE_DATE_TIME_FILTER_EVENT, NULL, &m_Storage.rdtReportTime );
return TRUE;
}
BOOL CLgReportProvider::DoWriteEvent( PREPORT_EVENT pEvent, PREPORT_POSITION pPosition )
{
CheckReset();
FILE *pFile = fopen( m_Storage.pPathEvent, m_Storage.nEventPosition ? _T("r+b") : _T("wb") );
if( !pFile )
{
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_ERROR, this, _T("CLgReportProvider::DoWriteEvent"), _T("Can't open event file <%s>"), (LPCTSTR)m_Storage.pPathEvent );
return FALSE;
}
if( fsetpos( pFile, &m_Storage.nEventPosition ) )
{
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_ERROR, this, _T("CLgReportProvider::DoWriteEvent"), _T("Can't set file position for read") );
fclose( pFile );
return FALSE;
}
if( fwrite( pEvent, sizeof(REPORT_EVENT), 1, pFile ) != 1 )
{
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_ERROR, this, _T("CLgReportProvider::DoWriteEvent"), _T("Can't store event") );
fclose( pFile );
return FALSE;
}
// return storage position
if( pPosition )
*pPosition = m_Storage.nEventPosition;
// increase storage position
m_Storage.nEventPosition += sizeof(REPORT_EVENT);
fclose( pFile );
// notify loader provider about changes
OnChange( RRT_SITE_DATE_TIME_EVENT, NULL, &m_Storage.rdtReportTime );
return TRUE;
}
BOOL CLgReportProvider::DoWriteImage( PREPORT_IMAGE pImage, PREPORT_POSITION pPosition )
{
CheckReset();
INT nFolderIndex = m_Storage.nImageIndex / IMAGES_IN_BLOCK;
INT nImageIndex = m_Storage.nImageIndex % IMAGES_IN_BLOCK;
// create new subfolder as nessessary
if( nFolderIndex != m_Storage.nImageFolderIndex )
{
_sntprintf( m_Storage.pPathImageFolder, MAX_PATH, PATH_IMAGE_FOLDER_FORMAT, m_Storage.pPathImage, nFolderIndex );
LgCreateDirectory( m_Storage.pPathImageFolder );
m_Storage.nImageFolderIndex = nFolderIndex;
}
TCHAR pImagePath[MAX_PATH];
_sntprintf( pImagePath, MAX_PATH, PATH_IMAGE_FORMAT, m_Storage.pPathImageFolder, nImageIndex );
if( !pImage->Save( pImagePath ) )
return FALSE;
if( pPosition )
{
FILE *pFile = fopen( m_Storage.pPathEvent, _T("r+b") );
if( !pFile )
{
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_ERROR, this, _T("CLgReportProvider::DoWriteImage"), _T("Can't open event file <%s>"), (LPCTSTR)m_Storage.pPathEvent );
return FALSE;
}
REPORT_EVENT re;
if( fsetpos( pFile, pPosition ) )
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_ERROR, this, _T("CLgReportProvider::DoWriteImage"), _T("Can't set file position for read") );
if( fread( &re, sizeof(REPORT_EVENT), 1, pFile ) != 1 )
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_ERROR, this, _T("CLgReportProvider::DoWriteImage"), _T("Can't read event") );
// update image index
if( re.bImageCount < REPORT_EVENT_IMAGE_MAX )
re.dwImageIndex[ re.bImageCount++ ] = m_Storage.nImageIndex++;
if( fsetpos( pFile, pPosition ) )
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_ERROR, this, _T("CLgReportProvider::DoWriteImage"), _T("Can't set file position for write") );
if( fwrite( &re, sizeof(REPORT_EVENT), 1, pFile ) != 1 )
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_ERROR, this, _T("CLgReportProvider::DoWriteImage"), _T("Can't store event") );
fclose( pFile );
}
// notify loader provider about changes
OnChange( RRT_SITE_DATE_TIME_IMAGE, NULL, &m_Storage.rdtReportTime );
return TRUE;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CLgReportProvider::BuildPath( LPSTR pPath, REPORT_PATH pReportPath, PREPORT_DATE pDate )
{
TCHAR pDateTimePath[MAX_PATH];
_sntprintf( pDateTimePath, MAX_PATH, PATH_DATE_FORMAT, pDate->bYear, pDate->bMonth, pDate->bDay );
_tcsncpy( pPath, m_pRootPath, MAX_PATH );
if( pReportPath && *pReportPath ) ::PathAppend( pPath, (LPCSTR)pReportPath );
::PathAppend( pPath, pDateTimePath );
}
void CLgReportProvider::BuildPathFull( LPSTR pPath, REPORT_PATH pReportPath, PREPORT_DATE_TIME pDateTime )
{
TCHAR pDateTimePath[MAX_PATH];
_sntprintf( pDateTimePath, MAX_PATH, PATH_DATE_FORMAT _T("\\") PATH_TIME_FORMAT, pDateTime->bYear, pDateTime->bMonth, pDateTime->bDay, pDateTime->bHour, pDateTime->bMinute, pDateTime->bSecond );
_tcsncpy( pPath, m_pRootPath, MAX_PATH );
if( pReportPath && *pReportPath ) ::PathAppend( pPath, (LPCSTR)pReportPath );
::PathAppend( pPath, pDateTimePath );
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// CLEANUP ENGINE
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CLgReportProvider::DeleteOldFiles(SYSTEMTIME st)
{
WIN32_FIND_DATA win32FD;
LARGE_INTEGER ft;
_TCHAR pBuffer[MAX_PATH];
LPTSTR pNames = NULL;
INT nNamesCount = 0;
::SystemTimeToFileTime( &st, (LPFILETIME)&ft );
ft.QuadPart -= DAY2NANO( m_nMaxStorageDays );
_tcsncpy( pBuffer, (LPCTSTR)m_pRootPath, MAX_PATH );
::PathAppend( pBuffer, _T("*.*") );
HANDLE hFF = ::FindFirstFile(pBuffer, &win32FD);
if(hFF != INVALID_HANDLE_VALUE)
do{
if( ( win32FD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) != FILE_ATTRIBUTE_DIRECTORY )
continue;
SYSTEMTIME cst = { 0 };
if( _stscanf( win32FD.cFileName, PATH_DATE_FORMAT, &cst.wYear, &cst.wMonth, &cst.wDay ) != 3 )
continue;
// if it is current report then don't delete it
if( m_Storage.stReportTime.wYear == cst.wYear &&
m_Storage.stReportTime.wMonth == cst.wMonth &&
m_Storage.stReportTime.wDay == cst.wDay )
continue;
LARGE_INTEGER cft;
::SystemTimeToFileTime(&cst, (LPFILETIME)&cft);
if( cft.QuadPart >= ft.QuadPart )
continue;
_tcsncpy( pBuffer, (LPCTSTR)m_pRootPath, MAX_PATH );
::PathAppend( pBuffer, win32FD.cFileName );
INT nLength = _tcslen(pBuffer) + 1;
pNames = (LPTSTR)realloc( pNames, sizeof( TCHAR ) * ( nNamesCount + nLength ) );
if( pNames )
{
memcpy( pNames + nNamesCount, pBuffer, nLength );
nNamesCount += nLength;
}
}while(::FindNextFile(hFF, &win32FD));
if( nNamesCount )
{
pNames = (LPTSTR)realloc( pNames, sizeof( TCHAR ) * ( nNamesCount + 1 ) );
if( pNames )
{
// add one more zero
pNames[nNamesCount] = 0;
SHFILEOPSTRUCT sfs = { 0 };
sfs.wFunc = FO_DELETE;
sfs.pFrom = pNames;
sfs.fFlags = FOF_NOCONFIRMATION | FOF_SIMPLEPROGRESS;
sfs.lpszProgressTitle = _T("Deleting old history...");
::SHFileOperation(&sfs);
delete pNames;
}
}
::FindClose(hFF);
}
BOOL CLgReportProvider::CheckOldFiles(SYSTEMTIME st)
{
WIN32_FIND_DATA win32FD;
LARGE_INTEGER ft;
_TCHAR pBuffer[MAX_PATH];
BOOL fExist = FALSE;
::SystemTimeToFileTime(&st, (LPFILETIME)&ft);
ft.QuadPart -= DAY2NANO( m_nMaxStorageDays );
_tcsncpy( pBuffer, (LPCTSTR)m_pRootPath, MAX_PATH );
::PathAppend( pBuffer, _T("*.*") );
HANDLE hFF = ::FindFirstFile(pBuffer, &win32FD);
if(hFF != INVALID_HANDLE_VALUE)
do{
if(win32FD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
SYSTEMTIME cst;
memset( &cst, 0, sizeof(cst) );
if(_stscanf(win32FD.cFileName, PATH_DATE_FORMAT, &cst.wYear, &cst.wMonth, &cst.wDay ) == 3)
{
LARGE_INTEGER cft;
memset( &cft, 0, sizeof(cft) );
cst.wYear += 2000;
if( !::SystemTimeToFileTime(&cst, (LPFILETIME)&cft) )
{
DWORD dwError = GetLastError();
}
if(cft.QuadPart < ft.QuadPart)
fExist = TRUE;
}
}
}while(::FindNextFile(hFF, &win32FD));
::FindClose(hFF);
return fExist;
}
DWORD CLgReportProvider::ThreadProc()
{
try
{
TIME_ZONE_INFORMATION tzi;
LARGE_INTEGER lt;
SYSTEMTIME st;
BOOL fNeedCheck = TRUE;
::GetTimeZoneInformation(&tzi);
HANDLE hTimer = ::CreateWaitableTimer(NULL, FALSE, NULL);
::GetLocalTime(&st);
st.wHour = st.wMinute = st.wSecond = st.wMilliseconds = 0;
::SystemTimeToFileTime( &st, (LPFILETIME)&lt );
lt.QuadPart += DAY2NANO( 1 ) + MINUTE2NANO( tzi.Bias );
::SetWaitableTimer(hTimer, &lt, 0UL, NULL, NULL, FALSE);
HANDLE phEvents[] = { CLgThread::GetThreadStopEvent(), hTimer };
if( CheckOldFiles( st ) )
{
// ask about deletition
CLgLocker Lock( &StorageProviderToClientLinkSP::GetLock() );
if( StorageProviderToClientLinkSP::GetLinkThis() &&
StorageProviderToClientLinkSP::GetLinkThis()->OnAskForDeleteOld() )
DeleteOldFiles(st);
}
while(TRUE)
{
switch(::WaitForMultipleObjects(2, phEvents, FALSE, INFINITE))
{
case WAIT_OBJECT_0 + 0:
goto THREAD_EXIT;
case WAIT_OBJECT_0 + 1:
::CancelWaitableTimer(hTimer);
::GetLocalTime(&st);
DeleteOldFiles(st);
st.wHour = st.wMinute = st.wSecond = st.wMilliseconds = 0;
::SystemTimeToFileTime(&st, (LPFILETIME)&lt);
lt.QuadPart += DAY2NANO( 1 ) + MINUTE2NANO( tzi.Bias );
::SetWaitableTimer(hTimer, &lt, 0UL, NULL, NULL, FALSE);
break;
default:
break;
}
} // end of while(TRUE)
THREAD_EXIT:
::CancelWaitableTimer(hTimer);
::CloseHandle(hTimer);
}
catch( ... )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportProvider::ThreadProc"), _T("C++ eception") );
}
return 0UL;
}
BOOL CLgReportProvider::SetMaxStoreTime(INT nDays)
{
m_nMaxStorageDays = nDays;
return TRUE;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////