ctkEventAdminService.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "ctkEventAdminService_p.h"
  16. #include "handler/ctkEASlotHandler_p.h"
  17. #include <ctkPluginConstants.h>
  18. ctkEventAdminService::ctkEventAdminService(ctkPluginContext* context,
  19. HandlerTasksInterface* managers,
  20. ctkEADefaultThreadPool* syncPool,
  21. ctkEADefaultThreadPool* asyncPool,
  22. int timeout,
  23. const QStringList& ignoreTimeout)
  24. : impl(managers, syncPool, asyncPool, timeout, ignoreTimeout),
  25. context(context)
  26. {
  27. }
  28. ctkEventAdminService::~ctkEventAdminService()
  29. {
  30. qDeleteAll(slotHandler);
  31. foreach(QList<ctkEASignalPublisher*> l, signalPublisher.values())
  32. {
  33. qDeleteAll(l);
  34. }
  35. }
  36. void ctkEventAdminService::postEvent(const ctkEvent& event)
  37. {
  38. impl.postEvent(event);
  39. }
  40. void ctkEventAdminService::sendEvent(const ctkEvent& event)
  41. {
  42. impl.sendEvent(event);
  43. }
  44. void ctkEventAdminService::publishSignal(const QObject* publisher, const char* signal,
  45. const QString& topic,
  46. Qt::ConnectionType type)
  47. {
  48. if (topic.isEmpty())
  49. {
  50. throw ctkInvalidArgumentException("topic must not be empty");
  51. }
  52. // check if the signal was already registered under the given topic
  53. if (signalPublisher.contains(publisher))
  54. {
  55. const QList<ctkEASignalPublisher*>& signalPublishers = signalPublisher[publisher];
  56. for(int i = 0; i < signalPublishers.size(); ++i)
  57. {
  58. if (signalPublishers[i]->getSignalName() == signal &&
  59. signalPublishers[i]->getTopicName() == topic)
  60. {
  61. return;
  62. }
  63. }
  64. }
  65. QList<ctkEASignalPublisher*>& signalList = signalPublisher[publisher];
  66. signalList.push_back(new ctkEASignalPublisher(this, signal, topic));
  67. if (type == Qt::DirectConnection)
  68. {
  69. connect(publisher, signal, signalList.back(), SLOT(publishSyncSignal(ctkDictionary)), Qt::DirectConnection);
  70. }
  71. else if (type == Qt::QueuedConnection)
  72. {
  73. connect(publisher, signal, signalList.back(), SLOT(publishAsyncSignal(ctkDictionary)), Qt::DirectConnection);
  74. }
  75. else
  76. {
  77. throw ctkInvalidArgumentException("type must be either Qt::DirectConnection or Qt::QueuedConnection");
  78. }
  79. }
  80. void ctkEventAdminService::unpublishSignal(const QObject* publisher, const char* signal,
  81. const QString& topic)
  82. {
  83. if (!signalPublisher.contains(publisher)) return;
  84. if (signal == 0 && topic.isEmpty())
  85. {
  86. // unpublish everything from the given publisher
  87. // this automatically disconnects signals
  88. qDeleteAll(signalPublisher.take(publisher));
  89. }
  90. else
  91. {
  92. QList<ctkEASignalPublisher*>& list = signalPublisher[publisher];
  93. if (signal == 0)
  94. {
  95. for (int i = 0; i < list.size(); )
  96. {
  97. if (list[i]->getTopicName() == topic)
  98. {
  99. // this automatically disconnects the signals
  100. delete list.takeAt(i);
  101. }
  102. else
  103. {
  104. ++i;
  105. }
  106. }
  107. }
  108. else {
  109. for (int i = 0; i < list.size(); )
  110. {
  111. if (list[i]->getSignalName() == signal)
  112. {
  113. if (topic.isEmpty() || list[i]->getTopicName() == topic)
  114. {
  115. // this automatically disconnects the signals
  116. delete list.takeAt(i);
  117. }
  118. }
  119. else
  120. {
  121. ++i;
  122. }
  123. }
  124. }
  125. if (list.isEmpty())
  126. {
  127. signalPublisher.remove(publisher);
  128. }
  129. }
  130. }
  131. qlonglong ctkEventAdminService::subscribeSlot(const QObject* subscriber, const char* member,
  132. const ctkDictionary& properties, Qt::ConnectionType type)
  133. {
  134. if (subscriber == 0) throw ctkInvalidArgumentException("subscriber cannot be NULL");
  135. if (member == 0) throw ctkInvalidArgumentException("slot cannot be NULL");
  136. if (type != Qt::AutoConnection && type != Qt::DirectConnection &&
  137. type != Qt::QueuedConnection && type != Qt::BlockingQueuedConnection)
  138. {
  139. throw ctkInvalidArgumentException("connection type invalid");
  140. }
  141. ctkEASlotHandler* handler = new ctkEASlotHandler();
  142. connect(handler, SIGNAL(eventOccured(ctkEvent)), subscriber, member, type);
  143. ctkServiceRegistration reg = context->registerService<ctkEventHandler>(handler, properties);
  144. handler->reg = reg;
  145. qlonglong id = reg.getReference().getProperty(ctkPluginConstants::SERVICE_ID).toLongLong();
  146. slotHandler.insert(id, handler);
  147. return id;
  148. }
  149. void ctkEventAdminService::unsubscribeSlot(qlonglong subscriptionId)
  150. {
  151. ctkEASlotHandler* handler = slotHandler.take(subscriptionId);
  152. if (handler)
  153. {
  154. handler->reg.unregister();
  155. delete handler;
  156. }
  157. }
  158. bool ctkEventAdminService::updateProperties(qlonglong subscriptionId, const ctkDictionary& properties)
  159. {
  160. ctkEASlotHandler* handler = slotHandler.value(subscriptionId);
  161. if (handler)
  162. {
  163. handler->updateProperties(properties);
  164. return true;
  165. }
  166. return false;
  167. }
  168. void ctkEventAdminService::stop()
  169. {
  170. impl.stop();
  171. }
  172. void ctkEventAdminService::update(HandlerTasksInterface* managers, int timeout,
  173. const QStringList& ignoreTimeout)
  174. {
  175. impl.update(managers, timeout, ignoreTimeout);
  176. }