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.

285 lines
6.5 KiB

  1. /*++
  2. Copyright (c) 2000, Microsoft Corporation
  3. Module Name:
  4. CollectionAdapterNotifySinks.cpp
  5. Abstract:
  6. Implement a collection of the CPrimaryControlChannel.cpp & CSecondaryControlChannel
  7. in a threa safe way.
  8. Author:
  9. JP Duplessis (jpdup) 08-Dec-2000
  10. Revision History:
  11. --*/
  12. #include "PreComp.h"
  13. #include "CollectionAdapterNotifySinks.h"
  14. #include "AlgController.h"
  15. CCollectionAdapterNotifySinks::~CCollectionAdapterNotifySinks()
  16. {
  17. RemoveAll();
  18. }
  19. //
  20. // Add an already created Adapter
  21. //
  22. HRESULT
  23. CCollectionAdapterNotifySinks::Add(
  24. IN IAdapterNotificationSink* pAdapterSinkToAdd, // AdapterSink to be added at the collection
  25. OUT DWORD* pdwNewCookie // Will be populated with the new unique id can be used later to retrieve the AdapterSink
  26. )
  27. {
  28. try
  29. {
  30. ENTER_AUTO_CS
  31. MYTRACE_ENTER("CCollectionAdapterNotifySinks::Add")
  32. if ( !pdwNewCookie )
  33. {
  34. MYTRACE_ERROR("Return Cookie address not supplied", 0);
  35. return E_INVALIDARG;
  36. }
  37. CAdapterSinkBuket* pNewBuketToAdd = new CAdapterSinkBuket(pAdapterSinkToAdd);
  38. if ( !pNewBuketToAdd )
  39. return E_OUTOFMEMORY;
  40. *pdwNewCookie = 1;
  41. //
  42. // Find a unique cookie
  43. //
  44. if ( m_ListOfAdapterSinks.empty() )
  45. {
  46. //
  47. // List is empty so obviously the cookie '1' is unique
  48. //
  49. MYTRACE("First SINK Cookie is %d", *pdwNewCookie);
  50. pNewBuketToAdd->m_dwCookie = *pdwNewCookie;
  51. }
  52. else
  53. {
  54. //
  55. // Travers the collection and stop when the cookie is not found
  56. // this schema could be optimize but the number of Sink is not expect to be large (1 per ALG modules)
  57. //
  58. MYTRACE("Current size %d", m_ListOfAdapterSinks.size() );
  59. while ( pNewBuketToAdd->m_dwCookie==0 )
  60. {
  61. MYTRACE("Search for unique Cookie %d", *pdwNewCookie);
  62. for ( LISTOF_ADAPTER_NOTIFICATION_SINK::iterator theIterator = m_ListOfAdapterSinks.begin();
  63. theIterator != m_ListOfAdapterSinks.end();
  64. theIterator++
  65. )
  66. {
  67. CAdapterSinkBuket* pAdapterSinkBuket = (CAdapterSinkBuket*)(*theIterator);
  68. if ( pAdapterSinkBuket->m_dwCookie == *pdwNewCookie )
  69. break;
  70. else
  71. {
  72. pNewBuketToAdd->m_dwCookie = *pdwNewCookie;
  73. break; // ok we found a unique cookie
  74. }
  75. }
  76. *pdwNewCookie = *pdwNewCookie + 1;
  77. }
  78. }
  79. //
  80. // Add Sync to Collection
  81. //
  82. m_ListOfAdapterSinks.push_back(pNewBuketToAdd);
  83. }
  84. catch(...)
  85. {
  86. return E_FAIL;
  87. }
  88. return S_OK;
  89. }
  90. //
  91. // Remove a adapter from the list (Thead safe)
  92. //
  93. HRESULT
  94. CCollectionAdapterNotifySinks::Remove(
  95. IN DWORD dwCookieToRemove
  96. )
  97. {
  98. try
  99. {
  100. ENTER_AUTO_CS
  101. MYTRACE_ENTER("CCollectionAdapterNotifySinks::Remove")
  102. HRESULT hr = S_OK;
  103. for ( LISTOF_ADAPTER_NOTIFICATION_SINK::iterator theIterator = m_ListOfAdapterSinks.begin();
  104. theIterator != m_ListOfAdapterSinks.end();
  105. theIterator++
  106. )
  107. {
  108. CAdapterSinkBuket* pAdapterSinkBuket = (CAdapterSinkBuket*)(*theIterator);
  109. if ( pAdapterSinkBuket->m_dwCookie == dwCookieToRemove )
  110. {
  111. delete pAdapterSinkBuket;
  112. m_ListOfAdapterSinks.erase(theIterator);
  113. return S_OK;
  114. }
  115. }
  116. }
  117. catch(...)
  118. {
  119. return E_FAIL;
  120. }
  121. return E_INVALIDARG; // if we are here that mean the cookie was not found
  122. }
  123. //
  124. // When an adapter form the collection
  125. //
  126. HRESULT
  127. CCollectionAdapterNotifySinks::RemoveAll()
  128. {
  129. try
  130. {
  131. ENTER_AUTO_CS
  132. MYTRACE_ENTER("CCollectionAdapterNotifySinks::RemoveAll")
  133. //
  134. // By deleting all the ControlChannel they will also cancel all their associated Redirection
  135. //
  136. LISTOF_ADAPTER_NOTIFICATION_SINK::iterator theIterator;
  137. MYTRACE("Collection has %d item", m_ListOfAdapterSinks.size());
  138. while ( m_ListOfAdapterSinks.size() > 0 )
  139. {
  140. theIterator = m_ListOfAdapterSinks.begin();
  141. delete (*theIterator);
  142. m_ListOfAdapterSinks.erase(theIterator);
  143. }
  144. }
  145. catch(...)
  146. {
  147. return E_FAIL;
  148. }
  149. return S_OK;
  150. }
  151. HRESULT
  152. CCollectionAdapterNotifySinks::Notify(
  153. eNOTIFY eAction,
  154. IAdapterInfo* pIAdapterInfo
  155. )
  156. /*++
  157. Routine Description:
  158. For all AdapterSink inteface in the current collection do a notify with the given action ADDED,REMOVED,MODIFIED
  159. Arguments:
  160. eAction - ADDED, REMOVED, MODIFIED
  161. pIAdapterInfo - Interface of the Adapter with the current action to be notify to alg modules
  162. Return Value:
  163. void - None
  164. Environment:
  165. --*/
  166. {
  167. try
  168. {
  169. ENTER_AUTO_CS
  170. MYTRACE_ENTER("CCollectionAdapterNotifySinks::NotifySink")
  171. MYTRACE("Collection size %d", m_ListOfAdapterSinks.size());
  172. for ( LISTOF_ADAPTER_NOTIFICATION_SINK::iterator theIterator = m_ListOfAdapterSinks.begin();
  173. theIterator != m_ListOfAdapterSinks.end();
  174. theIterator++
  175. )
  176. {
  177. CAdapterSinkBuket* pAdapterSinkBuket = (CAdapterSinkBuket*)(*theIterator);
  178. MYTRACE("Will notify AdapterSink with cookie #%d", pAdapterSinkBuket->m_dwCookie);
  179. switch ( eAction )
  180. {
  181. case eNOTIFY_ADDED:
  182. pAdapterSinkBuket->m_pInterface->AdapterAdded(pIAdapterInfo);
  183. break;
  184. case eNOTIFY_REMOVED:
  185. pAdapterSinkBuket->m_pInterface->AdapterRemoved(pIAdapterInfo);
  186. break;
  187. case eNOTIFY_MODIFIED:
  188. pAdapterSinkBuket->m_pInterface->AdapterModified(pIAdapterInfo);
  189. break;
  190. }
  191. }
  192. }
  193. catch(...)
  194. {
  195. return E_FAIL;
  196. }
  197. return S_OK;
  198. }