Leaked source code of windows server 2003
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.

147 lines
4.0 KiB

  1. #pragma once
  2. #ifndef _MONITOR_H_
  3. #define _MONITOR_H_
  4. #include "RefCount.h"
  5. #include "RefPtr.h"
  6. #include "MyString.h"
  7. #undef STRING_TRACE_LOG
  8. #ifdef STRING_TRACE_LOG
  9. # include <strlog.hxx>
  10. # define STL_PRINTF m_stl.Printf
  11. # define STL_PUTS(s) m_stl.Puts(s)
  12. #else
  13. # define STL_PRINTF
  14. # define STL_PUTS(s)
  15. #endif
  16. // a client supplies it's own derviation of CMonitorNotify to the monitor.
  17. // the notify method is called when the monitored object has changed
  18. class CMonitorNotify : public CRefCounter
  19. {
  20. public:
  21. virtual void Notify() = 0;
  22. };
  23. typedef TRefPtr<CMonitorNotify> CMonitorNotifyPtr;
  24. // the base object of anything that can be monitored
  25. class CMonitorNode : public CRefCounter
  26. {
  27. public:
  28. virtual void Notify() = 0;
  29. virtual HANDLE NotificationHandle() const = 0;
  30. };
  31. typedef TRefPtr<CMonitorNode> CMonitorNodePtr;
  32. // since we can only monitor directories, the file class,
  33. // preserves information about each file in a particular
  34. // directory
  35. class CMonitorFile : public CRefCounter
  36. {
  37. public:
  38. CMonitorFile( const String&, const CMonitorNotifyPtr& );
  39. bool CheckNotify();
  40. const String& FileName() const;
  41. private:
  42. virtual ~CMonitorFile();
  43. bool GetFileTime( FILETIME& );
  44. FILETIME m_ft;
  45. const String m_strFile;
  46. CMonitorNotifyPtr m_pNotify;
  47. };
  48. typedef TRefPtr<CMonitorFile> CMonitorFilePtr;
  49. // an implementaiton of CMonitorNode's interface for montioring directories
  50. class CMonitorDir : public CMonitorNode
  51. {
  52. public:
  53. CMonitorDir( const String& );
  54. // CMonitorNode interface
  55. virtual void Notify();
  56. virtual HANDLE NotificationHandle() const;
  57. void AddFile( const String&, const CMonitorNotifyPtr& );
  58. void RemoveFile( const String& );
  59. const String& Dir() const;
  60. ULONG NumFiles() const;
  61. private:
  62. virtual ~CMonitorDir();
  63. const String m_strDir;
  64. TVector<CMonitorFilePtr> m_files;
  65. HANDLE m_hNotification;
  66. };
  67. DECLARE_REFPTR(CMonitorDir,CMonitorNode);
  68. // an implementation of CMonitorNode's interface for monitoring a registry key
  69. class CMonitorRegKey : public CMonitorNode
  70. {
  71. public:
  72. CMonitorRegKey( HKEY, const String&, const CMonitorNotifyPtr& );
  73. // CMonitorNode interface
  74. virtual void Notify();
  75. virtual HANDLE NotificationHandle() const;
  76. // CMonitorRegKey interface
  77. const String& m_strKey;
  78. const HKEY m_hBaseKey;
  79. private:
  80. virtual ~CMonitorRegKey();
  81. HKEY m_hKey;
  82. HANDLE m_hEvt;
  83. CMonitorNotifyPtr m_pNotify;
  84. };
  85. DECLARE_REFPTR(CMonitorRegKey, CMonitorNode);
  86. // the main monitoring object
  87. class CMonitor
  88. {
  89. public:
  90. CMonitor();
  91. ~CMonitor();
  92. void MonitorFile( LPCTSTR, const CMonitorNotifyPtr& );
  93. void StopMonitoringFile( LPCTSTR );
  94. void MonitorRegKey( HKEY, LPCTSTR, const CMonitorNotifyPtr& );
  95. void StopMonitoringRegKey( HKEY, LPCTSTR );
  96. void StopAllMonitoring();
  97. private:
  98. static unsigned __stdcall ThreadFunc( void* );
  99. bool StartUp();
  100. DWORD DoMonitoring();
  101. TVector<CMonitorDirPtr> m_dirs;
  102. TVector<CMonitorRegKeyPtr> m_regKeys;
  103. CComAutoCriticalSection m_cs;
  104. HANDLE m_hevtBreak;
  105. HANDLE m_hevtShutdown;
  106. HANDLE m_hThread;
  107. volatile bool m_bRunning;
  108. volatile bool m_bStopping;
  109. #ifdef STRING_TRACE_LOG
  110. public:
  111. CStringTraceLog m_stl;
  112. #endif
  113. };
  114. #endif // ! _MONITOR_H_