dispatcher.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _DISPATCHER_H_
  23. #define _DISPATCHER_H_
  24. #ifndef _MESSAGE_H_
  25. #include "util/messaging/message.h"
  26. #endif
  27. #ifndef _CONSOLE_H_
  28. #include "console/console.h"
  29. #endif
  30. /// @addtogroup msgsys Message System
  31. // @{
  32. //-----------------------------------------------------------------------------
  33. /// @brief Namespace for the message dispatcher functions
  34. //-----------------------------------------------------------------------------
  35. namespace Dispatcher
  36. {
  37. // [tom, 2/19/2007] This semi colon prevents VS from auto indenting the comments
  38. // below, which is really annoying when you're trying to write docs.
  39. ;
  40. /// @addtogroup msgsys Message System
  41. // @{
  42. //-----------------------------------------------------------------------------
  43. // Interface for objects that receive messages
  44. //-----------------------------------------------------------------------------
  45. //-----------------------------------------------------------------------------
  46. /// @brief Listener interface for objects that receive messages
  47. ///
  48. /// @see ScriptMsgListener
  49. //-----------------------------------------------------------------------------
  50. class IMessageListener
  51. {
  52. protected:
  53. /// List of queues this listener is registered with.
  54. Vector<StringTableEntry> mQueues;
  55. public:
  56. virtual ~IMessageListener();
  57. //-----------------------------------------------------------------------------
  58. /// @brief Callback for when messages are received
  59. ///
  60. /// @param queue The name of the queue the message was dispatched to
  61. /// @param msg The type of message
  62. /// @param data The data for the message
  63. /// @return false to prevent other listeners receiving this message, true otherwise
  64. /// @see onMessageObjectReceived()
  65. //-----------------------------------------------------------------------------
  66. virtual bool onMessageReceived(StringTableEntry queue, const char *msg, const char *data) = 0;
  67. //-----------------------------------------------------------------------------
  68. /// @brief Callback for when message objects are received
  69. ///
  70. /// @param queue The name of the queue the message was dispatched to
  71. /// @param msg The message object
  72. /// @return false to prevent other listeners receiving this message, true otherwise
  73. /// @see onMessageReceived()
  74. //-----------------------------------------------------------------------------
  75. virtual bool onMessageObjectReceived(StringTableEntry queue, Message *msg ) = 0;
  76. //-----------------------------------------------------------------------------
  77. /// @brief Callback for when the listener is added to a queue
  78. ///
  79. /// The default implementation of onAddToQueue() and onRemoveFromQueue()
  80. /// provide tracking of the queues this listener is added to through the
  81. /// #mQueues member. Overrides of onAddToQueue() or onRemoveFromQueue()
  82. /// should ensure they call the parent implementation in any overrides.
  83. ///
  84. /// @param queue The name of the queue that the listener added to
  85. /// @see onRemoveFromQueue()
  86. //-----------------------------------------------------------------------------
  87. virtual void onAddToQueue(StringTableEntry queue);
  88. //-----------------------------------------------------------------------------
  89. /// @brief Callback for when the listener is removed from a queue
  90. ///
  91. /// The default implementation of onAddToQueue() and onRemoveFromQueue()
  92. /// provide tracking of the queues this listener is added to through the
  93. /// #mQueues member. Overrides of onAddToQueue() or onRemoveFromQueue()
  94. /// should ensure they call the parent implementation in any overrides.
  95. ///
  96. /// @param queue The name of the queue the listener was removed from
  97. /// @see onAddToQueue()
  98. //-----------------------------------------------------------------------------
  99. virtual void onRemoveFromQueue(StringTableEntry queue);
  100. };
  101. //-----------------------------------------------------------------------------
  102. /// @brief Internal class for tracking message queues
  103. //-----------------------------------------------------------------------------
  104. struct MessageQueue
  105. {
  106. StringTableEntry mQueueName;
  107. VectorPtr<IMessageListener *> mListeners;
  108. MessageQueue() : mQueueName("")
  109. {
  110. }
  111. bool isEmpty() { return mListeners.size() == 0; }
  112. bool dispatchMessage(const char* event, const char* data)
  113. {
  114. for(VectorPtr<IMessageListener *>::iterator i = mListeners.begin();i != mListeners.end();i++)
  115. {
  116. if( !(*i)->onMessageReceived(mQueueName, event, data) )
  117. return false;
  118. }
  119. return true;
  120. }
  121. bool dispatchMessageObject(Message *msg)
  122. {
  123. for(VectorPtr<IMessageListener *>::iterator i = mListeners.begin();i != mListeners.end();i++)
  124. {
  125. if( !(*i)->onMessageObjectReceived(mQueueName, msg) )
  126. return false;
  127. }
  128. return true;
  129. }
  130. };
  131. //-----------------------------------------------------------------------------
  132. // Message Dispatcher Functions
  133. //-----------------------------------------------------------------------------
  134. /// @name Message Queue Management
  135. // @{
  136. //-----------------------------------------------------------------------------
  137. /// @brief Check if a message queue is registered
  138. ///
  139. /// @param name The name of the message queue
  140. /// @return true if the queue is registered, false otherwise
  141. /// @see registerMessageQueue(), unregisterMessageQueue()
  142. //-----------------------------------------------------------------------------
  143. extern bool isQueueRegistered(const char *name);
  144. //-----------------------------------------------------------------------------
  145. /// @brief Register a message queue
  146. ///
  147. /// @param name The name of the message queue to register
  148. /// @see isQueueRegistered(), unregisterMessageQueue()
  149. //-----------------------------------------------------------------------------
  150. extern void registerMessageQueue(const char *name);
  151. //-----------------------------------------------------------------------------
  152. /// @brief Register an anonymous message queue
  153. ///
  154. /// @return name of anonymous message queue for passing to other functions
  155. /// @see isQueueRegistered(), unregisterMessageQueue()
  156. //-----------------------------------------------------------------------------
  157. extern const char *registerAnonMessageQueue();
  158. //-----------------------------------------------------------------------------
  159. /// @brief Unregister a message queue
  160. ///
  161. /// @param name The name of the message queue
  162. /// @see registerMessageQueue(), isQueueRegistered()
  163. //-----------------------------------------------------------------------------
  164. extern void unregisterMessageQueue(const char *name);
  165. //-----------------------------------------------------------------------------
  166. /// @brief Register a listener with a queue to receive messages
  167. ///
  168. /// @param queue The name of the queue to register the listener with
  169. /// @param listener The listener interface that receives messages
  170. /// @return true for success, false otherwise
  171. /// @see unregisterMessageListener()
  172. //-----------------------------------------------------------------------------
  173. extern bool registerMessageListener(const char *queue, IMessageListener *listener);
  174. //-----------------------------------------------------------------------------
  175. /// @brief Unregister a listener with a queue
  176. ///
  177. /// @param queue The name of the queue to unregister the listener
  178. /// @param listener The listener interface that was passed to registerMessageListener()
  179. /// @see registerMessageListener()
  180. //-----------------------------------------------------------------------------
  181. extern void unregisterMessageListener(const char *queue, IMessageListener *listener);
  182. // @}
  183. /// @name Message Dispatcher
  184. // @{
  185. //-----------------------------------------------------------------------------
  186. /// @brief Dispatch a message to a queue
  187. ///
  188. /// @param queue Queue to dispatch the message to
  189. /// @param msg Message to dispatch
  190. /// @param data Data for message
  191. /// @return true for success, false for failure
  192. /// @see dispatchMessageObject()
  193. //-----------------------------------------------------------------------------
  194. extern bool dispatchMessage(const char *queue, const char *msg, const char *data);
  195. //-----------------------------------------------------------------------------
  196. /// @brief Dispatch a message object to a queue
  197. ///
  198. /// @param queue Queue to dispatch the message to
  199. /// @param msg Message to dispatch
  200. /// @return true for success, false for failure
  201. /// @see dispatchMessage()
  202. //-----------------------------------------------------------------------------
  203. extern bool dispatchMessageObject(const char *queue, Message *msg);
  204. // @}
  205. //-----------------------------------------------------------------------------
  206. // Internal Functions
  207. //-----------------------------------------------------------------------------
  208. /// @name Internal Functions
  209. // @{
  210. //-----------------------------------------------------------------------------
  211. /// @brief Internal function: Lock the dispatcher mutex.
  212. /// @return true for success, false for failure
  213. /// @see unlockDispatcherMutex()
  214. //-----------------------------------------------------------------------------
  215. extern bool lockDispatcherMutex();
  216. //-----------------------------------------------------------------------------
  217. /// @brief Internal function: Unlock the dispatcher mutex.
  218. /// @see lockDispatcherMutex()
  219. //-----------------------------------------------------------------------------
  220. extern void unlockDispatcherMutex();
  221. //-----------------------------------------------------------------------------
  222. /// @brief Internal function: obtain message queue. Dispatcher mutex must be locked.
  223. ///
  224. /// @param name Name of the queue
  225. /// @return Message queue
  226. /// @see lockDispatcherMutex(), unlockDispatcherMutex()
  227. //-----------------------------------------------------------------------------
  228. extern MessageQueue *getMessageQueue(const char *name);
  229. // @}
  230. // @}
  231. } // end namespace Dispatcher
  232. // @}
  233. #endif // _DISPATCHER_H_