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.

157 lines
4.2 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: LgWatchDog.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgWatchDog.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "LgWatchDog.h"
  16. #include "..\LgLib\Version.h"
  17. CLgWatchDog::CLgWatchDog()
  18. {
  19. m_Thread.SetWatchDog( this );
  20. memset( m_pCommandLine, 0, sizeof(m_pCommandLine) );
  21. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgWatchDog::CLgWatchDog") );
  22. }
  23. CLgWatchDog::~CLgWatchDog()
  24. {
  25. Destroy();
  26. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgWatchDog::~CLgWatchDog") );
  27. }
  28. BOOL CLgWatchDog::Create( LPCTSTR pCommandLine )
  29. {
  30. _tcsncpy( m_pCommandLine, pCommandLine, MAX_PATH );
  31. if( !m_Thread.Start( FALSE ) )
  32. {
  33. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgWatchDog::Create"), _T("m_Thread.Start failed") );
  34. return FALSE;
  35. }
  36. return TRUE;
  37. }
  38. void CLgWatchDog::Destroy()
  39. {
  40. m_Thread.Stop( TRUE );
  41. memset( m_pCommandLine, 0, sizeof(m_pCommandLine) );
  42. CLgLocker Lock( &m_RecipientArray );
  43. m_RecipientArray.RemoveAll();
  44. }
  45. BOOL CLgWatchDog::Register( LPCTSTR pRecipientName, DWORD dwRecipientID, DWORD dwRecipientTimeout )
  46. {
  47. // check within existing recipients
  48. CLgLocker Lock( &m_RecipientArray );
  49. for( INT i = 0; i < m_RecipientArray.GetCount(); i++ )
  50. {
  51. WATCHDOG_RECIPIENT& wr = m_RecipientArray[i];
  52. if( wr.dwID == dwRecipientID )
  53. {
  54. // update time
  55. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgWatchDog::Register"), _T("Recipient with ID = 0x%08X already exist in the list"), dwRecipientID );
  56. return FALSE;
  57. }
  58. }
  59. // add new recipient
  60. WATCHDOG_RECIPIENT wr;
  61. wr.dwID = dwRecipientID;
  62. _tcsncpy( wr.pName, pRecipientName, MAX_PATH );
  63. wr.liTimeout.QuadPart = 10000000i64 * dwRecipientTimeout;
  64. ::GetSystemTimeAsFileTime( (LPFILETIME)&wr.liTime );
  65. return m_RecipientArray.Add( wr );
  66. }
  67. BOOL CLgWatchDog::Unregister( DWORD dwRecipientID )
  68. {
  69. // check within existing recipients
  70. CLgLocker Lock( &m_RecipientArray );
  71. for( INT i = 0; i < m_RecipientArray.GetCount(); i++ )
  72. {
  73. WATCHDOG_RECIPIENT& wr = m_RecipientArray[i];
  74. if( wr.dwID == dwRecipientID )
  75. {
  76. m_RecipientArray.Remove(i);
  77. return TRUE;
  78. }
  79. }
  80. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgWatchDog::Unregister"), _T("Recipient with ID = 0x%08X is absent in the list"), dwRecipientID );
  81. return FALSE;
  82. }
  83. void CLgWatchDog::Update( DWORD dwRecipientID )
  84. {
  85. CLgLocker Lock( &m_RecipientArray );
  86. for( INT i = 0; i < m_RecipientArray.GetCount(); i++ )
  87. {
  88. WATCHDOG_RECIPIENT& wr = m_RecipientArray[i];
  89. if( wr.dwID == dwRecipientID )
  90. {
  91. // update time
  92. ::GetSystemTimeAsFileTime( (LPFILETIME)&wr.liTime );
  93. break;
  94. }
  95. }
  96. }
  97. DWORD CLgWatchDog::WatchDogThreadProc()
  98. {
  99. BOOL bLoop = TRUE;
  100. while( bLoop )
  101. {
  102. // wait one second
  103. switch( ::WaitForSingleObject( m_Thread.GetThreadStopEvent(), 1000UL ) )
  104. {
  105. default:
  106. case WAIT_FAILED:
  107. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgWatchDog::ThreadProc"), _T("WaitForSingleObject failed") );
  108. case WAIT_OBJECT_0:
  109. bLoop = FALSE;
  110. continue;
  111. case WAIT_TIMEOUT:
  112. break;
  113. }
  114. // current time
  115. LARGE_INTEGER liTime;
  116. ::GetSystemTimeAsFileTime( (LPFILETIME)&liTime );
  117. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CLgWatchDog::ThreadProc"), _T("Check started...") );
  118. // check times
  119. CLgLocker Lock( &m_RecipientArray );
  120. for( INT i = 0; i < m_RecipientArray.GetCount(); i++ )
  121. {
  122. WATCHDOG_RECIPIENT& wr = m_RecipientArray[i];
  123. if( ( liTime.QuadPart - wr.liTime.QuadPart ) > wr.liTimeout.QuadPart )
  124. {
  125. OnTimeout( wr.pName, m_pCommandLine );
  126. // LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgWatchDog::ThreadProc"), _T("Timeout for %s was expaired"), wr.pName );
  127. break;
  128. }
  129. }
  130. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CLgWatchDog::ThreadProc"), _T("Check done") );
  131. }
  132. return 0UL;
  133. }