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

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: ReportServer.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of ReportServer.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "ReportServer.h"
  16. #include "..\LgLib\Version.h"
  17. CReportServer::CReportServer()
  18. {
  19. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CReportServer::CReportServer" );
  20. }
  21. CReportServer::~CReportServer()
  22. {
  23. Destroy();
  24. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CReportServer::~CReportServer" );
  25. }
  26. BOOL CReportServer::Create( CProfile *pProfile )
  27. {
  28. Destroy();
  29. if( !m_JobStream.Create() )
  30. return FALSE;
  31. m_pProfile = pProfile;
  32. return Start( FALSE );
  33. }
  34. void CReportServer::Destroy()
  35. {
  36. Stop( TRUE );
  37. }
  38. BOOL CReportServer::DoRemoteReportList( REMOTE_CONTEXT rc, LPCTSTR pFolderName, INT nSize )
  39. {
  40. REPORT_JOB job;
  41. job.rc = rc;
  42. job.nOperation = REPORT_JOB_LIST;
  43. memcpy( job.pName, pFolderName, nSize );
  44. memset( job.pName + nSize, 0, MAX_PATH - nSize );
  45. if( !m_JobStream.Put( job ) )
  46. return FALSE;
  47. return m_Event.Set();
  48. }
  49. BOOL CReportServer::DoRemoteReportFile( REMOTE_CONTEXT rc, LPCTSTR pFolderName, INT nSize, DWORD dwPosition )
  50. {
  51. REPORT_JOB job;
  52. job.rc = rc;
  53. job.nOperation = REPORT_JOB_FILE;
  54. memcpy( job.pName, pFolderName, nSize );
  55. memset( job.pName + nSize, 0, MAX_PATH - nSize );
  56. job.dwPosition = dwPosition;
  57. if( !m_JobStream.Put( job ) )
  58. return FALSE;
  59. return m_Event.Set();
  60. }
  61. DWORD CReportServer::ThreadProc()
  62. {
  63. HANDLE hEvents[] = { CLgThread::GetThreadStopEvent(), m_Event };
  64. while( TRUE )
  65. {
  66. // wait for events
  67. switch( ::WaitForMultipleObjects( 2, hEvents, FALSE, INFINITE ) )
  68. {
  69. default:
  70. case WAIT_OBJECT_0:
  71. goto EXIT_THREAD;
  72. case WAIT_OBJECT_0+1:
  73. break;
  74. }
  75. // perform jobs
  76. REPORT_JOB job;
  77. while( m_JobStream.Get( &job ) )
  78. {
  79. switch( job.nOperation )
  80. {
  81. case REPORT_JOB_LIST: OnJobList( &job ); break;
  82. case REPORT_JOB_TEXT: OnJobText( &job ); break;
  83. case REPORT_JOB_IMAGE: OnJobImage( &job ); break;
  84. case REPORT_JOB_FILE: OnJobFile( &job ); break;
  85. }
  86. }
  87. }
  88. EXIT_THREAD:
  89. return 0UL;
  90. }
  91. int ListCompareFunction( const void* pC1, const void* pC2 )
  92. {
  93. PCHAR pStr1 = (PCHAR)*(PBYTE*)pC1 + 4;
  94. PCHAR pStr2 = (PCHAR)*(PBYTE*)pC2 + 4;
  95. return strcmp( pStr1, pStr2 );
  96. }
  97. void CReportServer::OnJobList( PREPORT_JOB pJob )
  98. {
  99. TCHAR pFolder[MAX_PATH];
  100. if( pJob->pName[0] )
  101. {
  102. _tcscpy( pFolder, m_pProfile->SettingsReport() );
  103. ::PathAppend( pFolder, pJob->pName );
  104. }
  105. else
  106. {
  107. _tcscpy( pFolder, m_pProfile->SettingsReport() );
  108. }
  109. ::PathAppend( pFolder, "*.*" );
  110. PBYTE *ppString = NULL;
  111. INT nStringCount = 0;
  112. PBYTE pSumString = NULL;
  113. INT nSumStringCount = 0;
  114. WIN32_FIND_DATA win32FD;
  115. HANDLE hFindFile = ::FindFirstFile( pFolder, &win32FD );
  116. if( hFindFile != INVALID_HANDLE_VALUE)
  117. {
  118. do{
  119. if(win32FD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  120. {
  121. if (_tcsicmp(win32FD.cFileName, _T(".")) &&
  122. _tcsicmp(win32FD.cFileName, _T("..")))
  123. {
  124. INT nSize = _tcslen( win32FD.cFileName ) + 1 + 4;
  125. ppString = (PBYTE*)realloc( ppString, sizeof( PBYTE ) * ( nStringCount + 1 ) );
  126. if( !ppString )
  127. {
  128. nStringCount = 0;
  129. break;
  130. }
  131. ppString[ nStringCount ] = (PBYTE)malloc( sizeof(BYTE) * nSize );
  132. if( !ppString[ nStringCount ] )
  133. {
  134. break;
  135. }
  136. // copy size
  137. memcpy( ppString[nStringCount], &win32FD.nFileSizeLow, 4 );
  138. // copy substring
  139. memcpy( ppString[nStringCount] + 4, win32FD.cFileName, nSize - 4 );
  140. // increase string counter
  141. nStringCount++;
  142. }
  143. }
  144. else
  145. {
  146. BOOL bConverted = TRUE;
  147. // substitute ".BMP" to ".JPG"
  148. TCHAR pFileName[MAX_PATH];
  149. _tcscpy( pFileName, win32FD.cFileName );
  150. strupr( pFileName );
  151. if( !strstr( pFileName, ".JPG" ) && !strstr( pFileName, ".TXT" ) )
  152. bConverted = FALSE; // skip them
  153. /*
  154. if( strstr( pFileName, ".BMP" ) )
  155. {
  156. // prepare bmp name
  157. TCHAR pBmpFileName[MAX_PATH];
  158. strcpy( pBmpFileName, m_pProfile->SettingsReport() );
  159. ::PathAppend( pBmpFileName, pJob->pName);
  160. ::PathAppend( pBmpFileName, pFileName );
  161. // prepare jpeg name
  162. TCHAR pJpegFileName[MAX_PATH];
  163. strcpy( pJpegFileName, pBmpFileName );
  164. ::PathRemoveExtension( pJpegFileName );
  165. strcat( pJpegFileName, ".JPG" );
  166. // test if already exist
  167. bConverted = ::PathFileExists( pJpegFileName );
  168. if( !bConverted )
  169. {
  170. CLgImageDIB dib;
  171. if( dib.Load( pBmpFileName, TRUE ) )
  172. {
  173. CLgImageJPEG jpeg;
  174. if( jpeg.Create( dib, dib, 35 ) )
  175. {
  176. if( jpeg.Save( pJpegFileName ) )
  177. {
  178. PBYTE pBuffer = NULL;
  179. INT nSize = 0;
  180. jpeg.GetBuffer( &pBuffer, &nSize );
  181. strcpy( pFileName, ::PathFindFileName( pJpegFileName ) );
  182. win32FD.nFileSizeLow = nSize;
  183. bConverted = TRUE;
  184. }
  185. }
  186. }
  187. }
  188. else bConverted = FALSE; // don't send jpeg twice
  189. }
  190. */
  191. if( bConverted )
  192. {
  193. INT nSize = _tcslen( pFileName ) + 1 + 4;
  194. ppString = (PBYTE*)realloc( ppString, sizeof( PBYTE ) * ( nStringCount + 1 ) );
  195. if( !ppString )
  196. {
  197. nStringCount = 0;
  198. break;
  199. }
  200. ppString[ nStringCount ] = (PBYTE)malloc( sizeof(BYTE) * nSize );
  201. if( !ppString[ nStringCount ] )
  202. {
  203. break;
  204. }
  205. // copy size
  206. memcpy( ppString[nStringCount], &win32FD.nFileSizeLow, 4 );
  207. // copy substring
  208. memcpy( ppString[nStringCount] + 4, pFileName, nSize - 4 );
  209. // increase string counter
  210. nStringCount++;
  211. }
  212. }
  213. }
  214. while( ::FindNextFile( hFindFile, &win32FD ) );
  215. ::FindClose( hFindFile );
  216. }
  217. // sort strings
  218. if( nStringCount )
  219. {
  220. // sort strings
  221. qsort( ppString, nStringCount, sizeof(PBYTE), ListCompareFunction );
  222. PBYTE pSendBuffer = NULL;
  223. INT nSendSize = 0;
  224. for( INT i = 0; i < nStringCount; i++ )
  225. {
  226. PCHAR pString = (PCHAR)ppString[i];
  227. INT nSize = strlen( pString + 4 ) + 1 + 4;
  228. pSendBuffer = (PBYTE)realloc( pSendBuffer, nSendSize + nSize );
  229. if( !pSendBuffer )
  230. {
  231. // error
  232. nSendSize = 0;
  233. break;
  234. }
  235. // copy substring
  236. memcpy( pSendBuffer + nSendSize, pString, nSize );
  237. // add size
  238. nSendSize += nSize;
  239. // delete string
  240. free( pString );
  241. }
  242. free( ppString );
  243. if( !OnRemoteReportList( pJob->rc, (PBYTE)pSendBuffer, nSendSize ) )
  244. {
  245. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CReportServer::OnJobList"), _T("OnRemoteReportList 1 failed") );
  246. }
  247. if( pSendBuffer )
  248. free( pSendBuffer );
  249. }
  250. else
  251. {
  252. if( !OnRemoteReportList( pJob->rc, (PBYTE)NULL, 0 ) )
  253. {
  254. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CReportServer::OnJobList"), _T("OnRemoteReportList 2 failed") );
  255. }
  256. }
  257. }
  258. void CReportServer::OnJobText( PREPORT_JOB pJob )
  259. {
  260. }
  261. void CReportServer::OnJobImage( PREPORT_JOB pJob )
  262. {
  263. }
  264. void CReportServer::OnJobFile( PREPORT_JOB pJob )
  265. {
  266. TCHAR pFolder[MAX_PATH];
  267. if( pJob->pName[0] )
  268. {
  269. _tcscpy( pFolder, m_pProfile->SettingsReport() );
  270. ::PathAppend( pFolder, pJob->pName );
  271. }
  272. else
  273. {
  274. if( !OnRemoteReportFile( pJob->rc, NULL, 0 ) )
  275. {
  276. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CReportServer::OnJobFile"), _T("OnRemoteReportFile 1 failed") );
  277. }
  278. return;
  279. }
  280. HANDLE hFile = CreateFile( pFolder, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
  281. if( hFile == INVALID_HANDLE_VALUE )
  282. {
  283. if( !OnRemoteReportFile( pJob->rc, NULL, 0 ) )
  284. {
  285. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CReportServer::OnJobFile"), _T("OnRemoteReportFile 2 failed") );
  286. }
  287. return;
  288. }
  289. DWORD dwFileSizeHi = 0;
  290. DWORD dwFileSize = GetFileSize( hFile, &dwFileSizeHi );
  291. // transfer by 4 kb blocks
  292. DWORD dwToRead = dwFileSize - pJob->dwPosition;
  293. if( dwToRead > 1024 )
  294. dwToRead = 1024;
  295. PBYTE pBuffer = new BYTE[ dwToRead ];
  296. if( !pBuffer )
  297. {
  298. CloseHandle( hFile );
  299. if( !OnRemoteReportFile( pJob->rc, NULL, 0 ) )
  300. {
  301. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CReportServer::OnJobFile"), _T("OnRemoteReportFile 3 failed") );
  302. }
  303. return;
  304. }
  305. DWORD dwRead = 0;
  306. // move to nessesary position
  307. ::SetFilePointer( hFile, pJob->dwPosition, NULL, SEEK_SET );
  308. // read data
  309. ::ReadFile( hFile, pBuffer, dwToRead, &dwRead, NULL );
  310. if( !OnRemoteReportFile( pJob->rc, pBuffer, dwRead ) )
  311. {
  312. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CReportServer::OnJobFile"), _T("OnRemoteReportFile 4 failed") );
  313. }
  314. delete pBuffer;
  315. CloseHandle( hFile );
  316. }