dispatcher.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. /*
  23. ** Alive and Ticking
  24. ** (c) Copyright 2006 Burnt Wasp
  25. ** All Rights Reserved.
  26. **
  27. ** Filename: dispatcher.h
  28. ** Author: Tom Bampton
  29. ** Created: 19/8/2006
  30. ** Purpose:
  31. ** Message Dispatcher
  32. **
  33. */
  34. #include "messaging/message.h"
  35. #include "console/console.h"
  36. #ifndef _DISPATCHER_H_
  37. #define _DISPATCHER_H_
  38. /// @addtogroup msgsys Message System
  39. // @{
  40. //////////////////////////////////////////////////////////////////////////
  41. /// @brief Namespace for the message dispatcher functions
  42. //////////////////////////////////////////////////////////////////////////
  43. namespace Dispatcher
  44. {
  45. // [tom, 2/19/2007] This semi colon prevents VS from auto indenting the comments
  46. // below, which is really annoying when you're trying to write docs.
  47. ;
  48. /// @addtogroup msgsys Message System
  49. // @{
  50. //////////////////////////////////////////////////////////////////////////
  51. // Interface for objects that receive messages
  52. //////////////////////////////////////////////////////////////////////////
  53. //////////////////////////////////////////////////////////////////////////
  54. /// @brief Listener interface for objects that receive messages
  55. ///
  56. /// @see ScriptMsgListener
  57. //////////////////////////////////////////////////////////////////////////
  58. class IMessageListener
  59. {
  60. protected:
  61. /// List of queues this listener is registered with.
  62. Vector<StringTableEntry> mQueues;
  63. public:
  64. virtual ~IMessageListener();
  65. //////////////////////////////////////////////////////////////////////////
  66. /// @brief Callback for when messages are received
  67. ///
  68. /// @param queue The name of the queue the message was dispatched to
  69. /// @param msg The type of message
  70. /// @param data The data for the message
  71. /// @return false to prevent other listeners receiving this message, true otherwise
  72. /// @see onMessageObjectReceived()
  73. //////////////////////////////////////////////////////////////////////////
  74. virtual bool onMessageReceived(StringTableEntry queue, const char *msg, const char *data) = 0;
  75. //////////////////////////////////////////////////////////////////////////
  76. /// @brief Callback for when message objects are received
  77. ///
  78. /// @param queue The name of the queue the message was dispatched to
  79. /// @param msg The message object
  80. /// @return false to prevent other listeners receiving this message, true otherwise
  81. /// @see onMessageReceived()
  82. //////////////////////////////////////////////////////////////////////////
  83. virtual bool onMessageObjectReceived(StringTableEntry queue, Message *msg ) = 0;
  84. //////////////////////////////////////////////////////////////////////////
  85. /// @brief Callback for when the listener is added to a queue
  86. ///
  87. /// The default implementation of onAddToQueue() and onRemoveFromQueue()
  88. /// provide tracking of the queues this listener is added to through the
  89. /// #mQueues member. Overrides of onAddToQueue() or onRemoveFromQueue()
  90. /// should ensure they call the parent implementation in any overrides.
  91. ///
  92. /// @param queue The name of the queue that the listener added to
  93. /// @see onRemoveFromQueue()
  94. //////////////////////////////////////////////////////////////////////////
  95. virtual void onAddToQueue(StringTableEntry queue);
  96. //////////////////////////////////////////////////////////////////////////
  97. /// @brief Callback for when the listener is removed from a queue
  98. ///
  99. /// The default implementation of onAddToQueue() and onRemoveFromQueue()
  100. /// provide tracking of the queues this listener is added to through the
  101. /// #mQueues member. Overrides of onAddToQueue() or onRemoveFromQueue()
  102. /// should ensure they call the parent implementation in any overrides.
  103. ///
  104. /// @param queue The name of the queue the listener was removed from
  105. /// @see onAddToQueue()
  106. //////////////////////////////////////////////////////////////////////////
  107. virtual void onRemoveFromQueue(StringTableEntry queue);
  108. };
  109. //////////////////////////////////////////////////////////////////////////
  110. /// @brief Internal class for tracking message queues
  111. //////////////////////////////////////////////////////////////////////////
  112. struct MessageQueue
  113. {
  114. StringTableEntry mQueueName;
  115. VectorPtr<IMessageListener *> mListeners;
  116. MessageQueue() : mQueueName("")
  117. {
  118. }
  119. bool isEmpty() { return mListeners.size() == 0; }
  120. bool dispatchMessage(const char* event, const char* data)
  121. {
  122. for(VectorPtr<IMessageListener *>::iterator i = mListeners.begin();i != mListeners.end();i++)
  123. {
  124. if( !(*i)->onMessageReceived(mQueueName, event, data) )
  125. return false;
  126. }
  127. return true;
  128. }
  129. bool dispatchMessageObject(Message *msg)
  130. {
  131. for(VectorPtr<IMessageListener *>::iterator i = mListeners.begin();i != mListeners.end();i++)
  132. {
  133. if( !(*i)->onMessageObjectReceived(mQueueName, msg) )
  134. return false;
  135. }
  136. return true;
  137. }
  138. };
  139. //////////////////////////////////////////////////////////////////////////
  140. // Message Dispatcher Functions
  141. //////////////////////////////////////////////////////////////////////////
  142. /// @name Message Queue Management
  143. // @{
  144. //////////////////////////////////////////////////////////////////////////
  145. /// @brief Check if a message queue is registered
  146. ///
  147. /// @param name The name of the message queue
  148. /// @return true if the queue is registered, false otherwise
  149. /// @see registerMessageQueue(), unregisterMessageQueue()
  150. //////////////////////////////////////////////////////////////////////////
  151. extern bool isQueueRegistered(const char *name);
  152. //////////////////////////////////////////////////////////////////////////
  153. /// @brief Register a message queue
  154. ///
  155. /// @param name The name of the message queue to register
  156. /// @see isQueueRegistered(), unregisterMessageQueue()
  157. //////////////////////////////////////////////////////////////////////////
  158. extern void registerMessageQueue(const char *name);
  159. //////////////////////////////////////////////////////////////////////////
  160. /// @brief Unregister a message queue
  161. ///
  162. /// @param name The name of the message queue
  163. /// @see registerMessageQueue(), isQueueRegistered()
  164. //////////////////////////////////////////////////////////////////////////
  165. extern void unregisterMessageQueue(const char *name);
  166. //////////////////////////////////////////////////////////////////////////
  167. /// @brief Register a listener with a queue to receive messages
  168. ///
  169. /// @param queue The name of the queue to register the listener with
  170. /// @param listener The listener interface that receives messages
  171. /// @return true for success, false otherwise
  172. /// @see unregisterMessageListener()
  173. //////////////////////////////////////////////////////////////////////////
  174. extern bool registerMessageListener(const char *queue, IMessageListener *listener);
  175. //////////////////////////////////////////////////////////////////////////
  176. /// @brief Unregister a listener with a queue
  177. ///
  178. /// @param queue The name of the queue to unregister the listener
  179. /// @param listener The listener interface that was passed to registerMessageListener()
  180. /// @see registerMessageListener()
  181. //////////////////////////////////////////////////////////////////////////
  182. extern void unregisterMessageListener(const char *queue, IMessageListener *listener);
  183. // @}
  184. /// @name Message Dispatcher
  185. // @{
  186. //////////////////////////////////////////////////////////////////////////
  187. /// @brief Dispatch a message to a queue
  188. ///
  189. /// @param queue Queue to dispatch the message to
  190. /// @param msg Message to dispatch
  191. /// @param data Data for message
  192. /// @return true for success, false for failure
  193. /// @see dispatchMessageObject()
  194. //////////////////////////////////////////////////////////////////////////
  195. extern bool dispatchMessage(const char *queue, const char *msg, const char *data);
  196. //////////////////////////////////////////////////////////////////////////
  197. /// @brief Dispatch a message object to a queue
  198. ///
  199. /// @param queue Queue to dispatch the message to
  200. /// @param msg Message to dispatch
  201. /// @return true for success, false for failure
  202. /// @see dispatchMessage()
  203. //////////////////////////////////////////////////////////////////////////
  204. extern bool dispatchMessageObject(const char *queue, Message *msg);
  205. // @}
  206. //////////////////////////////////////////////////////////////////////////
  207. // Internal Functions
  208. //////////////////////////////////////////////////////////////////////////
  209. /// @name Internal Functions
  210. // @{
  211. //////////////////////////////////////////////////////////////////////////
  212. /// @brief Internal function: Lock the dispatcher mutex.
  213. /// @return true for success, false for failure
  214. /// @see unlockDispatcherMutex()
  215. //////////////////////////////////////////////////////////////////////////
  216. extern bool lockDispatcherMutex();
  217. //////////////////////////////////////////////////////////////////////////
  218. /// @brief Internal function: Unlock the dispatcher mutex.
  219. /// @see lockDispatcherMutex()
  220. //////////////////////////////////////////////////////////////////////////
  221. extern void unlockDispatcherMutex();
  222. //////////////////////////////////////////////////////////////////////////
  223. /// @brief Internal function: obtain message queue. Dispatcher mutex must be locked.
  224. ///
  225. /// @param name Name of the queue
  226. /// @return Message queue
  227. /// @see lockDispatcherMutex(), unlockDispatcherMutex()
  228. //////////////////////////////////////////////////////////////////////////
  229. extern MessageQueue *getMessageQueue(const char *name);
  230. // @}
  231. // @}
  232. } // end namespace Dispatcher
  233. // @}
  234. #endif // _DISPATCHER_H_