config.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*-------------------------------------------------------------------------
  2. * Config.h
  3. *
  4. * The definitions, etc. for a set of classes that are helpful in
  5. * maintaining a set of settings in an organized fashion. The class is
  6. * generic and extendible to support any implementaion of where the
  7. * settings are stored (e.g., registry, database, etc.).
  8. *
  9. * Owner:
  10. *
  11. * Copyright 1986-1998 Microsoft Corporation, All Rights Reserved
  12. *-----------------------------------------------------------------------*/
  13. #ifdef _CONFIG_H
  14. #error config.h included twice
  15. #endif
  16. #define _CONFIG_H
  17. #define HKLM_Sentinal "SYSTEM\\CurrentControlSet\\Services\\Sentinal\\Parameters"
  18. // Settings are grouped by "component". Here's the list of different components:
  19. typedef enum
  20. {
  21. kCompId_Sentinal, // Settings related to Sentinal
  22. // !!NOTE!! If you add to this table, update REGCFG.CPP's CompIdToKey()
  23. // so when we run in registry mode we know where to look...
  24. // !!NOTE!! Also, since data can be stored in the database, you should coordinate
  25. // adding values in here with everyone, so we don't get database
  26. // corruption, where two people use the same component ID in the database to store
  27. // different component's settings...
  28. kCompId_Last // Must be last...
  29. } ComponentId;
  30. typedef enum
  31. {
  32. kConfigType_SZ,
  33. kConfigType_DWORD,
  34. } ConfigItemType;
  35. class ConfigItem
  36. {
  37. public:
  38. ConfigItemType m_type; // Item is an int or string?
  39. ZString m_zsName; // Item's name.
  40. ZString m_zsValue; // Item's value.
  41. };
  42. typedef struct
  43. {
  44. char * pszKeyName;
  45. ConfigItemType type;
  46. int cb;
  47. int nRequired; // for ReadMany: True/False on if required.
  48. union // for ReadRgMany: Required up and including to this index...
  49. {
  50. DWORD dwDefault;
  51. char * pszDefault;
  52. };
  53. } ConfigInfo;
  54. typedef struct
  55. {
  56. ConfigInfo info;
  57. BYTE * pBuf;
  58. } ConfigInfoBuffer;
  59. // pure virtual base class for configuration reader.
  60. class ConfigReader
  61. {
  62. public:
  63. ConfigReader(ComponentId compId) :
  64. m_compId(compId)
  65. {
  66. }
  67. virtual ~ConfigReader() {}
  68. virtual BOOL ReadRg (const ConfigInfoBuffer *prgItems, int cItems);
  69. virtual BOOL ReadRgRg (int cRepeat, const ConfigInfoBuffer *prgItems, int cItems); // ReadMany() but, the szKeyName has "00x" appended to it (where x is 0 thru cRepeat). pb is assumed to be an array...
  70. virtual BOOL ReadInt (const char *pszItem, int *pnVal, BOOL fRequired, int nDefault);
  71. virtual BOOL ReadSz (const char *pszItem, char *psz, int cchMax, BOOL fRequired, const char *pszDefault);
  72. virtual BOOL WriteInt (const char *pszItem, int nVal);
  73. virtual BOOL WriteSz (const char *pszItem, const char *psz, int cch);
  74. virtual BOOL DeleteItem(const char *pszItem) = 0;
  75. protected:
  76. ComponentId m_compId;
  77. virtual BOOL Open() = 0;
  78. virtual BOOL Close() = 0;
  79. virtual BOOL FastReadInt (const char *pszItem, int *pnVal, BOOL fRequired, int nDefault) = 0;
  80. virtual BOOL FastReadSz (const char *pszItem, char *psz, int cchMax, BOOL fRequired, const char *pszDefault) = 0;
  81. virtual BOOL FastWriteInt(const char *pszItem, int nVal) = 0;
  82. virtual BOOL FastWriteSz (const char *pszItem, const char *psz, int cch) = 0;
  83. };
  84. // Registry based configuration reader:
  85. class RegConfigReader : public ConfigReader
  86. {
  87. public:
  88. RegConfigReader(ComponentId compId) :
  89. ConfigReader(compId),
  90. m_hk(NULL)
  91. {
  92. CompIdToKey();
  93. }
  94. virtual ~RegConfigReader()
  95. {
  96. }
  97. virtual BOOL DeleteItem(const char *pszItem);
  98. protected:
  99. virtual BOOL Open();
  100. virtual BOOL Close();
  101. virtual BOOL FastReadInt (const char *pszItem, int *pnVal, BOOL fRequired, int nDefault);
  102. virtual BOOL FastReadSz (const char *pszItem, char *psz, int cchMax, BOOL fRequired, const char *pszDefault);
  103. virtual BOOL FastWriteInt(const char *pszItem, int nVal);
  104. virtual BOOL FastWriteSz (const char *pszItem, const char *psz, int cch);
  105. private:
  106. BOOL CompIdToKey();
  107. char m_szKey[255];
  108. HKEY m_hk;
  109. };
  110. // This routine can be used to create the "appropriate" configuration reader type (registry
  111. // or whatever) based on whether a #define is set...
  112. // Currently, though we just have a Registry-based solution, so it doesn't matter much.
  113. ConfigReader * CreateConfigReader (ComponentId compId);
  114. RegConfigReader *CreateRegConfigReader(ComponentId compId);
  115. ////////////////////////////////////////////////////////////////////////
  116. // This object contains a list of settings stored for a given ComponentId, and list of settings.
  117. class Config
  118. {
  119. public:
  120. Config(ComponentId compId) : m_compId(compId), m_pArgs(NULL), m_nArgs(0), m_pConfigInfo(NULL) {}
  121. virtual ~Config() { KillArgs(); }
  122. BOOL Init(const ConfigInfo *pConfigInfo, int cItems, BOOL fRange, int cRange);
  123. BOOL GetDWORDArg(DWORD nArg, DWORD *pdw, int nRange); // only look at nRange, if m_fRange
  124. BOOL GetStringArg(DWORD nArg, char *pszBuf, int cchBuf, int nRange); // only look at nRange, if m_fRange
  125. // Caller must delete pointer returned:
  126. ConfigInfo *GetConfigInfo(int *pcConfig);
  127. inline ComponentId GetComponentId() const { return m_compId; }
  128. private:
  129. ComponentId m_compId;
  130. BOOL m_fRange; // TRUE if we're storing a bunch of settings (e.g. a bunch of strings or a bunch of DWORDs).
  131. BOOL m_cRange;
  132. typedef struct
  133. {
  134. ConfigItemType type;
  135. DWORD cb;
  136. union // for ReadRgMany: Required up and including to this index...
  137. {
  138. DWORD dwVal; // Used if we're not storing a range, and type == kConfigType_DWORD
  139. char *pszVal; // Used to store strings, for both range and non range storage
  140. DWORD *pdwVal; // Used to store DWORD's when we're storing a range.
  141. };
  142. } ARGS;
  143. ARGS *m_pArgs;
  144. DWORD m_nArgs;
  145. ConfigInfoBuffer *m_pConfigInfo;
  146. void KillArgs();
  147. };
  148. class ConfigCache
  149. {
  150. public:
  151. ConfigCache()
  152. {
  153. m_pcsEditList = new CriticalSection;
  154. ZeroMemory(&m_rgpConfigList, sizeof(m_rgpConfigList));
  155. }
  156. virtual ~ConfigCache()
  157. {
  158. DeleteConfigList();
  159. delete m_pcsEditList;
  160. }
  161. Config * FindConfig(ComponentId compId);
  162. void AddConfig(Config *pConfig);
  163. Config * ReplaceConfig(ComponentId compId, Config *pConfig);
  164. void AddConfigToTail(Config *pConfig);
  165. Config * RemoveHead(ComponentId compId);
  166. Config * RemoveHeadIfOld(ComponentId compId, DWORD dwDeltaOld);
  167. private:
  168. typedef struct tag_CONFIG_LIST
  169. {
  170. Config * pConfig;
  171. DWORD dwTimeAdded;
  172. struct tag_CONFIG_LIST *pNext;
  173. } CONFIG_LIST;
  174. CONFIG_LIST * m_rgpConfigList[kCompId_Last];
  175. CriticalSection *m_pcsEditList; // this is a pointer, solely cuz we CriticalSection isn't __declspec(dllexport)...
  176. void DeleteConfigList();
  177. };
  178. // This class maintains a bunch of different configuration information for various components.
  179. // It allows a new configuration to be loaded to replace an existing one in the list in a thread
  180. // safe manner, without needing to use a CriticalSection to guard list read access... It does
  181. // this by using garbage collection when it deletes a node, making the assumption that some thread
  182. // that accesses the list of configurations will traverse the entire list in a short period of time.
  183. class ConfigManager
  184. {
  185. public:
  186. ConfigManager() : m_hevtDie(NULL), m_hthrGarbageCollect(NULL), m_pcsAdd(NULL) {}
  187. virtual ~ConfigManager();
  188. BOOL Init();
  189. BOOL LoadConfig (ComponentId compId, const ConfigInfo *pInfo, int cInfo);
  190. BOOL ReloadConfig (ComponentId compId);
  191. BOOL IsConfigLoaded (ComponentId compId);
  192. // Routines for getting at our settings.
  193. BOOL GetConfigDWORD (ComponentId compId, DWORD nArg, DWORD *pdw);
  194. BOOL GetConfigString (ComponentId compId, DWORD nArg, char *pszBuf, int cchBuf);
  195. virtual void GarbageCollectThread();
  196. private:
  197. ConfigCache m_cacheComponent; // Settings stored by Component
  198. ConfigCache m_cacheGarbage; // Garbage collection guys -- we lazy delete these.
  199. CriticalSection * m_pcsAdd; // Guards adding to our list/deleting old guy. This is a ptr, solely cuz CriticalSection isn't __declspec(dllexport).
  200. HANDLE m_hevtDie; // Signalled when the Garbage Collect thread should go away.
  201. HANDLE m_hthrGarbageCollect; // Handle to garbage collector thread.
  202. static int CALLBACK GarbageCollectThreadThunk(ConfigManager *pMgr)
  203. {
  204. ZAssert(pMgr);
  205. pMgr->GarbageCollectThread();
  206. return 0;
  207. }
  208. };