ctkEventAdminImpl_p.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #ifndef CTKEVENTADMINIMPL_P_H
  16. #define CTKEVENTADMINIMPL_P_H
  17. #include "handler/ctkEAHandlerTasks_p.h"
  18. #include "tasks/ctkEADeliverTask_p.h"
  19. #include "dispatch/ctkEASyncMasterThread_p.h"
  20. class ctkEADefaultThreadPool;
  21. /**
  22. * This is the actual implementation of the OSGi R4 Event Admin Service (see the
  23. * Compendium 113 for details). The implementation uses a <tt>ctkEAHandlerTasks</tt>
  24. * in order to determine applicable <tt>ctkEventHandler</tt> for a specific event and
  25. * subsequently dispatches the event to the handlers via <tt>ctkEADeliverTask</tt>s.
  26. * To do this, it uses two different <tt>ctkEADeliverTask</tt>s one for asynchronous and
  27. * one for synchronous event delivery depending on whether its <tt>post()</tt> or
  28. * its <tt>send()</tt> method is called. Note that the actual work is done in the
  29. * implementations of the <tt>ctkEADeliverTask</tt>s. Additionally, a stop method is
  30. * provided that prevents subsequent events to be delivered.
  31. */
  32. template<class HandlerTasks, class SyncDeliverTasks, class AsyncDeliverTasks>
  33. class ctkEventAdminImpl
  34. {
  35. private:
  36. typedef ctkEAHandlerTask<HandlerTasks> HandlerTask;
  37. typedef ctkEADeliverTask<SyncDeliverTasks, HandlerTask> SyncDeliverTaskInterface;
  38. typedef ctkEADeliverTask<AsyncDeliverTasks, HandlerTask> AsyncDeliverTaskInterface;
  39. typedef ctkEAHandlerTasks<HandlerTasks> HandlerTasksInterface;
  40. // The factory used to determine applicable ctkEventHandlers - this will be replaced
  41. // by a null object in stop() that subsequently throws an IllegalStateException
  42. QAtomicPointer<HandlerTasksInterface> managers;
  43. // The asynchronous event dispatcher
  44. AsyncDeliverTaskInterface* postManager;
  45. // The (interruptible) thread where sync events are handled
  46. ctkEASyncMasterThread syncMasterThread;
  47. // The synchronous event dispatcher
  48. SyncDeliverTasks* sendManager;
  49. struct StoppedHandlerTasks : public ctkEAHandlerTasks<HandlerTasks>
  50. {
  51. /**
  52. * This is a null object and this method will throw an
  53. * std::logic_error due to the plugin being stopped.
  54. *
  55. * @param event An event that is not used.
  56. *
  57. * @return This method does not return normally
  58. *
  59. * @throws std::logic_error - This is a null object and this method
  60. * will always throw an std::logic_error
  61. */
  62. QList<ctkEAHandlerTask<HandlerTasks> > createHandlerTasks(const ctkEvent&)
  63. {
  64. throw std::logic_error("The EventAdmin is stopped");
  65. }
  66. };
  67. StoppedHandlerTasks stoppedHandlerTasks;
  68. public:
  69. /**
  70. * The constructor of the <tt>ctkEventAdmin</tt> implementation. The
  71. * <tt>HandlerTasksInterface</tt> factory is used to determine applicable
  72. * <tt>ctkEventHandler</tt> for a given event. Additionally, the two
  73. * <tt>ctkEADeliverTasks</tt> are used to dispatch the event.
  74. *
  75. * @param managers The factory used to determine applicable <tt>ctkEventHandler</tt>
  76. * @param syncPool The synchronous thread pool
  77. * @param asyncPool The asynchronous thread pool
  78. */
  79. ctkEventAdminImpl(HandlerTasksInterface* managers,
  80. ctkEADefaultThreadPool* syncPool,
  81. ctkEADefaultThreadPool* asyncPool,
  82. int timeout,
  83. const QStringList& ignoreTimeout);
  84. ~ctkEventAdminImpl();
  85. /**
  86. * Post an asynchronous event.
  87. *
  88. * @param event The event to be posted by this service
  89. *
  90. * @throws std::logic_error - In case we are stopped
  91. *
  92. * @see ctkEventAdmin#postEvent(const ctkEvent&)
  93. */
  94. void postEvent(const ctkEvent& event);
  95. /**
  96. * Send a synchronous event.
  97. *
  98. * @param event The event to be send by this service
  99. *
  100. * @throws std::logic_error - In case we are stopped
  101. *
  102. * @see ctkEventAdmin#sendEvent(const ctkEvent&)
  103. */
  104. void sendEvent(const ctkEvent& event);
  105. QString subscribeSlot(const QObject *subscriber, const char *member, const ctkProperties &properties);
  106. void updateProperties(const QString &subsriptionId, const ctkProperties &properties);
  107. /**
  108. * This method can be used to stop the delivery of events. The managers variable is
  109. * replaced with a null object that throws an std::logic_error on a call
  110. * to <tt>createHandlerTasks()</tt>.
  111. */
  112. void stop();
  113. /**
  114. * Update the event admin with new configuration.
  115. */
  116. void update(HandlerTasksInterface* managers, int timeout,
  117. const QStringList& ignoreTimeout);
  118. private:
  119. /**
  120. * This is a utility method that uses the given ctkEADeliverTasks to create a
  121. * dispatch task that subsequently is used to dispatch the given ctkEAHandlerTasks.
  122. */
  123. template<class DeliverTasks>
  124. void handleEvent(const QList<HandlerTask>& managers,
  125. DeliverTasks* manager);
  126. /**
  127. * This is a utility method that will throw a <tt>std::invalid_argument</tt>
  128. * in case that the given object is null. The message will be of the form
  129. * "${name} + may not be null".
  130. */
  131. void checkNull(void* object, const QString& name);
  132. };
  133. #include "ctkEventAdminImpl.tpp"
  134. #endif // CTKEVENTADMINIMPL_P_H