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
157 lines
4.2 KiB
//----------------------------------------------------------------------------
|
|
// Laser Guard Ltd.
|
|
// Copyright © 2000-2002. All Rights Reserved.
|
|
// Version 1.0 01/01/2000
|
|
// Last modification: 08/10/2003
|
|
// PROJECT:
|
|
// SUBSYSTEM:
|
|
// FILE: LgWatchDog.cpp
|
|
// AUTHOR: Sergey Chizhevskiy
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// Source file of LgWatchDog.cpp
|
|
//----------------------------------------------------------------------------
|
|
#include "stdafx.h"
|
|
#include "LgWatchDog.h"
|
|
#include "..\LgLib\Version.h"
|
|
|
|
CLgWatchDog::CLgWatchDog()
|
|
{
|
|
m_Thread.SetWatchDog( this );
|
|
|
|
memset( m_pCommandLine, 0, sizeof(m_pCommandLine) );
|
|
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgWatchDog::CLgWatchDog") );
|
|
}
|
|
|
|
CLgWatchDog::~CLgWatchDog()
|
|
{
|
|
Destroy();
|
|
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgWatchDog::~CLgWatchDog") );
|
|
}
|
|
|
|
BOOL CLgWatchDog::Create( LPCTSTR pCommandLine )
|
|
{
|
|
_tcsncpy( m_pCommandLine, pCommandLine, MAX_PATH );
|
|
|
|
if( !m_Thread.Start( FALSE ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgWatchDog::Create"), _T("m_Thread.Start failed") );
|
|
return FALSE;
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
void CLgWatchDog::Destroy()
|
|
{
|
|
m_Thread.Stop( TRUE );
|
|
|
|
memset( m_pCommandLine, 0, sizeof(m_pCommandLine) );
|
|
|
|
CLgLocker Lock( &m_RecipientArray );
|
|
m_RecipientArray.RemoveAll();
|
|
}
|
|
|
|
BOOL CLgWatchDog::Register( LPCTSTR pRecipientName, DWORD dwRecipientID, DWORD dwRecipientTimeout )
|
|
{
|
|
// check within existing recipients
|
|
CLgLocker Lock( &m_RecipientArray );
|
|
for( INT i = 0; i < m_RecipientArray.GetCount(); i++ )
|
|
{
|
|
WATCHDOG_RECIPIENT& wr = m_RecipientArray[i];
|
|
if( wr.dwID == dwRecipientID )
|
|
{
|
|
// update time
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgWatchDog::Register"), _T("Recipient with ID = 0x%08X already exist in the list"), dwRecipientID );
|
|
return FALSE;
|
|
}
|
|
}
|
|
|
|
// add new recipient
|
|
WATCHDOG_RECIPIENT wr;
|
|
wr.dwID = dwRecipientID;
|
|
_tcsncpy( wr.pName, pRecipientName, MAX_PATH );
|
|
wr.liTimeout.QuadPart = 10000000i64 * dwRecipientTimeout;
|
|
::GetSystemTimeAsFileTime( (LPFILETIME)&wr.liTime );
|
|
return m_RecipientArray.Add( wr );
|
|
}
|
|
|
|
BOOL CLgWatchDog::Unregister( DWORD dwRecipientID )
|
|
{
|
|
// check within existing recipients
|
|
CLgLocker Lock( &m_RecipientArray );
|
|
for( INT i = 0; i < m_RecipientArray.GetCount(); i++ )
|
|
{
|
|
WATCHDOG_RECIPIENT& wr = m_RecipientArray[i];
|
|
if( wr.dwID == dwRecipientID )
|
|
{
|
|
m_RecipientArray.Remove(i);
|
|
return TRUE;
|
|
}
|
|
}
|
|
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgWatchDog::Unregister"), _T("Recipient with ID = 0x%08X is absent in the list"), dwRecipientID );
|
|
return FALSE;
|
|
}
|
|
|
|
void CLgWatchDog::Update( DWORD dwRecipientID )
|
|
{
|
|
CLgLocker Lock( &m_RecipientArray );
|
|
for( INT i = 0; i < m_RecipientArray.GetCount(); i++ )
|
|
{
|
|
WATCHDOG_RECIPIENT& wr = m_RecipientArray[i];
|
|
if( wr.dwID == dwRecipientID )
|
|
{
|
|
// update time
|
|
::GetSystemTimeAsFileTime( (LPFILETIME)&wr.liTime );
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
DWORD CLgWatchDog::WatchDogThreadProc()
|
|
{
|
|
BOOL bLoop = TRUE;
|
|
while( bLoop )
|
|
{
|
|
// wait one second
|
|
switch( ::WaitForSingleObject( m_Thread.GetThreadStopEvent(), 1000UL ) )
|
|
{
|
|
default:
|
|
case WAIT_FAILED:
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgWatchDog::ThreadProc"), _T("WaitForSingleObject failed") );
|
|
|
|
case WAIT_OBJECT_0:
|
|
bLoop = FALSE;
|
|
continue;
|
|
|
|
case WAIT_TIMEOUT:
|
|
break;
|
|
}
|
|
|
|
// current time
|
|
LARGE_INTEGER liTime;
|
|
::GetSystemTimeAsFileTime( (LPFILETIME)&liTime );
|
|
|
|
LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CLgWatchDog::ThreadProc"), _T("Check started...") );
|
|
|
|
// check times
|
|
CLgLocker Lock( &m_RecipientArray );
|
|
for( INT i = 0; i < m_RecipientArray.GetCount(); i++ )
|
|
{
|
|
WATCHDOG_RECIPIENT& wr = m_RecipientArray[i];
|
|
if( ( liTime.QuadPart - wr.liTime.QuadPart ) > wr.liTimeout.QuadPart )
|
|
{
|
|
OnTimeout( wr.pName, m_pCommandLine );
|
|
// LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgWatchDog::ThreadProc"), _T("Timeout for %s was expaired"), wr.pName );
|
|
break;
|
|
}
|
|
}
|
|
|
|
LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CLgWatchDog::ThreadProc"), _T("Check done") );
|
|
}
|
|
|
|
return 0UL;
|
|
}
|