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.

129 lines
2.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "basicEventHeap.h"
  2. #include "basicEventHandler.h"
  3. #define LOG(X) m_pLog->Log("basicEventHeap", this, X)
  4. #define LOG_ERROR(X) m_pLog->Log("basicEventHeap", this, _LOG_ERROR, X)
  5. basicEventHeap * basicEventHeap::_instance = 0;
  6. /**
  7. * Returns the single instance of the object.
  8. */
  9. basicEventHeap * basicEventHeap::getInstance()
  10. {
  11. if (0 == _instance)
  12. {
  13. _instance = new basicEventHeap;
  14. };
  15. return _instance;
  16. }
  17. void basicEventHeap::addHandler(basicEventHandler * handler, bool atBottom)
  18. {
  19. m_pHandlerMutex->lock();
  20. if( atBottom ) {
  21. m_vHandlers.push_back( handler );
  22. } else {
  23. m_vHandlers.insert( m_vHandlers.begin(), handler );
  24. };
  25. m_pHandlerMutex->unlock();
  26. }
  27. void basicEventHeap::removeHandler(basicEventHandler * handler)
  28. {
  29. m_pHandlerMutex->lock();
  30. for (V_EVT_HANDLER::iterator idx = m_vHandlers.begin(); idx != m_vHandlers.end(); idx++)
  31. {
  32. if ((*idx) == handler)
  33. {
  34. m_vHandlers.erase(idx);
  35. break;
  36. };
  37. };
  38. m_pHandlerMutex->unlock();
  39. }
  40. void basicEventHeap::moveHandlerBottom( basicEventHandler * handler ){
  41. m_pHandlerMutex->lock();
  42. for (V_EVT_HANDLER::iterator idx = m_vHandlers.begin(); idx != m_vHandlers.end(); idx++)
  43. {
  44. if ((*idx) == handler)
  45. {
  46. m_vHandlers.erase(idx);
  47. break;
  48. };
  49. };
  50. m_vHandlers.push_back( handler );
  51. m_pHandlerMutex->unlock();
  52. }
  53. void basicEventHeap::moveHandlerTop( basicEventHandler * handler ){
  54. m_pHandlerMutex->lock();
  55. for (V_EVT_HANDLER::iterator idx = m_vHandlers.begin(); idx != m_vHandlers.end(); idx++)
  56. {
  57. if ((*idx) == handler)
  58. {
  59. m_vHandlers.erase(idx);
  60. break;
  61. };
  62. };
  63. m_vHandlers.insert( m_vHandlers.begin(), handler );
  64. m_pHandlerMutex->unlock();
  65. }
  66. void basicEventHeap::m_fThreadProc(void)
  67. {
  68. basicEventHandler * mpHandler = NULL;
  69. EVENT_CONTAINER * mpEvent = NULL;
  70. m_bThreadStatus = TH_WORKING;
  71. pthread_setname_np( pthread_self(), "EventHeap" );
  72. while (m_bThreadStatus < TH_ENDED)
  73. {
  74. if (m_bThreadStatus == TH_WORKING)
  75. {
  76. mpEvent = NULL;
  77. m_pDispatchingMutex->lock();
  78. if (! m_vEventQueue.empty())
  79. {
  80. mpEvent = m_vEventQueue.front();
  81. m_vEventQueue.pop();
  82. };
  83. m_pDispatchingMutex->unlock();
  84. if (mpEvent != NULL)
  85. {
  86. m_pHandlerMutex->lock();
  87. for (V_EVT_HANDLER::iterator idx = m_vHandlers.begin(); idx < m_vHandlers.end(); idx++)
  88. {
  89. mpHandler = (*idx);
  90. if (mpHandler != NULL)
  91. {
  92. if (! mpHandler->dispatchEvent(mpEvent))
  93. {
  94. break; // Stop Propogation if false
  95. }
  96. }
  97. };
  98. m_pHandlerMutex->unlock();
  99. utilizeBasicEvent(mpEvent);
  100. }
  101. else
  102. {
  103. SLEEP(1);
  104. }
  105. };
  106. };
  107. }
  108. void basicEventHeap::broadcast(EVENT_CONTAINER * Evt)
  109. {
  110. if (Evt)
  111. {
  112. m_pDispatchingMutex->lock();
  113. m_vEventQueue.push(Evt);
  114. m_pDispatchingMutex->unlock();
  115. };
  116. }
  117. #undef LOG
  118. #undef LOG_ERROR