ctkEventAdminImpl.tpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "dispatch/ctkEADefaultThreadPool_p.h"
  16. template<class HandlerTasks, class SyncDeliverTasks, class AsyncDeliverTasks>
  17. ctkEventAdminImpl<HandlerTasks,SyncDeliverTasks,AsyncDeliverTasks>::ctkEventAdminImpl(
  18. HandlerTasksInterface* managers, ctkEADefaultThreadPool* syncPool,
  19. ctkEADefaultThreadPool* asyncPool, int timeout,
  20. const QStringList& ignoreTimeout)
  21. : managers(managers)
  22. {
  23. checkNull(managers, "Managers");
  24. checkNull(syncPool, "syncPool");
  25. checkNull(asyncPool, "asyncPool");
  26. sendManager = new SyncDeliverTasks(syncPool, &syncMasterThread,
  27. (timeout > 100 ? timeout : 0),
  28. ignoreTimeout);
  29. postManager = new AsyncDeliverTasks(asyncPool, sendManager);
  30. }
  31. template<class HandlerTasks, class SyncDeliverTasks, class AsyncDeliverTasks>
  32. ctkEventAdminImpl<HandlerTasks,SyncDeliverTasks,AsyncDeliverTasks>::~ctkEventAdminImpl()
  33. {
  34. delete postManager;
  35. delete sendManager;
  36. }
  37. template<class HandlerTasks, class SyncDeliverTasks, class AsyncDeliverTasks>
  38. void ctkEventAdminImpl<HandlerTasks,SyncDeliverTasks,AsyncDeliverTasks>::postEvent(const ctkEvent& event)
  39. {
  40. handleEvent(managers.fetchAndAddOrdered(0)->createHandlerTasks(event), postManager);
  41. }
  42. template<class HandlerTasks, class SyncDeliverTasks, class AsyncDeliverTasks>
  43. void ctkEventAdminImpl<HandlerTasks,SyncDeliverTasks,AsyncDeliverTasks>::sendEvent(const ctkEvent& event)
  44. {
  45. handleEvent(managers.fetchAndAddOrdered(0)->createHandlerTasks(event), sendManager);
  46. }
  47. template<class HandlerTasks, class SyncDeliverTasks, class AsyncDeliverTasks>
  48. QString ctkEventAdminImpl<HandlerTasks,SyncDeliverTasks,AsyncDeliverTasks>::
  49. subscribeSlot(const QObject *subscriber, const char *member, const ctkProperties &properties)
  50. {
  51. Q_UNUSED(subscriber)
  52. Q_UNUSED(member)
  53. Q_UNUSED(properties)
  54. return QString();
  55. }
  56. template<class HandlerTasks, class SyncDeliverTasks, class AsyncDeliverTasks>
  57. void ctkEventAdminImpl<HandlerTasks,SyncDeliverTasks,AsyncDeliverTasks>::
  58. updateProperties(const QString &subsriptionId, const ctkProperties &properties)
  59. {
  60. Q_UNUSED(subsriptionId)
  61. Q_UNUSED(properties)
  62. }
  63. template<class HandlerTasks, class SyncDeliverTasks, class AsyncDeliverTasks>
  64. void ctkEventAdminImpl<HandlerTasks,SyncDeliverTasks,AsyncDeliverTasks>::stop()
  65. {
  66. // replace the HandlerTasks with a null object that will throw an
  67. // IllegalStateException on a call to createHandlerTasks
  68. HandlerTasksInterface* oldManagers =
  69. this->managers.fetchAndStoreOrdered(&stoppedHandlerTasks);
  70. delete oldManagers;
  71. syncMasterThread.stop();
  72. }
  73. template<class HandlerTasks, class SyncDeliverTasks, class AsyncDeliverTasks>
  74. void ctkEventAdminImpl<HandlerTasks,SyncDeliverTasks,AsyncDeliverTasks>::update(HandlerTasksInterface* managers, int timeout,
  75. const QStringList& ignoreTimeout)
  76. {
  77. HandlerTasksInterface* oldManagers = this->managers.fetchAndStoreOrdered(managers);
  78. delete oldManagers;
  79. this->sendManager->update(timeout, ignoreTimeout);
  80. }
  81. template<class HandlerTasks, class SyncDeliverTasks, class AsyncDeliverTasks>
  82. template<class DeliverTasks>
  83. void ctkEventAdminImpl<HandlerTasks,SyncDeliverTasks,AsyncDeliverTasks>::handleEvent(const QList<HandlerTask>& managers,
  84. DeliverTasks* manager)
  85. {
  86. if (0 < managers.size())
  87. {
  88. // This might throw an ctkIllegalStateException in case that we are stopped
  89. // and the null object for managers was not fast enough established
  90. // This is needed in the adapter/* classes due to them sending
  91. // events whenever they receive an event from their source.
  92. // Service importers that call us regardless of the fact that we are
  93. // stopped deserve an exception anyways
  94. manager->execute(managers);
  95. }
  96. }
  97. template<class HandlerTasks, class SyncDeliverTasks, class AsyncDeliverTasks>
  98. void ctkEventAdminImpl<HandlerTasks,SyncDeliverTasks,AsyncDeliverTasks>::checkNull(void* object, const QString& name)
  99. {
  100. if(0 == object)
  101. {
  102. throw ctkInvalidArgumentException(qPrintable(name + " may not be null"));
  103. }
  104. }