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.
 
 
 
 
 

382 lines
8.9 KiB

//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright © 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 08/10/2003
// PROJECT:
// SUBSYSTEM:
// FILE: ReportServer.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of ReportServer.cpp
//----------------------------------------------------------------------------
#include "stdafx.h"
#include "ReportServer.h"
#include "..\LgLib\Version.h"
CReportServer::CReportServer()
{
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CReportServer::CReportServer" );
}
CReportServer::~CReportServer()
{
Destroy();
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CReportServer::~CReportServer" );
}
BOOL CReportServer::Create( CProfile *pProfile )
{
Destroy();
if( !m_JobStream.Create() )
return FALSE;
m_pProfile = pProfile;
return Start( FALSE );
}
void CReportServer::Destroy()
{
Stop( TRUE );
}
BOOL CReportServer::DoRemoteReportList( REMOTE_CONTEXT rc, LPCTSTR pFolderName, INT nSize )
{
REPORT_JOB job;
job.rc = rc;
job.nOperation = REPORT_JOB_LIST;
memcpy( job.pName, pFolderName, nSize );
memset( job.pName + nSize, 0, MAX_PATH - nSize );
if( !m_JobStream.Put( job ) )
return FALSE;
return m_Event.Set();
}
BOOL CReportServer::DoRemoteReportFile( REMOTE_CONTEXT rc, LPCTSTR pFolderName, INT nSize, DWORD dwPosition )
{
REPORT_JOB job;
job.rc = rc;
job.nOperation = REPORT_JOB_FILE;
memcpy( job.pName, pFolderName, nSize );
memset( job.pName + nSize, 0, MAX_PATH - nSize );
job.dwPosition = dwPosition;
if( !m_JobStream.Put( job ) )
return FALSE;
return m_Event.Set();
}
DWORD CReportServer::ThreadProc()
{
HANDLE hEvents[] = { CLgThread::GetThreadStopEvent(), m_Event };
while( TRUE )
{
// wait for events
switch( ::WaitForMultipleObjects( 2, hEvents, FALSE, INFINITE ) )
{
default:
case WAIT_OBJECT_0:
goto EXIT_THREAD;
case WAIT_OBJECT_0+1:
break;
}
// perform jobs
REPORT_JOB job;
while( m_JobStream.Get( &job ) )
{
switch( job.nOperation )
{
case REPORT_JOB_LIST: OnJobList( &job ); break;
case REPORT_JOB_TEXT: OnJobText( &job ); break;
case REPORT_JOB_IMAGE: OnJobImage( &job ); break;
case REPORT_JOB_FILE: OnJobFile( &job ); break;
}
}
}
EXIT_THREAD:
return 0UL;
}
int ListCompareFunction( const void* pC1, const void* pC2 )
{
PCHAR pStr1 = (PCHAR)*(PBYTE*)pC1 + 4;
PCHAR pStr2 = (PCHAR)*(PBYTE*)pC2 + 4;
return strcmp( pStr1, pStr2 );
}
void CReportServer::OnJobList( PREPORT_JOB pJob )
{
TCHAR pFolder[MAX_PATH];
if( pJob->pName[0] )
{
_tcscpy( pFolder, m_pProfile->SettingsReport() );
::PathAppend( pFolder, pJob->pName );
}
else
{
_tcscpy( pFolder, m_pProfile->SettingsReport() );
}
::PathAppend( pFolder, "*.*" );
PBYTE *ppString = NULL;
INT nStringCount = 0;
PBYTE pSumString = NULL;
INT nSumStringCount = 0;
WIN32_FIND_DATA win32FD;
HANDLE hFindFile = ::FindFirstFile( pFolder, &win32FD );
if( hFindFile != INVALID_HANDLE_VALUE)
{
do{
if(win32FD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (_tcsicmp(win32FD.cFileName, _T(".")) &&
_tcsicmp(win32FD.cFileName, _T("..")))
{
INT nSize = _tcslen( win32FD.cFileName ) + 1 + 4;
ppString = (PBYTE*)realloc( ppString, sizeof( PBYTE ) * ( nStringCount + 1 ) );
if( !ppString )
{
nStringCount = 0;
break;
}
ppString[ nStringCount ] = (PBYTE)malloc( sizeof(BYTE) * nSize );
if( !ppString[ nStringCount ] )
{
break;
}
// copy size
memcpy( ppString[nStringCount], &win32FD.nFileSizeLow, 4 );
// copy substring
memcpy( ppString[nStringCount] + 4, win32FD.cFileName, nSize - 4 );
// increase string counter
nStringCount++;
}
}
else
{
BOOL bConverted = TRUE;
// substitute ".BMP" to ".JPG"
TCHAR pFileName[MAX_PATH];
_tcscpy( pFileName, win32FD.cFileName );
strupr( pFileName );
if( !strstr( pFileName, ".JPG" ) && !strstr( pFileName, ".TXT" ) )
bConverted = FALSE; // skip them
/*
if( strstr( pFileName, ".BMP" ) )
{
// prepare bmp name
TCHAR pBmpFileName[MAX_PATH];
strcpy( pBmpFileName, m_pProfile->SettingsReport() );
::PathAppend( pBmpFileName, pJob->pName);
::PathAppend( pBmpFileName, pFileName );
// prepare jpeg name
TCHAR pJpegFileName[MAX_PATH];
strcpy( pJpegFileName, pBmpFileName );
::PathRemoveExtension( pJpegFileName );
strcat( pJpegFileName, ".JPG" );
// test if already exist
bConverted = ::PathFileExists( pJpegFileName );
if( !bConverted )
{
CLgImageDIB dib;
if( dib.Load( pBmpFileName, TRUE ) )
{
CLgImageJPEG jpeg;
if( jpeg.Create( dib, dib, 35 ) )
{
if( jpeg.Save( pJpegFileName ) )
{
PBYTE pBuffer = NULL;
INT nSize = 0;
jpeg.GetBuffer( &pBuffer, &nSize );
strcpy( pFileName, ::PathFindFileName( pJpegFileName ) );
win32FD.nFileSizeLow = nSize;
bConverted = TRUE;
}
}
}
}
else bConverted = FALSE; // don't send jpeg twice
}
*/
if( bConverted )
{
INT nSize = _tcslen( pFileName ) + 1 + 4;
ppString = (PBYTE*)realloc( ppString, sizeof( PBYTE ) * ( nStringCount + 1 ) );
if( !ppString )
{
nStringCount = 0;
break;
}
ppString[ nStringCount ] = (PBYTE)malloc( sizeof(BYTE) * nSize );
if( !ppString[ nStringCount ] )
{
break;
}
// copy size
memcpy( ppString[nStringCount], &win32FD.nFileSizeLow, 4 );
// copy substring
memcpy( ppString[nStringCount] + 4, pFileName, nSize - 4 );
// increase string counter
nStringCount++;
}
}
}
while( ::FindNextFile( hFindFile, &win32FD ) );
::FindClose( hFindFile );
}
// sort strings
if( nStringCount )
{
// sort strings
qsort( ppString, nStringCount, sizeof(PBYTE), ListCompareFunction );
PBYTE pSendBuffer = NULL;
INT nSendSize = 0;
for( INT i = 0; i < nStringCount; i++ )
{
PCHAR pString = (PCHAR)ppString[i];
INT nSize = strlen( pString + 4 ) + 1 + 4;
pSendBuffer = (PBYTE)realloc( pSendBuffer, nSendSize + nSize );
if( !pSendBuffer )
{
// error
nSendSize = 0;
break;
}
// copy substring
memcpy( pSendBuffer + nSendSize, pString, nSize );
// add size
nSendSize += nSize;
// delete string
free( pString );
}
free( ppString );
if( !OnRemoteReportList( pJob->rc, (PBYTE)pSendBuffer, nSendSize ) )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CReportServer::OnJobList"), _T("OnRemoteReportList 1 failed") );
}
if( pSendBuffer )
free( pSendBuffer );
}
else
{
if( !OnRemoteReportList( pJob->rc, (PBYTE)NULL, 0 ) )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CReportServer::OnJobList"), _T("OnRemoteReportList 2 failed") );
}
}
}
void CReportServer::OnJobText( PREPORT_JOB pJob )
{
}
void CReportServer::OnJobImage( PREPORT_JOB pJob )
{
}
void CReportServer::OnJobFile( PREPORT_JOB pJob )
{
TCHAR pFolder[MAX_PATH];
if( pJob->pName[0] )
{
_tcscpy( pFolder, m_pProfile->SettingsReport() );
::PathAppend( pFolder, pJob->pName );
}
else
{
if( !OnRemoteReportFile( pJob->rc, NULL, 0 ) )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CReportServer::OnJobFile"), _T("OnRemoteReportFile 1 failed") );
}
return;
}
HANDLE hFile = CreateFile( pFolder, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
if( hFile == INVALID_HANDLE_VALUE )
{
if( !OnRemoteReportFile( pJob->rc, NULL, 0 ) )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CReportServer::OnJobFile"), _T("OnRemoteReportFile 2 failed") );
}
return;
}
DWORD dwFileSizeHi = 0;
DWORD dwFileSize = GetFileSize( hFile, &dwFileSizeHi );
// transfer by 4 kb blocks
DWORD dwToRead = dwFileSize - pJob->dwPosition;
if( dwToRead > 1024 )
dwToRead = 1024;
PBYTE pBuffer = new BYTE[ dwToRead ];
if( !pBuffer )
{
CloseHandle( hFile );
if( !OnRemoteReportFile( pJob->rc, NULL, 0 ) )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CReportServer::OnJobFile"), _T("OnRemoteReportFile 3 failed") );
}
return;
}
DWORD dwRead = 0;
// move to nessesary position
::SetFilePointer( hFile, pJob->dwPosition, NULL, SEEK_SET );
// read data
::ReadFile( hFile, pBuffer, dwToRead, &dwRead, NULL );
if( !OnRemoteReportFile( pJob->rc, pBuffer, dwRead ) )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CReportServer::OnJobFile"), _T("OnRemoteReportFile 4 failed") );
}
delete pBuffer;
CloseHandle( hFile );
}