dispatcher.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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.cc
  28. ** Author: Tom Bampton
  29. ** Created: 19/8/2006
  30. ** Purpose:
  31. ** Message Dispatcher
  32. **
  33. */
  34. #include "messaging/dispatcher.h"
  35. #include "platform/threads/mutex.h"
  36. #include "collection/simpleHashTable.h"
  37. #include "memory/safeDelete.h"
  38. #include "dispatcher_ScriptBinding.h"
  39. namespace Dispatcher
  40. {
  41. //////////////////////////////////////////////////////////////////////////
  42. // IMessageListener Methods
  43. //////////////////////////////////////////////////////////////////////////
  44. IMessageListener::~IMessageListener()
  45. {
  46. for(S32 i = 0;i < mQueues.size();i++)
  47. {
  48. unregisterMessageListener(mQueues[i], this);
  49. }
  50. }
  51. void IMessageListener::onAddToQueue(StringTableEntry queue)
  52. {
  53. // [tom, 8/20/2006] The dispatcher won't let us get added twice, so no need
  54. // to worry about it here.
  55. mQueues.push_back(queue);
  56. }
  57. void IMessageListener::onRemoveFromQueue(StringTableEntry queue)
  58. {
  59. for(S32 i = 0;i < mQueues.size();i++)
  60. {
  61. if(mQueues[i] == queue)
  62. {
  63. mQueues.erase(i);
  64. return;
  65. }
  66. }
  67. }
  68. //////////////////////////////////////////////////////////////////////////
  69. // Global State
  70. //////////////////////////////////////////////////////////////////////////
  71. //////////////////////////////////////////////////////////////////////////
  72. /// @brief Internal class used by the dispatcher
  73. //////////////////////////////////////////////////////////////////////////
  74. static struct _DispatchData
  75. {
  76. void *mMutex;
  77. SimpleHashTable<MessageQueue> mQueues;
  78. _DispatchData()
  79. {
  80. mMutex = Mutex::createMutex();
  81. }
  82. ~_DispatchData()
  83. {
  84. if(Mutex::lockMutex( mMutex ) )
  85. {
  86. mQueues.clearTables();
  87. Mutex::unlockMutex( mMutex );
  88. }
  89. Mutex::destroyMutex( mMutex );
  90. //SAFE_DELETE(mMutex);
  91. mMutex = NULL;
  92. }
  93. } gDispatchData;
  94. //////////////////////////////////////////////////////////////////////////
  95. // Queue Registration
  96. //////////////////////////////////////////////////////////////////////////
  97. bool isQueueRegistered(const char *name)
  98. {
  99. MutexHandle mh;
  100. if(mh.lock(gDispatchData.mMutex, true))
  101. {
  102. return gDispatchData.mQueues.retrieve(name) != NULL;
  103. }
  104. return false;
  105. }
  106. void registerMessageQueue(const char *name)
  107. {
  108. if(isQueueRegistered(name))
  109. return;
  110. if(Mutex::lockMutex( gDispatchData.mMutex, true ))
  111. {
  112. MessageQueue *queue = new MessageQueue;
  113. queue->mQueueName = StringTable->insert(name);
  114. gDispatchData.mQueues.insert(queue, name);
  115. Mutex::unlockMutex( gDispatchData.mMutex );
  116. }
  117. }
  118. void unregisterMessageQueue(const char *name)
  119. {
  120. MutexHandle mh;
  121. if(mh.lock(gDispatchData.mMutex, true))
  122. {
  123. MessageQueue *queue = gDispatchData.mQueues.remove(name);
  124. if(queue == NULL)
  125. return;
  126. // Tell the listeners about it
  127. for(S32 i = 0;i < queue->mListeners.size();i++)
  128. {
  129. queue->mListeners[i]->onRemoveFromQueue(name);
  130. }
  131. delete queue;
  132. }
  133. }
  134. //////////////////////////////////////////////////////////////////////////
  135. // Message Listener Registration
  136. //////////////////////////////////////////////////////////////////////////
  137. bool registerMessageListener(const char *queue, IMessageListener *listener)
  138. {
  139. if(! isQueueRegistered(queue))
  140. registerMessageQueue(queue);
  141. MutexHandle mh;
  142. if(! mh.lock(gDispatchData.mMutex, true))
  143. return false;
  144. MessageQueue *q = gDispatchData.mQueues.retrieve(queue);
  145. if(q == NULL)
  146. {
  147. Con::errorf("Dispatcher::registerMessageListener - Queue '%s' not found?! It should have been added automatically!", queue);
  148. return false;
  149. }
  150. for(VectorPtr<IMessageListener *>::iterator i = q->mListeners.begin();i != q->mListeners.end();i++)
  151. {
  152. if(*i == listener)
  153. return false;
  154. }
  155. q->mListeners.push_front(listener);
  156. listener->onAddToQueue(StringTable->insert(queue));
  157. return true;
  158. }
  159. void unregisterMessageListener(const char *queue, IMessageListener *listener)
  160. {
  161. if(! isQueueRegistered(queue))
  162. return;
  163. MutexHandle mh;
  164. if(! mh.lock(gDispatchData.mMutex, true))
  165. return;
  166. MessageQueue *q = gDispatchData.mQueues.retrieve(queue);
  167. if(q == NULL)
  168. return;
  169. for(VectorPtr<IMessageListener *>::iterator i = q->mListeners.begin();i != q->mListeners.end();i++)
  170. {
  171. if(*i == listener)
  172. {
  173. listener->onRemoveFromQueue(StringTable->insert(queue));
  174. q->mListeners.erase(i);
  175. return;
  176. }
  177. }
  178. }
  179. //////////////////////////////////////////////////////////////////////////
  180. // Dispatcher
  181. //////////////////////////////////////////////////////////////////////////
  182. bool dispatchMessage(const char *queue, const char *msg, const char *data)
  183. {
  184. MutexHandle mh;
  185. if(! mh.lock(gDispatchData.mMutex, true))
  186. return true;
  187. MessageQueue *q = gDispatchData.mQueues.retrieve(queue);
  188. if(q == NULL)
  189. {
  190. Con::errorf("Dispatcher::dispatchMessage - Attempting to dispatch to unknown queue '%s'", queue);
  191. return true;
  192. }
  193. return q->dispatchMessage(msg, data);
  194. }
  195. bool dispatchMessageObject(const char *queue, Message *msg)
  196. {
  197. MutexHandle mh;
  198. if(msg == NULL)
  199. return true;
  200. msg->addReference();
  201. if(! mh.lock(gDispatchData.mMutex, true))
  202. {
  203. msg->freeReference();
  204. return true;
  205. }
  206. MessageQueue *q = gDispatchData.mQueues.retrieve(queue);
  207. if(q == NULL)
  208. {
  209. Con::errorf("Dispatcher::dispatchMessage - Attempting to dispatch to unknown queue '%s'", queue);
  210. msg->freeReference();
  211. return true;
  212. }
  213. // [tom, 8/19/2006] Make sure that the message is registered with the sim, since
  214. // when it's ref count is zero it'll be deleted with deleteObject()
  215. if(! msg->isProperlyAdded())
  216. {
  217. SimObjectId id = Message::getNextMessageID();
  218. if(id != 0xffffffff)
  219. msg->registerObject(id);
  220. else
  221. {
  222. Con::errorf("dispatchMessageObject: Message was not registered and no more object IDs are available for messages");
  223. msg->freeReference();
  224. return false;
  225. }
  226. }
  227. bool bResult = q->dispatchMessageObject(msg);
  228. msg->freeReference();
  229. return bResult;
  230. }
  231. //////////////////////////////////////////////////////////////////////////
  232. // Internal Functions
  233. //////////////////////////////////////////////////////////////////////////
  234. MessageQueue * getMessageQueue(const char *name)
  235. {
  236. return gDispatchData.mQueues.retrieve(name);
  237. }
  238. extern bool lockDispatcherMutex()
  239. {
  240. return Mutex::lockMutex(gDispatchData.mMutex);
  241. }
  242. extern void unlockDispatcherMutex()
  243. {
  244. Mutex::unlockMutex(gDispatchData.mMutex);
  245. }
  246. } // end namespace Dispatcher