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.

377 lines
10 KiB

  1. //***************************************************************************
  2. //
  3. // File:
  4. //
  5. // Module: MS SNMP Provider
  6. //
  7. // Purpose:
  8. //
  9. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  10. //
  11. //***************************************************************************
  12. #ifndef _SMIREVT_H_
  13. #define _SMIREVT_H_
  14. //Number of connection points
  15. #define SMIR_NUMBER_OF_CONNECTION_POINTS 1
  16. #define SMIR_NOTIFY_CONNECTION_POINT 0
  17. class CSmirConnectionPoint;
  18. class CSmirWbemEventConsumer;
  19. class CSmirConnObject;
  20. typedef CSmirConnObject *PCSmirConnObject;
  21. class CEnumConnections;
  22. typedef CEnumConnections *PCEnumConnections;
  23. class CEnumConnectionPoints;
  24. typedef CEnumConnectionPoints *PCEnumConnectionPoints;
  25. /*
  26. * Each connection is saved so that we can enumerate, delete, and trigger
  27. * This template provides the container for the connections and cookies
  28. */
  29. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  30. class ConnectionMap : public CObject
  31. {
  32. private:
  33. BOOL m_bThreadSafe ;
  34. #ifdef NOT_IMPLEMENTED_AS_CLSCTX_INPROC_SERVER
  35. CMutex * m_criticalLock ;
  36. #else
  37. CCriticalSection * m_criticalLock ;
  38. #endif
  39. CMap <KEY, ARG_KEY, VALUE, ARG_VALUE> m_cmap ;
  40. protected:
  41. public:
  42. ConnectionMap ( BOOL threadSafe = FALSE ) ;
  43. virtual ~ConnectionMap () ;
  44. int GetCount () const ;
  45. BOOL IsEmpty () const ;
  46. BOOL Lookup(ARG_KEY key, VALUE& rValue) const ;
  47. VALUE& operator[](ARG_KEY key) ;
  48. void SetAt(ARG_KEY key, ARG_VALUE newValue) ;
  49. BOOL RemoveKey(ARG_KEY key) ;
  50. void RemoveAll () ;
  51. POSITION GetStartPosition() const ;
  52. void GetNextAssoc(POSITION& rNextPosition, KEY& rKey, VALUE& rValue) const ;
  53. } ;
  54. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  55. ConnectionMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: ConnectionMap ( BOOL threadSafeArg )
  56. : m_bThreadSafe ( threadSafeArg ) , m_criticalLock ( NULL )
  57. {
  58. if ( m_bThreadSafe )
  59. {
  60. m_criticalLock = new CCriticalSection ;
  61. }
  62. }
  63. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  64. ConnectionMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: ~ConnectionMap ()
  65. {
  66. if ( m_bThreadSafe )
  67. {
  68. delete m_criticalLock ;
  69. }
  70. }
  71. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  72. int ConnectionMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: GetCount() const
  73. {
  74. if ( m_bThreadSafe )
  75. {
  76. m_criticalLock->Lock () ;
  77. int count = m_cmap.GetCount () ;
  78. m_criticalLock->Unlock () ;
  79. return count ;
  80. }
  81. else
  82. {
  83. return m_cmap.GetCount () ;
  84. }
  85. }
  86. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  87. BOOL ConnectionMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: IsEmpty() const
  88. {
  89. if ( m_bThreadSafe )
  90. {
  91. m_criticalLock->Lock () ;
  92. BOOL isEmpty = m_cmap.IsEmpty () ;
  93. m_criticalLock->Unlock () ;
  94. return isEmpty ;
  95. }
  96. else
  97. {
  98. return m_cmap.IsEmpty () ;
  99. }
  100. }
  101. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  102. BOOL ConnectionMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: Lookup(ARG_KEY key, VALUE& rValue) const
  103. {
  104. if ( m_bThreadSafe )
  105. {
  106. m_criticalLock->Lock () ;
  107. BOOL lookup = m_cmap.Lookup ( key , rValue ) ;
  108. m_criticalLock->Unlock () ;
  109. return lookup ;
  110. }
  111. else
  112. {
  113. return m_cmap.Lookup ( key , rValue ) ;
  114. }
  115. }
  116. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  117. VALUE& ConnectionMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: operator[](ARG_KEY key)
  118. {
  119. if ( m_bThreadSafe )
  120. {
  121. m_criticalLock->Lock () ;
  122. VALUE &value = m_cmap.operator [] ( key ) ;
  123. m_criticalLock->Unlock () ;
  124. return value ;
  125. }
  126. else
  127. {
  128. return m_cmap.operator [] ( key ) ;
  129. }
  130. }
  131. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  132. void ConnectionMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: SetAt(ARG_KEY key, ARG_VALUE newValue)
  133. {
  134. if ( m_bThreadSafe )
  135. {
  136. m_criticalLock->Lock () ;
  137. m_cmap.SetAt ( key , newValue ) ;
  138. m_criticalLock->Unlock () ;
  139. }
  140. else
  141. {
  142. m_cmap.SetAt ( key , newValue ) ;
  143. }
  144. }
  145. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  146. BOOL ConnectionMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: RemoveKey(ARG_KEY key)
  147. {
  148. if ( m_bThreadSafe )
  149. {
  150. m_criticalLock->Lock () ;
  151. BOOL removeKey = m_cmap.RemoveKey ( key ) ;
  152. m_criticalLock->Unlock () ;
  153. return removeKey ;
  154. }
  155. else
  156. {
  157. return m_cmap.RemoveKey ( key ) ;
  158. }
  159. }
  160. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  161. void ConnectionMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: RemoveAll()
  162. {
  163. if ( m_bThreadSafe )
  164. {
  165. m_criticalLock->Lock () ;
  166. m_cmap.RemoveAll () ;
  167. m_criticalLock->Unlock () ;
  168. }
  169. else
  170. {
  171. m_cmap.RemoveAll () ;
  172. }
  173. }
  174. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  175. POSITION ConnectionMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: GetStartPosition() const
  176. {
  177. if ( m_bThreadSafe )
  178. {
  179. m_criticalLock->Lock () ;
  180. POSITION position = m_cmap.GetStartPosition () ;
  181. m_criticalLock->Unlock () ;
  182. return position ;
  183. }
  184. else
  185. {
  186. return m_cmap.GetStartPosition () ;
  187. }
  188. }
  189. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  190. void ConnectionMap <KEY, ARG_KEY, VALUE, ARG_VALUE>:: GetNextAssoc(POSITION& rNextPosition, KEY& rKey, VALUE& rValue) const
  191. {
  192. if ( m_bThreadSafe )
  193. {
  194. m_criticalLock->Lock () ;
  195. m_cmap.GetNextAssoc ( rNextPosition , rKey , rValue ) ;
  196. m_criticalLock->Unlock () ;
  197. }
  198. else
  199. {
  200. m_cmap.GetNextAssoc ( rNextPosition , rKey , rValue ) ;
  201. }
  202. }
  203. /*
  204. * The connectable object implements IUnknown and
  205. * IConnectionPointContainer. It is closely associated with
  206. * the connection point enumerator, CEnumConnectionPoints.
  207. */
  208. class CSmirConnObject : public IConnectionPointContainer
  209. {
  210. private:
  211. LONG m_cRef; //Object reference count
  212. //Array holding all the points we have.
  213. CSmirConnectionPoint **m_rgpConnPt;
  214. public:
  215. CSmirConnObject(CSmir *pSmir);
  216. virtual ~CSmirConnObject(void);
  217. BOOL Init(CSmir *pSmir);
  218. //IUnknown members
  219. STDMETHODIMP QueryInterface(REFIID, PPVOID);
  220. STDMETHODIMP_(DWORD) AddRef(void);
  221. STDMETHODIMP_(DWORD) Release(void);
  222. //IConnectionPointContainer members
  223. STDMETHODIMP EnumConnectionPoints(IEnumConnectionPoints **);
  224. STDMETHODIMP FindConnectionPoint(REFIID, IConnectionPoint **);
  225. //Other members
  226. // BOOL TriggerEvent(UINT, SMIR_NOTIFY_TYPE);
  227. // BOOL TriggerEvent(long lObjectCount, ISmirClassHandle *phClass);
  228. };
  229. /*
  230. * The connectable object implements IUnknown and
  231. * IConnectionPointContainer. It is closely associated with
  232. * the connection point enumerator, CEnumConnectionPoints.
  233. */
  234. //Enumerator class for EnumConnectionPoints
  235. class CEnumConnectionPoints : public IEnumConnectionPoints
  236. {
  237. private:
  238. LONG m_cRef; //Object reference count
  239. LPUNKNOWN m_pUnkRef; //IUnknown for ref counting
  240. ULONG m_iCur; //Current element
  241. ULONG m_cPoints; //Number of conn points
  242. IConnectionPoint **m_rgpCP; //Source of conn points
  243. public:
  244. CEnumConnectionPoints(LPUNKNOWN, ULONG, IConnectionPoint **);
  245. virtual ~CEnumConnectionPoints(void);
  246. //IUnknown members that delegate to m_pUnkRef.
  247. STDMETHODIMP QueryInterface(REFIID, LPVOID *);
  248. STDMETHODIMP_(ULONG) AddRef(void);
  249. STDMETHODIMP_(ULONG) Release(void);
  250. //IEnumConnectionPoints members
  251. STDMETHODIMP Next(ULONG, IConnectionPoint **, ULONG *);
  252. STDMETHODIMP Skip(ULONG);
  253. STDMETHODIMP Reset(void);
  254. STDMETHODIMP Clone(IEnumConnectionPoints **);
  255. };
  256. /*
  257. * The connection point object iself is contained within the
  258. * connection point container, which is the connectable object.
  259. * It therefore manages a back pointer to that connectable object,
  260. * and implement IConnectionPoint. This object has a few
  261. * member functions besides those in IConnectionPoint that are
  262. * used to fire the outgoing calls.
  263. */
  264. class CSmirConnectionPoint : public IConnectionPoint
  265. {
  266. private:
  267. LONG m_cRef; //Object reference count
  268. PCSmirConnObject m_pObj; //Containing object
  269. IID m_iid; //Our relevant interface
  270. LONG m_dwCookieNext; //Counter
  271. CCriticalSection criticalSection;
  272. protected:
  273. /*
  274. * For each connection we need to maintain
  275. * the sink pointer and the cookie assigned to it.
  276. */
  277. ConnectionMap <DWORD, DWORD, IUnknown *,IUnknown *> m_Connections ;
  278. public:
  279. CSmirConnectionPoint(PCSmirConnObject, REFIID, CSmir *pSmir);
  280. virtual ~CSmirConnectionPoint(void);
  281. //IUnknown members
  282. STDMETHODIMP QueryInterface(REFIID, LPVOID *);
  283. STDMETHODIMP_(ULONG) AddRef(void);
  284. STDMETHODIMP_(ULONG) Release(void);
  285. //IConnectionPoint members
  286. STDMETHODIMP GetConnectionInterface(IID *);
  287. STDMETHODIMP GetConnectionPointContainer
  288. (IConnectionPointContainer **);
  289. STDMETHODIMP Advise(LPUNKNOWN, DWORD *);
  290. STDMETHODIMP Unadvise(DWORD);
  291. STDMETHODIMP EnumConnections(IEnumConnections **);
  292. };
  293. class CSmirNotifyCP : public CSmirConnectionPoint
  294. {
  295. private:
  296. CSmirWbemEventConsumer *m_evtConsumer;
  297. BOOL m_bRegistered;
  298. public:
  299. STDMETHODIMP Advise(CSmir*,LPUNKNOWN, DWORD *);
  300. STDMETHODIMP Unadvise(CSmir*,DWORD);
  301. CSmirNotifyCP(PCSmirConnObject pCO, REFIID riid, CSmir *pSmir);
  302. ~CSmirNotifyCP();
  303. BOOL TriggerEvent();
  304. };
  305. //Enumeration class for EnumConnections
  306. class CEnumConnections : public IEnumConnections
  307. {
  308. private:
  309. LONG m_cRef; //Object reference count
  310. LPUNKNOWN m_pUnkRef; //IUnknown for ref counting
  311. ULONG m_iCur; //Current element
  312. ULONG m_cConn; //Number of connections
  313. LPCONNECTDATA m_rgConnData; //Source of connections
  314. public:
  315. CEnumConnections(LPUNKNOWN, ULONG, LPCONNECTDATA);
  316. virtual ~CEnumConnections(void);
  317. //IUnknown members that delegate to m_pUnkRef.
  318. STDMETHODIMP QueryInterface(REFIID, LPVOID *);
  319. STDMETHODIMP_(ULONG) AddRef(void);
  320. STDMETHODIMP_(ULONG) Release(void);
  321. //IEnumConnections members
  322. STDMETHODIMP Next(ULONG, LPCONNECTDATA, ULONG *);
  323. STDMETHODIMP Skip(ULONG);
  324. STDMETHODIMP Reset(void);
  325. STDMETHODIMP Clone(IEnumConnections **);
  326. };
  327. #endif